本帖最后由 liyinchao 于 2010-1-23 08:12 编辑
//-----LED&KEY.c-------
#include"HardWare.h"
#define MaxBit 7 //最大显示位数7位
#define Timer0_Tmp 60000 //调节刷新频率
#define Timer1_Tmp 60000
#define Max_Key_Time 30
uchar code WordTAB[]={0xed,0x09,0x7c,0x5d,0x99,0xd5,0xf5,0x49,0xfd,0xdd,0x10};
uchar code DigTAB[]={0x1f,0x3f,0x5f,0x7f,0x9f,0xbf,0xdf,0xff};
uchar DisplayBuf[7]; //显示缓冲区
uchar WordBit;
uchar Key_Press;
uchar Key_Time;
uchar Key;
void Display_Init(void)
{
//TMOD=0x15; //16位数方式(设方式)
TMOD=0x51; //16位数方式(设方式)
TH0=Timer0_Tmp/256;
TL0=Timer0_Tmp%256;
TH1=Timer1_Tmp/256;
TL1=Timer1_Tmp%256;//(赋初值)
ET0=1;
ET1=1; //开中断
TR0=1;
TR1=1; //启动之
EA=1;
WordBit=0;
DisplayBuf[0]=WordTAB[1];
DisplayBuf[1]=WordTAB[2];
DisplayBuf[2]=WordTAB[3];
DisplayBuf[3]=WordTAB[4];
DisplayBuf[4]=WordTAB[5];
DisplayBuf[5]=0;
DisplayBuf[6]=0;
}
void DisPlay(void)
{
WordBit++;
if(WordBit>=MaxBit)
WordBit=0;
Dig_Port=DigTAB[WordBit]; //选择位
Word_Port=DisplayBuf[WordBit]; //输出显示内容
Key_Press=0;
if((Key_Port&0x0f)!=0x0f)
{
Key_Press=1;
}
}
void Timer0_ISR(void) interrupt 1 using 1
{
TH0=Timer0_Tmp/256;
TL0=Timer0_Tmp%256;
DisPlay();
if(Key_Press)
Key_Time++;
if(Key_Time>=Max_Key_Time)
{
Key_Time=0;
Key=Key_Port;
}
}
void Timer1_ISR(void) interrupt 3 using 3
{
TH1=Timer1_Tmp/256;
TL1=Timer1_Tmp%256;
}
//-----Test.c---------------------------
#include"hardware.h"
extern void Display_Init(void);
extern uchar Key_Press;
extern uchar Key_Time;
extern uchar code WordTAB[];
extern uchar code DigTAB[];
extern uchar DisplayBuf[];
extern uchar Key;
uchar MotoState;
long CoilsCount;
void Num_Display(long Num);
void main(void)
{
Display_Init();
while(1)
{
switch(Key)
{
case KEY_0:
CoilsCount*=10;
Num_Display(CoilsCount);
Key=0;
break;
case KEY_1:
CoilsCount=CoilsCount*10+1;
Num_Display(CoilsCount);
Key=0;
break;
case KEY_2:
CoilsCount=CoilsCount*10+2;
Num_Display(CoilsCount);
Key=0;
break;
case KEY_3:
CoilsCount=CoilsCount*10+3;
Num_Display(CoilsCount);
Key=0;
break;
case KEY_4:
CoilsCount=CoilsCount*10+4;
Num_Display(CoilsCount);
Key=0;
break;
case KEY_5:
CoilsCount=CoilsCount*10+5;
Num_Display(CoilsCount);
Key=0;
break;
case KEY_6:
CoilsCount=CoilsCount*10+6;
Num_Display(CoilsCount);
Key=0;
break;
case KEY_7:
CoilsCount=CoilsCount*10+7;
Num_Display(CoilsCount);
Key=0;
break;
case KEY_8:
CoilsCount=CoilsCount*10+8;
Num_Display(CoilsCount);
Key=0;
break;
case KEY_9:
CoilsCount=CoilsCount*10+9;
Num_Display(CoilsCount);
Key=0;
break;
case KEY_Star:
CoilsCount=0;
Num_Display(CoilsCount);
Key=0;
break;
case KEY_Sharp:
break;
case KEY_Run_Stop:
if(MotoState==Run)
MotoState=Stop;
else
MotoState=Run;
break;
default:
break;
}
}
}
void Num_Display(ulong Num)
{
uchar N;
ulong Temp;
Temp=Num;
for(N=0;N<5;N++)
{
DisplayBuf[N]=WordTAB[Temp%10];
Temp/=10;
}
if(Num<10000)
DisplayBuf[4]=0;
if(Num<1000)
DisplayBuf[3]=0;
if(Num<100)
DisplayBuf[2]=0;
if(Num<10)
DisplayBuf[1]=0;
}
//-------hardware.h---------
#include"Reg52.h"
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define Word_Port P2
#define Dig_Port P0
#define Key_Port P0
#define SPI_Port P1
sbit MISO=P1^0;
sbit MOSI=P1^1;
sbit SCLK=P1^2;
sbit CS =P1^3;
#define KEY_1 0x1b //1
#define KEY_2 0x1d
#define KEY_3 0x1e
#define KEY_4 0x3b
#define KEY_5 0x3d
#define KEY_6 0x3e
#define KEY_7 0x5b
#define KEY_8 0x5d
#define KEY_9 0x5e
#define KEY_Star 0x7b //*
#define KEY_0 0x7d //0
#define KEY_Sharp 0x7e //#
#define KEY_Run_Stop 0x9e//启动/停止
#define Run 1
#define Stop 0
|