Tutorial 3

How to capture and display latitude, longitude, date and time on a Nokia 5110 graphic LCD


Introduction

This tutorial is for displaying latitude, longitude, date, and time on a Nokia 5110 graphic LCD.  The LCD comes as a breakout from Sparkfun.  Since the display uses SPI to communicate and its own frame buffer with a low power CMOS LCD, this makes it ideal to display coordinates from NavSpark.

The graphic display has 84 x 48 monochrome pixels, and it uses the PCD8544 controller that is in the Nokia 3310 LCD.  With NavSpark’s SPI capability, users are able to display latitude, longitude, date and time on the graphic LCD.


Breakout Pinouts

The monochrome display uses SPI to receive image data.  That means NavSpark will be able to display the through software SPI with 4 pins – clock, data in, chip select, and d/c (mode select). There are other pins that you want to use, so let’s look through them all.

·       VCC – Positive Power Supply (Input) – connect to 3 – 5 VDC

·       GND – Ground (Input)

·       SCE – Chip Select (Input)

·       RST – Reset (Input)

·       D/C – Mode Select (Input)

·       DN (MOSI) – Serial Data In (Input)

·       SCLK – Serial clock (Input)

·       LED – LED Backlight Supply (Input)


Breakout Wiring and Tests

Wiring up the display in SPI mode is easy and there aren’t many pins.

·       VCC connects to VCC33 (out) – red wire

·       GND connects to GND – black wire

·       SCE connects to GPIO 10 – yellow wire

·       RST connects to GPIO 5 – blue wire

·       D/C connects to GPIO 16 – yellow wire

·       DN (MOSI) connects to GPIO 28 – blue wire

·       SCLK connects to GPIO 30 – yellow wire

·       LED connects to GPIO 3 – blue wire

 
 

The Sketch

Download the NavSpark Example Code Files from our Downloads page and open the GPS Graphic LCD demo (demo_gps_info_graphic_lcd_Nokia5110)

Here is the sketch of the demo displaying latitude, longitude, date and time on the LCD.

Copy the code below and paste it as a sketch.

#include "Nokia_5110.h" // PCD8544 Controller
#include "sti_gnss_lib.h" // GNSS
#include "math.h" // fabs

#define _CHECK_MARK 0xf0  // Check mark

// Format String for Latitude
uint16_t GNSSLocationLatitudeLCDFormatString(char* str)
{
  char N_S = 'N';
  double absLatitude = GnssInfo.location.latitude();
  uint16_t deg;
  uint8_t min;
  
  if (GnssInfo.location.latitude() < 0) 
  {
    N_S = 'S';
    absLatitude = fabs(GnssInfo.location.latitude());
  }
  
  // Convert the latitude to the format 120 36'55.22"
  deg = (uint16_t) absLatitude;
  absLatitude = (absLatitude - deg) * 60;
  min = (uint8_t) absLatitude;
  absLatitude = (absLatitude - min) * 60;
  return sprintf(str, "LAT: %03d %02d\'%.2f\" %c", deg, min, absLatitude, N_S);
}

// Format String for longitude
uint16_t GNSSLocationLongitudeLCDFormatString(char* str)
{
  char E_W = 'E';
  double absLongitude = GnssInfo.location.longitude();
  uint16_t deg;
  uint8_t min;
  
  if (GnssInfo.location.longitude() < 0) 
  {
    E_W = 'W';
    absLongitude = fabs(GnssInfo.location.longitude());
  }
  
  // convert the latitude to the format 120 36'55.22"
  deg = (uint16_t) absLongitude;
  absLongitude = (absLongitude - deg) * 60;
  min = (uint8_t) absLongitude;
  absLongitude = (absLongitude - min) * 60;
  return sprintf(str, "LON: %03d %02d\'%.2f\" %c", deg, min, absLongitude, E_W);
}

