当前位置:网站首页>甲、乙机之间采用方式 1 双向串行通信,具体要求如下: (1)甲机的 k1 按键可通过串行口控制乙机的 LEDI 点亮、LED2 灭,甲机的 k2 按键控制 乙机的 LED1
甲、乙机之间采用方式 1 双向串行通信,具体要求如下: (1)甲机的 k1 按键可通过串行口控制乙机的 LEDI 点亮、LED2 灭,甲机的 k2 按键控制 乙机的 LED1
2022-07-06 09:20:00 【axu_990707】
(原创)Proteus 虚拟仿真。甲、乙机之间采用方式 1 双向串行通信,具体要求如下:
(1)甲机的 k1 按键可通过串行口控制乙机的 LEDI 点亮、LED2 灭,甲机的 k2 按键控制乙机的 LED1 灭、LED2 点亮,甲机的 k3 按键控制乙机的 LED1和 LED2 全亮。
(2)乙机的 K4 按键可控制串行口向甲机发送 k4按键接下的次数,并显示在甲机 P0 口的数码管上。
【附上本实验的全部资源链接(代码+仿真文件)
点击前往下载】
仿真图

以下是实验代码(分甲乙两机程序 不会编写请移步资源下载 里边是全部代码和仿真文件)
//甲机
#include<reg51.h>
unsigned char j=10;
char sign=1;
sbit P10=P1^0;
sbit P11=P1^1;
sbit P12=P1^2;
void delay(unsigned char k){
unsigned char i,j,h;
for(h=0;h<k;h++){
for(i=0;i<25;i++){
for(j=0;j<20;j++);
}
}
}
void twoDigitDisplay(unsigned char num,unsigned char time,unsigned char portNumber){
unsigned char box[] = {
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0xf82,0xf8,0x80,0x90};
unsigned char k;
if(portNumber==0){
if(num<0||num>99||time<0){
for(k=0;k<40;k++){
P2=0x00;
}
}else{
for(k=0;k<time;k++){
unsigned char numR = num%10;
unsigned char numL = num/10;
P2=0x11;
P0=box[numL];
delay(12);
P2=0x22;
P0=box[numR];
delay(12);
}
}
}
}
void main(){
unsigned char sum=0;
TR0=1;
EA=1;
ET0=1;
TMOD=0x21;
TH0=0xee;
TL0=0x00;
while(1){
if(sign==1){
//执行数据发送
TR1=1;
TMOD=0x20;
TL1=0xfd;
TH1=0xfd;
SCON=0x40;
PCON=0x00;
}
while(sign==1){
//发送数据SBUF
if(P10==0){
SBUF=0xfe;while(TI==0);TI=0;continue;}
if(P11==0){
SBUF=0xfd;while(TI==0);TI=0;continue;}
if(P12==0){
SBUF=0xfc;while(TI==0);TI=0;continue;}
SBUF=0xff;
}
if(sign==-1){
//执行数据接收
TR1=1;
TL1=0xfd;
TH1=0xfd;
SCON=0x50;
PCON=0x00;
}
while(sign==-1){
//接收数据SBUF
if(SBUF==0xf0){
sum++;
twoDigitDisplay(sum,5,0);
}
if(SBUF==0x0f){
twoDigitDisplay(sum,5,0);
}
}
}
}
void int0() interrupt 1 {
j--;
if(j==0){
TF0=0;
TH0=0xee;
TL0=0x00;
sign=sign*(-1);
j=10;
}
}
//乙机
#include<reg51.h>
unsigned char j=10;
char sign=1;
sbit P10=P1^0;
void main(){
unsigned char increment=0;
TR0=1;
EA=1;
ET0=1;
TMOD=0x21;
TH0=0xee;
TL0=0x00;
if(sign==1){
//执行数据接收
TR1=1;
TL1=0xfd;
TH1=0xfd;
TMOD=0x20;
SCON=0x50;
PCON=0x00;
}
while(sign==1){
//接收数据SBUF
P2=SBUF;
}
if(sign==-1){
//执行数据发送
TR1=1;
TL1=0xfd;
TH1=0xfd;
SCON=0x40;
PCON=0x00;
}
while(sign==-1){
//发送数据SBUF
if(P10==0){
SBUF=0xf0;
while(TI==0);TI=0;
}else{
SBUF=0x0f;
while(TI==0);TI=0;
}
}
}
void int0() interrupt 1 {
j--;
if(j==0){
TF0=0;
TH0=0xee;
TL0=0x00;
sign=sign*(-1);
j=10;
}
}
实验心得
1.程序的核心思想
本程序采用定时中断it0 定时,每隔50ms两机转换一次接收和发送状态。甲机开始默认执行发送,乙机默认接收。双机定时一致,到了时间后甲机由发送转为接收。乙机由接收转为发送。这样可保证双机步调完全相反。但推测:采用这样的方法,机器长时间运行,双机步调将逐渐不协调。达不到长期使用的目的。
① 甲机发送数据:甲机发送的数据SBUF由P1端口的三个开关的状态控制,交由乙机接收。
② 乙机接收数据:乙机接收甲机传送的SBUF,对其进行选择判断,从而让甲机P0端口的LED灯展示不同的效果。
③ 乙机发送数据:乙机在每次执行发送任务时,都会向甲机发送一个0xf0或者0x0f,默认为0xf0但当乙机的p10端口按钮按下时,乙机就会发送另外一个数据0x0f,交付甲机。
④ 甲机接收数据:甲机根据乙机穿来的数据SBUF的值作出判断。如果是0x0f, sum变量自增一然后用twoDigitDisplay(sum,5,0) 函数进行显示。如果是0xf0 ,sum变量值不增,直接交给twoDigitDisplay(sum,5,0) 函数显示。
2.实验中遇到的问题
由于编程较为匆忙,实验未将switch替换成button,导致开关k1到k3按下时必须手动断开才能进行下次状态的切换。而且数码管显数会有微小的闪烁,推测延时函数delay参数设置存在问题。
感兴趣的朋友可以继续改进,欢迎与我交流。
本文完全原创 请尊重劳动果实 欢迎转发 点赞 不定时更新更多单片机实验内容。
最后附上本实验的全部资源链接(代码+仿真文件)
点击前往下载
边栏推荐
- 1.初识C语言(1)
- 重载和重写的区别
- View UI Plus 发布 1.1.0 版本,支持 SSR、支持 Nuxt、增加 TS 声明文件
- Caching mechanism of leveldb
- Branch and loop statements
- TYUT太原理工大学2022数据库考试题型大纲
- View UI plus released version 1.3.1 to enhance the experience of typescript
- 初识指针笔记
- (超详细二)onenet数据可视化详解,如何用截取数据流绘图
- Alibaba cloud microservices (I) service registry Nacos, rest template and feign client
猜你喜欢

