📄 serial.c
字号:
/*-----------------------------------------------------------------------
Project : Single RTK
Author : kreal@163.net
Date : 04-10-10
Funtion : 串口驱动
------------------------------------------------------------------------*/
#define SERIAL_G
#include "sysincludes.h"
/*---------------------------------------------------------------------------
Function : Uart ISR
Input : None
Output : None
-----------------------------------------------------------------------------*/
void __irq ISR_Uart0 (void)
{
uchar iN, cnt ;
if (rSUBSRCPND & BIT_SUB_TXD0) {
rSUBSRCPND=(BIT_SUB_TXD0); // Clear Sub int pending.
}
else if( rSUBSRCPND & BIT_SUB_RXD0 )
{
cnt = rUFSTAT0 & 0x000F ;
for( iN = 0 ; iN < cnt ; iN ++ )
{
Com0.buf[Com0.pIn++] = rURXH0 ;
if( Com0.pIn>=UART_RX_MAX_SIZE )
Com0.pIn = 0 ;
}
rSUBSRCPND = BIT_SUB_RXD0 ;
}
ClearPending(BIT_UART0); // Clear master pending.
}
void __irq ISR_Uart1 (void)
{
uchar iN, cnt ;
uchar cbyte;
if (rSUBSRCPND & BIT_SUB_TXD1)
{
rSUBSRCPND=(BIT_SUB_TXD1); // Clear Sub int pending.
}
else if( rSUBSRCPND & BIT_SUB_RXD1 )
{
cbyte = rURXH1;
Receive_Mess(cbyte);
UartSendByte(0, cbyte);
rSUBSRCPND = BIT_SUB_RXD1 ;
}
ClearPending(BIT_UART1); // Clear master pending
}
void __irq ISR_Uart2 (void)
{
uchar iN, cnt ;
if (rSUBSRCPND & BIT_SUB_TXD2)
{
rSUBSRCPND=(BIT_SUB_TXD2); // Clear Sub int pending.
}
else if( rSUBSRCPND & BIT_SUB_RXD2 )
{
cnt = rUFSTAT2 & 0x000F ;
for( iN = 0 ; iN < cnt ; iN ++ )
{
Com2.buf[Com2.pIn++] = rURXH2 ;
if( Com2.pIn >= UART_RX_MAX_SIZE )
Com2.pIn = 0 ;
}
rSUBSRCPND = BIT_SUB_RXD2 ;
}
ClearPending(BIT_UART2); // Clear master pending.
}
/*------------------------------------------------------------------------
Function : 串口初始化
Input : ComNum -- 串口号
baud -- 串口波特率
Output : COM_NUM_ERR 不存在的串口
COM_BAUD_ERR 非法波特率
-------------------------------------------------------------------------*/
char ComInit( uchar ComNum, uint baud )
{
if( ComNum>2 ) // Para Limit
return COM_NUM_ERR ; // 不存在的串口
else if( baud > 115200 )
return COM_BAUD_ERR ; // 非法的波物率
if(ComNum==0 )
{
rULCON0 = ((0<<6)+(0<<3)+(0<<2)+(3<<0)); //stop bit and parity bit
rUCON0 = ((0<<10)+(0<<9)+(0<<8)+(0<<7)+(0<<6)+(0<<5)+(0<<4)+(1<<2)+(1<<0)); // interrupt mode or poll mode
rUFCON0 = ((0<<6)+( 2<<4)+(0<<2)+(0<<1)+(0<<0)); // fifo
rUMCON0 = ((0<<4)+(0<<0)); //disable the modem mode
rUBRDIV0 = ((int)(PCLK/(baud*16))-1); // baud
Com0.pIn = 0 ;
Com0.pOut= 0 ;
pISR_UART0 =(uint) ISR_Uart0 ;
rINTMOD &= ~(BIT_UART0); // IRQ mode.
rINTMSK &= ~(BIT_UART0); // 开 UART0 发送中断屏蔽位.
//rINTSUBMSK &= ~(BIT_SUB_TXD0);
rINTSUBMSK &= ~(BIT_SUB_RXD0) ;
}
else if( ComNum==1 )
{
rULCON1 = ((0<<6)+(0<<3)+(0<<2)+(3<<0)); //stop bit and parity bit
rUCON1 = ((0<<10)+(0<<9)+(1<<8)+(0<<7)+(0<<6)+(0<<5)+(0<<4)+(1<<2)+(1<<0)); // interrupt mode or poll mode
rUFCON1 = ((0<<6)+( 2<<4)+(0<<2)+(0<<1)+(0<<0)); // fifo
rUMCON1 = ((0<<4)+(0<<0)); //disable the modem mode
rUBRDIV1 = ((int)(PCLK/(baud*16))-1); // baud
Uart1.pIn = 0 ;
Uart1.pOut= 0 ;
pISR_UART1 =(uint) ISR_Uart1 ;
rINTMOD &= ~(BIT_UART1); // IRQ mode.
rINTMSK &= ~(BIT_UART1); // 开 UART0 发送中断屏蔽位.
//rINTSUBMSK &= ~(BIT_SUB_TXD1);
rINTSUBMSK &= ~(BIT_SUB_RXD1) ;
}
else if( ComNum == 2 )
{
rULCON2 = ((0<<6)+(0<<3)+(0<<2)+(3<<0)); //stop bit and parity bit
rUCON2 = ((0<<10)+(0<<9)+(0<<8)+(0<<7)+(0<<6)+(0<<5)+(0<<4)+(1<<2)+(1<<0)); // interrupt mode or poll mode
rUFCON2 = ((0<<6)+( 2<<4)+(0<<2)+(0<<1)+(1<<0)); // fifo
rUMCON2 = ((0<<4)+(0<<0)); //disable the modem mode
rUBRDIV2 = ((int)(PCLK/(baud*16))-1); // baud
Com2.pIn = 0 ;
Com2.pOut= 0 ;
pISR_UART2 =(uint) ISR_Uart2 ;
rINTMOD &= ~(BIT_UART2); // IRQ mode.
rINTMSK &= ~(BIT_UART2); // 开 UART0 发送中断屏蔽位.
// rINTSUBMSK &= ~(BIT_SUB_TXD2);
rINTSUBMSK &= ~(BIT_SUB_RXD2) ;
}
return COM_INIT_OK ;
}
//=====================================================================
void UartSendByte(uchar comnum, uchar data)
{
if( comnum ==0)
{
while(!(rUTRSTAT0 & 0x2));
WrUTXH0(data);
}
else if(comnum==1)
{
while(!(rUTRSTAT1 & 0x2)); //Wait until THR is empty.
rUTXH1 = data;
}
else if(comnum==2)
{
while(!(rUTRSTAT2 & 0x2)); //Wait until THR is empty.
rUTXH2 = data;
}
}
//====================================================================
void UartSendString(uchar comnum,uchar *pt)
{
while(*pt)
UartSendByte(comnum,*pt++);
}
void UartSendBuf( uchar comnum,uchar *buf,ushort len )
{
ushort iN ;
for(iN=0;iN<len;iN++) UartSendByte(comnum,buf[iN]);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -