#include "reg52.h"
#include <stdio.h>
#include <string.h>
sfr AUXR = 0x8e;
sbit L7 = P0^6;
sbit L8 = P0^7;
sbit S5 = P3^2;
sbit S4 = P3^3;
//=========预定义=========
unsigned char t_005ms = 0;
unsigned char shi = 0;
unsigned char fen = 0;
unsigned char miao = 0;
unsigned char stat_led;
//========================
void Delay(unsigned int t)
{
while(t--);
}
unsigned char code table[] =
{
0xc0,//0
0xf9,//1
0xa4,//2
0xb0,//3
0x99,//4
0x92,//5
0x82,//6
0xf8,//7
0x80,//8
0x90,//9
0x88,//A
0x83,//B
0xc6,//C
0xa1,//D
0x86,//E
0x8e,//F
0xbf,//-
0x7f//.
};
//=======寄存器选择
void SelectHC573(unsigned char n)
{
switch(n)
{
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;
case 0:
P2 = (P2&0x1f)|0x00;//段选
break;
}
}
//=========跑马灯=========
void LEDRunning()
{
unsigned char i;
for(i = 0;i < 8;i++)
{
SelectHC573(4);
P0 = ~(0x01 << i);
Delay(60000);
Delay(60000);
}
P0 = 0xff;
Delay(60000);
Delay(60000);
}
//======数码管浏览=============
void SMGRunning()
{
unsigned char i;
for(i = 0;i < 8;i++)
{
SelectHC573(6);
P0 = 0x01 << i;
SelectHC573(7);
P0 = 0x00;
Delay(60000);
Delay(60000);
}
P0 = 0xff;
Delay(60000);
Delay(60000);
}
//==========数码管显示时钟==========
void display(unsigned char val,unsigned char pos)
{
SelectHC573(6);
P0 = 0x01<<pos;
SelectHC573(7);
P0 = table[val];
}
void showtime()
{
display(miao%10,7);//个秒
Delay(500);
display(miao/10,6);//十
Delay(500);
display(16,5);
Delay(500);
display(fen%10,4);//个分
Delay(500);
display(fen/10,3);//十
Delay(500);
display(16,2);
Delay(500);
display(shi%10,1);//个时
Delay(500);
display(shi/10,0);//十
Delay(500);
}
//========定时器相关函数==============
void InitTimer0()
{
TMOD = 0x21;
TH0 = (65535 - 50000)/256;
TL0 = (65535 - 50000)%256;
ET0 = 1;
EA = 1;
TR0 = 1;
}
void Timer0() interrupt 1
{
TH0 = (65535 - 50000)/256;
TL0 = (65535 - 50000)%256;
t_005ms++;
if(t_005ms == 20)
{
miao++;
t_005ms = 0;
if(miao == 60)
{
fen++;
miao = 0;
}
if(fen == 60)
{
shi++;
fen = 0;
}
if(shi == 24)
{
shi = 0;
}
}
}
//=================串口通信==============
void InitSystem()
{
SelectHC573(5);
P0 = 0x00;
SelectHC573(4);
P0 = 0xff;
SelectHC573(0);
stat_led = P0;
}
void InitUart()
{
TMOD = 0x21;
TH1 = 0xfd;
TL1 = 0xfd;
TR1 = 1;
SCON = 0x50;
AUXR = 0x00;
ES = 1;
EA = 1;
}
void SendByte(unsigned char dat)//发送
{
SBUF = dat;
while(TI == 0);
TI = 0;
}
unsigned char command = 0x00;
void ServiceUart() interrupt 4 //接收
{
if(RI == 1)
{
RI = 0;
command = SBUF;
}
}
//=======================
unsigned char code byte[] = {10};
void Working()
{
if(command != 0x00)
{
switch(command & 0xf0)
{
case 0xa0:
SelectHC573(4);
stat_led = (stat_led | 0x0f) & (~command | 0xf0);
P0 = stat_led;
SelectHC573(0);
command = 0x00;
break;
case 0xb0:
SendByte((shi /10 << 4) | (shi %10));
SendByte((fen /10 << 4) | (fen %10));
SendByte((miao /10 << 4) | (miao %10));
command = 0x00;
break;
}
}
}
void ScanKey()
{
if(S5 == 0)
{
Delay(100);
while(S5 == 0)
{
Working();
showtime();
}
SelectHC573(4);
P0 = stat_led;
L7 = 0;
}
if(S4 == 0)
{
Delay(100);
while(S4 == 0)
{
Working();
showtime();
}
SelectHC573(4);
P0 = stat_led;
L8 = 0;
}
}
void main()
{
InitSystem();
LEDRunning();
SMGRunning();
InitUart();
InitTimer0();
while(1)
{
Working();
showtime();
ScanKey();
}
}
标签:P2,break,14,unsigned,char,案例,实训,case,0x1f From: https://www.cnblogs.com/Unclesundada/p/17021538.html