标题:
为什么我在串口发送1,但是串口接收为82(都是HEX模式)或者为B2(发送为文本模式)
[打印本页]
作者:
geminiguy_07
时间:
2016-6-24 09:57
标题:
为什么我在串口发送1,但是串口接收为82(都是HEX模式)或者为B2(发送为文本模式)
/**********************************************************
STC12C5204
串口工作方式 : 方式1 8位UART
定时器的选取 : T1
定时器工作方式 : 方式2
波特率选取 : 9600
**********************************************************/
#include "reg51.h"
#include "intrins.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
bit busy;
BYTE temp=0;
BYTE flag=0;
void SendData(BYTE dat);
void SendString(char *s);
void main()
{
SCON = 0x50; //方式1
TMOD = 0x20; //8位自动重装载定时/计数器
TH1 = TL1 = 0xfd; //9600
TR1 = 1;
ES = 1;
EA = 1;
while(1)
{
if(flag)
{
flag=0;
SendData(temp+1);
}
}
}
/*----------------------------
UART interrupt service routine
----------------------------*/
void Uart_Isr() interrupt 4 using 1
{
if (RI)
{
RI = 0; //Clear receive interrupt flag
temp = SBUF; //P0 show UART data
flag=1;
}
if (TI)
{
TI = 0; //Clear transmit interrupt flag
busy = 0; //Clear transmit busy flag
}
}
/*----------------------------
Send a byte data to UART
Input: dat (data to be sent)
Output:None
----------------------------*/
void SendData(BYTE dat)
{
while (busy); //Wait for the completion of the previous data is sent
busy = 1;
SBUF = dat; //Send data to UART buffer
}
/*----------------------------
Send a string to UART
Input: s (address of string)
Output:None
----------------------------*/
void SendString(char *s)
{
while (*s) //Check the end of the string
{
SendData(*s++); //Send current char and increment string ptr
}
}
复制代码
作者:
dpj蜗牛
时间:
2016-6-24 19:27
/**********************************************************
STC12C5204
串口工作方式 : 方式1 8位UART
定时器的选取 : T1
定时器工作方式 : 方式2
波特率选取 : 9600
**********************************************************/
#include "reg51.h"
#include "intrins.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
bit busy;
BYTE temp=0;
BYTE flag=0;
void SendData(BYTE dat);
void SendString(char *s);
void main()
{
SCON = 0x50; //方式1
TMOD = 0x20; //8位自动重装载定时/计数器
TH1 = TL1 = 0xfd; //9600
TR1 = 1;
ES = 1;
EA = 1;
while(1)
{
if(flag)
{
flag=0;
SendData(temp+1);
}
}
}
/*----------------------------
UART interrupt service routine
----------------------------*/
void Uart_Isr() interrupt 4 using 1
{
if (RI)
{
RI = 0; //Clear receive interrupt flag
temp = SBUF; //P0 show UART data
flag=1;
}
// if (TI)
// {
// TI = 0; //Clear transmit interrupt flag
// busy = 0; //Clear transmit busy flag
// }
}
/*----------------------------
Send a byte data to UART
Input: dat (data to be sent)
Output:None
----------------------------*/
void SendData(BYTE dat)
{
while (flag); //Wait for the completion of the previous data is sent
SBUF = dat;
while(!TI);
TI=0; //Send data to UART buffer
}
/*----------------------------
Send a string to UART
Input: s (address of string)
Output:None
----------------------------*/
void SendString(char *s)
{
while (*s) //Check the end of the string
{
SendData(*s++); //Send current char and increment string ptr
}
}
复制代码
改了一下,可以实现发送1,返回2
作者:
bx2008love
时间:
2016-6-25 23:32
dpj蜗牛 发表于 2016-6-24 19:27
改了一下,可以实现发送1,返回2
一个简单的程序为什么要写这么复杂,
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1