單片機(jī)通過串口通信調(diào)整時間的程序
/**************************通過串口通信調(diào)整時間******************************/
/**
*功能:單片機(jī)開始上電時點陣顯示倒計時3,2,1,0,然后點陣LED關(guān)閉顯示
* 定時器和串口開始工作,定時器開始一秒計時,并在數(shù)碼管上顯示時,分,秒
* 此計時器可以通過串口發(fā)送數(shù)據(jù)調(diào)整時間,可調(diào)整時與分,當(dāng)調(diào)整分鐘時秒清零
* 附加功能:當(dāng)單片機(jī)收到數(shù)據(jù)時有蜂鳴器響一聲作為提示
* 彩色流水燈以20毫秒的速度向左流動
*作者:徐冉
*日期:2013-06-15-09:15 - 12:10
*備注:程序已經(jīng)順利通過調(diào)試
*/
/***********51hei開發(fā)板************/
/********AT89C52-RC MCU***********/
#include
typedef unsigned int uint;
typedef unsigned char uchar;
sbit wela = P2^7;
sbit dula = P2^6;
sbit FM = P2^3;
sbit din = P2^0;
sbit cs = P2^1;
sbit clk = P2^2;
/************點陣LED顯示取模編碼表***********************/
uchar code dis[][8] = {
{0x00,0x3E,0x02,0x02,0x3E,0x02,0x02,0x3E},//3
{0x00,0x3E,0x02,0x02,0x3E,0x20,0x20,0x3E},//2
{0x00,0x18,0x38,0x08,0x08,0x08,0x08,0x3E},//1
{0x00,0x3C,0x24,0x24,0x24,0x24,0x24,0x3C},//0
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}//關(guān)閉顯示
};
/*****************數(shù)碼管顯示編碼表***************************/
uchar code table[] = {
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F //"9"
};
uchar shi = 0, fen = 0, miao = 0, counter = 0,flag= 0, j = 0;
/*************點陣LED-MAX7219***********/
/***************寫字節(jié)************************/
void write_byte(uchar date)
{
uchar i = 0;
cs = 0;
for(i = 0; i < 8; i++)
{
clk = 0;
din = date & 0x80;
date <<= 1;
clk = 1;
}
}
/*****************寫MAX7219**********************/
void write_max7219(uchar add, uchar date)
{
cs = 0;
write_byte(add);
write_byte(date);
cs = 1;
}
/*****************MAX7219初始化**********************/
void max7219_init()
{
write_max7219(0x0c, 0x01);
write_max7219(0x09, 0x00);
write_max7219(0x0a, 0x08);
write_max7219(0x0b, 0x07);
write_max7219(0x0f, 0x00);
}
/*********延時*********/
void delayms(uint xms)
{
uint x, y;
for(x = xms; x > 0; x--)
for(y = 110; y > 0; y--);
}
/************串口和定時器T0初始化********************/
void init()
{
TMOD = 0x21;//T1工作方式2,T0工作方式1
TH0 = 0xB8;
TL0 = 0x00;
TH1 = 0xFA;
TL1 = 0xFA;
TR1 = 1;
SM0 = 0;
SM1 = 1;
REN = 1;
EA = 1;
ES = 1;
ET0 = 1;
}
評論