STC12C5A60S2單片機 串口調試

來源:互聯網
上載者:User

標籤:

與串口1中斷相關的寄存器有IE、IPH和IP

串口中斷允許位ES位於中斷允許寄存器IE中。

EA:CPU的總中斷允許控制位,EA=1,CPU開總中斷;EA=0,CPU關總中斷。各中斷源首先受EA控制,其次還受各中斷源自己的中斷允許控制位控制。

與串口2中斷相關的寄存器IE2、IP2H和IP2

串口2中斷允許為ES2位於中斷寄存器IE2中。

 1 #ifndef _UART_H_   2 #define _UART_H_  3   4 #define uchar   unsigned char   5 #define uint    unsigned int   6    7 //定義串口1口開關,關閉則不能接收資料   8 #define OpenUART1()     ES=1   9 #define CloseUART1()    ES=0  10 #define OpenUART2()     IE2|=0x01  11 #define CloseUART2()    IE2&=0xFE  12   13 //緩衝串口1和串口2接收到的字元  14 extern uchar UART1_Recv_Val;  15 extern uchar UART2_Recv_Val;  16    17 void UART1_Init(uchar RELOAD, bit doubleBaud, bit timeMod);   18 void UART2_Init(uchar RELOAD, bit doubleBaud, bit timeMod);  19 void UART1_SendOneChar(uchar val);    20 void UART2_SendOneChar(uchar val);   21 void UART1_SendStr(uchar *str);   22 void UART2_SendStr(uchar *str);  23 #endif

uart.c代碼如下:

  1 #include <STC12C5A.H>     //STC12C5A系列單片機    2 #include <intrins.h>    3 #include "UART.H"    4    5 #define uchar   unsigned char    6 #define uint    unsigned int    7     8 //緩衝串口1和串口2接收到的字元    9 uchar UART1_Recv_Val = 0;   10 uchar UART2_Recv_Val = 0;   11    12    13    14 void UART1_Init(uchar RELOAD, bit doubleBaud, bit timeMod)   15 {   16     SCON |= 0x50;       //串口1方式1,接收充許   17    18     BRT = RELOAD;       //傳輸速率2400   19    20     if (timeMod == 1)       //1T   21     {   22         //T0x12   T1x12   UM0x6   BRTR    S2SMOD  BRTx12  EXTRAM  S1BRS   23         AUXR |= 0x15;       //串口1使用獨立傳輸速率發生器,獨立傳輸速率發生器1T   24     }   25     else                    //12T   26     {   27         AUXR |= 0x11;   28     }   29    30     if (doubleBaud == 1)   31     {   32         PCON |= 0x80;     //傳輸速率加倍   33     }   34     else   35     {   36         PCON &= 0x7F;     //傳輸速率不加倍   37     }   38    39     EA = 1;   40     ES = 1;             //充許串口1中斷   41 }   42    43    44    45 void UART2_Init(uchar RELOAD, bit doubleBaud, bit timeMod)   46 {   47     //S2SM0  S2SM1   S2SM2   S2REN   S2TB8   S2RB8   S2TI     S2RI   48     S2CON |= 0x50;      //串口2,方式1,接收充許   49    50     BRT = RELOAD;   51    52     if (timeMod == 1)       //1T   53     {   54         //T0x12   T1x12   UM0x6   BRTR    S2SMOD  BRTx12  EXTRAM  S1BRS   55         AUXR |= 0x14;       //串口1使用獨立傳輸速率發生器,獨立傳輸速率發生器1T   56     }   57     else                    //12T   58     {   59         AUXR = (AUXR | 0x10) & 0xFB;   60     }   61    62     if (doubleBaud == 1)   63     {   64         AUXR |= 0x08;       //傳輸速率加倍   65     }   66     else   67     {   68         AUXR &= 0xF7;       //傳輸速率不加倍   69     }   70    71     EA = 1;    72     //-       -       -       -       -       -       ESPI    ES2   73     IE2 |= 0x01;            //充許串口2中斷              74 }   75    76    77    78 void UART1_SendOneChar(uchar val)   79 {   80     //ES = 0;                   //關閉串口1中斷   81    82     SBUF = val;   83     while(TI == 0);   84     TI = 0;   85    86     //ES = 1;                  //恢複串口1中斷   87 }                             88    89    90    91 void UART2_SendOneChar(uchar val)   92 {   93     //IE2 = 0x00;                 //關閉串口2中斷   94    95     S2BUF = val;       96     while ((S2CON & 0x02) == 0);   97     S2CON &= 0xFD;   98    99     //IE2 = 0x01;                //恢複串口2中斷  100 }  101   102   103   104 void UART1_SendStr(uchar *str)  105 {  106     while( (*str)!=‘/0‘ )  107     {  108         UART1_SendOneChar(*str);  109         str++;  110     }  111 }  112   113   114   115 void UART2_SendStr(uchar *str)  116 {  117     while( (*str)!=‘/0‘ )  118     {  119         UART2_SendOneChar(*str);  120         str++;  121     }  122 }  123   124   125   126 void UART1_Int(void) interrupt 4  127 {  128     if (RI == 1)  129     {  130         RI = 0;  131         UART1_Recv_Val = SBUF;  132     }     133 }  134   135   136   137 void UART2_Int(void) interrupt 8  138 {  139     if ((S2CON & 0x01) == 1)  140     {  141         S2CON &= 0xFE;  142         UART2_Recv_Val = S2BUF;  143     }     144 }  

 

STC12C5A60S2單片機 串口調試

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.