樓主代碼錯誤占一半以上,主要是書寫錯誤:大小寫錯誤、中英符號錯誤、空格缺/多錯誤,符號缺/多錯誤及變量忘記賦值。
#include <AT89X51.H>
unsigned char a,b,c,d,r;
unsigned char code disaplay[]= {0x3f, 0x06, 0x5b, 0x4f, 0x66,0x6d, 0x7d, 0x07, 0x7f, 0x6f} ;
long counter=0; //旋轉編碼 器的脈沖
long position=0; //旋轉 編碼器的當前位置
long degree=0;
void main()
{
TMOD=0x06;
TH0=0xff;
TL0=0xff;
TH1= (65536-1000)/256;
TL1= (65536-1000)%256;
IE=0x0f;
EA = 1; //開總中斷
r=0;
while(1) //主程序循環顯示位置
{
a=degree%10; //計算參數的個十百千位
b= (degree/10)%10;
c= (degree/100)%10;
d= (degree/1000)%10;
P2_3=0;//顯示position值
P0=disaplay[a];
P2_3=1;
P2_2=0;
P0=disaplay[b];
P2_2=1;
P2_1=0;
P0=disaplay[c];
P2_1=1;
P2_0=0;
P0=disaplay[d];
P2_0=1;
}
}
void int0() interrupt 0 //A 相中斷
{
if(P2_7)position++; //判斷2_7 為高則正向,為低則反向
else position=0;/////
counter++; //累計脈沖
}
void int1() interrupt 1 //Z相中斷
{
position=0; //使位置參數置零
}
void tl_() interrupt 2 //定時0. ls刷新一次轉速和角
{//度參數
TH0=(65536-1000) /256;
TL0=(65536-1000) %256;
r=600*counter/ 300; //假設編碼器每轉300個脈沖
degree=1.2*position; //把脈沖數轉換為 角度數
if(!degree) //角度為負時加360度轉為正
degree+=360;
}
void t0_() interrupt 3 //計數器T0中斷顯示速度
{
unsigned char i;
i=100;
a=r%10;
b=(r/10)%10;
c=(r/100)%10;
d=(r/1000)%10;
while(i)
{
//中斷產生后循環100次顯示一段時間
P2_3=0;
P0=disaplay[a];
P2_3=1;
P2_2=0;
P0=disaplay[b];
P2_2=1;
P2_1=0;
P0=disaplay[c];
P2_1=1;
P2_0=0;
P0=disaplay[d];
P2_0=1;
i-- ;
}
} |