stm32控制步步进电机,角度控制与速度控制程序
引脚定义:
步进电机驱动器
PB8----PULS+:脉冲输出
PB9----DIR+:方向输出
按键
PB12----KEY1
PB13----KEY2
PB14----KEY3
PB15----KEY4
槽型光耦模块
PB11----Sensor_Input
24c16 EEPROM 存储器
PC13----IIC_SDA
PC14----IIC_SCL
串口通讯
PA09(TXD)----RXD
PA10(RXD)----TXD
单片机源程序如下:
- //步进电机启动程序
- #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- #include "input.h"
- #include "output.h"
- #include "24cxx.h"
- vu32 speed=1000;//步进电机速度设定
- s8 procedure;//记录移动次数
- u8 TEXT_Buffer[]={120,124,100,126,127};//存放将要写入24c02数据
- #define SIZE sizeof(TEXT_Buffer)//获取字节长度,保证写入读取数据长度相同
- u8 datatemp[SIZE];//存放从24c02读取的数据
- u32 TEXT_32Buffer=65535;//存放将要写入24c02数据32位
- u32 data32_temp;//存放从24c02读取的数据32位
- u8 a=0,b=0;
- int main(void)
- {
- delay_init();//延时函数初始化
- uart_init(9600);
- Signal_Input_Init();//GPIO IN初始化
- Signal_Output_Init();//GPIO OUT初始化
- AT24CXX_Init();
- while(AT24CXX_Check())//检测不到24c02
- {
- printf("检测不到24cXX\r\n") ;
- delay_ms(1000);
- break;
- }
- while(1)
- {
- if(KEY_Scan(0,KEY1))
- {
- Fixed_Pulse_Output(0,625);
- procedure++;
- if(procedure>16)procedure=0;
- printf("procedure+=%d\r\n",procedure);
- AT24CXX_Write(0,TEXT_Buffer,SIZE);//写入数据
-
- // for(a=0;a<SIZE;a++)
- // {
- // printf("TEXT_Buffer=%d\r\n",TEXT_Buffer[a]);
- // }
- AT24CXX_Read(0,datatemp,SIZE);//读取数据
- // for(b=0;b<SIZE;b++)
- // {
- // printf("datatemp=%d\r\n",datatemp[b]);
- // }
-
-
- }
- if(KEY_Scan(0,KEY2))
- {
- Fixed_Pulse_Output(1,625);
- procedure--;
- if(procedure<0)procedure=16;
- AT24CXX_WriteLenByte(10,TEXT_32Buffer,4);
- // printf("TEXT_32Buffer=%d\r\n",TEXT_32Buffer);
- data32_temp=AT24CXX_ReadLenByte(10,4);
- //printf("data32_temp=%d\r\n",data32_temp);
- printf("procedure-=%d\r\n",procedure);
- }
- if(KEY_Scan(0,KEY3))
- {
- while(speed)
- {
- Succession_Pulse_Output(0,speed );
- // printf("speed=%d\r\n",speed) ;
- if(KEY_Scan(0,KEY4))
- {
- speed+=1000;
- if(speed>10000)speed=1000;
- printf("speed=%d\r\n",speed) ;
- }
- if(Sensor_Input==1)
- {
- procedure=0;
- break;
- }
- }
- printf("procedure=%d\r\n",procedure) ;
- }
-
-
- }
- }
复制代码
所有代码51hei附件下载:
步进电机驱动程序-STM32F103C8T6.7z
(187.6 KB, 下载次数: 86)
|