PIC16f690 2x16 LCD not working.

Discussion in 'Embedded Systems and Microcontrollers' started by Tamás Fegyvári, Oct 10, 2017.

  1. Tamás Fegyvári

    Thread Starter New Member

    Sep 26, 2017
    11
    0
    Hi,
    I have a 2x16 LCD and a PIC16f690.
    I just cant make them work together. Is that possible, that this PIC is not able to do this?
    I connect the pins like in the image but i used the internal oscillator.
    [​IMG]
    [​IMG]
    [​IMG]
    And here is an example code. Its simple but is should work, but there is nothing on the display. I also used a resistor to set the contrast.
    Code (CSS):
    1. #include <TestLCD.h>
    2. #fuses   nomclr
    3. #include <LCD.C>
    4.  
    5. #include <stdlib.h>
    6. #include <string.h>
    7. #include <stdio.h>
    8. #define LCD_ENABLE_PIN PIN_C2
    9. #define LCD_RS_PIN PIN_C0
    10. #define LCD_RW_PIN PIN_C1
    11. #define LCD_DATA4 PIN_C3
    12. #define LCD_DATA5 PIN_C4
    13. #define LCD_DATA6 PIN_C5
    14. #define LCD_DATA7 PIN_C6
    15.  
    16. #include <lcd.c>
    17.  
    18. void main()
    19. {
    20.  
    21.  
    22.    lcd_init();
    23.  
    24.    lcd_init();
    25.    delay_ms(1000);
    26.    lcd_putc("\fReady...\n");
    27.  
    28.    while(TRUE)
    29.    {
    30.  
    31.  
    32.  
    33.             lcd_putc('\f');
    34.             lcd_gotoxy(3, 1);
    35.             printf(lcd_putc, "Temperature:");
    36.             delay_ms(1000);
    37.  
    38.    }
    39.  
    40. }
    Any help is much appreciated
     
  2. Tamás Fegyvári

    Thread Starter New Member

    Sep 26, 2017
    11
    0
    The code is stuck at the lcd_init(); I have no ide why. Checked the connections 100 times.
     
  3. be80be

    AAC Fanatic!

    Jul 5, 2008
    1,693
    333
    Try this and change wiring to match
    Code (C):
    1.  
    2. #include <TestLCD.h>
    3. #fuses   nomclr
    4. #include <LCD.C>
    5.  
    6. #include <stdlib.h>
    7. #include <string.h>
    8. #include <stdio.h>
    9. #define LCD_ENABLE_PIN PIN_C2
    10. #define LCD_RS_PIN PIN_C0
    11. #define LCD_RW_PIN PIN_C1
    12. #define LCD_DATA4 PIN_B4
    13. #define LCD_DATA5 PIN_B5
    14. #define LCD_DATA6 PIN_B6
    15. #define LCD_DATA7 PIN_B7
    16.  
    17. #include <lcd.c>
    18.  
    19. void main()
    20. {
    21.  
    22. ANSEL = 0x00; // have to cheak for what your compler is using
    23.    lcd_init();
    24.  
    25.    lcd_init();
    26.    delay_ms(1000);
    27.    lcd_putc("\fReady...\n");
    28.  
    29.    while(TRUE)
    30.    {
    31.  
    32.  
    33.  
    34.             lcd_putc('\f');
    35.             lcd_gotoxy(3, 1);
    36.             printf(lcd_putc, "Temperature:");
    37.             delay_ms(1000);
    38.  
    39.    }
    40.  
    41. }
    42.  
    43.  
     
  4. Tamás Fegyvári

    Thread Starter New Member

    Sep 26, 2017
    11
    0
    Thanks for the help.
    Unfortunately still nothing.

    I got an error for the ANSEL. "Undefined identifier"
    I added this line " #byte ANSEL = 0x11e " After this i got no error. I found this solutionin an other forum but i have eno idea what does it mean.
     
  5. AlbertHall

    AAC Fanatic!

    Jun 4, 2014
    6,544
    1,523
    Before sorting out the code, make sure the LCD is ready to display. If you adjust the contrast pot you should be able to get the top line of the display to show 16 boxes where the characters will be. Finally set the pot so the boxes are just barely visible. If you can't get the boxes then check the LCD wiring.
     
    xox likes this.
  6. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    6,861
    3,011

    It might help if you actually posted the code for lcd_init(). :rolleyes:
     
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    6,861
    3,011

    How does using PORTB for the data inputs fix the issue????
     
  8. JohnInTX

    Moderator

    Jun 26, 2012
    3,363
    1,686
    It does not look like you have initialized the PIC I/O ports. At the least you need:

    PORTC=0x00; // init port
    TRISC = 0x00; // make port pins all outputs
    ANSEL = 0x00; // make pins digital
    ANSELH= 0x00;
    ADCON0 = 0x00; // turn ADC off
     
    Last edited: Oct 10, 2017
  9. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    6,861
    3,011
    Forget about getting the LCD to work for now .Begin at the beginning. Write a small program to independently toggle each of your outputs. Verify that each output toggles at the LCD pin.
     
    MrChips likes this.
  10. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    6,861
    3,011

    That is why I asked for the code for lcd_init. I assume it is in there. Or I hope it is in there. It is the way I write my code. Or close to it. I use an lcd_config.h file to do all of my custom hardware configurations.
     
  11. MrChips

    Moderator

    Oct 2, 2009
    17,124
    5,294
    Getting an LCD to work from an MCU is like a 10-step recipe. Get a single step wrong and nothing works. Gets even more complicated when you've got 3 or more things wrong.

    @spinnaker is right. You have to be able to validate each step along the way.

    Start by making sure you can toggle an LED on a specific I/O pin.

    Then try to write your own code without any third party library. You don't know yet that the library works.
     
    absf likes this.
  12. shteii01

    AAC Fanatic!

    Feb 19, 2010
    4,425
    700
    I am not a PIC guy...
    Is it valid approach to use 877 wiring and code in 690? Are they actually identical when it comes to this application?
     
  13. be80be

    AAC Fanatic!

    Jul 5, 2008
    1,693
    333
    Most Lcd code is using upper port or lower port Like PORTC 0 to 3 or 4 to 7 not C3 to C6
    And I no port B works better with that chip it's only the upper port 4 to 7 witch is good and there nothing on them no adc no Comparators
     
    absf likes this.
  14. MrChips

    Moderator

    Oct 2, 2009
    17,124
    5,294
    You can use any output port. Just make sure that your code matches the wiring.
    Hence write your own code. It is not that difficult.

    I can provide code to match the PIC16F877F with the LCD.
    There are also some hardware issues that need attention.
     
  15. be80be

    AAC Fanatic!

    Jul 5, 2008
    1,693
    333
    The stock lcd library for ccs uses upper or lower port it loads 4 bits at time lower 4 or upper 4
     
  16. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    6,861
    3,011

    All you did was confuse the TS. Heck you are confusing me. ;)
     
  17. MrChips

    Moderator

    Oct 2, 2009
    17,124
    5,294
    To the TS, are you flexible on which pins you use?
    Why make life difficult?
    Use RC0-RC3 or RC4-RC7 for your 4-bit data port
    or RB0-RB3 or RB4-RB7 as the man says.
     
  18. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    6,861
    3,011
    I don't see how changing pins makes life any easier except with the actual wiring or PCB design.

    With any well written LCD library there should be no reason to have the data pins all on the same port. Assuming the library is configured according to how you have things wired. Any time I design a project with LCD, I first consider the pinout of the form factor I am using. I could make things easier when designing for PCB to make running the traces easier. Last thing I care about is which port I am coming from for each pin as long as the pin selected on the mcu does not present mcu pin conflicts life is good. For a perf board type of wiring it really doesn't matter unless you really want to keep things neat and organized while they run to the LCD.
     
    Last edited: Oct 10, 2017
  19. Picbuster

    Active Member

    Dec 2, 2013
    817
    101
    I use the interface (code below) or all my pic projects 690 /8722 /87k22 and more.
    Code (C):
    1. /*
    2. * LCD interface example
    3. * Uses routines from delay.c
    4. * This code will interface to a standard LCD controller
    5. * like the Hitachi HD44780. It uses it in 4 bit mode, with
    6. * the hardware connected as follows (the standard 14 pin
    7. * LCD connector is used):
    8. *
    9. * PORTC bits 0-3 are connected to the LCD data bits 4-7 (high nibble)
    10. * PORTA bit 3 is connected to the LCD RS input (register select)
    11. * PORTA bit 1 is connected to the LCD EN bit (enable)
    12. *
    13. * To use these routines, set up the port I/O (TRISA, TRISD) then
    14. * call lcd_init(), then other routines as required.
    15. *
    16. */
    17.  
    18. #ifndef _XTAL_FREQ
    19. // Unless specified elsewhere, 4MHz system frequency is assumed
    20.   #define _XTAL_FREQ 8000000
    21. #endif
    22.  
    23.  
    24. #include <htc.h>
    25. #include "lcd.h"
    26.  
    27. #define LCD_RS RA0
    28. #define LCD_RW RA1
    29. #define LCD_EN RA2
    30. #define LCD_DATA PORTC
    31.  
    32. #define LCD_STROBE() ((LCD_EN = 1),(LCD_EN=0))
    33.  
    34. //  write a byte to the LCD in 4 bit mode */
    35.  
    36.  
    37.  
    38.  
    39.  
    40.  
    41. void
    42. lcd_write(unsigned char c)
    43. {
    44. __delay_us(40);
    45. LCD_DATA = ( ( c >> 4 ) & 0x0F );
    46. LCD_STROBE();
    47. LCD_DATA = ( c & 0x0F );
    48. LCD_STROBE();
    49. __delay_us(20);
    50. }
    51.  
    52. /*
    53. *  Clear and home the LCD
    54. */
    55.  
    56. void
    57. lcd_clear(void)
    58. {
    59. LCD_RS = 0;
    60. lcd_write(0x1);
    61. __delay_ms(2);
    62. }
    63.  
    64. /* write a string of chars to the LCD */
    65.  
    66. void
    67. lcd_puts(const char * s)
    68. {
    69. LCD_RS = 1; // write characters
    70. while(*s)
    71. lcd_write(*s++);
    72. }
    73.  
    74. /* write one character to the LCD */
    75.  
    76. void
    77. lcd_putch(char c)
    78. {
    79. LCD_RS = 1; // write characters
    80. lcd_write( c );
    81. }
    82.  
    83.  
    84. /*
    85. * Go to the specified position
    86. */
    87.  
    88. void
    89. lcd_goto(unsigned char pos)
    90. {
    91. LCD_RS = 0;
    92. lcd_write(0x80+pos);
    93. }
    94.  
    95. /* initialise the LCD - put into 4 bit mode */
    96. void
    97. lcd_init()
    98. {
    99. char init_value;
    100.  
    101. ADCON1 = 0x06; // Disable analog pins on PORTA
    102.  
    103. init_value = 0x3;
    104. // TRISA=0;
    105. // TRISC=0;
    106. LCD_RS = 0;
    107. LCD_EN = 0;
    108. LCD_RW = 0;
    109.  
    110. __delay_ms(15); // wait 15mSec after power applied,
    111. LCD_DATA  = init_value;
    112. LCD_STROBE();
    113. __delay_ms(5);
    114. LCD_STROBE();
    115. __delay_us(200);
    116. LCD_STROBE();
    117. __delay_us(200);
    118. LCD_DATA = 2; // Four bit mode
    119. LCD_STROBE();
    120.  
    121. lcd_write(0x28); // Set interface length
    122. lcd_write(0xE); // Display On, Cursor On, Cursor Blink
    123. lcd_clear(); // Clear screen
    124. lcd_write(0x6); // Set entry Mode
    125. }
    Moderators note: please use code tags when posting pieces of code
     
    Last edited by a moderator: Oct 11, 2017
  20. Tamás Fegyvári

    Thread Starter New Member

    Sep 26, 2017
    11
    0
    Wow. Thanks for the many answers.
    I totally forgot to mention that is use CCS C. Sorry about this.

    I even tried the compilers lcd example code. Still nothing.
    Now i will try every method you mentiond.

    At this moment i dont have a pot, but i tried some single resistors and set the contrast where the upper boxes barely visible.
     
Loading...