LF20140408相5线步进电机+51单片机+UL2003+LCM12864+按键+百分比send 设计资料
步进电机仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include "macro.h"
- uchar buf_Direction; //设定步进电机的转向
- uint set_speed; //设定步进电机的转速
- uchar step_motor_loop[8]={0x01,0x03,0x02,0x06,0x04,0x0C,0x08,0x09};
- uchar step_index;
-
- #define CST_STEP_MOTOR_Z 10 //正转
- #define CST_STEP_MOTOR_F 20 //反转
- #define CST_STEP_MOTOR_ST 0 //停
- #define fosc_12MHz 12
- #define fosc_24MHz 24
- #define fosc_setting fosc_12MHz
- //工作方式1
- #define CST_TIME_MS 1000
- #define CST_TH0 (65536-CST_TIME_MS*fosc_setting/fosc_12MHz)/256
- #define CST_TL0 (65536-CST_TIME_MS*fosc_setting/fosc_12MHz)%256
- uchar buf_SpeedString[]=" ";
- void iniLCD(void);
- void list_write_HZ_Str( int x1,int y1, uchar *point ) ;
- void step_motor_driver(void)
- {
- static uint speed_delay=0;
- speed_delay++;
- if( buf_Direction==CST_STEP_MOTOR_Z)
- {
- P0=step_motor_loop[step_index];
- if(speed_delay >=set_speed )
- {
- speed_delay =0;
- step_index--;
- if(step_index <=0)
- step_index =7;
- }
- }
- else if( buf_Direction==CST_STEP_MOTOR_F)
- {
- P0=step_motor_loop[step_index];
-
- if(speed_delay >=set_speed)
- {
- speed_delay =0;
- step_index++; //大于7,从头再来
- if(step_index >7)
- step_index =0;
- }
- }
- else
- {
- speed_delay =0;
- P0=0x00;
- }
- }
- void caluate(void)
- {
- uchar temp=0;
- uint u16Temp=0;
- u16Temp=(100-set_speed)*100/85;
- buf_SpeedString[0]='0'+(u16Temp/100);
- buf_SpeedString[1]='0'+(u16Temp%100/10);
- buf_SpeedString[2]='0'+(u16Temp%100%10);
- buf_SpeedString[3]='%';
- list_write_HZ_Str(6,60,&buf_SpeedString);
- }
- //*********************************************************
- //******************按键处理程序****************************
- //************************************************************
- void key_Scan (void)
- {
- static uchar key_loose=0;
- uchar temp=0;
- P1=0XFF;
- temp=P1&0xFF;
- if(key_loose >0)
- {
- if(temp==0xFF)
- key_loose=0;
- return;
- }
- else if(temp !=0xFF)
- {
- key_loose=10;
- temp=P1&0xFF;
- switch(temp)
- {
- case 0xFE:
- buf_Direction=CST_STEP_MOTOR_Z;
- list_write_HZ_Str(6,20,"顺向");
- caluate();
- break;
-
- case 0xFD:
- buf_Direction=CST_STEP_MOTOR_F;
- list_write_HZ_Str(6,20,"逆向");
- caluate();
- break;
- case 0xFB:
- buf_Direction=CST_STEP_MOTOR_ST;
- list_write_HZ_Str(6,20,"停止");
- list_write_HZ_Str(6,60," ");
- break;
- case 0xEF:
- if(buf_Direction !=CST_STEP_MOTOR_ST)
- {
- if(set_speed <80)
- set_speed +=2;; //减速
- caluate();
- }
-
- break;
- case 0xF7:
- if(buf_Direction !=CST_STEP_MOTOR_ST)
- {
- if(set_speed >14)
- set_speed -=2; //加速
- caluate();
- }
-
- break;
- default:
- break; //其它值返回
- }
- }
- }
- void isr_timer0 (void) interrupt 1 using 1
- {
- TH0=CST_TH0;
- TL0=CST_TL0;
- step_motor_driver();
- }
- void init_timer0(void)
- {
- TCON=0x00;
- TMOD=0x00;
- TL0=0x00;
- TH0=0x00;
- TCON=0x00;
- // Timer 0 C/T=0,定时工作方式
- //Timer 0 M1,M0=0,1 .work mode 1 www.gdzs.si.gov.cn
- TMOD=0x01; //GATE=0,以运行控制位TR0 启动定时器0 C/T=0,定时工作方式
- TH0=CST_TH0;
- TL0=CST_TL0;
- TF0=0;//clear 0. count over flag.
- TR0=1;// enable TIMER0 ,start count
- ET0=1; //enable accept interrupt
- }
- //***********************主***控****程********序********************
- void main(void)
- {
- P2=0xff;
- P1=0xff;
- P0=0Xff;
- init_timer0();
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
4相5线步进电机 51单片机 UL2003 LCM12864 .zip
(137.83 KB, 下载次数: 370)
|