|
大神請指教 本程序測得是增量式編碼器位置 說白了就是計數(shù)A B脈沖 但是燒進(jìn)去之后示數(shù)一直是0 轉(zhuǎn)動編碼器不會有反應(yīng) 前后改了幾次實在是改不出來了 求大神支招
#include <REG51.H>
#include <intrins.h>
#define uchar unsigned char
#define GPIO_DIG P0
sbit LSA=P2^0;
sbit LSB=P2^1;
sbit LSC=P2^2;
sbit PINA = P1^0; //A相
sbit PINB = P1^1; //B相
sbit PIND = P1^2;
static unsigned int counter = 0; //編碼器脈沖計數(shù)
int xx[3]={0,0,0};
unsigned char code DIG_CODE[17]=
{
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71
};
/************************************
void Timer0Init(void)
{
TMOD &= 0xF0; //設(shè)置定時器模式
TMOD |= 0x01; //設(shè)置定時器模式
TL0 = 0x30; //設(shè)置定時初值
TH0 = 0xF8; //設(shè)置定時初值
PT0 = 0;
TF0 = 0; //清除TF0標(biāo)志
ET0 = 1;
TR0 = 1; //定時器0開始計時
PT0 = 1; //數(shù)碼管刷新優(yōu)先
EA = 1;
}
*****利用STC89系列的外中斷模式*******
void Inti_INT0()
{
//set INT1 int type (1:Falling only 0:Low level)
//enable INT1 interrupt
EA = 1;
}
************************************/
void main (void)
{
TMOD &= 0xF0; //設(shè)置定時器模式
TMOD |= 0x01; //設(shè)置定時器模式
TL0 = 0x30; //設(shè)置定時初值
TH0 = 0xF8; //設(shè)置定時初值
PT0 = 0;
TF0 = 0; //清除TF0標(biāo)志
ET0 = 1;
TR0 = 1; //定時器0開始計時
PT0 = 1; //數(shù)碼管刷新優(yōu)先
EA = 1;
while(1)
{
}
}
void Timer0() interrupt 1
{
static unsigned char i;
unsigned int j;
unsigned char check[8];
check[0]=counter/100;
check[1]=counter%100/10;
check[2]=counter%100%10;
check[3]=0;
check[4]=0;
check[5]=0;
check[6]=0;
check[7]=0;
TL0 = 0x30; //設(shè)置定時初值
TH0 = 0xF8; //設(shè)置定時初值
switch(i) //位選,選擇點亮的數(shù)碼管,
{
case(0):
LSA=0;LSB=0;LSC=0; break;//顯示第0位
case(1):
LSA=1;LSB=0;LSC=0; break;//顯示第1位
case(2):
LSA=0;LSB=1;LSC=0; break;//顯示第2位
case(3):
LSA=1;LSB=1;LSC=0; break;//顯示第3位
case(4):
LSA=0;LSB=0;LSC=1; break;//顯示第4位
case(5):
LSA=1;LSB=0;LSC=1; break;//顯示第5位
case(6):
LSA=0;LSB=1;LSC=1; break;//顯示第6位
case(7):
LSA=1;LSB=1;LSC=1; break;//顯示第7位
}
GPIO_DIG=DIG_CODE[check[i]];//發(fā)送段碼
j=5;
i++;
if(i==8)
{
i=0;
} //掃描間隔時間設(shè)定
while(j--);
GPIO_DIG=0x00;//消隱
}
void exint1() interrupt 0 //(location at 0013H)
{
static bit Curr_encoder_b; //定義一個變量來儲存當(dāng)前B信號
static bit Last_encoder_b; //定義一個變量來儲存上次B腳信號
static bit updata= 0;
if(PINA && PINB) //編碼器無轉(zhuǎn)動退出
{
updata = 0;
return;
}
Last_encoder_b = PINB;
while(!PINA)
{
Curr_encoder_b = PINB;
updata = 1;
}
if(updata)
{
updata = 0;
if(Last_encoder_b==1&&Curr_encoder_b==0)
{
if(counter == 400)
return;
counter++;
}
else if(Last_encoder_b==0&&Curr_encoder_b==1)
{
if(counter == 0)
return;
counter--;
}
}
}
|
|