|
|
樓主代碼錯(cuò)誤占一半以上,主要是書(shū)寫(xiě)錯(cuò)誤:大小寫(xiě)錯(cuò)誤、中英符號(hào)錯(cuò)誤、空格缺/多錯(cuò)誤,符號(hào)缺/多錯(cuò)誤及變量忘記賦值。
#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; //旋轉(zhuǎn)編碼 器的脈沖
long position=0; //旋轉(zhuǎn) 編碼器的當(dāng)前位置
long degree=0;
void main()
{
TMOD=0x06;
TH0=0xff;
TL0=0xff;
TH1= (65536-1000)/256;
TL1= (65536-1000)%256;
IE=0x0f;
EA = 1; //開(kāi)總中斷
r=0;
while(1) //主程序循環(huán)顯示位置
{
a=degree%10; //計(jì)算參數(shù)的個(gè)十百千位
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++; //累計(jì)脈沖
}
void int1() interrupt 1 //Z相中斷
{
position=0; //使位置參數(shù)置零
}
void tl_() interrupt 2 //定時(shí)0. ls刷新一次轉(zhuǎn)速和角
{//度參數(shù)
TH0=(65536-1000) /256;
TL0=(65536-1000) %256;
r=600*counter/ 300; //假設(shè)編碼器每轉(zhuǎn)300個(gè)脈沖
degree=1.2*position; //把脈沖數(shù)轉(zhuǎn)換為 角度數(shù)
if(!degree) //角度為負(fù)時(shí)加360度轉(zhuǎn)為正
degree+=360;
}
void t0_() interrupt 3 //計(jì)數(shù)器T0中斷顯示速度
{
unsigned char i;
i=100;
a=r%10;
b=(r/10)%10;
c=(r/100)%10;
d=(r/1000)%10;
while(i)
{
//中斷產(chǎn)生后循環(huán)100次顯示一段時(shí)間
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-- ;
}
} |
|