/*------------------------------------------------------------------------ GPS串口2接收中断服务程序(接收GPS数据) 菜农HotPower之PIC24菜鸟毕业留念 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数据 if (ch == '$')//是GPS引导字符 { cData = ch;//记录引导字符 cPosition = GPSRxWriteCount;//引导字符位置记录 GPSRxBuffers[GPSRxWriteCount ++] = ch;//存入数据 } else if (cData == '$')//收到引导字符 { if (ch >= ' ')//可能是有效数据 { GPSRxBuffers[GPSRxWriteCount ++] = ch;//存入数据 } else if (ch == '*')//GPS校验引导字符 { GPSRxBuffers[GPSRxWriteCount ++] = ch;//存入数据 } else if ((ch == '\r') && (GPSRxBuffers[GPSRxWriteCount - 3] == '*'))//回车字符校验 { GPSRxBuffers[GPSRxWriteCount ++] = ch;//存入数据 } else { if ((ch == '\n') && (GPSRxBuffers[GPSRxWriteCount - 1] == '\r'))//换行字符校验 {//只取GPRMC语句 if (GPSGPRMCTest((char *)&GPSRxBuffers[cPosition + 1])) { GPSRxBuffers[GPSRxWriteCount ++] = ch;//存入数据 GPSbits.RXFlag = 1;//激活GPS接收成功解码程序(在“零耗时键盘OS”的空闲任务内细加工处理) } } 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中断优先级位1111) -------------------------------------------------------------------------*/ IPC7bits.U2RXIP0 = 1; IPC7bits.U2RXIP1 = 1; IPC7bits.U2RXIP2 = 1; IEC1bits.U2RXIE = 1; IFS1bits.U2RXIF = 0;//清除RX1中断标志 IEC1bits.U2TXIE = 1; IFS1bits.U2TXIF = 0;//清除TX1中断标志 }
|