我用的单片机STC89C52芯片,光电编码器AB引脚。A引脚接外部中断INT0,B引脚接P2^2引脚,编码器接了上拉电阻,信号产生高低电平非常精确,但是在显示的时候,编码器左右旋转,数码管计数只增不减,不知道哪里出问题了,求大神指导!!
#include
#define uint unsigned int
#define uchar unsigned char
//sbit BMA=P3^2;
sbit BMB=P2^2;
sbit dula=P2^6;
sbit wela=P2^7;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x51,0x79,0x71};
uchar ge,shi,bai;
int count;
void display(uchar ge,uchar shi,uchar bai)
{
P0=0xfe;
wela=1;
P0=0xff;
wela=0;
// delay(5);
dula=1;
P0=table[ge];
dula=0;
// delay(5);
P0=0xfb;
wela=1;
P0=0xff;
wela=0;
// delay(5);
dula=1;
P0=table[shi];
dula=0;
// delay(5);
P0=0xfd;
wela=1;
P0=0xff;
wela=0;
// delay(5);
dula=1;
P0=table[bai];
dula=0;
// delay(5);
}
void main()
{
count=0;
TMOD=0x01; //定时器0工作方式1
TH0=(65536-1000)/256; //12M晶振10MS计时常数
TL0=(65536-1000)%256;
EA=1; //开总中断
ET0=1; //定时器T0中断允许
EX0=1; //外部中断0中断允许
IT0=1; //选择负跳变来触发外中断
TR0=1; //启动定时器0
// IE0=1;
// m=0;
while(1);
}
void INT0_time() interrupt 0
{
if(BMB)
count++;
else
count--;
}
void T0_time() interrupt 1
{
TH0=(65536-1000)/256; //12M晶振10MS计时常数
TL0=(65536-1000)%256;
ge=count%10;
shi=(count%100)/10;
bai=count/100;
display(ge,shi,bai);
}
另求一个光电编码器计数程序。。。[/color]
如果把count--改成count=count-3(1,2都还是会加,3以上示数就会减了),反转示数就会减了,很是奇怪,求大神指导!!(硬件部分应该没什么问题,示波器可以测出正常波形)