• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

reading gps with pic18

Status
Not open for further replies.

pawnda

New Member
Hi. I am trying to read a gps I have (garmin 18 lvc) and all I can get is garbage on the lcd. I can successfully read the gps through hyperterminal at 4800 8/n/1. I am using MPLAB with C18 Compiler. Any help is appreciated.

gps datasheet http://www.electro-tech-online.com/custompdfs/2009/09/425_TechnicalSpecification.pdf

Here are my connections
GPS PIC
Serial --------> RC7
GND --------> Vss
+5 --------> Vdd

and here is the code (excluding lcd functions)
Code:


Code:
void main()
{
 OSCCON = 0b01110010; // Clock set to 8Mhz
 TRISC = 0x00;
 TRISB = 0x00;
 PORTC = 0x00;
 PORTB = 0x00;
 ADCON1 = 0xFF;
 lcd_init();
  while (1)
  {
  serial();
  }
}

void serial(void)
{
  int i;
  TXSTAbits.SYNC = 0;
  RCSTAbits.SPEN = 1;                               
  RCSTAbits.CREN = 0;
  RCSTAbits.RX9 = 0;                   
  TRISCbits.TRISC7 = 1;
  TRISCbits.TRISC6 = 1;                   
  BAUDCTLbits.BRG16 = 0;                 
     OpenUSART (USART_TX_INT_OFF &
          USART_RX_INT_OFF &
           USART_ASYNCH_MODE &
           USART_EIGHT_BIT &
           USART_CONT_RX &
           USART_BRGH_LOW, 25);            // calculated for 4800 baud
      BAUDCONbits.RXDTP = 1;               // gps uses inverted logic
          while (!DataRdyUSART());
              prnt(ReadUSART());
     
}

Thank you.
 

Pommie

Well-Known Member
Most Helpful Member
If you're not using them for anything else, and assuming your pic has them, you can use one of the comparators to invert the signal. Alternatively, bit bang it and do the inversion in software.

Mike.
 
Last edited:

pawnda

New Member
This line:

BAUDCONbits.RXDTP = 1;

tells the pic that the data coming in is inverted. I don't have any hardware in between the gps and pic.

Thanks
 

Pommie

Well-Known Member
Most Helpful Member
It should work then. However, you are reinitializing the UART every time you call serial(). Try separating the init and receive parts and just put the receive part in the while(1) loop.

Edit, BTW, I'd try that invert bit both ways as it's unclear what invert actually means.

Mike.
 
Last edited:

pawnda

New Member
Thanks for your time. The LCD still displays garbage, but only displays three characters. Should I be utilizing interrupts?

I changed my code to this:

Code:
void main()
{
   int i;
   OSCCON = 0b11100010; // Clock set to 8Mhz
   TRISC = 0x00;
   TRISB = 0x00;
   PORTC = 0x00;
   PORTB = 0x00;
   ADCON1 = 0xFF;
   lcd_init();
	TRISCbits.TRISC7 = 1;
	TRISCbits.TRISC6 = 1;
	RCSTAbits.SPEN = 1;
	     OpenUSART (USART_TX_INT_OFF &
          USART_RX_INT_OFF &
           USART_ASYNCH_MODE &
           USART_EIGHT_BIT &
           USART_CONT_RX &
           USART_BRGH_LOW, 25);            // calculated for 4800 baud
      BAUDCONbits.RXDTP = 1;
while (1)
	{  
	while (!DataRdyUSART());
				prnt(ReadUSART());	

	}
}
 
Last edited:

Pommie

Well-Known Member
Most Helpful Member
Can you try,
Code:
    TRISCbits.TRISC7=1;
    TRISCbits.TRISC6=1;
    TXSTA=0b00100000;
    RCSTA=0b10010000;
    BAUDCON=0b0010000;
    SPBRG=25;
    while(1){
        if(PIE1bits.RCIF)
            prnt(RCREG);    
    }
Also try it with BAUDCON=0.

Edit, also check the value of OSCCON. It's normally 0x72, in the second code you posted, you have 0xe2. Which chip is this?

