單片機通過串口通信調(diào)整時間的程序
/*************數(shù)碼管顯示時間************************/
void display(uchar shi, uchar fen, uchar miao)
{
uchar shi_s, shi_g;
uchar fen_s, fen_g;
uchar miao_s, miao_g;
shi_s = shi / 10 % 10;
shi_g = shi % 10;
fen_s = fen / 10 % 10;
fen_g = fen % 10;
miao_s = miao / 10 % 10;
miao_g = miao % 10;
dula = 1;
P0 = table[shi_s];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfe;
wela = 0;
P0 = 0x00;
delayms(1);
dula = 1;
P0 = table[shi_g];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfd;
wela = 0;
P0 = 0x00;
delayms(1);
dula = 1;
P0 = table[fen_s];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfb;
wela = 0;
P0 = 0x00;
delayms(1);
dula = 1;
P0 = table[fen_g];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xf7;
wela = 0;
P0 = 0x00;
delayms(1);
dula = 1;
P0 = table[miao_s];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xef;
wela = 0;
P0 = 0x00;
delayms(1);
dula = 1;
P0 = table[miao_g];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xdf;
wela = 0;
P0 = 0x00;
delayms(1);
}
/*****************點陣顯示程序****************/
void display_max7219()
{
uchar m, n;
for(m = 0; m < 5; m++)
{
for(n = 0; n < 8; n++)
{
write_max7219(n+1, dis[m][n]);
delayms(1);
}
delayms(1000);
}
TR0 = 1;
}
/****************主程序********************/
void main(void)
{
init();
max7219_init();
display_max7219();
while(1)
{
display(shi, fen, miao);
if(flag == 1)
{
flag= 0;
ES = 0;
SBUF = shi;
while(!TI);
TI = 0;
SBUF = fen;
while(!TI);
TI = 0;
SBUF = miao;
while(!TI);
TI = 0;
ES = 1;
}
}
}
/***************定時器T0中斷程序**********************/
void timer0_int() interrupt 1
{
TH0 = 0xB8;
TL0 = 0x00;
counter++;
P1 = ~(1 << j++);
if(j >= 8)
{
j = 0;
}
if(counter == 50)
{
counter = 0;
miao++;
if(miao >= 60)
{
miao = 0;
fen++;
if(fen >= 60)
{
fen = 0;
shi++;
if(shi >= 24)
{
shi = 0;
}
}
}
}
}
/*************串口中斷服務程序***********************/
void uart_int() interrupt 4
{
RI = 0;
if(SBUF >= 23 && SBUF <= 59)
{
fen = SBUF;
miao = 0;
FM = 0;
delayms(30);
FM = 1;
}
if(SBUF >= 0 && SBUF <= 23)
{
shi = SBUF;
fen = SBUF;
FM = 0;
delayms(30);
FM = 1;
}
flag= 1;
}本文引用地址:http://m.butianyuan.cn/article/201612/324615.htm
評論