点亮灯之后,师兄说你研究研究PWM吧,一个小时后,另一个师兄看着我抱着数据库欲哭无泪的脸说,纯新手看着也太难了,给了我几本电子书=。=可是师兄…原谅我太爱纸质书,而且=。=那本废话有点多。。。
话说另外一个师兄也在学STM32F,但是他是407,而且他有51的基础啊啊啊。所以,求虐模式变为ctrl加c或加v模式,看过例程,看过师兄学这个,再加上跟哥哥聊天,顿悟=。=尼玛就是复制粘贴啊,但是关键在于逻辑以及函数理解,以及有针对性的数据改动。
这个是今天的成果
- #include “stm32f10x.h”
- #include “systick.h”
- #include & lt; stdio.h & gt;
- void PWM1_Configuration( void );
- void GPIO_Configuration( void );
- int main( void )
- {
- GPIO_Configuration();
- PWM1_Configuration();
- while ( 1 )
- {
- }
- }
- void PWM1_Configuration( void )
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM2, ENABLE );
- TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
- TIM_TimeBaseStructure.TIM_Period = 9999;
- TIM_TimeBaseStructure.TIM_ClockDivision = 0;
- TIM_TimeBaseStructure.TIM_Prescaler = 7;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
- TIM_TimeBaseInit( TIM2, & TIM_TimeBaseStructure );
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_Pulse = 500;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
- TIM_OC3Init( TIM2, & TIM_OCInitStructure );
- TIM_OC3PreloadConfig( TIM2, TIM_OCPreload_Enable );
- TIM_ARRPreloadConfig( TIM2, ENABLE );
- TIM_Cmd( TIM2, ENABLE );
- TIM_CtrlPWMOutputs( TIM2, ENABLE );
- }
- void GPIO_Configuration( void )
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE );
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init( GPIOA, & GPIO_InitStructure );
- }
附图一张,带解析的程序,原谅我懒得打…
有注释的都截图了,更正一下这个是向下计数