这是基于STC系列的串口驱动,代码如下:
//以下是DV_Timer.H头文件部分
#ifndef __DV_UART__
#define __DV_UART__
/*******************************************************************************************/
//接口函数:
void DV_Uart_Init(void);
void DV_Uart_Service(void);
#endif
//以下是DV_Timer.c正文文件部分
#include "stc15w.h"
#include "DV_Uart.h"
#define S1_S1 0x40 //P_SW1.6
#define S1_S0 0x80 //P_SW1.7
unsigned short uartidex = 256;//cmd字符计数器、cmd有效与否检测
unsigned short uartmax = 120;
extern xdata unsigned char buf[256];//每次1个字符作为控制命令
/*******************************************************************************************/
void DV_Uart_Init()
{
ACC = P_SW1;
ACC &= ~(S1_S0 | S1_S1); //S1_S0=0 S1_S1=0
P_SW1 = ACC; //(P3.0/RxD, P3.1/TxD)
//434190bps@33.000MHz
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x09; //串口1选择定时器2为波特率发生器
AUXR |= 0x08; //定时器2时钟为Fosc,即1T
T2L = 0xEA; //设定定时初值
T2H = 0xF0; //设定定时初值
AUXR |= 0x06; //启动定时器2
ES = 1; //使能串口1中断
EA = 1;
}
/*******************************************************************************************/
void Uart() interrupt 4 using 1
{
if(RI)
{
RI = 0; //清除RI位
if(uartidex < uartmax)
buf[uartidex++] = SBUF;
else
SBUF = 0x33;
}
}
/*******************************************************************************************/
void DV_Uart_Service()
{
// if(uartidex >= uartmax){
// uartidex = 1;
// while(uartidex < uartmax){
// SBUF = buf[uartidex++];
// TI = 0;
// while(!TI);
// }
// uartidex = 256;
// }
}