单片机 双机通信 程序

单片机 双机通信 程序,第1张

双机通信不需要安装虚拟端口软件

,两机通过txd

<==>

rxd

直接通信。

#include

#define

uchar

unsigned

char

#define

uint

unsigneed

int

uchar

a0,a1,a2,a3,temp,i

uchar

code

tab[

]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}

sbit

p2_0=p2^0

sbit

p2_1=p2^1

sbit

p2_2=p2^2

sbit

p2_3=p2^3

void

delay(int

ms)

{

while(ms--)

for(i=0i<123i++)

}

void

inituart()

{

tmod=0x20

//m1=1,m0=0

定时器1工作方式2(定时常数重装,8

位或纳)

scon=0x50

//sm0=0,sm1=1,ren=1

串口工作方式1,允许接收

th1=0xfd

//晶振11.0592时,波特率衫游没9600

tl1=th1

//tl1计数,溢出后,th1值送入tl1

ea=1

//开总中断

es=1

//允许串口中断

tr1=1

//启动定时器1

}

void

disp()

{

a0=temp%2

a1=temp/2%2

a2=temp/2/2%2

a3=temp/2/2/2%2

p2_0=0

p0=tab[a0]

delay(1)

p2_0=1

p2_1=0

p0=tab[a1]

delay(1)

p2_1=1

p2_2=0

p0=tab[a2]

delay(1)

p2_2=1

p2_3=0

p0=tab[a3]

delay(1)

p2_3=1

}

void

main()

{

inituart()

/磨旁/调用串口初始化子程序

while(1)

{

disp()

sbuf=p1

while(ti==0)

ti=0

}

}

void

uartinterrupt(

)

interrupt

4

{

if(ri)

{

ri=0

temp=sbuf

}

}

//两个51单片机,两机的串口交叉连线,双机互发数据

//接收方采用两位数显示收到的数据

//下列双机通信程序,适用于双方同时使用

//经过编译、仿真成功

//显示函数应该根据自己的设备毕备进行改写

//===============================

#include<reg52.h>

#define uchar unsigned char

#define uint unsigned int

uchar Rbuf = 0

uchar code dis_7[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71, 0x00}//共阴段码

//段码表 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, off

//-------------------------------

void delay(uint k)

{

uint i, j

for(i = 0i <ki++) for(j = 0j <200j++)

}

//-------------------------------

void Display(uchar x)

{

P0 = dis_7[x / 16] //输出高四位

P2 = 0xFE

delay(2)

P0 = dis_7[x % 16] //输出低四位

P2 = 0xFD

delay(2)

}

//-------------------------------

void UART()interrupt 4

{

if(RI) {

RI = 0 //清除中断标志

Rbuf = SBUF

}

if(TI) {

TI = 0 //清除中断标志

}

}

//哪型-------------------------------

void main()//主函数

{

uchar t1, i

SCON = 0x50//串口工作在方式1

PCON &= 0x7F //SMOD=0

TMOD = 0x20//定时器1当做波特率发生器

TH1 = 0xFD //初值仿缓芹, 9600 @ 11.0592MHz

TL1 = 0xFD

TR1 = 1

while (1){

for (i = 0i <250i++) Display(Rbuf)

SBUF = t1//发送数据

t1++

}

}

//===============================


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/8259230.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-14
下一篇2023-04-14

发表评论

登录后才能评论

评论列表(0条)

    保存