#include<reg51.h> #include <intrins.h> #define uint unsigned int #define uchar unsigned char
sbit key_right= P1^6; sbit key_left= P1^7;
uchar m; uchar NP; bit flag;
const uchar motor_tb[]={0XCC,0XBD,0XAE,0X9F,0x8F,0X1F,0X2E,0X3D, 0X4C,0X5B,0X6A,0X79,0X78,0X71,0X62,0X53, 0X44,0X35,0X26,0X17,0X87,0X97,0XA6,0XB5, 0XC4,0XD3,0XE2,0XF1,0XF8,0XF9,0XEA,0XDB}; const uchar th0_tb[]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff,0xff}; const uchar tl0_tb[]={0x33,0x66,0x99,0xcc,0xff,0x99,0x33,0xcc,0x66,0xff,0x99,0x33,0x66,0x99,0xcc,0xff,0x33,0x66,0x99,0xcc,0xff,0x99,0x33,0xcc,0x66,0xff,0x99,0x33,0x66,0x99,0xcc,0xff};
void wait(uchar t) { uchar i; for (i=0;i<t;i++) _nop_(); }
void delay(void) { flag=0; TR0=0x01; while(1) { if( flag==1)break;} }
void int_t0(void) interrupt 1 { TMOD=0X01; //模式1,16位计数器 ET0=0x01; //T0开中断 EA=0x01; //CPU开中断
m++; if(m>=31) { m=31; TH0=th0_tb[m]; TL0=tl0_tb[m]; } else { TH0=th0_tb[m]; TL0=tl0_tb[m]; }
flag=1; }
void fourth_step(uchar d) //步进电机走1.8度 d=0 正转d=1 反转 { if (d&0x01) { if (NP==0) NP=31; else NP--; //NP记录步数,全局变量 } else { if (NP==31) NP=0; else NP++; } P2=motor_tb[NP]; P0=motor_tb[NP]; //输出到单片机的P2口 delay(); }
void four_step(uchar d)// 步进电机走四步 { uchar i; for (i=0;i<15;i++) fourth_step(d);
} void a_turn(uchar d) // 步进电机走一圈 { uchar i; for(i=0;i<50;i++) four_step(d); } void main() { TMOD=0X01; TH0=0X03; //定时初值设定,实现35ms定时 TL0=0Xff; ET0=0x03; EA=0xff; m=0; NP=0; P2=0xFF; P0=0xFF;
while(1) { while(( key_right==0)||( key_left==0)) { while(key_right==0) { wait(10); if(key_right==1) break; four_step(1); wait(10); if(key_right==1) break; a_turn(1); } while(key_left==0) { wait(10); if(key_left==1) break; four_step(0); wait(10); if(key_left==1) break; a_turn(0); }
} while(( key_right==1)&&( key_left==1)) {P2=0xFF; break; P0=0xFF; break;} } }
程序编译后出现问题 Build target 'Target 1' assembling STARTUP.A51... compiling motor5.c... linking... *** ERROR L107: ADDRESS SPACE OVERFLOW SPACE: DATA SEGMENT: ?DT?MOTOR5 LENGTH: 0062H *** ERROR L105: PUBLIC REFERS TO IGNORED SEGMENT SYMBOL: M SEGMENT: ?DT?MOTOR5 *** ERROR L105: PUBLIC REFERS TO IGNORED SEGMENT SYMBOL: MOTOR_TB SEGMENT: ?DT?MOTOR5 *** ERROR L105: PUBLIC REFERS TO IGNORED SEGMENT SYMBOL: TL0_TB SEGMENT: ?DT?MOTOR5 *** ERROR L105: PUBLIC REFERS TO IGNORED SEGMENT SYMBOL: TH0_TB SEGMENT: ?DT?MOTOR5 *** ERROR L105: PUBLIC REFERS TO IGNORED SEGMENT SYMBOL: NP SEGMENT: ?DT?MOTOR5 Program Size: data=107.1 xdata=0 code=500 Target not created
想问下各位大虾,,为什么呢??这个程序我用在四细分电机驱动上安全没问题啊 |