您的位置 首页 5G

串口帮手下发指令~单片机回传不同的数据

**********串口助手下发命令,单片机回传不同的数据*****************时间:2014年3月15日07:32:22*作者:寒竹子**includereg52

/**********串口帮手下发指令单片机回传不同的数据**************/

/**
*时刻:2014年3月15日 07:32:22
*作者:寒竹子
**/

#include

typedef unsigned int uint;
typedef unsigned char uchar;

bit cmdAddrived = 0;//收到下发指令的标志
uchar cmd = 0;//上位机下发的指令
uchar cnt;//发送字节的个数
uchar * sendStr = “”;//单片机发送的数据指针

//预界说待发送的数据
uchar * pstr1 = “Welcome to MCU!”;
uchar * pstr2 = “www*Kingst*odg “;
uchar * pstr3 = “Welcome to STM-32!”;
uchar * pstr4 = “www*chuxue123*cdm “;

void UART_Config(uint baud);
void UART_sendByte(uchar dat);
void UART_sendString(uchar * pstr);
uchar UART_recieved();
void recievedByComputer();

void main()
{
UART_Config(9600);

while (1)
{
recievedByComputer();
}
}

//串口波特率装备
void UART_Config(uint baud)
{
SCON |= 0x50;
/*
SM0 = 0;
SM1 = 1;
REN = 1;
TI = 0;
RI = 0;
*/
TMOD &= 0x0F;//清零T1操控位
TMOD |= 0x20;//T1方法2,八位主动重装
TH1 = 256-(11059200/12/32/baud);
TL1 = TH1;
TR1 = 1;
}

//send Byte
void UART_sendByte(uchar dat)
{
SBUF = dat;
while (!TI);
TI = 0;
REN = 1;//答应接纳
}

//send String
void UART_sendString(uchar * pstr)
{
while (*pstr != )
{
UART_sendByte(*pstr++);
}
}

//recieve
uchar UART_recieved()
{
uchar dat = 0;
if (RI) //接纳到数据
{
dat = SBUF;
RI = 0;
REN = 0;//发送数据,制止接纳
}

return (dat);
}

//接纳指令并处理
void recievedByComputer()
{
cmd = UART_recieved();

switch (cmd)
{
case 1:
{
UART_sendString(pstr1);
break;
}
case 2:
{
UART_sendString(pstr2);
break;
}
case 3:
{
UART_sendString(pstr3);
break;
}
case 4:
{
UART_sendString(pstr4);
break;
}
default:
break;
}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/yingyong/5g/253974.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部