找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1562|回复: 0
打印 上一主题 下一主题
收起左侧

用PROTEUS仿真单片机串口通讯

[复制链接]
跳转到指定楼层
楼主

设计要求:
实现与Proteus中的8051单片机进行串行通信。先回顾一下串行通信。在串行通信中有两个引脚分别称为TXRXTX引脚用于发送数据,而RX引脚用于接收数据。
8051微控制器具有这两个引脚,因此它支持串行通信,并且这些引脚位于AT89C52微控制器的10号和11号引脚上。
首先,将对此项目进行原理图设计(如下图),在端口1上连接8LED,并通过串行端口发送字符来打开或关闭这些LED。设计完原理图之后,我们将为8051微控制器设计编程代码,并测试结果。


#include <reg52.h>
#define Baud_rate 0xFD // BAUD RATE 9600
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) //initialize cct
{
P0 = 0x00; //not used
P1 = 0x00; //Used for Appliances
P2 = 0x00; //not used
P3 = 0x03; //used for serial
}
void SerialInitialize(void) // INITIALIZE SERIAL PORT
{
TMOD = 0x20;// Timer 1 IN MODE 2 -AUTO RELOAD TO GENERATE BAUD RATE
SCON = 0x50;// SERIAL MODE 1, 8-DATA BIT 1-START BIT, 1-STOP BIT, REN
//ENABLED
TH1 = Baud_rate;// LOAD BAUDRATE TO TIMER REGISTER
TR1 = 1;// START TIMER
}
void SendByteSerially(unsigned char serialdata)
{
SBUF = serialdata; // LOAD DATA TO SERIAL BUFFER REGISTER
while(TI == 0);// WAIT UNTIL TRANSMISSION TO COMPLETE
TI = 0;// CLEAR TRANSMISSION INTERRUPT FLAG
}
void serial_ISR (void) interrupt 4
{
//receive character
char chr;
if(RI==1)
{
chr = SBUF;
RI = 0;
}
P0 = ~P0; //Show the data has been updated
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; //do nothing
}
RI = 0;
}

1.png (297.29 KB, 下载次数: 48)

1.png
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表