element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Embedded and Microcontrollers
  • Technologies
  • More
Embedded and Microcontrollers
Pic Microcontrollers Forum PIC18F452 with GPS NEO6M UART
  • Blog
  • Forum
  • Documents
  • Quiz
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Embedded and Microcontrollers to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 2 replies
  • Subscribers 188 subscribers
  • Views 548 views
  • Users 0 members are here
  • pic controllers
  • pic18
  • microchip
  • pic18f452
  • programming
  • pic
Related

PIC18F452 with GPS NEO6M UART

ArthurHEL
ArthurHEL over 1 year ago

Hello,

I'm an electronics student and for my final project I'm trying to display data from a GPS (nemo 6m) operating via RS232 communication on an LCD screen with my PIC18F452. I had made a code for the LCD without a library, which worked without a hitch and which I used in this code.

The code for displaying data from an RS232 communication is relatively simple and works very well with a TTL to USB card.

The code below for displaying GPS data on the LCD works, but only once. When I run the program, it displays the time correctly, but I have to press the reset button to refresh. It seems that the code is stuck somewhere....

After many tests, I don't understand why and that's why I'm coming to you.
Thanks in advance for taking a look.

Technical specification:
16MHz quartz

// Pour les commandes du LCD : https://binaryupdates.com/interface-lcd-with-lpc2148-arm7/jhd162a-lcd-command-codes/
// Voir datasheet p174 et p176 : https://ww1.microchip.com/downloads/aemDocuments/documents/MCU08/ProductDocuments/DataSheets/39564c.pdf
#include <p18f452.h> 
#include <delays.h>
#include <stdio.h>
 
#pragma config OSC = HS   // Travailler à 16MHz
#pragma config PWRT = ON 
#pragma config BOR = OFF 
#pragma config WDT = OFF  
#pragma config LVP = OFF  
 

// ****************************** Déclarations des PINs de sortie ***************************************
// ****************************** Output PIN declarations ***************************************

// Définition pour broches en sortie
// Definition for output pins
#define data4DEF TRISBbits.TRISB4
#define data5DEF TRISBbits.TRISB5
#define data6DEF TRISBbits.TRISB6
#define data7DEF TRISBbits.TRISB7
#define RSDEF TRISCbits.TRISC0
#define RWDEF TRISCbits.TRISC1
#define EDEF TRISCbits.TRISC2

// Définition pour l'état des broches (même que pour TRIS)
// Definition of pin status (same as TRIS)
#define data4 PORTBbits.RB4
#define data5 PORTBbits.RB5
#define data6 PORTBbits.RB6
#define data7 PORTBbits.RB7
#define RS PORTCbits.RC0
#define RW PORTCbits.RC1
#define E PORTCbits.RC2

// Paramètre interne (pas toucher)
// Internal parameter (do not touch)
int delai = 1;


// ****************************************** Toutes les fonctions *****************************************************


void LCD_Cmnd (unsigned char cmnd)
{
    // Permet d'appliquer les bits de la commande sur les bonnes PINs de sortie (en fonction des define)
    // Enables command bits to be applied to the correct output PINs (depending on define)
    data4 = (cmnd & 0x10) >> 4;
    data5 = (cmnd & 0x20) >> 5;
    data6 = (cmnd & 0x40) >> 6;
    data7 = (cmnd & 0x80) >> 7;
    
    RS = 0;                     // Mode commande
    E = 1;
    Delay10KTCYx (delai);
    E = 0;
    
    data4 = cmnd & 0x01;
    data5 = (cmnd & 0x02) >> 1;
    data6 = (cmnd & 0x04) >> 2;
    data7 = (cmnd & 0x08) >> 3;
    
    E = 1;
    Delay10KTCYx (delai);
    E = 0;
}

void LCD_Charact (unsigned char charact)
{
    // Permet d'appliquer les bits du caractère sur les bonnes PINs de sortie (en fonction des define)
    // Enables character bits to be applied to the correct output PINs (depending on define)
    data4 = (charact & 0x10) >> 4;
    data5 = (charact & 0x20) >> 5;
    data6 = (charact & 0x40) >> 6;
    data7 = (charact & 0x80) >> 7;
    
    RS = 1;                         // mode caractère
    E = 1;
    Delay10KTCYx (delai); 
    E = 0;
    
    data4 = charact & 0x01;
    data5 = (charact & 0x02) >> 1;
    data6 = (charact & 0x04) >> 2;
    data7 = (charact & 0x08) >> 3;
    
    E = 1;
    Delay10KTCYx (delai); 
    E = 0;
}


