Roish
Newbie level 1
Hello,
Im having probloms when trying to read data from a **broken link removed**.
The GPS module is interfaced to the PIC via USART and sends a custom binary protocol with 2 byte's hader of 0xD0 0xDD.
I am abel to get some kind of data from the GPS but i think its corrupted because the string dosnt conatine the 0xD0 0xDD header.
Project details:
PIC18F25K22
Clock - 4 MHz
Vdd - 5v
USART#1
TX - RC6
RX - RC7
GPS
MediaTek MT3329 GPS
Powerd @ 5v
I/O connected thrue 3.3v logic level converter
Baud - 38400 bps
Code:
Main:
/*
* File: I2C_CONT.c
* Author:
*
* Created on 1 ??????? 2011, 14:08
*/
void rx_handler (void);
#pragma config FOSC = XT
#pragma config BOREN = OFF
#pragma config WDTEN = OFF, WDTPS = 1
#pragma config LVP = OFF
#pragma config PWRTEN = OFF
char gpstmp,gps[30] = { 0 } ;
int z= 0;
#include <p18f25k22.h>
#include <delays.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<timers.h>
#include"rs_usart.h"
#pragma code high_interrupt = 0x08
void rx_int (void)
{
_asm goto rx_handler _endasm
}
#pragma code
#pragma interrupt rx_handler
void rx_handler (void)
{
if(z<29)
{
gps[z]=getc1USART();
if ((gps[z] == 0xDD) && (gps[z-1] == 0xD0) ) PORTAbits.RA0 =1;
z++;
if (z==29)
{
z=0;
PORTAbits.RA1 =1;
}
}
}
void main_init (void)
{
test = 0;
/* Enable interrupt priority */
RCONbits.IPEN = 1;
/* Make receive interrupt high priority */
IPR1bits.RCIP = 1;
/* Enable all high priority interrupts */
ANSELC=0;
ANSELA=0;
INTCONbits.PEIE_GIEL = 1;
INTCONbits.GIEH = 1;
TRISAbits.RA0 = 0;
TRISAbits.RA1= 0;
PORTAbits.RA0 = 0;
PORTAbits.RA1 = 0;
Open1USART(0x19);
}
void main(void)
{
main_init ();
while (1)
{
Delay10KTCYx(250);
PORTAbits.RA1 = 0;
}
return;
}
USART Code:
/*
* File: rs_usart.c
* Description: USART library header file for Microchip PIC18F25K22
* Ver: 1.0
*
*/
void Open1USART (unsigned int spbrg) // asychronous 8 bit mode only
{
TXSTA1 = 0; // Reset USART registers to POR state
RCSTA1 = 0;
SPBRG1 = spbrg;
SPBRGH1 = 0;
TXSTA1bits.BRGH = 1; // Baud rate select
BAUDCON1bits.BRG16 = 1;
PIE1bits.RC1IE = 1;
TRISCbits.RC6 = 1;
TRISCbits.RC7 = 1;
RCSTA1bits.CREN = 1; // Enable receiver
TXSTA1bits.TXEN = 1; // Enable transmitter
RCSTA1bits.SPEN = 1;
}
char getc1USART (void)
{
char flags;
char data; // Holds received data
flags = RCSTA1;
data = RCREG1; // Read data
if (RCSTA1bits.OERR)
{
RCSTA1bits.CREN = 0;
RCSTA1bits.CREN = 1;
}
return (data); // Return the received data
}
void putc1USART (char data)
{
TXREG1 = data; // Write the data byte to the USART2
}
char DataRdy1USART(void)
{
if(PIR1bits.RC1IF) // If RCIF is set
return 1; // Data is available, return TRUE
return 0; // Data not available, return FALSE
}
void gets1USART(char *buffer, unsigned char len)
{
char i; // Length counter
unsigned char data;
for(i=0;i<len;i++) // Only retrieve len characters
{
while(!DataRdy1USART());// Wait for data to be received
data = getc1USART(); // Get a character from the USART
// and save in the string
*buffer = data;
buffer++; // Increment the string pointer
}
}
char Busy1USART(void)
{
if(!TXSTA1bits.TRMT) // Is the transmit shift register empty
return 1; // No, return FALSE
return 0; // Return TRUE
}
void putrs1USART(const rom char *data)
{
do
{ // Transmit a byte
while(Busy1USART());
putc1USART(*data);
} while( *data++ );
}
void puts1USART( char *data)
{
do
{ // Transmit a byte
while(Busy1USART());
putc1USART(*data);
} while( *data++ );
}
Im having probloms when trying to read data from a **broken link removed**.
The GPS module is interfaced to the PIC via USART and sends a custom binary protocol with 2 byte's hader of 0xD0 0xDD.
I am abel to get some kind of data from the GPS but i think its corrupted because the string dosnt conatine the 0xD0 0xDD header.
Project details:
PIC18F25K22
Clock - 4 MHz
Vdd - 5v
USART#1
TX - RC6
RX - RC7
GPS
MediaTek MT3329 GPS
Powerd @ 5v
I/O connected thrue 3.3v logic level converter
Baud - 38400 bps
Code:
Main:
/*
* File: I2C_CONT.c
* Author:
*
* Created on 1 ??????? 2011, 14:08
*/
void rx_handler (void);
#pragma config FOSC = XT
#pragma config BOREN = OFF
#pragma config WDTEN = OFF, WDTPS = 1
#pragma config LVP = OFF
#pragma config PWRTEN = OFF
char gpstmp,gps[30] = { 0 } ;
int z= 0;
#include <p18f25k22.h>
#include <delays.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<timers.h>
#include"rs_usart.h"
#pragma code high_interrupt = 0x08
void rx_int (void)
{
_asm goto rx_handler _endasm
}
#pragma code
#pragma interrupt rx_handler
void rx_handler (void)
{
if(z<29)
{
gps[z]=getc1USART();
if ((gps[z] == 0xDD) && (gps[z-1] == 0xD0) ) PORTAbits.RA0 =1;
z++;
if (z==29)
{
z=0;
PORTAbits.RA1 =1;
}
}
}
void main_init (void)
{
test = 0;
/* Enable interrupt priority */
RCONbits.IPEN = 1;
/* Make receive interrupt high priority */
IPR1bits.RCIP = 1;
/* Enable all high priority interrupts */
ANSELC=0;
ANSELA=0;
INTCONbits.PEIE_GIEL = 1;
INTCONbits.GIEH = 1;
TRISAbits.RA0 = 0;
TRISAbits.RA1= 0;
PORTAbits.RA0 = 0;
PORTAbits.RA1 = 0;
Open1USART(0x19);
}
void main(void)
{
main_init ();
while (1)
{
Delay10KTCYx(250);
PORTAbits.RA1 = 0;
}
return;
}
USART Code:
/*
* File: rs_usart.c
* Description: USART library header file for Microchip PIC18F25K22
* Ver: 1.0
*
*/
void Open1USART (unsigned int spbrg) // asychronous 8 bit mode only
{
TXSTA1 = 0; // Reset USART registers to POR state
RCSTA1 = 0;
SPBRG1 = spbrg;
SPBRGH1 = 0;
TXSTA1bits.BRGH = 1; // Baud rate select
BAUDCON1bits.BRG16 = 1;
PIE1bits.RC1IE = 1;
TRISCbits.RC6 = 1;
TRISCbits.RC7 = 1;
RCSTA1bits.CREN = 1; // Enable receiver
TXSTA1bits.TXEN = 1; // Enable transmitter
RCSTA1bits.SPEN = 1;
}
char getc1USART (void)
{
char flags;
char data; // Holds received data
flags = RCSTA1;
data = RCREG1; // Read data
if (RCSTA1bits.OERR)
{
RCSTA1bits.CREN = 0;
RCSTA1bits.CREN = 1;
}
return (data); // Return the received data
}
void putc1USART (char data)
{
TXREG1 = data; // Write the data byte to the USART2
}
char DataRdy1USART(void)
{
if(PIR1bits.RC1IF) // If RCIF is set
return 1; // Data is available, return TRUE
return 0; // Data not available, return FALSE
}
void gets1USART(char *buffer, unsigned char len)
{
char i; // Length counter
unsigned char data;
for(i=0;i<len;i++) // Only retrieve len characters
{
while(!DataRdy1USART());// Wait for data to be received
data = getc1USART(); // Get a character from the USART
// and save in the string
*buffer = data;
buffer++; // Increment the string pointer
}
}
char Busy1USART(void)
{
if(!TXSTA1bits.TRMT) // Is the transmit shift register empty
return 1; // No, return FALSE
return 0; // Return TRUE
}
void putrs1USART(const rom char *data)
{
do
{ // Transmit a byte
while(Busy1USART());
putc1USART(*data);
} while( *data++ );
}
void puts1USART( char *data)
{
do
{ // Transmit a byte
while(Busy1USART());
putc1USART(*data);
} while( *data++ );
}