股票

大一小菜鸟血泪史:开启STM32F103ZET6的求虐模式(二)PWM输出方波

点亮灯之后,师兄说你研究研究PWM吧,一个小时后,另一个师兄看着我抱着数据库欲哭无泪的脸说,纯新手看着也太难了,给了我几本电子书=。=可是师兄…原谅我太爱纸质书,而且=。=那本废话有点多。。。

话说另外一个师兄也在学STM32F,但是他是407,而且他有51的基础啊啊啊。所以,求虐模式变为ctrl加c或加v模式,看过例程,看过师兄学这个,再加上跟哥哥聊天,顿悟=。=尼玛就是复制粘贴啊,但是关键在于逻辑以及函数理解,以及有针对性的数据改动。

这个是今天的成果

  1. #include “stm32f10x.h”
  2. #include “systick.h”
  3. #include & lt; stdio.h & gt;
  4. void PWM1_Configuration( void );
  5. void GPIO_Configuration( void );
  6. int main( void )
  7. {
  8.     GPIO_Configuration();
  9.     PWM1_Configuration();
  10.     while ( 1 )
  11.     {
  12.     }
  13. }
  14. void PWM1_Configuration( void )
  15. {
  16.     TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  17.     TIM_OCInitTypeDef   TIM_OCInitStructure;
  18.     RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM2, ENABLE );
  19.     TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  20.     TIM_TimeBaseStructure.TIM_Period        = 9999;
  21.     TIM_TimeBaseStructure.TIM_ClockDivision     = 0;
  22.     TIM_TimeBaseStructure.TIM_Prescaler     = 7;
  23.     TIM_TimeBaseStructure.TIM_CounterMode       = TIM_CounterMode_Down;
  24.     TIM_TimeBaseInit( TIM2, & TIM_TimeBaseStructure );
  25.     TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  26.     TIM_OCInitStructure.TIM_OCMode      = TIM_OCMode_PWM1;
  27.     TIM_OCInitStructure.TIM_Pulse       = 500;
  28.     TIM_OCInitStructure.TIM_OCPolarity  = TIM_OCPolarity_High;
  29.     TIM_OC3Init( TIM2, & TIM_OCInitStructure );
  30.     TIM_OC3PreloadConfig( TIM2, TIM_OCPreload_Enable );
  31.     TIM_ARRPreloadConfig( TIM2, ENABLE );
  32.     TIM_Cmd( TIM2, ENABLE );
  33.     TIM_CtrlPWMOutputs( TIM2, ENABLE );
  34. }
  35. void GPIO_Configuration( void )
  36. {
  37.     GPIO_InitTypeDef GPIO_InitStructure;
  38.     RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE );
  39.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  40.     GPIO_InitStructure.GPIO_Speed   = GPIO_Speed_50MHz;
  41.     GPIO_InitStructure.GPIO_Mode    = GPIO_Mode_AF_PP;
  42.     GPIO_Init( GPIOA, & GPIO_InitStructure );
  43. }

附图一张,带解析的程序,原谅我懒得打…

大一小菜鸟血泪史:开启STM32F103ZET6的求虐模式(二)PWM输出方波

有注释的都截图了,更正一下这个是向下计数

打赏
原文链接:,转发请注明来源!

发表评论