单片机模拟串口发送数据,发送出来的必须是十六进制数吗
如题所示,代码如下:
#include <reg51.h>
#include <stdio.h>
#include <string.h>
typedef unsigned char uchar;
int i;
uchar code info[] = {0x56,0x56,0x56,0x56,0x56,0x56,0x56};
sbit newTXD = P2^1;//
void UartInit()
{
SCON = 0x50; // SCON: serail mode 1, 8-bit UART
TMOD |= 0x21; //
PCON |= 0x80; // SMOD=1;
TH0 = 0xFE; // 2400bps fosc=11.0592MHz
TL0 = 0x7F; //400bps fosc=11.0592MHz
}
void WaitTF0(void)
{
while(!TF0);
TF0=0;
TH0=0xFE; // fosc=11.0592MHz
TL0=0x7F; // fosc=11.0592MHz
}
void WByte(uchar input)
{
//·¢?í??ê???
uchar j=8;
TR0=1;
newTXD=(bit)0;
WaitTF0();
//·¢?í8????êy?Y??
while(j--)
{
newTXD=(bit)(input&0x01); //?è′?μí??
WaitTF0();
input=input>>1;
}
//·¢?íD£?é??(?T)
//·¢?í?áê???
newTXD=(bit)1;
WaitTF0();
TR0=0;
}
void Sendata()
{
for(i=0;i<sizeof(info);i++)//ía2??-?·£?±éàúêy×é
{
WByte(info[i]);
}
}
void main()
{
UartInit();
while(1)
{
Sendata();
}
}