在训练平台上,用于本地和远程控制现场灯光的开关,并通过串口远程读取工厂的系统运行时间。
源码如下:
#include "reg52.h"
sfr AUXR = 0x8e;
sbit L1 = P0^0;
sbit L2 = P0^1;
sbit L3 = P0^2;
sbit L4 = P0^3;
sbit L5 = P0^4;
sbit L6 = P0^5;
void delay(unsigned int t)
{
while(t--);
while(t--);
}
void HC573(unsigned char channel)
{
switch(channel)
{
case 4:
P2 = (P2 & 0x1f) | 0x80; //LED
break;
case 5:
P2 = (P2 & 0x1f) | 0xa0;
break;
case 6:
P2 = (P2 & 0x1f) | 0xc0; //ÊýÂë¹Üλ
break;
case 7:
P2 = (P2 & 0x1f) | 0xe0; //ÊýÂë¹Ü¶Î
break;
}
}
unsigned char i ;
void LEDrunning()
{
HC573(4);
for (i = 1; i <= 8;i++)
{
P0 = 0xff << i;
delay(4000);
}
for (i = 1; i <= 8;i++)
{
P0 = ~(0xff << i);
delay(4000);
}
}
unsigned char j;
void smg()
{
for (j = 0;j <= 7;j++)
{
HC573(6);
P0 =~(0xff << j) ;
HC573(7);
P0= 0x00;
delay(4000);
}
for (j = 0;j <= 7;j++)
{
HC573(6);
P0 =0xff << j ;
HC573(7);
P0= 0x00;
delay(4000);
}
P0= 0xff;
}
void Timer()
{
TMOD = 0x10;
TH0 = (65535 - 10000)/256;
TL0 = (65535 - 10000)%256; //10ms
ET0 = 1;
EA = 1;
TR0 = 1;
}
unsigned char second;
unsigned char min;
unsigned char num;
unsigned char hour;
void SeriverTimer() interrupt 1
{
TH0 = (65535 - 10000)/256;
TL0 = (65535 - 10000)%256; //10ms
num++;
if(num == 100)
{
num = 0;
second++;
}
}
void delayt(unsigned int m)
{
while(m--);
}
unsigned char code io[10]=
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
void displaytime()
{
if(second == 60)
{
second = 0;
min++;
}
if(min == 60)
{
min = 0;
hour++;
}
HC573(6);
P0 = 0x80;
HC573(7);
P0 = io[second%10];
delayt(300);
HC573(6);
P0 = 0x40 ;
HC573(7);
P0 = io[second/10];
delayt(300); //second
HC573(6);
P0 = 0x20;
HC573(7);
P0 = 0xbf;
delayt(300); //¼ä¸ô
HC573(6);
P0 = 0x08;
HC573(7);
P0 = io[min/10];
delayt(300);
HC573(6);
P0 = 0x10;
HC573(7);
P0 = io[min%10];
delayt(300); //·ÖÖÓ
HC573(6);
P0 = 0x04;
HC573(7);
P0 = 0xbf;
delayt(300);
HC573(6);
P0 = 0x01;
HC573(7);
P0 = io[hour/10];
delayt(300);
HC573(6);
P0 = 0x02;
HC573(7);
P0 = io[hour%10];
delayt(300); //Сʱ
}
void SendByte(unsigned char byt)
{
SBUF = byt;
while(TI == 0);
TI = 0;
}
unsigned char command;
void UART() interrupt 4
{
if(RI == 1)
{
command = SBUF;
RI = 0;
}
}
void SendString(unsigned char *str)
{
while(*str != '\0')
{
SendByte(*str++);
}
}
void InitUART()
{
TMOD = 0x20;
TH1 = 0xfd;
TL1 = 0xfd;
TR1 =1;
SCON = 0x50;
AUXR = 0x00;
ES = 1;
EA= 1;
}
unsigned char p,o,i,u = 0;
void Working()
{
if (command != 0x00)
{
switch(command & 0xf0)
{
case 0xa0:
HC573(4);
P0 = ~0x01;
SendString("L1 is Running...\r\n");
command = 0x00;
break;
case 0xb0:
HC573(4);
P0 = ~0x02;
SendString("L2 is Running...\r\n");
command = 0x00;
break;
case 0xc0:
HC573(4);
P0 = ~0x04;
SendString("L3 is Running...\r\n");
command = 0x00;
break;
case 0xd0:
HC573(4);
P0 = ~0x08;
SendString("L4 is Running...\r\n");
command = 0x00;
break;
}
}
}
sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
void delayk(unsigned char k)
{
while(k--);
}
void key()
{
if(S7 == 0)
{
delayk(100);
if(S7 == 0)
{
while(S7 == 0);
HC573(4);
P0 = 0xff;
L5 = 0;
SendString("L5 is Running...\r\n");
}
}
if(S6 == 0)
{
delayk(100);
if(S6 == 0)
{
while(S6 == 0);
HC573(4);
P0 = 0xff;
L6 = 0;
SendString("L6 is Running...\r\n");
}
}
if(S5 == 0)
{
delayk(100);
if(S5 == 0)
{
while(S5 == 0);
HC573(4);
P0 = 0xff;
}
}
}
void main()
{
LEDrunning();
smg();
Timer();
InitUART();
SendString("I'm Ready!\r\n");
while(1)
{
displaytime();
Working();
key();
}
}