完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
#include "bsp_spwm_output.h"
uint16_t CCR1_Val ; uint16_t CCR2_Val = 1500 ; uint16_t CCR3_Val = 900; static void TIM1_Mode_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; /* GPIOA clocks enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); /* TIM1 clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA,&GPIO_InitStructure); /* Connect TIM pins to AF6 */ GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_6); GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_6); GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_6); /* Time Base configuration */ TIM_TimeBaseStructure.TIM_Period =2999; TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //ÉèÖÃÔ¤·ÖƵ£º²»Ô¤·ÖƵ£¬¼´Îª72MHz TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //ÉèÖÃʱÖÓ·ÖƵϵÊý£º²»·ÖƵ(ÕâÀïÓò»µ½) TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1; //ÏòÉϼÆÊýģʽ TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); /*PWM1 Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //ÅäÖÃΪPWMģʽ1 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR2_Val; //ÉèÖÃÌø±äÖµ£¬µ±¼ÆÊýÆ÷¼ÆÊýµ½Õâ¸öֵʱ£¬µçƽ·¢ÉúÌø±ä TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //µ±¶¨Ê±Æ÷¼ÆÊýֵСÓÚCCR1_ValʱΪ¸ßµçƽ TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set; TIM_OC2Init(TIM1, &TIM_OCInitStructure); //ʹÄÜͨµÀ1 TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); TIM_ARRPreloadConfig( TIM1,ENABLE); TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR3_Val; TIM_OC3Init(TIM1, &TIM_OCInitStructure); //ʹÄÜͨµÀ4 TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable); TIM_ARRPreloadConfig( TIM1,ENABLE); TIM_ARRPreloadConfig( TIM1,ENABLE); TIM_UpdateRequestConfig( TIM1, TIM_UpdateSource_Regular); TIM_ITConfig(TIM1,TIM_IT_Update | TIM_IT_Trigger,ENABLE);/* TIM3 enable counter */ TIM_Cmd(TIM1, ENABLE); //ʹÄܶ¨Ê±Æ÷3 TIM_ClearFlag(TIM1, TIM_FLAG_Update); TIM_CtrlPWMOutputs(TIM1, ENABLE); } void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn; //TIM1ÖÐ¶Ï NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 6; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void TIM1_SPWM_Init(void) { TIM1_Mode_Config(); NVIC_Configuration(); } // /*****/ |
|
相关推荐
8个回答
|
|
|
|
|
|
触发中断,我用TIM2可以,TIM1就不行,不知道哪里出问题了。。。
|
|
|
|
签到签到
|
|
|
|
先用TIM1调调定时中断能进去不,先保证配置什么没有问题。
|
|
|
|
进不去啊!
|
|
|
|
,你上面的代码不是把TIMER1配置成了PWM模式了吗?
|
|
|
|
既不是定时模式,也不是计数模式,更不是输入捕获。
|
|
|
|
看看函数名与.S的对的起不
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
2234 浏览 1 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
2053 浏览 3 评论
4664 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
2197 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
2734 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-26 10:47 , Processed in 0.566442 second(s), Total 54, Slave 49 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号