• 已解决 73482 个问题
  • 已帮助 5993 位优秀工程师

请问单片机串口的初始化都有哪些必需的步骤呢?

mingming 2020-12-09 浏览量:2687
比如说我要使用单片机的串口1传输和接收数据,以下的串口初始化正确吗?

void init()  //系统初始化
{
TMOD |=0X20;//定时器T1,方式2,波特率由PCON寄存器的SMOD决定
SCON=0x50; //REN RI TI,RI为0,TI为0
//串行口1方式1 SCON是串行口1的串行控制寄存器,REN为1,允许接收
PCON=0x00;//各工作方式波特率加倍
TH1=0xfD;//9600bps@11.0592
TL1=0xfD;
TR1=1; //定时器1中断打开
EA=1;//cpu总中断允许位,1为开放中断
ES=1;// 1允许串行口中断
}


请问以上有没有缺失什么步骤?
0 0 收起

我来回答

上传资料:
选择文件 文件大小不超过15M(格式支持:doc、ppt、xls、pdf、zip、rar、txt)
最佳答案
  • 有太多型号的单片机,可能具体不同的型号单片机串口初始化具体过程有差异的,因为你没有具体说是那款,所以只能泛泛的说。

    1. 需要有定时器初始化等等,这和波特率的控制有关,此外需要有明确的波特率设置

    2. 需要配置到相应的IO口,因为串口是有收、发口的,有些单片机是成组的

    3. 需要有对应的中断处理初始化过程

    4. 最后是使能定时器、中断等

    • 发布于 2020-12-09
    • 举报
    • 评论 0
    • 0
    • 0

其他答案 数量:5
  • 设置的对的没有问题,如果不放心可以用stc的isp软件生成串口初始化代码,这样比较简单
    • 发布于2020-12-09
    • 举报
    • 评论 0
    • 0
    • 0



  • 串口的范例,可以参考下

    #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 NONE_PARITY   //Testing even parity

    sbit bit9 = P2^2;           //P2.2 show UART data bit9
    bit busy;

    void SendData(BYTE dat);
    void SendString(char *s);
    void main()
    {
    #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("STC12C5A60S2\r\nUart Test !\r\n");
        while(1);
    }

    /@@*----------------------------
    UART interrupt service routine
    ----------------------------*/
    void Uart_Isr() interrupt 4
    {
        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
        }
    }

    • 发布于2020-12-09
    • 举报
    • 评论 0
    • 0
    • 0

  • 没问题。串口范例程序,可以参考下

    #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 NONE_PARITY   //Testing even parity

    sbit bit9 = P2^2;           //P2.2 show UART data bit9
    bit busy;

    void SendData(BYTE dat);
    void SendString(char *s);

    void main()
    {
    #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("STC12C5A60S2\r\nUart Test !\r\n");
        while(1);
    }


    /@@*----------------------------
    UART interrupt service routine
    ----------------------------*/
    void Uart_Isr() interrupt 4
    {
        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
        }
    }

    • 发布于2020-12-09
    • 举报
    • 评论 0
    • 0
    • 0

  • 要看你是什么单片机了

    51的只是配置个IO就行

    STM32的还要配置时钟,或是中断,IO复用

    • 发布于2020-12-10
    • 举报
    • 评论 0
    • 0
    • 0

  • 设分辨率,数据长度,停止位,起始位,校验,是否启用中断等,将这些信息写入串口设置寄存器
    • 发布于2020-12-17
    • 举报
    • 评论 0
    • 0
    • 0

相关问题

问题达人换一批

请问单片机串口的初始化都有哪些必需的步骤呢?