// Check if GPS is in fixed mode
uint8_t CheckMark(void)
{
  if(GnssInfo.fixMode() >= 3)
  {
    return _CHECK_MARK;  // In fixed mode (check mark)
  }
  else
  {
    return '?';  // Not in fixed mode
  }
}

// Format string for time and fixed mode verification
uint16_t GnssInfoTimeLCDFormatString(char *str)
{
  return sprintf(str, "%02d:%02d:%02d.%01d %c",
                      GnssInfo.time.hour(),
                      GnssInfo.time.minute(),
                      GnssInfo.time.second(),
                      GnssInfo.time.decisecond(),
                      CheckMark());
}

const int satLed = 0;
const int buttonPin = 13;

void setup() 
{
  // put your setup code here, to run once:
  GnssConf.setNavMode(STGNSS_NAV_MODE_AUTO);
  GnssConf.setUpdateRate(STGNSS_POSITION_UPDATE_RATE_10HZ);
  GnssConf.setDopMaskMode(STGNSS_DOP_MASK_AUTO);
  GnssConf.setPdopMask(30.0);
  GnssConf.setHdopMask(30.0);
  GnssConf.setGdopMask(30.0);
  GnssConf.init(); // Initialization for GNSS
  
  // Configure GPIO pins.
  pinMode(PCD8544_LED_PIN, OUTPUT);
  pinMode(PCD8544_SCE_PIN, OUTPUT);
  pinMode(PCD8544_DC_PIN, OUTPUT);
  pinMode(PCD8544_SCLK_PIN, OUTPUT);
  pinMode(PCD8544_SDIN_PIN, OUTPUT);
  pinMode(PCD8544_RES_PIN, OUTPUT);
  pinMode(satLed, OUTPUT);
  pinMode(buttonPin, INPUT);

  digitalWrite(PCD8544_LED_PIN, HIGH);
  digitalWrite(PCD8544_SCE_PIN, HIGH);
  digitalWrite(PCD8544_DC_PIN, HIGH);
  digitalWrite(PCD8544_SCLK_PIN, LOW);
  digitalWrite(PCD8544_SDIN_PIN, LOW);
  digitalWrite(PCD8544_RES_PIN, HIGH);

  // Initialize the LCD controller
  pcd8544Init();
}

void loop() 
{  
  int len, len2, len3, len4;
  char buf[64];
  static uint16_t delay = 0;
  
  if ((delay % 2500) == 0) 
  {
    GnssInfo.update();
    
    // LED lights up when GNSS is updated
    if(GnssInfo.isUpdated() == true)
    {
      digitalWrite(0, HIGH);
    }
    else
    {
      digitalWrite(0, LOW);
    }
    
    // Print latitude on the first 2 rows
    pcd8544Init();
    len = GNSSLocationLatitudeLCDFormatString(buf);
    for(int i = 0; i < len; i++)
    {
      printOnLCD(buf[i]);
    }
  
    // Print longitude on third and fourth rows
    pcd8544SetCursor(2, 0);
    len2 = GNSSLocationLongitudeLCDFormatString(buf);
    for(int i = 0; i < len2; i++)
    {
      printOnLCD(buf[i]);
    }
  
    // Print date on the fifth row
    pcd8544SetCursor(4, 0);
    len3 = GnssInfo.date.formatString(buf);
    for(int i = 0; i < len3; i++)
    {
      printOnLCD(buf[i]);
    }
  
    // Print time and fix mode verification on the sixth row
    pcd8544SetCursor(5, 0);
    len4 = GnssInfoTimeLCDFormatString(buf);
    for(int i = 0; i < len4; i++)
    {
      printOnLCD(buf[i]);
    }
  }
  delay ++;
}

 

The Sketch in Action

If the LCD is displaying correctly, you can wire the wires more neatly.  The display will begin to display the correct date and UTC time.  After a while, it will display the latitude and longitude of your location, the satellite LED will light up, and a check mark will displayed at the bottom right corner to verify position fix.