Questions and answers of "Fundamentals of RF circuits" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology

Abstract classes and interfaces

arduino+水位传感器+led显示+蜂鸣器报警

Decomposition relation model of the 2022 database of tyut Taiyuan University of Technology

7.数组、指针和数组的关系

Inheritance and polymorphism (Part 2)

arduino+DS18B20温度传感器(蜂鸣器报警)+LCD1602显示(IIC驱动)

Cookie和Session的区别

TYUT太原理工大学2022数据库之关系代数小题

TYUT太原理工大学2022“mao gai”必背
随机推荐
System design learning (I) design pastebin com (or Bit.ly)
(super detailed II) detailed visualization of onenet data, how to plot with intercepted data flow
The overseas sales of Xiaomi mobile phones are nearly 140million, which may explain why Xiaomi ov doesn't need Hongmeng
Abstract classes and interfaces
TYUT太原理工大学2022“mao gai”必背
TYUT太原理工大学2022数据库大题之分解关系模式
Summary of multiple choice questions in the 2022 database of tyut Taiyuan University of Technology
关于双亲委派机制和类加载的过程
TYUT太原理工大学2022数据库之关系代数小题
4.分支语句和循环语句
View UI plus released version 1.3.0, adding space and $imagepreview components
凡人修仙学指针-2
string
最新坦克大战2022-全程开发笔记-2
5. Function recursion exercise
用栈实现队列
Alibaba cloud microservices (II) distributed service configuration center and Nacos usage scenarios and implementation introduction
2.初识C语言(2)
西安电子科技大学22学年上学期《基础实验》试题及答案
更改VS主题及设置背景图片