void init_LCD (void)
{
    // Mise en sortie des PINs choisis en sortie
    // Output selected PINs
    data4DEF = 0;
    data5DEF = 0;
    data6DEF = 0;
    data7DEF = 0;
    RSDEF = 0;
    RWDEF = 0;
    EDEF = 0;
    RW = 0;
    LCD_Cmnd(0x28);           // Mode 4bits, 2 lignes 
    LCD_Cmnd(0x01);           // Clear l'écran
    LCD_Cmnd(0x0C);           // Display on, cursor off (0x0F - Display on, cursor clignotant)
}

void init_UART(void) {
    SPBRG = 25;                 // Basé sur la formule (((4000000/9600)/64)-1) //Mettre la trensmition sur 2400 si SPBRG=25
    TXSTAbits.SYNC = 0;         // Mode asynchrone
    TXSTAbits.BRGH = 0;         // Mode low speed
    RCSTAbits.SPEN = 1;         // Activer le port série (TX et RX)
    RCSTAbits.CREN = 1;         // Activer la réception               
}



// ********************************************* Code Principal *********************************************

void main (void) 
{  
    char tabGPS[100];
    int i;
    int y;
    
    init_UART();
    init_LCD();
    
    while (1){
        
        // Charger le tableau avec les données du GPS
        // Load table with GPS data
        for (i=0;i<100;i++){
            while (PIR1bits.RCIF == 0);         // Attendre qu'un caractère soit reçu
            tabGPS[i]= RCREG; 
        }
        
        // Parcouir le tableau et afficher ce qui nous interesse 
        // Browse the table and display what interests us 
        for (y=0;y<100;y++){
           if (tabGPS[y] == 'G' && tabGPS[y+1] == 'P' && tabGPS[y+2] == 'R'&& tabGPS[y+3] == 'M'&& tabGPS[y+4] == 'C'){
                LCD_Cmnd(0x80);
                LCD_Charact(tabGPS[y+6]);
                LCD_Charact(tabGPS[y+7]);
                LCD_Charact(':');
                LCD_Charact(tabGPS[y+8]);
                LCD_Charact(tabGPS[y+9]);
                LCD_Charact(':');
                LCD_Charact(tabGPS[y+10]);
                LCD_Charact(tabGPS[y+11]);
                }
            } 
    }
} 

  • Sign in to reply
  • Cancel

Top Replies

  • ArthurHEL
    ArthurHEL over 1 year ago in reply to JWx +3
    Hello again, Your reply made me think a lot and after a few tests, I found out what was wrong and it's quit simple ahah. When I come out of my first loop, I have to disable communication (probably…
  • JWx
    JWx over 1 year ago +2
    it seems that it generates buffer overflow - what happens when (inside second loop) y > 95? Loop reads data beyond tabGPS end... Question is if it can hang the code? Maybe you can put some debug (cycle…
Parents
  • JWx
    JWx over 1 year ago

    it seems that it generates buffer overflow - what happens when (inside second loop) y > 95? Loop reads data beyond tabGPS end... Question is if it can hang the code? Maybe you can put some debug (cycle some led or print on uart console) inside while loop to check if it iterates or stops?

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • JWx
    JWx over 1 year ago

    it seems that it generates buffer overflow - what happens when (inside second loop) y > 95? Loop reads data beyond tabGPS end... Question is if it can hang the code? Maybe you can put some debug (cycle some led or print on uart console) inside while loop to check if it iterates or stops?

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
Children
  • ArthurHEL
    ArthurHEL over 1 year ago in reply to JWx

    Hello again,

    Your reply made me think a lot and after a few tests, I found out what was wrong and it's quit simple ahah.


    When I come out of my first loop, I have to disable communication (probably to avoid reading unwanted data) and re-enable it when I've finished displaying my various data!
    I've displayed the time and one of the two GPS coordinates and it's working perfectly!


    Thanks for your help, have a nice day!

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube