51單片機串口接收和發送資料

來源:互聯網
上載者:User

標籤:

第一種方法:在中斷中處理

typedef unsigned char         UINT8;volatile  UINT8 u8Uart_Data;void InitialUART0_Timer1(){        SCON = 0x50;        //2015-05-04    TMOD = 0x20;        TH1 = 0XFD;                              // 9600BPS     2015-05-04     wangrong    TL1 = 0XFD;    TR1 = 1;                               // start timer0    ES = 1;                             // Enable serial interrupt    EA = 1;                             // Enable global interrupt}//-----------------------------------------------------------------------------------------------------------void main (void){    //--------------------------------------------------------------------------------    InitialUART0_Timer1();                   // 9600 Baud Rate @ 11.0592MHz                        while(1)    {         //    Receive_Data_From_PC();        //Send_Data_To_PC(Receive_Data_From_PC());        while(recv_data)        {            recv_data = 0;            TI = 0;            SBUF = u8Uart_Data;        }                }}//-----------------------------------------------------------------------------------------------------------void UART_ISR(void) interrupt 4{    if (RI)    {                                   // If reception occur        RI = 0;                         // Clear reception flag for next reception        u8Uart_Data = 0;        u8Uart_Data = SBUF;             // Read receive data        recv_data = 1;        //SBUF = u8Uart_Data;           // Send back same data on UART       }    if (TI)                                // If emission occur    {                                   // Clear emission flag for next emission        TI = 0;    }}//-----------------------------------------------------------------------------------------------------------

第二種方法:不在中斷中處理

//-----------------------------------------------------------------------------------------------------------// Use timer1 as baud rate generatorvoid InitialUART0_Timer1(){        SCON = 0x50;        //2015-05-04    TMOD = 0x20;#if 0#ifdef FOSC_110592    TH1 = 256 - (28800/u32Baudrate); /* 11.059M/384=28800 */#endif#ifdef FOSC_184320    TH1 = 256 - (48000/u32Baudrate); /* 18.4320M/384=48000 */#endif#ifdef FOSC_221184     TH1 = 256 - (57600/u32Baudrate); /* 22.1184M/384=57600 */#endif#ifdef FOSC_331776     TH1 = 256 - (86400/u32Baudrate); /* 33.1776M/384=86400 */#endif#ifdef FOSC_368640     TH1 = 256 - (96000/u32Baudrate); /* 36.8640M/384=96000 */#endif#endif /*    #if 0    */        TH1 = 0XFD;                              // 9600BPS     2015-05-04     wangrong    TL1 = 0XFD;    TR1 = 1;                               // start timer0    ES = 1;                             // Enable serial interrupt    EA = 1;                             // Enable global interrupt}//-----------------------------------------------------------------------------------------------------------UINT8 Receive_Data_From_PC(void){    UINT8 c;    while (!RI);    c = SBUF;    RI = 0;    return (c);}//-----------------------------------------------------------------------------------------------------------void Send_Data_To_PC (UINT8 c){    while (!TI);    TI = 0;    SBUF = c;}//-----------------------------------------------------------------------------------------------------------//-----------------------------------------------------------------------------------------------------------void main (void){    //--------------------------------------------------------------------------------        InitialUART0_Timer1();                   // 9600 Baud Rate @ 11.0592MHz                        //---------------------------------------------------------------------------------            while(1)    {                 Receive_Data_From_PC();        Send_Data_To_PC(Receive_Data_From_PC());    }}

 

51單片機串口接收和發送資料

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.