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); //ʹÄܶ¨Ê±Æ÷3ʱÖÓ 9 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //ʹÄÜGPIOÍâÉèºÍAFIO¸´Óù¦ÄÜÄ£¿éʱÖÓ 10 11 GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //Timer3²¿·ÖÖØÓ³Éä TIM3_CH2->PB5 12 13 //ÉèÖøÃÒý½ÅΪ¸´ÓÃÊä³ö¹¦ÄÜ,Êä³öTIM3 CH2µÄPWMÂö³å²¨ÐÎ GPIOB.5 14 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //TIM_CH2 15 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö 16 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 17 GPIO_Init(GPIOB, &GPIO_InitStructure);//³õʼ»¯GPIO 18 19 //³õʼ»¯TIM3 20 TIM_TimeBaseStructure.TIM_Period=arr; //ÉèÖÃÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ 21 TIM_TimeBaseStructure.TIM_Prescaler=psc; //ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ 22 TIM_TimeBaseStructure.TIM_ClockDivision = 0; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim 23 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòÉϼÆÊýģʽ 24 TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure); //¸ù¾ÝTIM_TimeBaseInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î» 25 26 //³õʼ»¯TIM3 Channel2 PWMģʽ 27 TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ2 28 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±È½ÏÊä³öʹÄÜ 29 TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High; //Êä³ö¼«ÐÔ:TIMÊä³ö±È½Ï¼«ÐÔ¸ß 30 TIM_OC2Init(TIM3, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC2 31 32 TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable); //ʹÄÜTIM3ÔÚCCR2ÉϵÄԤװÔؼĴæÆ÷ 33 34 TIM_Cmd(TIM3, ENABLE); //ʹÄÜTIM3 35 } 36 int main(void) 37 { 38 u16 led0pwmval=0; 39 u8 dir=1; 40 delay_init(); //ÑÓʱº¯Êý³õʼ»¯ 41 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //ÉèÖÃNVICÖжϷÖ×é2:2λÇÀÕ¼ÓÅÏȼ¶£¬2λÏìÓ¦ÓÅÏȼ¶ 42 uart_init(115200); //´®¿Ú³õʼ»¯Îª115200 43 LED_Init(); //LED¶Ë¿Ú³õʼ»¯ 44 TIM3_PWM_Init(899,0); //²»·ÖƵ¡£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 }