New driver MM32F103 test (IV) Advanced timer PWM[Copy link]
This post was last edited by lising on 2018-11-1 10:20 This experiment uses the 100KHz PWM wave output frequency of CH1 of MM32F103C8T6 advanced timer TIM1 to drive the LED to emit light. 1. Experimental resources 1. MM32F103 development board; 2. KEIL 5.25.2; 3. J-LINK V9; 4. Development routines provided by New Driving Force; 2. Experimental process 1. According to the circuit diagram, the LED light-emitting tube D2 on the development board is connected to the PA8 port of the MM32F103C8T6 chip. Since the cathode end of D2 is connected to the PA8 port, the LED is lit when PA8 is at a low level. According to the data sheet, the multiplexing function of the PA8 port is TIM1_CH1. The experiment directly uses the CH1 of TIM1 to output a PWM wave with a frequency of 100KHz and a variable duty cycle at the PA8 port to drive D2 to produce a display effect of dark->gradually bright->bright->gradually dark->dark.
2. The PWM output program is modified directly from the official routine. In the program, TIM1_CH1 is first initialized, such as configuring the PWM frequency directly to 100KHz and using PWM mode 2. The code is as follows:
void TIM1_PWM_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; /*Enable GPIO peripherals and TIM1 clock*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /*Set PA8 multiplexing output function to output PWM pulse of TIM1_CH1*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /*Set the value of the auto-reload register period to 720-1, that is, the PWM period is 72MHz/720=100KHz; The timer clock prescaler is 0; Up counting is used; PWM is mode 2; The output polarity is high; */ TIM_TimeBaseStructure.TIM_Period = (720-1); TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //Output polarity: TIM output comparison polarity highTIM_OC1Init(TIM1, &TIM_OCInitStructure); TIM_CtrlPWMOutputs(TIM1,ENABLE); //MOE main output enableTIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH1 preload enableTIM_ARRPreloadConfig(TIM1, ENABLE); //Enable TIM1 preload register on ARRTIM_Cmd(TIM1,ENABLE); //Enable TIM1 }
复制代码
3. Function implementation. In the main program, the initialization program is first called. In the while(1) loop, two for loop statements are used to write the changing parameters into TIM1_CCR1 at 5ms intervals to change the duty cycle of PWM: int main(void) { delay_init(); //Delay function initialization LED_Init(); //Initialize the hardware interface connected to the LED TIM1_PWM_Init(); while(1) { for(PwmDuty=0;PwmDuty<500;PwmDuty++) { TIM_SetCompare1(TIM1,PwmDuty); delay_ms(5); } for(PwmDuty=500;PwmDuty>0;PwmDuty--) { TIM_SetCompare1(TIM1,PwmDuty); delay_ms(5); } } }[/code] 4. Experimental phenomenon. As mentioned above;
3. Experimental summary. The experiment was too rushed, and only the experimental phenomenon was presented, but there is still a great lack of understanding of the official routines. Next, we must make good use of these routines and combine them with the manual for in-depth understanding and study. This content is originally created by EEWORLD forum user lising. If you need to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source