/*------------------------------------------------------------------------
GPS串口2接收中斷服務程序(接收GPS數(shù)據(jù))
菜農(nóng)HotPower之PIC24菜鳥畢業(yè)留念 2007.12.20 2:40 于菜地留念
-------------------------------------------------------------------------*/
void __attribute__((interrupt, no_auto_psv)) _U2RXInterrupt( void )
{
static unsigned char cData = 0, cPosition;
unsigned char ch;
if (IFS1bits.U2RXIF)//確實是GPS串口2接收中斷
{
if (IEC1bits.U2RXIE)//GPS串口2接收中斷已被允許
{
ch = U2RXREG;//讀取串口GPS數(shù)據(jù)
if (ch == '$')//是GPS引導字符
{
cData = ch;//記錄引導字符
cPosition = GPSRxWriteCount;//引導字符位置記錄
GPSRxBuffers[GPSRxWriteCount ++] = ch;//存入數(shù)據(jù)
}
else if (cData == '$')//收到引導字符
{
if (ch >= ' ')//可能是有效數(shù)據(jù)
{
GPSRxBuffers[GPSRxWriteCount ++] = ch;//存入數(shù)據(jù)
}
else if (ch == '*')//GPS校驗引導字符
{
GPSRxBuffers[GPSRxWriteCount ++] = ch;//存入數(shù)據(jù)
}
else if ((ch == '\r') && (GPSRxBuffers[GPSRxWriteCount - 3] == '*'))//回車字符校驗
{
GPSRxBuffers[GPSRxWriteCount ++] = ch;//存入數(shù)據(jù)
}
else
{
if ((ch == '\n') && (GPSRxBuffers[GPSRxWriteCount - 1] == '\r'))//換行字符校驗
{//只取GPRMC語句
if (GPSGPRMCTest((char *)&GPSRxBuffers[cPosition + 1]))
{
GPSRxBuffers[GPSRxWriteCount ++] = ch;//存入數(shù)據(jù)
GPSbits.RXFlag = 1;//激活GPS接收成功解碼程序(在“零耗時鍵盤OS”的空閑任務內(nèi)細加工處理)
}
}
cData = 0;//結束或出錯
}
}
else
{
cData = 0;//出錯
}
}
}
IFS1bits.U2RXIF = 0;//清除RX2中斷標志
}
/*------------------------------------------------------------------------
GPS串口2初始化程序
-------------------------------------------------------------------------*/
void Uart2Init(void)
{
TRIS_TX2 = PORTOUTMODE;//設置TX2為輸出IO
TRIS_RX2 = PORTINPUTMODE;//設置RX2為輸入IO
U2MODE = 0;
// U2MODEbits.STSEL = 0;// 1-stop bit
// U2MODEbits.PDSEL = 0;// No Parity, 8-data bits
// U2MODEbits.ABAUD = 0;// Autobaud Disabled
U2STA = 0;
// U2STAbits.UTXISEL0 = 0; // Interrupt after one Tx character is transmitted
// U2STAbits.UTXISEL1 = 0;
// U2STAbits.URXISEL = 3; // Interrupt after one RX character is received
U2BRG = BRGVAL2;//BAUD Rate Setting for 19200
U2MODEbits.WAKE = 1;//啟動位時喚醒使能位
U2MODEbits.UARTEN = 1; // Enable UART
U2STAbits.UTXEN = 1; // Enable UART Tx
/*------------------------------------------------------------------------
定義GPS串口2中斷優(yōu)先級位1111)
-------------------------------------------------------------------------*/
IPC7bits.U2RXIP0 = 1;
IPC7bits.U2RXIP1 = 1;
IPC7bits.U2RXIP2 = 1;
IEC1bits.U2RXIE = 1;
IFS1bits.U2RXIF = 0;//清除RX1中斷標志
IEC1bits.U2TXIE = 1;
IFS1bits.U2TXIF = 0;//清除TX1中斷標志
}
|
|