Code:
#include <REGX51.H>
sbit A1 = P1^0;
sbit A2 = P1^1;
sbit A3 = P1^2;
char str[3];
unsigned char i=0,tram,chuc,donvi;
int x;
code unsigned char dataled[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
void delay(int time)
{int n;
for(n=0;n<time;n++)
{
;
}
}
void hienthi(unsigned char num)
{A1 = 1;
P0 = dataled[num/100];
delay(50);
A1 = 0;
P0 = 0xff;
A2 = 1;
P0 = dataled[num/10%10];
delay(50);
A2 = 0;
P0 = 0xff;
A3 = 1;
P0 = dataled[num%10];
delay(50);
A2 = 0;
P0 = 0xff;
}
void Setup_isr()
{
SCON = 0x52; /* Che do 1: 8-bit UART, cho phep truyen */
TMOD = 0x20; /* timer 1 che do 2: 8-Bit tu dong nap lai. */
TH1 = 0xfd ; /* toc do 9600 baud */
TL1 = 0xfd ;
TI = 0; /* co ngat nha^n.=0*/
RI=0; /* co ngat' truye^n =0*/
TR1 = 1; /* timer 1 run */
ES =1; /* cho phep ngat noi tiep */
}
//===========================
void nhan(void) interrupt 4 //Chuong trinh con dung ngat noi tiep
{
char ch;
if(RI)
{
RI=0;
ch=SBUF;
str[i]=ch;
i++;
if(i==3)
{
donvi = str[0]-48;
chuc = str[1]-48;
tram = str[2]-48;
x= tram*100+chuc*10+donvi;
str[3]=0;
i=0;}}}
void main()
{
Setup_isr();
EA=1;
while(1)
{
hienthi(x);
}
}

Comment