/*通用数码显示模板*/
/*作者:田开坤*/
/*时间:2010年11月*/
#include <reg52.h>
#include <absacc.h>
#define TIME_PER_SEC 600 //定义数码管扫描频率,8*40Hz
#define CLOCK 22118400 //定义时钟晶振,单位Hz
#define KEY 0xB700
sbit SH_CP = P2^4;
sbit ST_CP = P1^5;
sbit DS = P2^5;
unsigned char led_buff[8]={0,0,0,0,0,0,0,0};
unsigned char xdata *pKey;
unsigned char key;
void write_595(unsigned char d)
{
unsigned char i;
for(i=0;i<8;i++)
{
DS = d&0x01; //发送最低位
SH_CP = 1;
SH_CP = 0; //移位寄存器移位
d = d>>1; //数据移位
}
}
void seg595_init()
{
SH_CP = 0;
ST_CP = 0;
DS = 0;
write_595(0);
write_595(0);
ST_CP = 1;
ST_CP = 0;
}
//定时器0初始化
void timer0_init(void)
{
TMOD = 0x01; //定时器0工作在模式1,16Bit定时器模式
TH0 = (65536-CLOCK/TIME_PER_SEC/12) / 256;
TL0 = (65536-CLOCK/TIME_PER_SEC/12) % 256;
TR0 =1;
ET0 =1;//开启定时器和中断
}
void main()
{
unsigned char temp;
seg595_init();
timer0_init();
EA=1;
pKey = KEY;
while(1)
{
if(key!=temp)
{
temp=key;
led_buff[7]=temp%10;
led_buff[6]=temp/10%10;
led_buff[5]=temp/100%10;
}
}
}
void timer0_int(void) interrupt 1
{
unsigned char code led_seg[]={0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09};
unsigned char code led_bit[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned char code key_tab[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
static unsigned char cycle=0;
unsigned char temp,i,j;
TH0 = (65536-CLOCK/TIME_PER_SEC/12) / 256;
TL0 = (65536-CLOCK/TIME_PER_SEC/12) % 256;
i = cycle & 0x07;
write_595(led_seg[led_buff[i]]);
write_595(led_bit[i]);
ST_CP=1;ST_CP=0;
i = cycle & 0x0f;
temp = *(pKey+i);//扫描键盘
if(temp!=0xff)
{
for(j=100;j>0;j--); //适当延时去抖动
temp = *(pKey+i);
if(temp!=0xff)
{
for(j=0;j<8;j++) if(temp==key_tab[j]) break;
key = i + (j<<4); //相当于i+j*16
}
}
cycle++;
}