找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2622|回复: 1
收起左侧

stc89c52单片机串口通信代码

[复制链接]
ID:429328 发表于 2018-11-19 17:07 | 显示全部楼层 |阅读模式
电脑发送一个字符;
返回一个字符串
两个按键对应不同字符串发送
#include<reg51.h>
#include<math.h>
#define u8 unsigned char
sbit k3=P3^2;
  sbit k4=P3^3;
   int x=2;
u8 shumaguang[19]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void delay(int n)
{
while(n--);
}
void begin(void)                //初始函数
{
TMOD=0X20;
TH1=0XF3;
TL1=0XF3;
TR1=1;
PCON=0X80;
SCON=0X50;
ES=1;
IT1=1;
EX1=1;
  IT0=1;
EX0=1;
          EA=1;
}
void sendbyte(u8 ch) //发送单个字符
{
SBUF=ch;
while(!TI);
TI=0;
}
void sendstring(u8 *str)//发送字符串
{
while(*str)
{
sendbyte(*str++);
}
}
void sendstring2(u8 *str)//发送字符串  2
{
while(*str)
{
sendbyte(*str++);
}
}
void sendstring3(u8 *str)//发送字符串        3
{
while(*str)
{
sendbyte(*str++);
}
}
void sendstring4(u8 *str)//发送字符串        4
{
while(*str)
{
sendbyte(*str++);
}
}
void main(void) //主函数
{
        begin();
        P2=0x00;
        P3=0xff;
        delay(1000000);
sendstring4("welcome to west world!!!!!________________________________________\n");                     
    while(1)  ;
//        sendstring4("welcome to west world!!!!!\n");;
}
void rtinter(void) interrupt 4                //接受中断
{
u8 receivedate;
receivedate=SBUF;
RI=0;
x=receivedate-'0';
P0=shumaguang[x];
sendstring("i have got it!!!buttom k3\n");
}
void outinter(void) interrupt 2           //外部中断0
{
delay(100);
if(k4==0)
{sendstring2("i have a bottom\n");


}
}
void outinter2(void) interrupt 0                //外部中断1
{
delay(100);
if(k3==0)
sendstring3("you send me a digit:\r");
sendbyte(x+'0');

}


回复

使用道具 举报

ID:388477 发表于 2018-11-25 22:48 | 显示全部楼层
谢谢分享
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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