Mike.
 
Last edited:

pawnda

New Member
Hi Mike. I ran your code and it wouldn't compile. I assumed you meant this (which compiles)

Code:
	PIE1bits.RCIE = 1;
    TRISCbits.TRISC7=1;
    TRISCbits.TRISC6=1;
    TXSTA=0b00100000;
    RCSTA=0b10010000;
    BAUDCON=0b0000000;
    SPBRG=25;
    while(1){
        if(PIR1bits.RCIF)
            prnt(RCREG);    
    }
I changed back OSCCON to 0x72. Still garbage characters on the lcd screen even with BAUDCON=0

Could it be something with the config bits? Here is my current config

Code:
#pragma config OSC = INTIO67
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = ON
#pragma config BOREN = OFF
#pragma config WDT = OFF
#pragma config MCLRE = OFF  
#pragma config PBADEN = OFF
#pragma config LVP = OFF
 

Pommie

Well-Known Member
Most Helpful Member
Yes, it should be PIR not PIE. You don't want the first line unless you intend using interrupts. You config looks fine and so I have no idea why it's not working.

Mike.
 

pawnda

New Member
ok so now i can read the gps, sorta. If I use getsUSART( input, 15) I can get 15 valid characters from the gps. But it seems like the freezes and doesn't update. When using readUSART() I can read the first 3 characters successfully but then the pic freezes. I investigated into overrun and framing errors and added code to check for the error bits. I can still get the first 3 characters to display but after that it just displays garbage.

am i checking for errors correctly?
Code:
void main()
{ 
   int i=0;
	char input[34];
	char temp;
   OSCCON = 0b01110010; // Clock set to 8Mhz
	TRISC= 0x00;
   TRISB = 0x00;
   PORTC = 0x00;
   PORTB = 0x00;
   ADCON1 = 0xFF;
   lcd_init();
	TRISCbits.TRISC7 = 1;
	TRISCbits.TRISC6 = 1;
	RCSTAbits.SPEN = 1;
	BAUDCONbits.RXDTP = 1;
	RCSTAbits.CREN = 1;
	     OpenUSART (USART_TX_INT_OFF &
          USART_RX_INT_OFF &
           USART_ASYNCH_MODE &
           USART_EIGHT_BIT &
           USART_CONT_RX &
           USART_BRGH_LOW, 25);            // calculated for 4800 baud
    
while (1) 
{
	while(!DataRdyUSART());
			if (RCSTAbits.OERR == 1)
			{
				RCSTAbits.CREN = 0;
				RCSTAbits.CREN = 1;
			}
			else if (RCSTAbits.FERR == 1)
			{
				ReadUSART();
			}
			else	
			{
				prnt(ReadUSART());
			}
}
}
 

Pommie

Well-Known Member
Most Helpful Member
The only reason I can think of that would cause that is if your LCD routines are very slow. Assuming it is using delays and not the RW pin, check that the delays are not too long. For a data write it should be no more that 50uS. I have seen routines with a mS delay.

If you're unsure, post the LCD code.

Edit, BTW, there is no point writing to any registers before the OpenUSART function as it resets all registers to zero as the first instructions. The exception is the RXDTP bit but I would still set that after the open function.

Mike.
 
Last edited:

pawnda

New Member
Thanks Mike. Decreasing the delays in my LCD function seemed to do the trick. When would I need to worry about errors, Fifo, etc?

Thanks again
 

Pommie

Well-Known Member
Most Helpful Member
If you are doing lots of processing then it may be worth moving the serial routines onto an interrupt and implementing a fifo. I posted some fifo code on this thread.

Mike.
 

WTP Pepper

Active Member
This line:

BAUDCONbits.RXDTP = 1;

tells the pic that the data coming in is inverted. I don't have any hardware in between the gps and pic.

Thanks
Then be careful if the GPS is true RS232. Not only will you have a data inversion you will also be driving the PIC input voltages outside their limits. RS232 can use upto +/-18V and you PIC will use 5V.
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top