[STM32F103]定时器PWM输入
Posted zhaohu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[STM32F103]定时器PWM输入相关的知识,希望对你有一定的参考价值。
typedef struct
{
uint16_t TIM_OCMode; //PWM模式1或者模式2
uint16_t TIM_OutputState; //输出使能 OR失能
uint16_t TIM_OutputNState;
uint16_t TIM_Pulse; //比较值,写CCRx
uint16_t TIM_OCPolarity; //比较输出极性
uint16_t TIM_OCNPolarity;
uint16_t TIM_OCIdleState;
uint16_t TIM_OCNIdleState;
} TIM_OCInitTypeDef;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //PWM模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure. TIM_Pulse=100;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OC2Init(TIM3, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM3 OC2
PWM输出配置步骤流程:
l 使能定时器3和相关IO口时钟。
n 使能定时器3时钟:RCC_APB1PeriphClockCmd();
n 使能GPIOB时钟:RCC_APB2PeriphClockCmd();
l 初始化IO口为复用功能输出。函数:
n GPIO_Init();
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 推挽复用输出控制LED等
l 这里我们是要把PB5用作定时器的PWM输出引脚,所以要重映射配置,
n 所以需要开启AFIO时钟。同时设置重映射。
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE);
l 初始化定时器:ARR,PSC等:
n TIM_TimeBaseInit();
l 初始化输出比较参数:
n TIM_OC2Init();
l 使能预装载寄存器:
n TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
l 使能定时器。
n TIM_Cmd();
l 不断改变比较值CCRx,达到不同的占空比效果:
n TIM_SetCompare2();
l 源代码:
1 void TIM3_PWM_Init(u16 arr,u16 psc) 2 { 3 GPIO_InitTypeDef GPIO_InitStructure; 4 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; 5 TIM_OCInitTypeDef TIM_OCInitStructure; 6 7 8 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //ê1?ü?¨ê±?÷3ê±?ó 9 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //ê1?üGPIOíaéèoíAFIO?′ó?1|?ü?£?éê±?ó 10 11 GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //Timer32?·???ó3é? TIM3_CH2->PB5 12 13 //éè????òy???a?′ó?ê?3?1|?ü,ê?3?TIM3 CH2μ?PWM??3?2¨D? GPIOB.5 14 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //TIM_CH2 15 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //?′ó?í?íìê?3? 16 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 17 GPIO_Init(GPIOB, &GPIO_InitStructure);//3?ê??ˉGPIO 18 19 //3?ê??ˉTIM3 20 TIM_TimeBaseStructure.TIM_Period=arr; //éè???ú??ò????üD?ê??t×°è????ˉμ?×??ˉ??×°????′??÷?ü?úμ??μ 21 TIM_TimeBaseStructure.TIM_Prescaler=psc; //éè??ó?à′×÷?aTIMxê±?ó?μ?ê3yêyμ??¤·??μ?μ 22 TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éè??ê±?ó·???:TDTS = Tck_tim 23 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM?òé???êy?£ê? 24 TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure); //?ù?YTIM_TimeBaseInitStruct?D???¨μ?2?êy3?ê??ˉTIMxμ?ê±???ùêyμ¥?? 25 26 //3?ê??ˉTIM3 Channel2 PWM?£ê? 27 TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2; //?????¨ê±?÷?£ê?:TIM??3??í?èμ÷???£ê?2 28 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è??ê?3?ê1?ü 29 TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High; //ê?3???D?:TIMê?3?±è????D??? 30 TIM_OC2Init(TIM3, &TIM_OCInitStructure); //?ù?YT???¨μ?2?êy3?ê??ˉíaéèTIM3 OC2 31 32 TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable); //ê1?üTIM3?úCCR2é?μ??¤×°????′??÷ 33 34 TIM_Cmd(TIM3, ENABLE); //ê1?üTIM3 35 } 36 int main(void) 37 { 38 u16 led0pwmval=0; 39 u8 dir=1; 40 delay_init(); //?óê±oˉêy3?ê??ˉ 41 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //éè??NVIC?D??·?×é2:2???à??ó??è??£?2???ìó|ó??è?? 42 uart_init(115200); //′??ú3?ê??ˉ?a115200 43 LED_Init(); //LED???ú3?ê??ˉ 44 TIM3_PWM_Init(899,0); //2?·??μ?£PWM?μ?ê=72000000/900=80Khz 45 while(1) 46 { 47 delay_ms(10); 48 if(dir)led0pwmval++; 49 else led0pwmval--; 50 51 if(led0pwmval>300)dir=0; 52 if(led0pwmval==0)dir=1; 53 TIM_SetCompare2(TIM3,led0pwmval); 54 } 55 }
以上是关于[STM32F103]定时器PWM输入的主要内容,如果未能解决你的问题,请参考以下文章