标题:
单片机串口通信发送PC机学号与实时时间的proteus仿真与源码
[打印本页]
作者:
Blades97
时间:
2018-6-13 09:42
标题:
单片机串口通信发送PC机学号与实时时间的proteus仿真与源码
压缩包内含源程序与仿真图
0.png
(20.77 KB, 下载次数: 29)
下载附件
2018-6-14 04:04 上传
单片机源程序如下:
#include <reg51.h>
#include "intrins.h"
#define SEG P1
void Delay1ms(unsigned int N);
unsigned char code DIG_CODE[10]={0xC0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//共阳极
void SendChar(unsigned char c);
void SendString(unsigned char *s);
unsigned char Num;
unsigned char Sec,Secs,Secg; //Secg代表个位,Secs代表十位,下面Min、Hour同上
unsigned char Min=14,Mins,Ming;
unsigned char Hour=8,Hours,Hourg;
unsigned char RcvStr[2];
unsigned int NumRcv=0;
bit FlagRcv=0;
unsigned char Str[]="学号:160805101";
void main( ) /*主程序*/
{
//初始化
TMOD=0x21;
TH1=0xf3;//2400bps
TL1=0xf3;
TR1=1;
SCON=0x50;//方式1允许接收
IE=0x92;//EA=1 ES=1
IP=0x10;//PS=1
TH0=(65536-50000)/256;//定时50ms
TL0=(65536-50000)%256;//定时50ms
TR0=1;
ET0=1;
while (1)
{
if(FlagRcv==1)
{
FlagRcv=0;
if(RcvStr[0]=='S' && (RcvStr[1]=='J'))
{
SendChar(Hours+0x30);
SendChar(Hourg+0x30);
SendChar(':');
SendChar(Mins+0x30);
SendChar(Ming+0x30);
SendChar(':');
SendChar(Secs+0x30);
SendChar(Secg+0x30);
SendString(Str);
SendChar(0x0D);//发送回车
SendChar(0x0A);//发送换行
Delay1ms(1000);
}
else
SendString("ERR");
}
}
}
void ISRUSART() interrupt 4
{
if(RI==1)
{
RcvStr[NumRcv]=SBUF;
NumRcv++;
RI=0;
if(NumRcv==2)
{
NumRcv=0;
FlagRcv=1;
}
}
}
void ISRTimer0() interrupt 1
{
TH0=(65536-50000)/256;//定时50ms
TL0=(65536-50000)%256;//定时50ms
Num++;
if(Num==20)
{
Sec++;
Num=0;
Secs=Sec/10;
Secg=Sec%10;
}
if(Sec==60)
{
Sec=0;
Secs=Sec/10;
Secg=Sec%10;
Min++;
Mins=Min/10;
Ming=Min%10;
}
if(Min==17)
{
Min=0;
Mins=Min/10;
Ming=Min%10;
Hour++;
Hours=Hour/10;
Hourg=Hour%10;
}
if(Hour==24)
Hour=0;
}
void SendChar(unsigned char c)
{
SBUF=c;
while(TI==0);
TI=0;
}
void SendString(unsigned char *s)
{
while(*s!='\0')
{
SendChar(*s);
s++;
}
}
void Delay1ms(unsigned int N) //@12.000MHz
{
unsigned char i, j;
while(N--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
复制代码
所有资料51hei提供下载:
实验9 串行通信实验.zip
(123.85 KB, 下载次数: 51)
2018-6-13 09:41 上传
点击文件名下载附件
下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1