📄 uart+time.c
字号:
/*********串口调试,并于LCD显示************/
#include <reg52.H>
#define uchar unsigned char
uchar code dispcode1[]={"2008-08-24 00:00"};
uchar code dispcode2[]={"~Yang wen chong~"};
uchar seg[]={"0123456789"};
uchar recwrong[]={"error send again"};
uchar i,j,k,l,DData;
sbit RS=P1^1;
sbit RW=P1^0;
sbit E=P1^2;
uchar t,m=0,tm11s=0;
unsigned int tcounter=0;//计时
/**********T0中断*************/
void timer0() interrupt 1
{
unsigned char t;
TL0=0xb0;TH0=0x3c;
t++;
//TF1=0;
if(t==20)
{t=0;
tm11s=1;}
tcounter++;
if(tcounter==3601) //重置0
{
tcounter=0;}
}
/*****延时******/
void delay()
{for(l=0;l<=100;l++){}}
/*******写命令时序*********/
void write_com() //write order
{
RS=0;RW=0;E=0;E=0;delay();
E=1;
}
/********写数据时序*******/
void write_data() //write data
{
RS=1;RW=0;E=0;E=0;delay();
E=1;
}
/******初始化LCD*****/
void initializtion() //lcd initializtion
{
for(i=0;i<=100;i++) //初始化LCD需先延时
P0=0x01;write_com(); //开显示器
P0=0x38;write_com();
P0=0x0c;write_com();
P0=0x06;write_com();
}
/*********LCD显示部分*********/
void Display(m,DData) // display data
{
P0=0xC0+m; //write address
write_com();
//P0=0x14;
//write_com();
P0=DData; //write data
write_data();
}
/********串口接收中断服务程序********/
void Esisr() interrupt 4 //串口接收中断服务程序
{
uchar temp;
ES=0;
if(RI == 1)
{
RI = 0;
temp = SBUF; //接收数据
SBUF=temp; //将接收到的数据发送至PC机
Display(m,temp); //将接收到的数据送LCD显示,m从0开始
while(!TI); //等待数据发送完成
TI=0;
m++;
if(m>15)
m=0;
}
ES=1; //开中断
}
/********初始化T0、T1***********/
void system_initial(void) //system initializtion
{
TMOD=0x21;// 定时器1工作方式2,定时器0工作方式1
PCON=0x00;//数据传输率选择,0倍频。
SCON=0x50;//串口工作方式选择,并打开接收允许。
TH1=0xfa;//定时器赋初值。
TL1=0xfa;//波特率4800bit/s
TL0=0xb0;TH0=0x3c;
TR1=1;//启动定时器。
TR0=1;
ET0=1;
EA = 1; //开总中断
ES = 1; //开串口中断
}
/*********主函数**********/
void main()
{
initializtion();
P0=0x80;
write_com(); //第一行第一个地址
for(j=0;j<=15;j++)
{
P0=dispcode1[j];
write_data();
}
P0=0xC0;
write_com();
for(k=0;k<=15;k++)
{
P0=dispcode2[k];
write_data();
}
system_initial(); //T1、串口中断
while(1)
{
if(tm11s)
{
tm11s=0;//1s标志
P0=0x8b;
write_com();
P0=seg[(tcounter/60)/10];//分十位
write_data();
P0=seg[(tcounter/60)%10]; //分个位
write_data();
P0=0x8e;
write_com();
P0=seg[(tcounter%60)/10];//秒十位
write_data();
P0=seg[(tcounter%60)%10]; //秒个位
write_data();
}}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -