微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 自己写的UART通信,遇到一个问题,请进

自己写的UART通信,遇到一个问题,请进

时间:10-02 整理:3721RD 点击:
自己写的UART通信,RTX接收一个字节,之后从P0.0发送。用开发板+串口调试助手实现,为什么16进制发20,收到的是16进制60?
#include<reg52.h>
unsigned char shuju;
sbit k=P0^0;
void UART_init()
{
    SCON=0x50;
    TMOD=0x20; //波特率9600
    TH1=0xFD; //波特率9600
    TL1=0xFD;  //波特率9600
    ES=1;
    EA=1;
    TR1=1;
    k=1;
}

void send_bit(unsigned char i)
{
    unsigned char j;
    k=0;
    for(j=0;j<8;j++)
    {
        k=i&0x01;
        i=i>>1;   
    }
    k=1;
}
main()
{
    UART_init();
    while(1)
    {
        if(RI)
        {  
            send_bit(shuju);
            RI=0;            
            REN=1;
        }   
    }
}
void jieshou() interrupt 4
{
    if(RI)
    {
        shuju=SBUF;
        REN=0;
    }
}

在main中使用if(RI)应该会出现问题,因为中断函数中也有RI的判断,RI应该在中断中置零,不然二者应该要冲突的吧,建议将send_bit使用中断中的变量进行触发
以下参考官方ISP软件中的例子:
#include "reg51.h"
#include "intrins.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
#define FOSC 11059200L      //System frequency
#define BAUD 9600           //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
sfr AUXR1 = 0xA2;
#define UART_P1 0x80        //(AUXR1.7) switch RXD/TXD from P3.0/P3.1 to P1.6/P1.7
sbit bit9 = P2^2;           //P2.2 show UART data bit9
bit busy;
void SendData(BYTE dat);
void SendString(char *s);
void main()
{
    AUXR1 |= UART_P1;       //switch RXD/TXD from P3.0/P3.1 to P1.6/P1.7
#if (PARITYBIT == NONE_PARITY)
    SCON = 0x50;            //8-bit variable UART
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
    SCON = 0xda;            //9-bit variable UART, parity bit initial to 1
#elif (PARITYBIT == SPACE_PARITY)
    SCON = 0xd2;            //9-bit variable UART, parity bit initial to 0
#endif
    TMOD = 0x20;            //Set Timer1 as 8-bit auto reload mode
    TH1 = TL1 = -(FOSC/12/32/BAUD); //Set auto-reload vaule
    TR1 = 1;                //Timer1 start run
    ES = 1;                 //Enable UART interrupt
    EA = 1;                 //Open master interrupt switch
    SendString("STC10/11xx\r\nUart Test !\r\n");
    while(1);
}
/*----------------------------
UART interrupt service routine
----------------------------*/
void Uart_Isr() interrupt 4 using 1
{
    if (RI)
    {
        RI = 0;             //Clear receive interrupt flag
        P0 = SBUF;          //P0 show UART data
        bit9 = RB8;         //P2.2 show parity bit
    }
    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
    ACC = dat;              //Calculate the even parity bit P (PSW.0)
    if (P)                  //Set the parity bit according to P
    {
#if (PARITYBIT == ODD_PARITY)
        TB8 = 0;            //Set parity bit to 0
#elif (PARITYBIT == EVEN_PARITY)
        TB8 = 1;            //Set parity bit to 1
#endif
    }
    else
    {
#if (PARITYBIT == ODD_PARITY)
        TB8 = 1;            //Set parity bit to 1
#elif (PARITYBIT == EVEN_PARITY)
        TB8 = 0;            //Set parity bit to 0
#endif
    }
    busy = 1;
    SBUF = ACC;             //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
    }
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top