STC12C5A60S2单片机的串口扩展
扫描二维码
随时随地手机看文章
STC12C5A60S2单片机的串口从传统的一个扩展到了两个,
而且还增加了一个独立波特率发生器,把定时器1解放了出来,真的不是一般的方便,
还而且能用1T模式,速度大大滴提高了。。。
UART.C
#include <STC12C5A.H> //STC12C5A系列单片机
#include <intrins.h>
#include "UART.H"
#define uchar unsigned char
#define uint unsigned int
//缓存串口1和串口2接收到的字符
uchar UART1_Recv_Val = 0;
uchar UART2_Recv_Val = 0;
void UART1_Init(uchar RELOAD, bit doubleBaud, bit timeMod)
{
SCON |= 0x50; //串口1方式1,接收充许
BRT = RELOAD; //波特率2400
if (timeMod == 1) //1T
{
//T0x12 T1x12 UM0x6 BRTR S2SMOD BRTx12 EXTRAM S1BRS
AUXR |= 0x15; //串口1使用独立波特率发生器,独立波特率发生器1T
}
else //12T
{
AUXR |= 0x11;
}
if (doubleBaud == 1)
{
PCON |= 0x80; //波特率加倍
}
else
{
PCON &= 0x7F; //波特率不加倍
}
EA = 1;
ES = 1; //充许串口1中断
}
void UART2_Init(uchar RELOAD, bit doubleBaud, bit timeMod)
{
//S2SM0 S2SM1 S2SM2 S2REN S2TB8 S2RB8 S2TI S2RI
S2CON |= 0x50; //串口2,方式1,接收充许
BRT = RELOAD;
if (timeMod == 1) //1T
{
//T0x12 T1x12 UM0x6 BRTR S2SMOD BRTx12 EXTRAM S1BRS
AUXR |= 0x14; //串口1使用独立波特率发生器,独立波特率发生器1T
}
else //12T
{
AUXR = (AUXR | 0x10) & 0xFB;
}
if (doubleBaud == 1)
{
AUXR |= 0x08; //波特率加倍
}
else
{
AUXR &= 0xF7; //波特率不加倍
}
EA = 1;
//- - - - - - ESPI ES2
IE2 |= 0x01; //充许串口2中断
}
void UART1_SendOneChar(uchar val)
{
//ES = 0; //关闭串口1中断
SBUF = val;
while(TI == 0);
TI = 0;
//ES = 1; //恢复串口1中断
}
void UART2_SendOneChar(uchar val)
{
//IE2 = 0x00; //关闭串口2中断
S2BUF = val;
while ((S2CON & 0x02) == 0);
S2CON &= 0xFD;
//IE2 = 0x01; //恢复串口2中断
}
void UART1_SendStr(uchar *str)
{
while( (*str)!='/0' )
{
UART1_SendOneChar(*str);
str++;
}
}
void UART2_SendStr(uchar *str)
{
while( (*str)!='/0' )
{
UART2_SendOneChar(*str);
str++;
}
}
void UART1_Int(void) interrupt 4
{
if (RI == 1)
{
RI = 0;
UART1_Recv_Val = SBUF;
}
}
void UART2_Int(void) interrupt 8
{
if ((S2CON & 0x01) == 1)
{
S2CON &= 0xFE;
UART2_Recv_Val = S2BUF;
}
}
UART.H
#ifndef _UART_H_
#define _UART_H_
#define uchar unsigned char
#define uint unsigned int
//定义串口1口开关,关闭则不能接收数据
#define OpenUART1() ES=1
#define CloseUART1() ES=0
#define OpenUART2() IE2|=0x01
#define CloseUART2() IE2&=0xFE
//缓存串口1和串口2接收到的字符
extern uchar UART1_Recv_Val;
extern uchar UART2_Recv_Val;
void UART1_Init(uchar RELOAD, bit doubleBaud, bit timeMod);
void UART2_Init(uchar RELOAD, bit doubleBaud, bit timeMod);
void UART1_SendOneChar(uchar val);
void UART2_SendOneChar(uchar val);
void UART1_SendStr(uchar *str);
void UART2_SendStr(uchar *str);
#endif
main.c
#include <STC12C5A.H> //STC12C5A系列单片机
#include <intrins.h>
#include "UART.H"
#define uchar unsigned char
#define uint unsigned int
//独立波特率发生器初值,1T模式
//Fosc = 晶振频率, Baud0 = 标准波特率
//RELOAD = 256 - INT(Fosc/Baud0/32 + 0.5)
//Baud = Fosc/(256 - RELOAD)/32
//error = (Baud - Baud0)/Baud0 * 100%
uchar RELOAD = 0xD9; //Fosc = 12MHz, Baud0 = 9600
//波特率是否加倍,0不倍,1加倍
bit doubleBaud = 0;
//独立波特率发生器,0为12T模式,1为1T模式
bit timeMod = 1;
sbit LED1 = P1^0;
sbit LED2 = P1^1;
void main(void)
{
//串口标志位,0使用串口1,1使用串口2
bit UART_flag = 1;
LED1 = 1;
LED1 = 1;
//串口1和串口2初始化
UART1_Init(RELOAD, doubleBaud, timeMod);
UART2_Init(RELOAD, doubleBaud, timeMod);
//先用串口1接收字符
OpenUART1();
CloseUART2();
UART1_SendOneChar(0x0C); //超级终端清屏
UART1_SendStr("/r/n");
UART1_SendStr("/r/n");
UART1_SendStr("1.串口1通讯/r/n");
UART2_SendStr("2.串口2通讯/r/n");
while (UART1_Recv_Val == 0);
UART1_SendStr("/r/n"); //换行
if (UART1_Recv_Val == '1')
{
OpenUART1();
CloseUART2();
UART1_SendStr("Light LED(UART1):/r/n");
UART_flag = 0;
}
else
{
CloseUART1();
OpenUART2();
UART2_SendStr("Light LED(UART2):/r/n");
UART_flag = 1;
}
UART1_Recv_Val = 0; //缓存清零
UART2_Recv_Val = 0; //缓存清零
while (1)
{
if (UART_flag == 0) //串口1接收字符
{
if (UART1_Recv_Val != 0)
{
switch (UART1_Recv_Val)
{
case '1':
LED1 = ~LED1;
break;
case '2':
LED2 = ~LED2;
break;
default:
LED1 = 1;
LED1 = 1;
break;
}
UART1_Recv_Val = 0; //缓存清零
}
}
else //串口2接收字符
{
if (UART2_Recv_Val != 0)
{
switch (UART2_Recv_Val)
{
case '1':
LED1 = ~LED1;
break;
case '2':
LED2 = ~LED2;
break;
default:
LED1 = 1;
LED1 = 1;
break;
}
UART2_Recv_Val = 0; //缓存清零
}
}
}
}