STM32 advanced timer TIM1 generates two complementary PWM waves (with dead band)

Test environment: Keil 5.20.0.0 STM32F103RBT6 firmware library version: stm32f10x? Stdperiph? Lib? V3.5.0 (2011)

In this paper, we use channel 1 and 2 of TIM1 to generate two complementary PWM waves of 1khz and 1us dead time.

IO port used: as shown in the figure below, we use pins PA9 and PA10, and use PB14 and PB15 for complementary output

Some codes are as follows:

 1 /* I/O used when configuring TIM1 multiplex output PWM  */
 2 static void TIM1_GPIO_Config(void) 
 3 {
 4   GPIO_InitTypeDef GPIO_InitStructure;
 5 
 6   /* TIM1 clock enable */
 7   RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
 8 
 9   /* GPIOA and GPIOB clock enable */
10   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
11     
12   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_9 | GPIO_Pin_10;
13   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
14   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
15 
16   GPIO_Init(GPIOA, &GPIO_InitStructure);
17 
18   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_14 | GPIO_Pin_15;
19   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
20   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
21 
22   GPIO_Init(GPIOB, &GPIO_InitStructure);
23 
24 }
Initialize IO

Initialize timer function configuration

 1 u16 CCR2_Val = 500;
 2 u16 CCR3_Val = 500;//Duty cycle 1000
 3 
 4 /*Configure the mode of PWM signal output by TIM1, such as cycle, polarity, duty cycle */
 5 void TIM1_Mode_Config(void)
 6 {
 7     TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
 8     TIM_BDTRInitTypeDef      TIM1_BDTRInitStruct;
 9     TIM_OCInitTypeDef        TIM_OCInitStructure;
10 
11     /* Time base configuration */
12     TIM_TimeBaseStructure.TIM_Period = 1000-1; //Count cycle, write up to this number, and clear the count value
13     TIM_TimeBaseStructure.TIM_Prescaler = 72-1;//Timer division factor, Ftimer = 72M/(TIM_Prescaler+1) = 1ms
14     TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//Related to deadband time division
15     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//Count up mode
16     TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
17     
18     /****** Configure BDTR register and dead time****************/
19     /*
20        Tdts = 13.89ns for timer clock 72m Tim ﹣ clockdivision = Tim ﹣ CKD ﹣ div1
21        0 - 1.764us  Using algorithm 1
22        1.778us - 3.505us  Using algorithm two
23        3.556us - 7.000us  Using algorithm three 
24        7.1117us - 14us    Using algorithm four
25        Take longer, use Tim? Clockdivision
26     */
27     TIM1_BDTRInitStruct.TIM_OSSRState = TIM_OSSRState_Disable;
28     TIM1_BDTRInitStruct.TIM_OSSIState = TIM_OSSIState_Disable;
29     TIM1_BDTRInitStruct.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
30     TIM1_BDTRInitStruct.TIM_DeadTime = 205; //Dead time 72:1us 172:3us 205:5us
31     TIM_BDTRConfig(TIM1,&TIM1_BDTRInitStruct);
32  
33 //    TIM1->BDTR |= 72;   //Set deadband note:The above method is also OK. It's fast and simple
34     
35      /* PWM1 Mode configuration: Channel2 */
36      TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;//PWM2 Pattern
37      TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //Compare output enable
38      TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;//Compare complementary output enable
39      TIM_OCInitStructure.TIM_Pulse = CCR2_Val;   //Comparison value, i.e. duty cycle
40      TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;  //Output polarity
41      TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;//Complementary output polarity
42      TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;//Specify the TIM Output the pin status of the comparison.
43      TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;//Specify the TIM Pin status of complementary output comparison.
44      TIM_OC2Init(TIM1, &TIM_OCInitStructure);   //Initialize channel 2 comparison output
45      TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);      //Configure channel 2, automatic reload enable
46 
47 
48     /* PWM1 Mode configuration: Channel3 */
49     TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
50     TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
51     TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
52     TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
53     TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
54     TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
55     TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
56     TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;      
57     TIM_OC3Init(TIM1, &TIM_OCInitStructure);
58     TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);    
59 
60     TIM_ARRPreloadConfig(TIM1, ENABLE);//Reload load value ENABLE Effective immediately, DISABLE Effective next comparison cycle
61 
62     /* TIM1 enable counter */
63     TIM_Cmd(TIM1, ENABLE);//Enable timer 1
64     
65     TIM_CtrlPWMOutputs(TIM1, ENABLE);//Enable PWM Peripheral output    
66 }

 

 1 int main(void)
 2 {
 3     TIM1_GPIO_Config();
 4     TIM1_Mode_Config();    
 5     while(1)
 6     {
 7          TIM1->CCR2 = CCR2_Val;
 8          TIM1->CCR3 = CCR3_Val;
 9          CCR2_Val+=5;
10          CCR3_Val+=10;
11          if(CCR2_Val>900)  CCR2_Val = 100;
12          if(CCR3_Val>900)  CCR3_Val = 100;   
13          Delay_mS(200);
14     }
15 }
main function

 

About dead time calculation:

First, post some pictures of TIM clock:

The first picture: about the deadband time division factor. (see line 14 in the initialization timer function configuration code section for the code)

Figure 2: dead time calculation

 

Timer 1 clock is hung on the APB2 bus, and the clock is 72M. When Tim ﹣ clockdivision = Tim ﹣ CKD ﹣ div1, Tdts = 1/72M = 13.89ns
Algorithm 1 for 0 - 1.764us
Algorithm 2 for 1.778us - 3.505us
3.556us - 7.000us algorithm III
7.1117us - 14us algorithm IV
It takes a longer time. After using Tim ﹣ clockdivision to divide the frequency, set the dead time.

 

Test data:

Experimental phenomenon: two complementary PWM signals with a dead time of 1us are generated, the frequency is 1kHz, the duty cycle is changing from 10% to 90%, and channel 3 changes faster than channel 2.

Dead time (take channel 2 as an example): (yellow: PA10 green: PB15)

  

Capture waveform of channel 2 (yellow: PA10, green: PB15)

Capture waveform of channel 3 (yellow: PA9 green: PB14)

Channel 2 and 3 (yellow: PA9, green: PA10)

Tags: C

Posted on Sat, 04 Apr 2020 16:07:47 -0700 by burfo