里面含有单片机控制步进电机的C语言程序及proteus仿真图。
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include<reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit CS1=P1^6; /*片选1*/
- sbit CS2=P1^7; /*片选2*/
- sbit SEG_LE=P1^0;
- sbit LCD_LE=P1^1;
- sbit SEG_OE=P1^2;
- sbit LCD_OE=P1^3;
- uchar code seg[]=//数码管:正向位置;共阳极;正向连接
- {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};//0-9
- uchar code table_speed_num1[]=
- {
- 0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,/*1*/
- 0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,/*5*/
- 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,/*0*/
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,/*.*/
- 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00/*0*/
- }; //150.0
- uchar code table_speed_num2[]={
- 0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,/*7*/
- 0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,/*5*/
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,/*.*/
- 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,/*0*/
- 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00/*0*/
- }; //75.00
- uchar code table_speed_num3[]={
- 0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,/*3*/
- 0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,/*7*/
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,/*.*/
- 0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,/*5*/
- 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00/*0*/
- }; //37.50
- uchar code table_speed_num4[]={
- 0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,/*1*/
- 0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,/*8*/
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,/*.*/
- 0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,/*7*/
- 0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00/*5*/
- }; //18.75
- uchar code table_speed_num5[]={
- 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,/*9*/
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,/*.*/
- 0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,/*3*/
- 0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,/*7*/
- 0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00/*5*/
- }; //9.375
- uchar code table_direct_state_Y[]=
- {
- 0x00,0xFE,0x00,0xFC,0x00,0xFE,0x00,0xF2,0x12,0x1A,0x96,0x12,0x12,0xF2,0x02,0x00,0x20,0x1F,0x00,0x0F,0x00,0x7F,0x00,0x47,
- 0x20,0x10,0x0F,0x10,0x20,0x67,0x00,0x00,
- 0x00,0xFC,0x44,0x44,0x44,0xFC,0x10,0x90,0x10,0x10,0x10,0xFF,0x10,0x10,0x10,0x00,0x00,0x07,0x04,0x04,0x04,0x07,0x00,0x00,
- 0x03,0x40,0x80,0x7F,0x00,0x00,0x00,0x00,
- 0x40,0x30,0x2C,0xEB,0x28,0x28,0x08,0x40,0x40,0x40,0xFF,0x40,0x40,0x40,0x40,0x00,0x01,0x01,0x01,0x7F,0x21,0x11,0x09,0x00,
- 0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00
- }; //顺时针
- uchar code table_direct_state_N[]=
- {
- 0x40,0x42,0xCC,0x00,0x00,0x08,0xC9,0x0E,0x08,0xF8,0x0C,0x0B,0xC8,0x08,0x00,0x00,0x40,0x20,0x1F,0x20,0x40,0x40,0x51,0x49,
- 0x47,0x41,0x41,0x41,0x41,0x40,0x40,0x00,
- 0x00,0xFC,0x44,0x44,0x44,0xFC,0x10,0x90,0x10,0x10,0x10,0xFF,0x10,0x10,0x10,0x00,0x00,0x07,0x04,0x04,0x04,0x07,0x00,0x00,
- 0x03,0x40,0x80,0x7F,0x00,0x00,0x00,0x00,
- 0x40,0x30,0x2C,0xEB,0x28,0x28,0x08,0x40,0x40,0x40,0xFF,0x40,0x40,0x40,0x40,0x00,0x01,0x01,0x01,0x7F,0x21,0x11,0x09,0x00,
- 0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00
- }; //逆时针
- uchar code table_state_roll[]=
- {
- 0x40,0x41,0xCE,0x04,0x00,0x20,0x22,0xA2,0x62,0x22,0xA2,0x22,0x22,0x22,0x20,0x00,0x40,0x20,0x1F,0x20,0x28,0x4C,0x4A,0x49,
- 0x48,0x4C,0x44,0x45,0x5E,0x4C,0x40,0x00,
- 0x10,0x08,0x84,0xC6,0x73,0x22,0x40,0x44,0x44,0x44,0xC4,0x44,0x44,0x44,0x40,0x00,0x02,0x01,0x00,0xFF,0x00,0x00,0x00,0x00,
- 0x40,0x80,0x7F,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0xFC,0x08,0x08,0x08,0x08,0xFF,0x08,0x08,0x08,0x08,0xFC,0x08,0x00,0x00,0x00,0x00,0x07,0x02,0x02,0x02,0x02,0xFF,
- 0x02,0x02,0x02,0x02,0x07,0x00,0x00,0x00,
- 0x00,0xC0,0xC0,0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
- }; //运行中...
- uchar code table_state_pause[]=
- {
- 0x80,0x92,0x9A,0x96,0xFB,0x52,0x52,0x80,0x7E,0x12,0x12,0x12,0xF2,0x12,0x10,0x00,0x00,0x00,0x00,0x7E,0x4B,0x4A,0x4B,0x4A,
- 0x4A,0x4A,0x4A,0x7E,0x01,0x00,0x00,0x00,
- 0x80,0x40,0x20,0xF8,0x07,0x02,0x04,0x74,0x54,0x55,0x56,0x54,0x74,0x04,0x04,0x00,0x00,0x00,0x00,0xFF,0x00,0x03,0x01,0x05,
- 0x45,0x85,0x7D,0x05,0x05,0x05,0x03,0x00,
- 0x00,0x00,0xFC,0x08,0x08,0x08,0x08,0xFF,0x08,0x08,0x08,0x08,0xFC,0x08,0x00,0x00,0x00,0x00,0x07,0x02,0x02,0x02,0x02,0xFF,
- 0x02,0x02,0x02,0x02,0x07,0x00,0x00,0x00,
- 0x00,0xC0,0xC0,0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
- }; //暂停中...
- uchar code stepmotor_Y_table[]={0x02,0x06,0x04,0x0C,0x08,0x09,0x01,0x03};//B-BC-C-CD-D-DA-A-AB-B 顺时针 八拍
- uchar code stepmotor_N_table[]={0x01,0x09,0x08,0x0C,0x04,0x06,0x02,0x03};//A-AD-D-DC-C-CB-B-BA-A 逆时针 八拍
- uchar p; //控制转一圈的八次转动
- uchar direct; //转动方向标志
- uchar speed=3,delay=0; //转动速度标志
- extern display(uchar page,uchar column,uchar code *hzk,uchar num); //显示汉字函数
- extern display_char(uchar page,uchar column,uchar code *hzk,uchar num); //显示字符函数
- extern Keys_Scan(); //按键检测函数
- extern LCD_DIS(); //LCD初始化函数
- //x=1时延时2ms
- void DelayMS(unsigned int x)
- {
- unsigned int i,j;
- for(i=x;i>0;i--)
- {
- for(j=0;j<247;j++)
- ;
- }
- }
- /*********显示速度数值函数****************/
- void dis_speed_num(uchar *v)
- {
- CS1=0;
- CS2=1;
- display_char(2,48,v,2);
- CS1=1;
- CS2=0;
- display_char(2,0,&v[32],3);
- }
- /*********显示方向函数****************/
- void dis_direct_Y_N(uchar dir)
- {
- CS1=1;
- CS2=0;
- if(dir==1)
- display(4,0,table_direct_state_Y,3);
- if(dir==2)
- display(4,0,table_direct_state_N,3);
- }
- /*********显示电机状态函数****************/
- void dis_state_P_R(uchar *buffer)
- {
- CS1=1;
- CS2=0;
- display(6,0,buffer,4);
- }
- void main()
- {
- uchar k=0,i=0,j=0,key=0;
- TMOD=0x11;
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- EA=1;
- ET0=1;
- SEG_OE=1; //关数码管输出使能
- LCD_OE=0; //开LCD输出使能
- LCD_LE=1; //关LCD锁存输出
- LCD_DIS();
- // TR0=1;
- while(1)
- {
- while(key==0)
- key=Keys_Scan(); //检测按键键值
- if(key==3)
- {
- direct=1; //转动方向标志为1,顺时针转动
- dis_direct_Y_N(direct);
- key=0;
- }
- if(key==4)
- {
- direct=2; //转动方向标志为2,逆时针转动
- dis_direct_Y_N(direct);
- key=0;
- }
- if(key==5)
- {
- TR0=0; //关T0中断,电机暂停转动
- dis_state_P_R(table_state_pause);
- key=0;
- }
- if(key==6)
- {
- TR0=1; //开T0中断,电机开始转动
- dis_state_P_R(table_state_roll);
- key=0;
- }
- if(key==1)
- {
- speed=speed-1;; //转动一次所需延时时间减少,转动加速
- if(speed<=1)
- speed=1; //最少延时一次
- key=0;
- }
- if(key==2)
- {
- speed=speed+1; //转动一次所需延时时间增加,转动减速
- if(speed>=5)
- speed=5; //最多延时五次
- key=0;
- }
- if(speed==1) //显示相应延时时间的速度数值
- dis_speed_num(table_speed_num1);
- if(speed==2)
- dis_speed_num(table_speed_num2);
- if(speed==3)
- dis_speed_num(table_speed_num3);
- if(speed==4)
- dis_speed_num(table_speed_num4);
- if(speed==5)
- dis_speed_num(table_speed_num5);
- LCD_LE=0; //关LCD输出使能
- SEG_OE=0; //开数码管输出使能
- SEG_LE=1; //关数码管锁存输出
- P0=seg[speed]; //输出当前速度档位1-5,也就是延时时间
- SEG_LE=0; //开数码管锁存输出,锁存输出值
- LCD_LE=1; //关LCD锁存输出
- DelayMS(100); //延时一会,等待外围器件工作,
- //没有延时,在加速加速时可能会产生多加速或多减速
- }
- }
- /**********电机转动函数************/
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
步进电机.zip
(136.81 KB, 下载次数: 130)
|