新手上路
- 积分
- 33
- 金钱
- 33
- 注册时间
- 2015-4-21
- 在线时间
- 0 小时
|
5金钱
- /* MAIN.C file<br />
- * <br />
- * Copyright (c) 2002-2005 STMicroelectronics<br />
- */<br />
- <br />
- <br />
- /* MAIN.C file<br />
- 摘要:<br />
- 1.PD7延时反转,指示系统工作。<br />
- 2.上电,按指定PWM输出换相,实现电机基本运转功能。<br />
- 3.换相依据为HALl信号,采用PORTD中断功能完成.<br />
- 总之,上电根据HALL状态换相,并在输出PWM信号后,电机会转动,<br />
- 同时HALL信号不断改变,然后进入捕获中断,进行及时换相。<br />
- 如此实现电机的运转控制。<br />
- 2013.5.8<br />
- */<br />
- #include "stm8s.h"<br />
- <br />
- #define STM8_FREQ_MHZ 16<br />
- #define PWM_FREQUENCY 16000<br />
- <br />
- static const u16 hArrPwmVal = ((u16)((STM8_FREQ_MHZ * (u32)1000000)/PWM_FREQUENCY));<br />
- //PWM信号周期<br />
- <br />
- #define PWMOUT 20<br />
- //按15%占空比输出<br />
- <br />
- const unsigned char PWM_EN1_TAB[6]={0x00,0x00,0x10,0x10,0x01,0x01};<br />
- //六步法中,CH1\CH2通道极性及使能配置<br />
- const unsigned char PWM_EN2_TAB[6]={0x01,0x01,0x00,0x00,0x0,0x00};<br />
- //六步法中,CH3通道极性及使能配置<br />
- <br />
- //上桥臂开关控制端口定义<br />
- #define MCO1_PORT GPIOC<br />
- #define MCO1_PIN GPIO_PIN_1<br />
- #define MCO3_PORT GPIOC<br />
- #define MCO3_PIN GPIO_PIN_2<br />
- #define MCO5_PORT GPIOC<br />
- #define MCO5_PIN GPIO_PIN_3<br />
- <br />
- //下桥臂开关控制端口定义<br />
- #define MCO0_PORT GPIOB<br />
- #define MCO0_PIN GPIO_PIN_0<br />
- #define MCO2_PORT GPIOB<br />
- #define MCO2_PIN GPIO_PIN_1<br />
- #define MCO4_PORT GPIOB<br />
- #define MCO4_PIN GPIO_PIN_2<br />
- //下桥臂低电平开关管导通<br />
- #define PWM_A_OFF MCO0_PORT->ODR |= (u8)MCO0_PIN; <br />
- #define PWM_B_OFF MCO2_PORT->ODR |= (u8)MCO2_PIN; <br />
- #define PWM_C_OFF MCO4_PORT->ODR |= (u8)MCO4_PIN; <br />
- <br />
- #define PWM_A_ON MCO0_PORT->ODR &= (u8)(~MCO0_PIN); <br />
- #define PWM_B_ON MCO2_PORT->ODR &= (u8)(~MCO2_PIN); <br />
- #define PWM_C_ON MCO4_PORT->ODR &= (u8)(~MCO4_PIN); <br />
- <br />
- //霍尔引脚定义<br />
- #define H1_PORT GPIOD->IDR<br />
- #define H1_PIN BIT2<br />
- <br />
- #define H2_PORT GPIOD->IDR<br />
- #define H2_PIN BIT3<br />
- <br />
- #define H3_PORT GPIOD->IDR<br />
- #define H3_PIN BIT4<br />
- <br />
- unsigned char bHallSteps[2][8]={<br />
- {7,5,3,4,1,0,2,7},//正转<br />
- {7,2,0,1,4,3,5,7}//反转<br />
- };<br />
- <br />
- /* Private vars and define */<br />
- #define BIT0 0x01<br />
- #define BIT1 0x02<br />
- #define BIT2 0x04<br />
- #define BIT3 0x08<br />
- #define BIT4 0x10<br />
- #define BIT5 0x20<br />
- #define BIT6 0x40<br />
- #define BIT7 0x80<br />
- <br />
- //换相子函数声明<br />
- void Commutation(unsigned char bHallStartStep,unsigned int OutPwmValue);<br />
- <br />
- unsigned int outpwm=0;<br />
- <br />
- <br />
- //初始化按键,指示灯端口<br />
- void GPIO_int(void)<br />
- {<br />
- /* LEDs */<br />
- GPIO_Init(GPIOD, GPIO_PIN_7, GPIO_MODE_OUT_PP_HIGH_FAST);<br />
- }<br />
- <br />
- //系统时钟配置:内部16M<br />
- void Clock_init(void)<br />
- {<br />
- /* Select fCPU = 16MHz */<br />
- CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV1);<br />
- }<br />
- <br />
- //换相电路开关管IO初始化<br />
- void PWM_IO_init(void)<br />
- { <br />
- //PB012 下桥臂0有效 ,配置为高电平<br />
- GPIO_Init(MCO0_PORT, MCO0_PIN,GPIO_MODE_OUT_PP_HIGH_FAST);<br />
- GPIO_Init(MCO2_PORT, MCO2_PIN,GPIO_MODE_OUT_PP_HIGH_FAST);<br />
- GPIO_Init(MCO4_PORT, MCO4_PIN,GPIO_MODE_OUT_PP_HIGH_FAST);<br />
- <br />
- //PC123 上桥臂1有效,配置为低电平<br />
- GPIO_Init(MCO1_PORT, MCO1_PIN,GPIO_MODE_OUT_PP_LOW_FAST);<br />
- GPIO_Init(MCO3_PORT, MCO3_PIN,GPIO_MODE_OUT_PP_LOW_FAST);<br />
- GPIO_Init(MCO5_PORT, MCO5_PIN,GPIO_MODE_OUT_PP_LOW_FAST); <br />
- }<br />
- <br />
- void Tim1_init(void)<br />
- {<br />
- /* TIM1 Peripheral Configuration */ <br />
- TIM1_DeInit();<br />
- <br />
- TIM1_TimeBaseInit(0, TIM1_COUNTERMODE_UP, hArrPwmVal, 0);<br />
- <br />
- TIM1_OC1Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_DISABLE, hArrPwmVal*0, TIM1_OCPOLARITY_HIGH, TIM1_OCNPOLARITY_LOW, TIM1_OCIDLESTATE_RESET, TIM1_OCNIDLESTATE_SET); <br />
- <br />
- TIM1_OC2Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_DISABLE, hArrPwmVal*0, TIM1_OCPOLARITY_HIGH, TIM1_OCNPOLARITY_LOW, TIM1_OCIDLESTATE_RESET, TIM1_OCNIDLESTATE_SET); <br />
- <br />
- TIM1_OC3Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_DISABLE, hArrPwmVal*0, TIM1_OCPOLARITY_HIGH, TIM1_OCNPOLARITY_LOW, TIM1_OCIDLESTATE_RESET, TIM1_OCNIDLESTATE_SET); <br />
- <br />
- TIM1_CCPreloadControl(DISABLE);<br />
- TIM1_Cmd(ENABLE);<br />
- }<br />
- <br />
- //初始化HALL捕获输入定义器,采用PD口外部中断捕获输入<br />
- void HALLEXTI_INIT(void)<br />
- {<br />
- GPIO_Init(GPIOD, GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4,GPIO_MODE_IN_PU_IT); //HALL端口模式配置<br />
- }<br />
- <br />
- //根据HALL状态换相,启动电机运行<br />
- void HALL_START(void)<br />
- {<br />
- u8 bHStatus = 0,bHallStartStep;<br />
- <br />
- // Read status of H1<br />
- if (H1_PORT & H1_PIN)<br />
- {<br />
- bHStatus |= BIT2;<br />
- }<br />
- <br />
- // Read status of H2 <br />
- if (H2_PORT & H2_PIN)<br />
- {<br />
- bHStatus |= BIT1;<br />
- }<br />
- <br />
- // Read status of H3 <br />
- if (H3_PORT & H3_PIN)<br />
- { <br />
- bHStatus |= BIT0;<br />
- }<br />
- <br />
- bHallStartStep = bHallSteps[1][bHStatus];//得到换相步序<br />
- <br />
- if (bHallStartStep == 7)//不该出现的HALL状态<br />
- { <br />
- return;<br />
- }<br />
- <br />
- <br />
- GPIOD->CR2|=BIT3|BIT2|BIT4;<br />
- EXTI->CR1|=BIT6|BIT7;<br />
- Commutation(bHallStartStep,outpwm);//输出PWM信号,启动电机<br />
- }<br />
- <br />
- @far @interrupt void EXTI_PORTD_IRQHandler(void)<br />
- {<br />
- /* in order to detect unexpected events during development, <br />
- it is recommended to set a breakpoint on the following instruction<br />
- */<br />
- u8 bHStatus =0,i,bHallStartStep;<br />
- <br />
- bHStatus=(GPIO_ReadInputData(GPIOD)>>2)&0x07;<br />
- for(i=0;i<50;i++);//滤波时间<br />
- i=(GPIO_ReadInputData(GPIOD)>>2)&0x07;<br />
- if(bHStatus!=i)return;<br />
- <br />
- bHStatus=0; <br />
- // Read status of H1<br />
- if (H1_PORT & H1_PIN)<br />
- {<br />
- bHStatus |= BIT2;<br />
- }<br />
- <br />
- // Read status of H2 <br />
- if (H2_PORT & H2_PIN)<br />
- {<br />
- bHStatus |= BIT1;<br />
- }<br />
- <br />
- // Read status of H3 <br />
- if (H3_PORT & H3_PIN)<br />
- {<br />
- bHStatus |= BIT0;<br />
- }<br />
- <br />
- bHallStartStep = bHallSteps[1][bHStatus];//得到换相步序<br />
- <br />
- if (bHallStartStep == 7)<br />
- { //故障,停止输出<br />
- TIM1->BKR &= (uint8_t)(~TIM1_BKR_MOE);//禁止PWM输出<br />
- PWM_A_OFF;<br />
- PWM_B_OFF;<br />
- PWM_C_OFF;<br />
- return;<br />
- }<br />
- <br />
- <br />
- EXTI->CR1|=BIT6|BIT7;<br />
- Commutation(bHallStartStep,outpwm);//输出PWM信号,启动电机<br />
- return;<br />
- }<br />
- <br />
- void ChongD(void)<br />
- { <br />
- unsigned int tem_c=0;<br />
- PWM_A_ON;PWM_B_ON;PWM_C_ON;<br />
- for(tem_c=0;tem_c<50000;tem_c++);<br />
- PWM_A_OFF;PWM_B_OFF;PWM_C_OFF;<br />
- }<br />
- <br />
- main()<br />
- {<br />
- unsigned int tem_c=0;<br />
- unsigned char step=0;<br />
- <br />
- for(tem_c=0;tem_c<50000;tem_c++);//上电延时,等待系统稳定<br />
- <br />
- Clock_init();//指示灯端口初始化<br />
- GPIO_int();//时钟配置<br />
- <br />
- PWM_IO_init();//开关管控制端口初始化<br />
- Tim1_init();//高级定时器配置 <br />
- HALLEXTI_INIT();//HALL端口初始化配置<br />
- ChongD();<br />
- outpwm=hArrPwmVal*PWMOUT/100;//固定占空比的PWM值<br />
- HALL_START();//根据HALL状态输出PWM状态,启动电机运行<br />
- enableInterrupts();<br />
- <br />
- while (1);<br />
- {<br />
- for(tem_c=0;tem_c<50000;tem_c++);//延时时间<br />
- GPIO_WriteReverse(GPIOD,GPIO_PIN_7);//PD7指示灯反转<br />
- }<br />
- }<br />
- <br />
- //换向输出PWM值,<br />
- //bHallStartStep:当前换相步序0-5,OutPwmValue 输出PWM值<br />
- void Commutation(unsigned char bHallStartStep,unsigned int OutPwmValue)<br />
- { <br />
- TIM1->BKR &= (uint8_t)(~TIM1_BKR_MOE);//禁止PWM输出<br />
- if(bHallStartStep!=3&&bHallStartStep!=4)<br />
- PWM_A_OFF;<br />
- if(bHallStartStep!=0&&bHallStartStep!=5)<br />
- PWM_B_OFF;<br />
- if(bHallStartStep!=1&&bHallStartStep!=2)<br />
- PWM_C_OFF;<br />
- <br />
- //根据换相步序,打开不同的开关管,并施加正确的PWM信号<br />
- if(bHallStartStep==0)//AB<br />
- {<br />
- TIM1->CCR3H = (uint8_t)(OutPwmValue >> 8);<br />
- TIM1->CCR3L = (uint8_t)(OutPwmValue);<br />
- PWM_B_ON;<br />
- }<br />
- else if(bHallStartStep==1) //AC<br />
- {<br />
- TIM1->CCR3H = (uint8_t)(OutPwmValue >> 8);<br />
- TIM1->CCR3L = (uint8_t)(OutPwmValue);<br />
- PWM_C_ON;<br />
- }<br />
- else if(bHallStartStep==2) //BC<br />
- {<br />
- TIM1->CCR2H = (uint8_t)(OutPwmValue >> 8);<br />
- TIM1->CCR2L = (uint8_t)(OutPwmValue);<br />
- PWM_C_ON;<br />
- }<br />
- else if(bHallStartStep==3) //BA<br />
- {<br />
- TIM1->CCR2H = (uint8_t)(OutPwmValue >> 8);<br />
- TIM1->CCR2L = (uint8_t)(OutPwmValue);<br />
- PWM_A_ON;<br />
- }<br />
- else if(bHallStartStep==4)//CA<br />
- {<br />
- TIM1->CCR1H = (uint8_t)(OutPwmValue >> 8);<br />
- TIM1->CCR1L = (uint8_t)(OutPwmValue);<br />
- PWM_A_ON;<br />
- }<br />
- else if(bHallStartStep==5) //CB<br />
- {<br />
- TIM1->CCR1H = (uint8_t)(OutPwmValue >> 8);<br />
- TIM1->CCR1L = (uint8_t)(OutPwmValue);<br />
- PWM_B_ON;<br />
- }<br />
- <br />
- TIM1->CCER1=PWM_EN1_TAB[bHallStartStep];<br />
- TIM1->CCER2=PWM_EN2_TAB[bHallStartStep]; <br />
- TIM1->BKR|=TIM1_BKR_MOE;//使能PWM输出<br />
- }<br />
- <br />
- #ifdef USE_FULL_ASSERT<br />
- <br />
- /**<br />
- * @brief Reports the name of the source file and the source line number<br />
- * where the assert_param error has occurred.<br />
- * @param file: pointer to the source file name<br />
- * @param line: assert_param error line source number<br />
- * @retval : None<br />
- */<br />
- void assert_failed(u8* file, u32 line)<br />
- { <br />
- /* User can add his own implementation to report the file name and line number,<br />
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */<br />
- <br />
- /* Infinite loop */<br />
- while (1)<br />
- {<br />
- }<br />
- }<br />
- #endif
复制代码
程序控制三相120°无刷直流电机,芯片为stm8s207rb其中有六状态,本应该是下面六种相序
UH WL UH VL
WH VL WH UL
VH UL VH WL
用PD口外部中断捕获霍尔信号,却出现
UH VL WL UH UL WL
WH WL UL WH WL UL
VH VL UL VH VL WL
写的程序出现了上下桥同时导通的情况
请问各位大神,这个该怎么解决?????
|
|