3882 views|3 replies

821

Posts

0

Resources
The OP
 

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:
  1. 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; 视频.mp4 (14.95 MB, downloads: 65) 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

Latest reply

This also requires connecting PA8 and D2 with a jumper cap   Details Published on 2019-10-18 14:36
 
 

1368

Posts

6

Resources
2
 
Good, I also have to organize my test article, I only tested it and forgot to post it

Comments

I post it as soon as I finish it, otherwise I will have to do it again if it takes a long time.  Details Published on 2018-11-2 09:19
Personal signature专注智能产品的研究与开发,专注于电子电路的生产与制造……QQ:2912615383,电子爱好者群: void
 
 
 

821

Posts

0

Resources
3
 
Lazy Cat Loves Flying posted on 2018-11-2 08:31 Yes, I also have to organize my test article. I only tested it and forgot to post it.
I will post it as soon as I finish it. If it takes a long time, I will have to do it again
 
 
 

5

Posts

0

Resources
4
 

This also requires connecting PA8 and D2 with a jumper cap

 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list