📄 uart.#1
字号:
/****************************************************************************
**
** 文件名: UART.c
** 功能: C8051Fxxx串口驱动;
** 创建时间:2005.08.05
** 修改时间:2006.08.28
** 修改说明:
** 作者: 李立学
** 版权申明:可以拷贝,可以修改,但必须保留修改时间和作者信息
**
****************************************************************************/
#include "LZK.H"
/****************************************************************************
** 函数名称: UART0_Init()
** 功能描述: 串口0初始化.
** 入口参数: 无
** 出口参数: 无
** 说明: 设置工作模式和波特率等,禁止波特率加倍方式.
****************************************************************************/
void UART0_Init(void) // USE T4,初始化为19200bps
{
SFRPAGE = UART0_PAGE;
SCON0 = 0x70; // 0111_0000
// SCON0(00000000): UART0 Control Register.
// Bits7-6: SM00-SM10: Serial Port Operation Mode.
// 00 Mode 0: Synchronous Mode
// 01 Mode 1: 8-Bit UART, Variable Baud Rate
// 10 Mode 2: 9-Bit UART, Fixed Baud Rate
// 11 Mode 3: 9-Bit UART, Variable Baud Rate
// Bit5: SM20: Multiprocessor Communication Enable.
// Under Mode 0: No effect
// Mode 1: Checks for valid stop bit.
// Mode 2 and 3: Multiprocessor Communications Enable.
// Bit4: REN0: Receive Enable.
// Bit3: TB80: Ninth Transmission Bit.
// Bit2: RB80: Ninth Receive Bit.
// Bit1: TI0: Transmit Interrupt Flag.
// Bit0: RI0: Receive Interrupt Flag.
SSTA0 = 0x0f; // 0000_1111
// SSTA0(00000000): UART0 Status and Clock Selection Register.
// Bit7: FE0: Frame Error Flag.
// Bit6: RXOV0: Receive Overrun Flag.
// Bit5: TXCOL0: Transmit Collision Flag.
// Bit4: SMOD0: UART0 Baud Rate Doubler Enable.
// Bits3-2: UART0 Transmit Baud Rate Clock Selection Bits.
// 00 Timer 1 generates UART0 TX Baud Rate
// 01 Timer 2 Overflow generates UART0 TX baud rate
// 10 Timer 3 Overflow generates UART0 TX baud rate
// 11 Timer 4 Overflow generates UART0 TX baud rate
// Bits1-0: UART0 Receive Baud Rate Clock Selection Bits.
// 00 Timer 1 generates UART0 RX Baud Rate
// 01 Timer 2 Overflow generates UART0 RX baud rate
// 10 Timer 3 Overflow generates UART0 RX baud rate
// 11 Timer 4 Overflow generates UART0 RX baud rate
// SBUF0: UART0 Data Buffer Register.
// SADDR0: UART0 Slave Address Register.
// SADEN0: UART0 Slave Address Enable Register.
SFRPAGE = 0x00;
}
/****************************************************************************
** 函数名称: UART0_SetBaudRate()
** 功能描述: 串口0波特率设置.
** 入口参数: uiBaudRate,波特率,如9600等.
** 出口参数: 无
** 全局变量: 使用定时器T4.
** 调用模块: 无
****************************************************************************/
void UART0_SetBaudRate(uint16 uiBaudRate) // Using T4.
{
switch(uiBaudRate)
{
case 19200: SFRPAGE = TMR4_PAGE;
RCAP4L = 0xD9; // 0xFFB2.
RCAP4H = 0xFF;
SFRPAGE = 0x00;
break;
case 9600: SFRPAGE = TMR4_PAGE; // 0xFF64.
RCAP4L = 0xB2;
RCAP4H = 0xFF;
SFRPAGE = 0x00;
break;
case 4800: SFRPAGE = TMR4_PAGE; // 0xFEC8.
RCAP4L = 0x64;
RCAP4H = 0xFF;
SFRPAGE = 0x00;
break;
case 2400: SFRPAGE = TMR4_PAGE; // 0xFD8F.
RCAP4L = 0xC8;
RCAP4H = 0xFE;
SFRPAGE = 0x00;
break;
default: break;
}
}
/****************************************************************************
** 函数名称: UART1_Init()
** 功能描述: 串口1初始化,固定为19200bps
** 入口参数: 无
** 出口参数: 无
** 说明: 设置工作模式,定义波特率发生器等.1START_8BIT_1STOP_NOP.
****************************************************************************/
void UART1_Init(void) // USE T1,57600bps
{
SFRPAGE = UART1_PAGE;
SCON1 = 0x10; // 0011_0000
// SCON1(0000000): Serial Port 1 Control Register.
// Bit7: S1MODE: Serial Port 1 Operation Mode.
// 0/1: 8-bit / 9-bit UART with Variable Baud Rate.
// Bit6: UNUSED. Read = 1b. Write = don’t care.
// Bit5: MCE1: Multiprocessor Communication Enable.
// Bit4: REN1: Receive Enable.
// Bit3: TB81: Ninth Transmission Bit.
// Bit2: RB81: Ninth Receive Bit.
// Bit1: TI1: Transmit Interrupt Flag, MUST be cleared manually by software.
// Bit0: RI1: Receive Interrupt Flag, MUST be cleared manually by software.
// SBUF1: Serial (UART1) Port Data Buffer Register
SFRPAGE = 0x00;
}
/****************************************************************************
** 函数名称: UART0_SendByte()
** 功能描述: 串口0发送1个字节.
** 入口参数: ucDATA(要发送的数据)
** 出口参数: 无
** 说明:
****************************************************************************/
bit UART0_SendByte(uint8 ucDATA)
{
uint16 data TimOutCnt;
SFRPAGE = UART0_PAGE;
TimOutCnt = 0;
TI0 = 0;
SBUF0 = ucDATA;
while( TI0 != 1 )
{
TimOutCnt++;
if(TimOutCnt > 50000) //
return 1; // Time OUT!
}
SFRPAGE = 0x00;
return 0;
}
/****************************************************************************
** 函数名称: UART0_RecvByte()
** 功能描述: 串口0接收1个字节.
** 入口参数: 无
** 出口参数: 接收到的数据,否则返回0x8000.
** 说明:
****************************************************************************/
uint16 UART0_RecvByte(void)
{
uint16 data TimOutCnt;
uint8 data ucTMP;
SFRPAGE = UART0_PAGE;
TimOutCnt = 0;
RI0 = 0;
while( RI0 != 1 ) // Wait for
{
TimOutCnt++;
if(TimOutCnt > 50000)
return 0x8000; // Time OUT!
}
ucTMP = SBUF0; // Receive DATA
SFRPAGE = 0x00;
return ucTMP;
}
/****************************************************************************
** 函数名称: UART1_SendByte()
** 功能描述: 串口1发送1个字节.
** 入口参数: ucDATA(要发送的数据)
** 出口参数: 无
** 说明:
****************************************************************************/
bit UART1_SendByte(uint8 ucDATA)
{
uint16 data TimOutCnt;
SFRPAGE = UART1_PAGE;
TimOutCnt = 0;
TI1 = 0;
SBUF1 = ucDATA;
while( TI1 != 1)
{
TimOutCnt++;
if( TimOutCnt > 5000 )
return 1; // Time OUT!
}
SFRPAGE = 0x00;
return 0;
}
/****************************************************************************
** 函数名称: UART1_RecvByte()
** 功能描述: 串口1接收1个字节.
** 入口参数: 无
** 出口参数: 接收到的数据,否则返回0x8000.
** 说明:
****************************************************************************/
uint16 UART1_RecvByte(void)
{
uint8 data TimOutCnt;
uint8 data ucTMP;
SFRPAGE = UART1_PAGE;
TimOutCnt = 0;
RI1 = 0;
while( RI1 != 1 ) // Wait for
{
TimOutCnt++;
if( TimOutCnt > 5000 )
return 0x8000; // Time OUT!
}
ucTMP = SBUF1; // Receive DATA
RI1 = 0;
SFRPAGE = 0x00;
return ucTMP;
}
/************************************************************
********* UART的中断服务程序入口 ****************************
*************************************************************
void UART1_ISR(void) interrupt 20
{
}
void UART0_ISR(void) interrupt 4
{
}
***********************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -