打印

12c5a60s2第二串口问题

[复制链接]
2037|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
我用的程序是芯片开发说明书里的例程序,但跑程序的时候跑出了一堆乱码。求解救
#include "reg51.h"
#include "intrins.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
#define FOSC 18432000L //System frequency
#define BAUD 115200 //UART baudrate
/*Define UART parity mode*/
#define NONE_PARITY 0 //None parity
#define ODD_PARITY 1 //Odd parity
#define EVEN_PARITY 2 //Even parity
#define MARK_PARITY 3 //Mark parity
#define SPACE_PARITY 4 //Space parity
#define PARITYBIT EVEN_PARITY //Testing even parity
/*Declare SFR associated with the UART2 */
sfr AUXR = 0x8e; //Auxiliary register
sfr S2CON = 0x9a; //UART2 control register
sfr S2BUF = 0x9b; //UART2 data buffer
sfr BRT = 0x9c; //Baudrate generator
sfr IE2 = 0xaf; //Interrupt control 2
#define S2RI 0x01 //S2CON.0
#define S2TI 0x02 //S2CON.1
#define S2RB8 0x04 //S2CON.2
#define S2TB8 0x08 //S2CON.3
bit busy;
void SendData(BYTE dat);
void SendString(char *s);

void main()
{
#if (PARITYBIT == NONE_PARITY)
S2CON = 0x50; //8-bit variable UART
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
S2CON = 0xda; //9-bit variable UART, parity bit initial to 1
#elif (PARITYBIT == SPACE_PARITY)
S2CON = 0xd5; //9-bit variable UART, parity bit initial to 0
#endif
BRT = -(FOSC/32/BAUD); //Set auto-reload vaule of baudrate generator
AUXR = 0x14; //Baudrate generator work in 1T mode
IE2 = 0x01; //Enable UART2 interrupt
EA = 1; //Open master interrupt switch
SendString("STC12C5A60S2\r\nUart2 Test !\r\n");
while(1);
}
/*----------------------------
UART2 interrupt service routine
----------------------------*/
void Uart2() interrupt 8 using 1
{
if (S2CON & S2RI)
{
S2CON &= ~S2RI; //Clear receive interrupt flag
P0 = S2BUF; //P0 show UART data
P2 = (S2CON & S2RB8); //P2.2 show parity bit
}
if (S2CON & S2TI)
{
S2CON &= ~S2TI; //Clear transmit interrupt flag
busy = 0; //Clear transmit busy flag
}
}
void SendData(BYTE dat)
{
while (busy); //Wait for the completion of the previous data is sent
ACC = dat; //Calculate the even parity bit P (PSW.0)
if (P) //Set the parity bit according to P
{
#if (PARITYBIT == ODD_PARITY)
S2CON &= ~S2TB8; //Set parity bit to 0
#elif (PARITYBIT == EVEN_PARITY)
S2CON |= S2TB8; //Set parity bit to 1
#endif
}
else
{
#if (PARITYBIT == ODD_PARITY)
S2CON |= S2TB8; //Set parity bit to 1
#elif (PARITYBIT == EVEN_PARITY)
S2CON &= ~S2TB8; //Set parity bit to 0
#endif
}
busy = 1;
S2BUF = ACC; //Send data to UART2 buffer
}
void SendString(char *s)
{
while (*s) //Check the end of the string
{
SendData(*s++); //Send current char and increment string ptr
}
}

程序入上,测试结果如下,调了好多次都没成功

求大神搭救

相关帖子

沙发
abtc1130| | 2013-11-4 10:19 | 只看该作者
选中16进制显示

使用特权

评论回复
板凳
puppet21|  楼主 | 2013-11-4 10:23 | 只看该作者
abtc1130 发表于 2013-11-4 10:19
选中16进制显示

16进制显示的是 5A 6B这些东西,我需要得到的是字母啊,我发送的本来就是字母啊?为什么会这样?

使用特权

评论回复
地板
puppet21|  楼主 | 2013-11-4 10:32 | 只看该作者
abtc1130 发表于 2013-11-4 10:19
选中16进制显示

这个是芯片手册里面的程序,我改为自己的也不行,波特率这些都调了,都不行。

我把输出改为ABC后,直接把我的USB转TTL烧掉了,求助啊~

使用特权

评论回复
5
coody| | 2013-11-4 11:57 | 只看该作者
字符发送可以把硬件烧掉?求方法。。。。

使用特权

评论回复
6
puppet21|  楼主 | 2013-11-4 12:04 | 只看该作者
coody 发表于 2013-11-4 11:57
字符发送可以把硬件烧掉?求方法。。。。

估计是我没写延时程序,瞬间大量数据往232灌,结果就发臭了(我在顺风位,还好同事发现了)。现在还能识别设备,但不敢挂单片机上去烧写了。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

16

主题

140

帖子

0

粉丝