BLDC 的FOC程序,分享别人的资料
单片机源程序如下:
- /*--------------------------------------------------
- 无线串口
- uart1与NRF24L01P实现
- STM32F103C8T6:
- IO:
- PA0~PA15,
- PB0~PB15,
- PC13~PC15,
- PD0~PD1,
- FLASH: 64K
- RAM: 20K
-
- 外晶振:4~16MHZ
- PA3(ADC12.3):AD_ISC
- PA4(ADC12.4):AD_ISB
- PA5(ADC12.5):AD_ISA
- PA9(TXD1)
- PA10(RXD1)
- PA7(TIM3.2):PWMA
- PB0(TIM3.3):PWMB
- PB1(TIM3.4):PWMC
- 资源配置:
- UART1:
- TIM3.2\TIM3.3\TIM3.4:三路PWM,向下计数模式使PWM后沿对齐,便于同时采样电流。
- 注意:
- 工程路径修改:
- options for target->C/C++->include paths
- ..\FWlib\inc;.\
- 两个点表示上一级目录,一个点表示与工程相同的目录
- 根据电流角度直接指定电压角度,电机可以反转,像电扇的同步电机一样
- 有必要测量电机的电感、电阻参数,这样才能估算反电势,从而得到转子位置
- 关于转子磁铁的定位:定子线圈的反电势过零点处对齐磁铁
- 20181018:发现获取电机L、R参数时存在问题,指定MOT_AMP后没加延时,所以参数不准,反电势也不准
- 如果参数准确,反电势测量准确,则在开环启动时手捏电机卡死,则测到的反电势应很小
- 转动时的电阻与静止时的电阻不一样?
- ----------------------------------------------------*/
- //--------------------------------------------------
- // 头文件
- //--------------------------------------------------
- #include "stm32f10x.h"
- #include "main.h"
- //--------------------------------------------------
- #define PWA_H IOSET(GPIOA->ODR,BIT(7))
- #define PWA_L IOCLR(GPIOA->ODR,BIT(7))
- #define PWA_XOR IOXOR(GPIOA->ODR,BIT(7))
- #define PWB_H IOSET(GPIOB->ODR,BIT(0))
- #define PWB_L IOCLR(GPIOB->ODR,BIT(0))
- #define PWB_XOR IOXOR(GPIOB->ODR,BIT(0))
- #define PWC_H IOSET(GPIOB->ODR,BIT(1))
- #define PWC_L IOCLR(GPIOB->ODR,BIT(1))
- #define PWC_XOR IOXOR(GPIOB->ODR,BIT(1))
- #define PTEST_H IOSET(GPIOA->ODR,BIT(2))
- #define PTEST_L IOCLR(GPIOA->ODR,BIT(2))
- #define PTEST_XOR IOXOR(GPIOA->ODR,BIT(2))
- //--------------------------------------------------
- U16 mainflag=0;
- enum {
- FLAG_START=BIT(0),
- FLAG_RUN=BIT(1)
- };
- void delayus(U32 t);
- U8 myatan(S16 x,S16 y);
- U16 GetRegAdc(U8 ch);
- //U16 DMA_ADCBuf[DMA_ADCBUFSIZE];
- //--------------------------------------------------
- // 声明
- //--------------------------------------------------
- #include "MOTFOC.C"
- #include "uart.c"
- #include "stm32f10x_it.c"
- //--------------------------------------------------
- // 短延时
- //--------------------------------------------------
- void delay(__IO uint32_t nCount)
- {
- for(; nCount != 0; nCount--);
- }
- //--------------------------------------------------
- // 微秒延时
- //--------------------------------------------------
- void delayus(U32 t)
- {
- U32 i;
- for(;t;t--)
- {
- for(i=8;i;i--);
- }
- }
- //--------------------------------------------------
- // 毫秒延时
- //--------------------------------------------------
- void delayms(U32 t)
- {
- U32 i;
- while(t--)
- {
- for(i=FOSC/4800;i>0;i--);
- }
- }
- //--------------------------------------------------
- //电机发声
- //--------------------------------------------------
- /*void Beep(U8 m,U8 t)
- {
- U16 i,t1;
- if(m==0)return;
- i=1000/m;
- t1=(i+100)*t*m/10;
- while(t1--)
- {
- BEEP_L;
- delayus(100);
- BEEP_H;
- delayus(i);
- }
- }*/
- /*
- void Beep(U8 m,U8 t)
- {
- U16 t1;
- t1=6000/(m+6);
- beep_td=t;
- if(m)
- {
- TIM_SetAutoreload(TIM4,t1);
- TIM_SetCompare1(TIM4,50);
- }
- }
- //等待鸣响完成
- void BeepW(U8 m,U8 t)
- {
- Beep(m,t);
- while(beep_td);
- }
- */
- //--------------------------------------------------
- //ADC初始化
- //--------------------------------------------------
- void ADC_Conf(void)
- {
- ADC_InitTypeDef ADC_InitStructure;
- //DMA_InitTypeDef DMA_InitStructure;
- //RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//使能TIM2的RCC时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1|RCC_APB2Periph_ADC2, ENABLE); // Enable ADC1 and GPIOC clock
- ADC_InitStructure.ADC_Mode =ADC_Mode_Independent;//独立模式 ADC_Mode_RegSimult;//规则同步模式//
- ADC_InitStructure.ADC_ScanConvMode = DISABLE;//ENABLE;//扫描模式?
- ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//单次模式//ENABLE;//连续模式
- ADC_InitStructure.ADC_ExternalTrigConv =ADC_ExternalTrigConv_None;//由软件触发,不由外部触发 ADC_ExternalTrigConv_T2_CC2; //ADC_ExternalTrigConv_T2_CC2;//定时器2的CC2触发//
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//右对齐
- ADC_InitStructure.ADC_NbrOfChannel = 1;
- ADC_Init(ADC1, &ADC_InitStructure);
- // ADC1 regular channel14 configuration
- ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 1, ADC_SampleTime_1Cycles5);
- ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 2, ADC_SampleTime_1Cycles5);
- ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 3, ADC_SampleTime_1Cycles5);
-
- //ADC_InjectedSequencerLengthConfig(ADC1, 1);//设置注入通道长度
- //ADC_InjectedChannelConfig(ADC1, ADC_Channel_3, 1, ADC_SampleTime_13Cycles5);//注入组为一个ADC3
- //ADC_ExternalTrigInjectedConvConfig(ADC1, ADC_ExternalTrigInjecConv_None);//不用外触发
- //ADC_SoftwareStartInjectedConvCmd(ADC1, ENABLE);//开始注入通道数据采样和转换
-
- // Enable ADC1 DMA
- //ADC_DMACmd(ADC1, ENABLE);//使能DMA
- //ADC_ExternalTrigConvCmd(ADC1, ENABLE);//使能外部触发
- // Enable ADC1
- ADC_Cmd(ADC1, ENABLE);//启动一次转换?
- // Enable ADC1 reset calibaration register
- ADC_ResetCalibration(ADC1);//重置指定ADC的校准寄存器
- //Check the end of ADC1 reset calibration register
- while(ADC_GetResetCalibrationStatus(ADC1));
- // Start ADC1 calibaration
- ADC_StartCalibration(ADC1);
- // Check the end of ADC1 calibration
- while(ADC_GetCalibrationStatus(ADC1));
- // Start ADC1 Software Conversion
- //ADC_SoftwareStartConvCmd(ADC1, ENABLE);//使能软件转换启动
- ADC_Cmd(ADC1, ENABLE);//启动一次转换?
- }
- //--------------------------------------------------
- //取得规则组ADC值
- //--------------------------------------------------
- U16 GetRegAdc(U8 ch)
- {
- U16 r;
- r=ADC1->SQR3;//规则组第一次转换通道在SQR3低0~4位
- r=(r&0xffe0)|ch;
- ADC1->SQR3=r;
- //ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 1, ADC_SampleTime_7Cycles5);
- ADC_SoftwareStartConvCmd(ADC1,ENABLE);
- while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));
- return ADC_GetConversionValue(ADC1);
- }
- //--------------------------------------------------
- //等待采集完数据后发送给上位机显示波形
- //--------------------------------------------------
- void SendADbuf(void)
- {
- S16 i;
- while(ad_p<ADCBUFSIZE);
- for(i=0;i<ADCBUFSIZE;i++)
- {
- UART1_SendByte(0xaa);
- UART1_SendByte(10);
- UART1_SendByte(adbuf[0][i]>>8);
- UART1_SendByte(adbuf[0][i]);
- UART1_SendByte(adbuf[1][i]>>8);
- UART1_SendByte(adbuf[1][i]);
- UART1_SendByte(adbuf[2][i]>>8);
- UART1_SendByte(adbuf[2][i]);
- UART1_SendByte(adbuf[3][i]>>8);
- UART1_SendByte(adbuf[3][i]);
- UART1_SendByte(adbuf[4][i]>>8);
- UART1_SendByte(adbuf[4][i]);
- }
- }
- //--------------------------------------------------
- // IO口初始化
- //--------------------------------------------------
- void io_init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB
- |RCC_APB2Periph_AFIO,ENABLE); //使能外设时钟//AFIO用于REMAP
-
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//某些引脚需重映射
- //GPIO_PinRemapConfig(GPIO_Remap_SPI1,DISABLE);
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- /*
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin = BIT(5)|BIT(7);//SPI1_SCK,SPI1_MOSI
- GPIO_Init(GPIOA, &GPIO_InitStructure); */
- //AD_ISA,AD_ISB,AD_ISC
- GPIO_InitStructure.GPIO_Pin = BIT(5)|BIT(4)|BIT(3);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- //PWMA
- GPIO_InitStructure.GPIO_Pin = BIT(7);//
- GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP;// GPIO_Mode_Out_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- //PTEST
- GPIO_InitStructure.GPIO_Pin = BIT(2);//
- GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- //PWMB,PWMC
- GPIO_InitStructure.GPIO_Pin = BIT(1)|BIT(0);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//GPIO_Mode_Out_PP;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
- //--------------------------------------------------
- // 定时器初始化
- //APB1=56M/2=28M
- //TIM2用于触发ADC
- //TIM3用于定时处理(10KHz)
- //TIM4_CH1:蜂鸣器
- //--------------------------------------------------
- void Timer_Init(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
-
- TIM_TimeBaseStructure.TIM_Period= 256-1; //自动重装值,为0时不工作
- TIM_TimeBaseStructure.TIM_Prescaler= 10-1; //时钟频率预分频(变为1M)
- //向上计数:0~TIM_Period 向下计数:TIM_Period~0
- //TIM_TimeBaseStructure.TIM_CounterMode= TIM_CounterMode_Up; //向上计数模式
- TIM_TimeBaseStructure.TIM_CounterMode= TIM_CounterMode_Down; //向下计数模式
- TIM_TimeBaseStructure.TIM_ClockDivision= TIM_CKD_DIV1; //时钟分频因子
-
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);//使能TIM3的RCC时钟
- TIM_DeInit(TIM3);
- TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
- TIM_InternalClockConfig(TIM3);//由内部提供时钟.
-
- TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);//使能更新事件中断
- TIM_Cmd(TIM3,ENABLE); //使能定时器
-
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//TIM_OCMode_Toggle// 输出比较触发模式
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//貌似必须设置为输出才行
- TIM_OCInitStructure.TIM_Pulse = 100;//
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
- TIM_OC2Init(TIM3, &TIM_OCInitStructure);
- TIM_OC3Init(TIM3, &TIM_OCInitStructure);
- TIM_OC4Init(TIM3, &TIM_OCInitStructure);
-
- TIM3->CCR2=0;
- TIM3->CCR3=40;
- TIM3->CCR4=255;
- //TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable);
- //TIM_PrescalerConfig(TIM2,1,TIM_PSCReloadMode_Immediate);//立即装入预分频
- //TIM_ITConfig(TIM2,TIM_IT_CC2,ENABLE);//使能TIM2捕获\比较中断
- }
- //--------------------------------------------------
- // NVIC初始化
- //--------------------------------------------------
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- //EXTI_InitTypeDef EXTI_InitStructure;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;//占先
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //
- NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //选择TIM3中断
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;//
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能
- NVIC_Init(&NVIC_InitStructure);
-
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;//占先
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_Init(&NVIC_InitStructure);
-
- }
- //--------------------------------------------------
- //使用HSI:8/2*16=64MHZ
- //--------------------------------------------------
- void RCC_Configuration(void)
- {
- RCC_DeInit();
-
- RCC_HSICmd(ENABLE);
-
- while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET)
- {
-
- }
-
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- FLASH_SetLatency(FLASH_Latency_2);
- RCC_HCLKConfig(RCC_SYSCLK_Div1); //AHB时钟为系统时钟SYSCLK
- RCC_PCLK2Config(RCC_HCLK_Div1); //APB1时钟为HCLK/2,其中HCLK为AHB时钟
- RCC_PCLK1Config(RCC_HCLK_Div2); //APB2时钟为HCLK
- //设置 PLL 时钟源及倍频系数
- RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_16); //8/2*16=64MHZ
- //使能或者失能 PLL,这个参数可以取:ENABLE或者DISABLE
- RCC_PLLCmd(ENABLE);//如果PLL被用于系统时钟,那么它不能被失能
- //等待指定的 RCC 标志位设置成功 等待PLL初始化成功
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
- {
-
- }
- //设置系统时钟(SYSCLK) 设置PLL为系统时钟源
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
- //等待PLL成功用作于系统时钟的时钟源
- // 0x00:HSI 作为系统时钟
- // 0x04:HSE作为系统时钟
- // 0x08:PLL作为系统时钟
- while(RCC_GetSYSCLKSource() != 0x08)
- {
-
- }
- //RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
- //RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
- }
- //--------------------------------------------------
- // 初始化所有的设备
- //--------------------------------------------------
- void init_devices(void)
- {
- //SystemInit();
- RCC_Configuration();
- io_init();
- UART_Config(38400);
- Timer_Init();
- ADC_Conf();
- NVIC_Configuration();
- delayms(100);
- //LCD_Init();
- }
- //--------------------------------------------------
- // 主函数
- //--------------------------------------------------
- int main(void)
- {
- S16 i=1,u,v,w,x,y;
- U8 p;
- init_devices();
- /*
- //------------------------------myatan验证
- for(i=0;i<256;i++)//一周期
- {
- p=i;
- u=costab[p];
- p-=256/3;//360度/3=120度
- v=costab[p];
- p-=256/3;//360度/3=120度
- w=costab[p];
-
- x=myatan(u,v);
- UART1_SendByte(0xaa);
- UART1_SendByte(8);
- UART1_SendByte(u>>8);
- UART1_SendByte(u);
- UART1_SendByte(v>>8);
- UART1_SendByte(v);
- UART1_SendByte(w>>8);
- UART1_SendByte(w);
- UART1_SendByte(x>>8);
- UART1_SendByte(x);
- }
- //------------------------------Clarke变换验证
- for(i=0;i<256;i++)//一周期
- {
- p=i;
- u=costab[p];
- p-=256/3;//360度/3=120度
- v=costab[p];
- ClarkeTrans(u,v,&x,&y);
- x+=10;//偏移10以免波形重叠
- y+=10;
- UART1_SendByte(0xaa);
- UART1_SendByte(8);
- UART1_SendByte(u>>8);
- UART1_SendByte(u);
- UART1_SendByte(v>>8);
- UART1_SendByte(v);
- UART1_SendByte(x>>8);
- UART1_SendByte(x);
- UART1_SendByte(y>>8);
- UART1_SendByte(y);
- }
- //------------------------------Clarke逆变换验证
- for(i=0;i<256;i++)//一周期
- {
- p=i;
- u=costab[p];
- p-=256/3;//360度/3=120度
- v=costab[p];
- ClarkeTrans(u,v,&x,&y);
- ClarkeInvTrans(x,y,&u,&v,&w);
- x+=10;//偏移10以免波形重叠
- y+=10;
- UART1_SendByte(0xaa);
- UART1_SendByte(8);
- UART1_SendByte(u>>8);
- UART1_SendByte(u);
- UART1_SendByte(v>>8);
- UART1_SendByte(v);
- UART1_SendByte(x>>8);
- UART1_SendByte(x);
- UART1_SendByte(y>>8);
- UART1_SendByte(y);
- }
- //------------------------------Park变换验证
- for(i=0;i<256;i++)//一周期
- {
- p=0;//u\v固定为0度
- u=costab[p];
- p-=256/4;//360度/4=90度
- v=costab[p];
- ParkTrans(u,v,i,&x,&y);//Park变换(旋转i度(2PI=256))
- UART1_SendByte(0xaa);
- UART1_SendByte(8);
- UART1_SendByte(u>>8);
- UART1_SendByte(u);
- UART1_SendByte(v>>8);
- UART1_SendByte(v);
- UART1_SendByte(x>>8);
- UART1_SendByte(x);
- UART1_SendByte(y>>8);
- UART1_SendByte(y);
- }
- //i=1;
- //while(i);
- delayms(3000);// */
- //------------------------------电流校零
- Mot_State=0;
- delayms(10);
- Mot_State=1;
- delayms(10);//等待电流校零
- //------------------------------获取电机参数
- //指定相电压
- Mot_AMP=(7*256/47);//需要0.5V,电源4.7V
- Mot_State=2;//取得电机参数
- delayms(100);//由于电机会略有转动,等长一点时间后重新采集数据
- Mot_State=0;//停止
- delayms(1); //等到续流为0后再开始采集
- ad_p=0;//开始采集数据
- Mot_State=2;//取得电机参数
- delayms(100);//iu=0,42,68,...,109
- while(ad_p<ADCBUFSIZE);//等数据采集完
- Mot_State=0;
- if(adbuf[0][1]==adbuf[0][0])//防止除数为0
- adbuf[0][1]=adbuf[0][0]+1;
- Mot_L=(U16)Mot_AMP*100/(adbuf[0][1]-adbuf[0][0]);//表示pwm=1时每周期的电流增量*100
- Mot_R=(U16)Mot_AMP*100/adbuf[0][ADCBUFSIZE-1];//表示PWM每变化1时电流的变化量*100
- adbuf[3][ADCBUFSIZE-1]=Mot_L;//在上位机中查看此数据
- adbuf[4][ADCBUFSIZE-1]=Mot_R;
- /*
- SendADbuf();//发给上位机查看波形
-
- //------------------------------验证电感、电阻计算公式
- Mot_AMP=(5*256/47);//相电压0.5V
- Mot_State=2;//取得电机参数
- delayms(50);//由于电机会略有转动,等长一点时间后重新采集数据
- Mot_State=0;//停止
- delayms(1);
- ad_p=0;//开始采集数据
- Mot_State=2;//取得电机参数
- delayms(20);//iu=0,42,68,...,109
- Mot_State=0;
- SendADbuf();// */
- //------------------------------
- Mot_State=0;
- delayms(1);
- //------------------------------开环启动
- Mot_AMP=45;
- ad_p=0;
- Mot_State=3;//开环启动
- SendADbuf();
- delayms(1000);
- ad_p=0;
- SendADbuf();
-
- ad_p=0;
- while(ad_p<ADCBUFSIZE/3);
- Mot_State=4;//开始闭环
- SendADbuf();
- while(1)
- {
- ad_p=0;
- SendADbuf();
- delayms(2000);
- }
- /* while(1)//调压
- {
- Mot_AMP=50;delayms(2000);
- ad_p=0;
- SendADbuf();
- Mot_AMP=150;delayms(2000);
- ad_p=0;
- SendADbuf();
- } */
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval : None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* Infinite loop */
- while (1){}
- }
- #endif
复制代码
所有资料51hei提供下载:
程序.rar
(878.55 KB, 下载次数: 380)
|