?? 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.
} //判斷是否為發(fā)送中斷,清零標志位
else if( rSUBSRCPND & BIT_SUB_RXD0 ) //判斷是否為接收中斷,并接受數(shù)據
{
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); // 判斷是否結束,結束就添\0,否則存儲
UartSendByte(0, cbyte); //收到什么發(fā)送什么
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 發(fā)送中斷屏蔽位.
//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 發(fā)送中斷屏蔽位.
//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 發(fā)送中斷屏蔽位.
// 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;
}
}
//******************************************=====不帶長度的字符串發(fā)送
void UartSendString(uchar comnum,uchar *pt)
{
while(*pt)
UartSendByte(comnum,*pt++);
}
//************************************************帶長度的字符串發(fā)送
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 + -