码迷,mamicode.com
首页 > 其他好文 > 详细

STC12C5A60S2单片机 串口调试

时间:2015-07-16 16:04:41      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

与串口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单片机 串口调试

标签:

原文地址:http://www.cnblogs.com/killer-xc/p/4651213.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!