stm32 刹车

来源:互联网 发布:js改变div display 编辑:程序博客网 时间:2024/06/09 15:34

摘自:http://blog.csdn.net/ylgm44/article/details/39370053


需求:

1. PWM互补输出

2. 过流保护,产生过流,立即停止pwm输出,并保证按照互补方式停止pwm输出。

3. 单周期保护,产生过流,当前脉冲周期停止输出,下一个脉冲周期自动回复输出,停止输出按照互补方式停止。

一路pwm的保护只能采用2/3中的一种。


过程:

pwm互补输出配置比较简单,没有问题。过流保护采用刹车功能进行保护。

第一次尝试,刹车功能不好使,刹车部分代码如下:


刹车管脚配置:

[cpp] view plain copy
  1. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;  
  2. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_2;  
  3. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;  
  4. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;  
  5. GPIO_Init(GPIOB , &GPIO_InitStructure);   
  6. GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_2);  

定时器设置:

[cpp] view plain copy
  1. TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;  
  2. TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;  
  3. TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;  
  4. TIM_BDTRInitStructure.TIM_DeadTime = 48;  
  5. TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;  
  6. TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low;  
  7. TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;  
  8. TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);  

结果,如果设置刹车引脚有效极性为低电平:

[cpp] view plain copy
  1. TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low;  

则pwm一直不输出。


如果设置刹车引脚有效极性为高电平:

[cpp] view plain copy
  1. TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Hight;  
则pwm一直输出,刹车引脚置为低电平,也不能停止pwm输出。

增加刹车中断,发现,如果设置刹车引脚为低,一直进中断。怀疑管脚复用有问题,更改PB12的管脚模式为复用模式:

[cpp] view plain copy
  1. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;  
问题解决。


代码:

[cpp] view plain copy
  1. #include "stm32f0xx.h"  
  2.   
  3. void TIM_Config(void)  
  4. {  
  5.   GPIO_InitTypeDef GPIO_InitStructure;  
  6.   
  7.   RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);  
  8.     
  9.   
  10.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_7;  
  11.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;                    
  12.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  13.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  
  14.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;  
  15.   GPIO_Init(GPIOA, &GPIO_InitStructure);  
  16.     
  17.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_2);  
  18.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_2);  
  19.   
  20.       
  21.   //GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; <span style="white-space:pre">   </span>// 原先错误的配置  
  22.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;<span style="white-space:pre">      </span>// 正确的配置  
  23.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_2;  
  24.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;  
  25.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;  
  26.   GPIO_Init(GPIOB , &GPIO_InitStructure);     
  27.   GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_2);  
  28.         
  29. }  
  30.   
  31. void TIM_PWM_Config(void)  
  32. {  
  33.   TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;  
  34.   TIM_OCInitTypeDef<span style="white-space:pre"> </span>   TIM_OCInitStructure;  
  35.   TIM_BDTRInitTypeDef<span style="white-space:pre">   </span>   TIM_BDTRInitStructure;  
  36.       
  37.   uint16_t<span style="white-space:pre">      </span>   TimerPeriod = 0;  
  38.   uint16_t<span style="white-space:pre">      </span>   Channel1Pulse = 0;  
  39.   
  40.   // calc freq    
  41.   TimerPeriod = (SystemCoreClock / 10000 ) - 1;  
  42.   // calc duty cycle  
  43.   Channel1Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10);  
  44.   
  45.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);  
  46.     
  47.   TIM_TimeBaseStructure.TIM_Prescaler = 0;  
  48.   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
  49.   TIM_TimeBaseStructure.TIM_Period = TimerPeriod;  
  50.   TIM_TimeBaseStructure.TIM_ClockDivision = 0;  
  51.   TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;  
  52.   TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);  
  53.   
  54.   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;  
  55.   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;   
  56.   TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;  
  57.   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;  
  58.   TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;  
  59.   TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;  
  60.   TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;  
  61.   TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;  
  62.   TIM_OC1Init(TIM1, &TIM_OCInitStructure);  
  63.       
  64.   TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;  
  65.   TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;  
  66.   TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;  
  67.   TIM_BDTRInitStructure.TIM_DeadTime = 48;  
  68.   TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;  
  69.   TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low;  
  70.   //TIM_AutomaticOutput_Enable: 刹车后,刹车输入电平变为无效无效后,下一个脉冲周期,自动回复脉冲输出。  
  71.   //TIM_AutomaticOutput_Disable: 刹车后,脉冲输出永久禁止,除非手动调用TIM_CtrlPWMOutputs(TIM1, ENABLE),否则不能启动pwm输出。  
  72.   TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;  
  73.   TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);  
  74.   
  75.   TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);  
  76.   TIM_ARRPreloadConfig(TIM1, ENABLE);                                                         
  77.       
  78.   
  79.   TIM_Cmd(TIM1, ENABLE);  
  80.       
  81.   TIM_CtrlPWMOutputs(TIM1, ENABLE);   
  82. }  
  83.   
  84. int main(void)  
  85. {  
  86.   TIM_Config();  
  87.   TIM_PWM_Config();  
  88.   while (1)  
  89.   {  
  90.   }  
  91. }