Display GPS position on a LCD with PIC16F628A

Discussion in 'Embedded Systems and Microcontrollers' started by nerdegutta, Mar 31, 2012.

  1. nerdegutta

    Thread Starter Moderator

    Dec 15, 2009
    2,517
    785
    Hi,

    I'm trying to build a little circuit which displays some info from a GPS-module.

    The module is EM - 411
    The display is a 2x20 LCD.

    I know the display is working. (I've tested with another code)
    I know the GPS is working. (I've tested it against another PCs serialport via MAX232 IC)

    I've googled and read datasheets for days, but now I'm stuck, and I think I've found where it goes down the drain...

    This is the main.c - file:

    Code ( (Unknown Language)):
    1.  
    2. /*
    3. Circuit
    4. LCD module:
    5.  
    6. PIN 1: GND -> GND
    7. PIN 2: +5v -> +5v
    8. PIN 3: 5Kpot
    9. PIN 4: RS  -> RA2
    10. PIN 5: RW  -> RA3
    11. PIN 6: EN  -> RB3
    12.  
    13. PIN 11: D4 -> RB4
    14. PIN 12: D5 -> RB5
    15. PIN 13: D6 -> RB6
    16. PIN 14: D7 -> RB7
    17.  
    18. GSP module:
    19. PIN 1: GND
    20. PIN 2: Vin
    21. PIN 3: TX -> RB1
    22. PIN 4: RX -> RB2
    23. PIN 5: GND
    24. PIN 6: Not Connected
    25.  
    26.  
    27. */
    28.  
    29. // Definitions and includes
    30. #ifndef _XTAL_FREQ
    31. // Unless specified elsewhere, 4MHz system frequency is assumed
    32. #define _XTAL_FREQ 4000000
    33. #endif
    34.  
    35. #include <htc.h>
    36. #include <string.h>
    37. #include <stdio.h>
    38. #include <stdlib.h>
    39. #include "conio.h"
    40. #include "usart.h"
    41. #include "usart.c"
    42. #include "lcd-mod.c"
    43.  
    44.  
    45. // Configuration
    46. __CONFIG (WDTEN & PWRTEN & MCLRDIS & BOREN & LVPDIS & UNPROTECT & INTIO);
    47.  
    48. // Prototype functions
    49.  
    50.  
    51. // Gloabal variables
    52. char input;
    53. char buffer[19];
    54.  
    55.  
    56. // Functions
    57.  
    58. // Main
    59. void main()
    60. {
    61. INTCON = 0;
    62. CMCON =0x07;
    63.  
    64. init_comms();
    65. lcd_init();
    66. lcd_goto(0);
    67. while (1)
    68. {
    69. input = getchar();
    70. itoa(buffer, input, 10);
    71.  
    72.     lcd_puts(buffer);
    73. __delay_us(250);
    74. } // end while
    75. } // end main
    76.  
    This is the lcd_mod.h - file:
    Code ( (Unknown Language)):
    1.  
    2. /*
    3. * lcd-mod.c
    4. */
    5.  
    6. #include "lcd.h"
    7.  
    8. #define LCD_RS RA2
    9. #define LCD_RW RA3
    10. #define LCD_EN RB3
    11.  
    12. #define LCD_DATA PORTB
    13.  
    14.  
    15. #define LCD_STROBE() ((LCD_EN = 1),(LCD_EN=0))
    16.  
    17. /* write a byte to the LCD in 4 bit mode */
    18. void
    19. lcd_write(unsigned char c)
    20. {
    21. __delay_us(40);
    22. LCD_DATA = ( c & 0xF0 );//skriv mest signifikant nibble (4 bit)
    23. LCD_STROBE();
    24. LCD_DATA = ((c >> 4) | (c << 4)& 0xF0);//Skriv minst signifikant nibble
    25. //LCD_DATA = ( ( c >> 4 ) & 0x0F );
    26. LCD_STROBE();
    27. }
    28.  
    29. /*
    30. * Clear and home the LCD
    31. */
    32. void
    33. lcd_clear(void)
    34. {
    35. LCD_RS = 0;
    36. lcd_write(0x10);
    37. __delay_ms(20);
    38. }
    39.  
    40.  
    41. /* write a string of chars to the LCD */
    42.  
    43. void
    44. lcd_puts(const char * s)
    45. {
    46. LCD_RS = 1; // write characters
    47. while(*s)
    48. lcd_write(*s++);
    49. }
    50.  
    51.  
    52. /* write one character to the LCD */
    53.  
    54. void
    55. lcd_putch(char c)
    56. {
    57. LCD_RS = 1; // write characters
    58. lcd_write( c );
    59. }
    60.  
    61.  
    62. /* Go to the specified position */
    63.  
    64. void
    65. lcd_goto(unsigned char pos)
    66. {
    67. LCD_RS = 0;
    68. lcd_write(0x80+pos);
    69. }
    70.  
    71.  
    72. /* initialise the LCD - put into 4 bit mode */
    73.  
    74. void
    75. lcd_init()
    76. {
    77. char init_value;
    78. // ADCON1 = 0x06; // Disable analog pins on PORTA
    79. init_value = 0x30;//endret fra 0x3 til 0x30
    80. TRISA=0;
    81. TRISB= 0b00000100;
    82. LCD_RS = 0;
    83. LCD_EN = 0;
    84. LCD_RW = 0;
    85.  
    86. __delay_ms(15); // wait 15mSec after power applied,
    87. LCD_DATA = init_value;
    88. LCD_STROBE();
    89. __delay_ms(5);
    90. LCD_STROBE();
    91. __delay_us(200);
    92. LCD_STROBE();
    93. __delay_us(200);
    94. LCD_DATA = 0x20;//endret fra 2 til 0x20
    95. // Four bit mode
    96. LCD_STROBE();
    97. lcd_write(0x28); // Set interface length
    98. lcd_write(0xF); // Display On, Cursor On, Cursor Blink
    99. lcd_clear(); // Clear screen
    100. lcd_write(0x6); // Set entry Mode
    101. }
    102.  
    This is the usart.c - file
    Code ( (Unknown Language)):
    1.  
    2. #ifndef _SERIAL_H_
    3. #define _SERIAL_H_
    4.  
    5. #define BAUD 9600      
    6. #define FOSC 4000000L
    7. #define NINE 0     /* Use 9bit communication? FALSE=8bit */
    8.  
    9. #define DIVIDER ((int)(FOSC/(16UL * BAUD) -1))
    10. #define HIGH_SPEED 1
    11.  
    12. #if NINE == 1
    13. #define NINE_BITS 0x40
    14. #else
    15. #define NINE_BITS 0
    16. #endif
    17.  
    18. #if HIGH_SPEED == 1
    19. #define SPEED 0x4
    20. #else
    21. #define SPEED 0
    22. #endif
    23.  
    24. #ifndef _16F628
    25.     #define RX_PIN TRISB1
    26.     #define TX_PIN TRISB2
    27. #endif
    28.  
    29. /* Serial initialization */
    30. #define init_comms()\
    31.     RX_PIN = 1; \
    32.     TX_PIN = 1;       \
    33.     SPBRG = DIVIDER;        \
    34.     RCSTA = (NINE_BITS|0x90);   \
    35.     TXSTA = (SPEED|NINE_BITS|0x20)
    36.  
    37. void putch(unsigned char);
    38. unsigned char getch(void);
    39. unsigned char getche(void);
    40.  
    41. #endif
    42.  
    43.  
    I want the PIC to receive data from the EM-411 in PIN 7 (RB1), and display whatever on the LCD. I know that input = getchar only puts one character in input, but that is ok for now. Then I know that it's working.

    I think the error is in the lcd-mod.c - file. The line:

    Code ( (Unknown Language)):
    1. #define LCD_DATA PORTB
    Defines all of PORTB to LCD_DATA and the PIC's RX-pin is RB1, so I think that this overrules the Rx function.

    Am I right or way off?

    If I'm right, how should I configure LCD_DATA to be only RB4:RB7? Which is the datapins for the LCD.

    MPLAB 8.63
    Hi tech C V9.8
     
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    I think it's possible that all you need to do is change the TRISB line in LCD init to match init_comms. If the pins are correctly set up as the USART then writing to the port should make no difference.
     
    nerdegutta likes this.
  3. nerdegutta

    Thread Starter Moderator

    Dec 15, 2009
    2,517
    785
    I'm blind!

    Thanks, now some characters are starting to come, and I can start formatting and sorting.
     
    Last edited: Apr 1, 2012
Loading...