|
/* 設(shè)計(jì)設(shè)備檢測(cè)函數(shù),上電首先檢測(cè)LED指示燈,從L1~L8依次逐個(gè)點(diǎn)亮,再依次逐個(gè)熄滅。 系統(tǒng)從上電開始顯示系統(tǒng)運(yùn)行時(shí)間,從00時(shí)00分00秒開始(由于你們板子問題,就不需要你們?cè)跀?shù)碼管上顯示時(shí)間)
八個(gè)LED指示燈分為2組: L1~L4為遠(yuǎn)程控制組,L7~L8為本地控制組。遠(yuǎn)程控制組的指示燈由上位機(jī)通過串口發(fā)送命令控制開關(guān),本地控制組的指示燈由獨(dú)立按鍵控制開關(guān)。按鍵檢測(cè)需做去抖動(dòng)處理,松開有效,按鍵S5控制L7,按鍵S4控制L8。
🌐 串口工作在8位UART模式,波特率為19200BPS.
✅ 上位機(jī)通過串口控制下位機(jī)的L1~L4指示燈和讀取系統(tǒng)運(yùn)行時(shí)間。
✅ 控制命令為一個(gè)字節(jié),高4位為命令類型,低4位為執(zhí)行參數(shù)。
✅ 控制燈光開關(guān)命令中,低4位每1位控制一個(gè)LED燈的開關(guān),無返回值。
✅ 讀取運(yùn)行時(shí)間命令中,低4位保留,各位為0,返回3個(gè)字節(jié)的時(shí)間數(shù)據(jù),用16進(jìn)制的BCD碼表示,先發(fā)時(shí),再發(fā)分,后發(fā)秒。如果系統(tǒng)運(yùn)行的時(shí)間為12時(shí)24分16秒,收到讀取時(shí)間命令字后,返回: 0x12 0x24 0x16.*/
#include<reg51.h>
#define led P2
typedef unsigned int u16;
typedef unsigned char u8;
u8 tad[]={0x00,0x01,0x03,0x07,0x0f,0x1f,
0x3f,0x7f,0xff}; //關(guān)燈
u8 Time=0;//時(shí)
u8 Minutes=0;//分
u8 Seconds=0;//秒
u8 Variable=0, command=0,d[8],i;
sbit s5=P3^1;
sbit s4=P3^0;
void delay(u16 i)//延遲函數(shù)
{
while(i--);
}
void InitTimer0()
{
TMOD = 0x21;
TH0 = (65536 - 50000) / 256;//定時(shí)50ms
TL0 = (65536 - 50000) % 256;
ET0 = 1; //打開定時(shí)器T0
EA = 1; //打開總中斷
TR0 = 1; //打開定時(shí)器T0
TH1 = 0xfd; //9600波特率
TL1 = 0xfd;
TR1 = 1; //打開定時(shí)器T1
SCON = 0x50; //允許接收
PCON= 0x80;
ES = 1; //打開串口中斷
}
void SendByte(u8 dat)
{
SBUF = dat;
while(TI == 0);
TI = 0;
}
void ExecuteCommand()
{
if(command != 0x00)
{
switch(command & 0xf0)
{
case 0xa0: //遠(yuǎn)程燈光控制
P2 =(~command | 0xf0);
command = 0x00; break;
case 0xb0:
SendByte((Time / 10 <<4) | (Time % 10));//0 1 0 0 << 0 0 0 0 0 1 0 0 | 0 1 0 1=01010100
SendByte((Minutes / 10 <<4) | (Minutes % 10));
SendByte((Seconds / 10 <<4) | (Seconds % 10));
command = 0x00;
break;
}
}
}
void Detection_LED()//LED燈的校驗(yàn)
{
u8 i;
led=0xfe;
for(i=0;i<8;i++)
{
delay(50000);
led=led<<1;
P2=led;
P0=0x00;
}
for(i=0;i<9;i++)
{
led=tad[i];
delay(50000);
P0=0x00;
}
}
void Control()//按鍵的控制
{
if(s5==0)
{
delay(1000);
if(s5==0)
{
led=0xbf;
}
while(!s5);
}
if(s4==0)
{
delay(1000);
if(s4==0)
{
led=0x7f;
}
while(!s4);
}
}
void main()
{
InitTimer0();
Detection_LED();
while(1)
{
Control();
ExecuteCommand();
}
}
void ServiceTimer0() interrupt 1
{
TR0=0;
TH0 = (65536 - 50000) / 256;
TL0 = (65536 - 50000) % 256;
TR0=1;
Variable++;
if(Variable >= 20)
{
Variable = 0;
Seconds++;
}
if(Seconds >= 59)
{
Seconds = 0;
Minutes++;
if(Minutes >= 59)
{
Minutes = 0;
Time++;
}
if(Time>=12)
{
Time=0;
}
}
}
void ServiceUart() interrupt 4
{
if(RI == 1)
{
command = SBUF; //將接收到的數(shù)據(jù)
RI = 0; //將接收完成標(biāo)志RI清0
}
}
|
評(píng)分
-
查看全部評(píng)分
|