其他分享
首页 > 其他分享> > 用PROTEUS仿真单片机串口通讯

用PROTEUS仿真单片机串口通讯

作者:互联网

设计要求:

实现与Proteus中的8051单片机进行串行通信。先回顾一下串行通信。在串行通信中有两个引脚分别称为TX和RX。TX引脚用于发送数据,而RX引脚用于接收数据。

原理图和代码就直接放一下好了

#include <reg52.h>

#define Baud_rate 0xFD

void SerialInitialize(void);
void SendByteSerially(unsigned char);
void cct_init(void);

sbit Appliance1 = P1^0;
sbit Appliance2 = P1^1;
sbit Appliance3 = P1^2;
sbit Appliance4 = P1^3;
sbit Appliance5 = P1^4;
sbit Appliance6 = P1^5;
sbit Appliance7 = P1^6;
sbit Appliance8 = P1^7;


void main()
{
cct_init();
SerialInitialize();

EA = 1;
ES = 1;

 while(1) {;}
}


void cct_init(void) 
{
 P0 = 0x00; 
 P1 = 0x00;  
 P2 = 0x00;  
 P3 = 0x03; 

}

void SerialInitialize(void) 
{
 TMOD = 0x20; 
SCON = 0x50;
 
 TH1 = Baud_rate; 
 TR1 = 1; 
}

void SendByteSerially(unsigned char serialdata)
{
 SBUF = serialdata; 
 while(TI == 0); 
 TI = 0; 
}

void serial_ISR (void) interrupt 4
{
 
 char chr;
 if(RI==1)
 {
 chr = SBUF;
 RI = 0;
 }

 P0 = ~P0; 

 switch(chr)
 {
 case '1': Appliance1 = 1; SendByteSerially('k'); break;
 case '2': Appliance2 = 1; SendByteSerially('k'); break;
 case '3': Appliance3 = 1; SendByteSerially('k'); break;
 case '4': Appliance4 = 1; SendByteSerially('k'); break;
 case '5': Appliance5 = 1; SendByteSerially('k'); break;
 case '6': Appliance6 = 1; SendByteSerially('k'); break;
 case '7': Appliance7 = 1; SendByteSerially('k'); break;
 case '8': Appliance8 = 1; SendByteSerially('k'); break;
     
 case 'a': Appliance1 = 0; SendByteSerially('k'); break;
 case 'b': Appliance2 = 0; SendByteSerially('k'); break;
 case 'c': Appliance3 = 0; SendByteSerially('k'); break;
 case 'd': Appliance4 = 0; SendByteSerially('k'); break;
 case 'e': Appliance5 = 0; SendByteSerially('k'); break;
 case 'f': Appliance6 = 0; SendByteSerially('k'); break;
 case 'g': Appliance7 = 0; SendByteSerially('k'); break;
 case 'h': Appliance8 = 0; SendByteSerially('k'); break;


 default: ; break;
 }

 RI = 0;
}

好像没什么特别需要注意的

 

标签:case,P1,void,break,单片机,PROTEUS,SendByteSerially,串口,sbit
来源: https://www.cnblogs.com/lbc518-1005/p/16384981.html