|
數(shù)碼管跳一位數(shù) 比如100到101 之間會有一段什么都不顯示 下面是我的程序(編碼器的) 希望會的大神不嗇賜教~~~
#include <REG51.H>
#include <INTRINS.H> // 空操作函數(shù),移位函數(shù)頭文件包含
#define uchar unsigned char
#define uint unsigned int
unsigned char counter;
unsigned char b;
sbit PINA = P1^0;//csn信號white
sbit PINB = P1^1;//這個是時鐘blue
sbit PIND = P1^2;//這個是數(shù)據(jù)green
sbit LED2 = P2^2;//LED顯示位選控制 百位
sbit LED3 = P2^1;//LED顯示位選控制 十位
sbit LED4 = P2^0;//LED顯示位選控制 個位
uchar led_buf[4]; /*定義LED顯示緩沖區(qū),將帶顯示數(shù)值放入該區(qū)域*/
/*------------------------------------------------
數(shù)碼管段碼表
------------------------------------------------*/
uchar code disp_buff[]= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,};
/*------------------------------------------------
延時子程序
------------------------------------------------*/
void delay(unsigned int cnt)
{
while(--cnt);
}
/*void delay(uint t)
{
uint i;
while(t--)
{
for(i=0;i<125;i++){}
}
}*/
/*------------------------------------------------
取編碼器位置值子程序
------------------------------------------------*/
long GetSSI(void)
{
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; //記錄B信號
while(!PINA) //等待A由低變高
{
Curr_encoder_b = PINB; //記錄等待期間的B信號(指當(dāng)前B信號)
updata = 1; }
if(updata)
{
updata = 0 ;
if( (Last_encoder_b == 0)&&(Curr_encoder_b== 1) ) //B從0到1為正轉(zhuǎn)
{
if(counter == 255)
{ return; }
counter++; //正轉(zhuǎn)計數(shù)加
}
else if( (Last_encoder_b == 1)&&(Curr_encoder_b == 0) ) //B從1到0為反轉(zhuǎn)
{
if(counter == 0)
{return; }
counter--; //反轉(zhuǎn)計數(shù)減
}
}
}
//LED 共陰
void main()
{
counter = 0;
P0 = 0xff;
P1 = 0xff;
P2 = 0xff;
TMOD &= 0xf1; //設(shè)置定時器模式
TMOD |= 0x01; //設(shè)置定時器模式
TL0 = (65636-50000)/256; //設(shè)置定時初值
TH0 = (65636-50000)%256; //設(shè)置定時初值
TF0 = 0; //清除TF0標(biāo)志
ET0 = 1;
TR0 = 1; //定時器0開始計時;
IT0 = 1 ;
EX0 = 1;
EA = 1;
while(1)
{
GetSSI();//取絕對值編碼器位置數(shù)據(jù)
led_buf[2]=(counter%1000)/100;//百位
led_buf[1]=(counter%100)/10;//十位
led_buf[0]=counter%10; //個位
//將數(shù)據(jù)的各位數(shù)字放入指定緩沖區(qū)
P0 = disp_buff[led_buf[2]]; //取顯示數(shù)據(jù),段碼
LED2 = 0; //點亮LED2(第二位百位)
delay(200); //延時一小段時間
LED2 = 1; //熄滅LED2
P0 = disp_buff[led_buf[1]]; //取顯示數(shù)據(jù),段碼
LED3 = 0; //點亮LED3(第三位十位)
delay(200); //延時一小段時間
LED3 = 1; //熄滅LED3
P0 = disp_buff[led_buf[0]]; //取顯示數(shù)據(jù),段碼
LED4 = 0; //點亮LED4(第四位)
delay(200); //延時一小段時間
LED4 = 1; //熄滅LED4
// delay(300);
}
}
|
|