/** * static int Gps_set(void * _self, va_list *app) - Perform a new gps acquisition. * \param *_self - pointer to the device * \param *app - none * \return: <LI><Breturn = 0:</B>when the GPS device is connected on a Grove nest DIG port, with or without a valid connection </LI> <LI><Breturn = -1:</B>when the GPS device is not connected on a Grove nest DIG port. </LI> </UL> */ static int Gps_set(void *_self,va_list *app) { struct Gps *self = _self; char gps_sentences[70]; unsigned int timer = 65000; flag = 1; nmea_zero_INFO(&self->info); nmea_parser_init(&self->parser); UARTOn(self->uart_module); UARTFlush(self->uart_module); vTaskDelay(50); while((UARTBufferSize(self->uart_module)<3) && timer) timer--; if(!timer) return -1; timer = 500; while((gps_sentences[0] != '$') && timer) { timer--; while(UARTBufferSize(self->uart_module)<1); UARTRead(self->uart_module,gps_sentences,1); if(gps_sentences[0] == '$') { while(UARTBufferSize(self->uart_module) < 5); UARTRead(self->uart_module,(gps_sentences+1),5); //Reads all the chars in the if((gps_sentences[4] == 'M')) { while(UARTBufferSize(self->uart_module) < 65); UARTRead(self->uart_module,(gps_sentences+6),64); //Reads all the chars in the gps_sentences[70] = '\0'; int i = 0; i = nmea_parse(&self->parser, gps_sentences, (int)strlen(gps_sentences), &self->info); if(i) { flag = 0; self->lat = nmea_ndeg2degree (self->info.lat); self->lon = -nmea_ndeg2degree (self->info.lon); self->year = self->info.utc.year+1900; self->mon = self->info.utc.mon; self->day = self->info.utc.day; self->hour = self->info.utc.hour; self->min = self->info.utc.min; self->sec = self->info.utc.sec; self->speed = self->info.speed; break; } } } gps_sentences[0]= 0; } nmea_parser_destroy(&self->parser); UARTOff(self->uart_module); return 0; }
void UARTInit() { TXSTAbits.SYNC=0; //asynchronous RCSTAbits.SPEN=1; //enable serial port pins RCSTAbits.CREN=1; //enable reception RCSTAbits.SREN=0; //no effect TXSTAbits.TX9=0; //8-bit transmission RCSTAbits.RX9=0; //8-bit reception TXSTAbits.TXEN=0; //reset transmitter TXSTAbits.TXEN=1; //enable the transmitter //LATBbits.LATB5=0; UART_RX_TRIS =1; //input UART_TX_TRIS =0; //output UART_DIR_TRIS =0; //output UART_DIR=0; //read by default BAUDCONbits.BRG16=1; TXSTAbits.BRGH=0; SPBRGH=1; SPBRG =0; PIE1bits.TXIE=0; PIE1bits.RCIE=0; //enable timer T0CONbits.PSA=0; T0CONbits.T0PS0=1; T0CONbits.T0PS1=0; T0CONbits.T0PS2=1; T0CONbits.T08BIT=1; T0CONbits.TMR0ON=1; T0CONbits.T0CS=0; INTCONbits.TMR0IE=0; UARTRead(); }
int main(void) { char c; // PORTC_PCR5 = PORT_PCR_MUX(0x1); // LED is on PC5 (pin 13), config as GPIO (alt = 1) // GPIOC_PDDR = (1<<5); // make this an output pin // LED_OFF; // start with LED off UARTInit(TERM_UART, TERM_BAUD); // open UART for comms UARTWrite(hello, strlen(hello)); EnableInterrupts; while (1) { while (UARTAvail() != 0) { UARTRead(&c, 1); UARTWrite(&c, 1); if (c == '\r') UARTWrite("\n", 1); } } return 0; // should never get here! }