1. 程式人生 > >[STM32F103]定時器PWM輸入

[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();

初始化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);

初始化定時器:ARR,PSC等:

  n TIM_TimeBaseInit();

初始化輸出比較引數:

  n TIM_OC2Init();

使能預裝載暫存器:

  n TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);

l 使能定時器。

  n TIM_Cmd();

不斷改變比較值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  }