Hooking up my LCD

Discussion in 'Embedded Systems and Microcontrollers' started by beeson76, Jul 8, 2010.

  1. beeson76

    Thread Starter Member

    Apr 19, 2010
    185
    1
    Just wondering if anyone can help me with my LCD now. This is kinda a continuation of the thread Building My First Microcontroller Circuit by me (beeson76).

    I believe everything is correct as far as connections. When I plugged it in I got nothing but black boxes on the first line. I did some reading and found out that Pin 3 controls the contrast, by controlling current I believe--maybe its voltages. So I stuck on a Potentiometer and can now control the contrast. Here is the website for datasheet. It is a simple little LCD (C-1602A-1YN from Jameco). For the circuit I have 7.5vdc 300 ma AC adaptor going through a 7805 Voltage Regulator. Voltage output is 5 volts which I have going to my Pin 2 of the LCD. I have ground coming from Pin 1 that goes to ground of my Voltage Regulator. What should be connected to Pin 3.

    This is my first circuit with a microcontroller, and certainly my first circuit with LCD attached. Any help is greatly appreciated. Also maybe website with LCD schematics would be awesome too. I certainly appreciate any help. Thanks
     
  2. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    I though you said you had the contrast working?? Which pin did you connect the pot to?? Pin 3 of an LCD is usually the contrast control input....

    You need to connect the center pin (wiper) of the pot to that pin, and connect one side of the pot to VCC(+) and the other to GND.... make sure to check the datasheet for the proper ratings for the pot, I believe a standard is somewhere between 20K to 100K.



    B. Morse
     
  3. Flood_of_SYNs

    New Member

    Jul 8, 2010
    1
    0
    Your LCD module has 16 pins so it may use the standard HD44780 commands and pinout.
    * = only needed for 8-bit operation, if using 4-bit mode then leave these disconnected.
     
  4. beeson76

    Thread Starter Member

    Apr 19, 2010
    185
    1
    Sorry BMorse. I do have the pot connected to Pin 3. I have the the middle pin connected to Pin 3 and one of the "outer pins" connected to Vcc. The other pin is not connected to anything because the place where I found the information didn't tell me to connect it to ground. I will do that. That could be why the circuit is acting a little "weird" like when I push the button to display on the lCD it kinda like shorts out and just goes blank. I will give that a try. I wish I could send a schematic of my circuit but I really don't know how to except on autocad I could draw something up.
     
  5. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    Sounds as if you have more than just a contrast connection issue, that should have nothing to do with what you are sending to the LCD, is there anyway you can post the code you have for the LCD routines?? and how you have the LCD connected to the pic?

    B. Morse
     
  6. beeson76

    Thread Starter Member

    Apr 19, 2010
    185
    1
    Thanks BMorse for the reply. I do believe I am making a little progress. When I hooked up the ground on the Pot, I now have 2 lines of black boxes. I can adjust the contrast for both lines. Before, it was just one line of black boxes. Here is the main code

    Code ( (Unknown Language)):
    1.  
    2. /*This program controls a matrix switch.  The switch consists of 3 columns and 4 rows.
    3. //Whenever a button is pushed, that button  is displayed on an LCD screen.
    4. //
    5. */
    6.  
    7. #include <htc.h>
    8. #include "lcd.h"
    9.  
    10. #define  KEY_PORT               PORTB                                          
    11. #define  KEY_SET(bits)          KEY_PORT |= (bits)         
    12. #define  KEY_CLR                KEY_PORT &=~ (bits)        
    13. #define  KEY_FLP(bits)          KEY_PORT ^= (bits)         
    14. #define  KEY_IN(bits)           TRISB |= (bits)            
    15. #define  KEY_OUT(bits)          TRISB &=~ (bits)           
    16. #define  KEY_COL                0b10000011                           
    17. #define  KEY_ROW                0b01111000                 
    18. #define  DelayS(T)              {unsigned char i; for (i = 0; i < T * 10; i++) __delay_ms(100);}
    19. #define  _XTAL_FREQ             4000000                     //Needs to be set for __delay_ms
    20.  
    21. __CONFIG (XT, WDTDIS, PWRTEN, BORDIS, LVPDIS, UNPROTECT);
    22.  
    23.  
    24. unsigned char key_read(unsigned char in_bits, unsigned char out_bits)
    25.     {
    26.         KEY_IN(in_bits);
    27.         KEY_SET(out_bits);
    28.         KEY_OUT(out_bits);
    29.         return KEY_PORT & in_bits;
    30.     }
    31.  
    32. unsigned char key_detect(void)
    33.     {
    34.         unsigned char tmp1, tmp2;
    35.         tmp2 = key_read(KEY_ROW, KEY_COL);
    36.         tmp1 = key_read(KEY_COL, KEY_ROW);
    37.         return tmp1 | tmp2;
    38.     }
    39.  
    40. main ()                                        
    41. {                                                                              
    42.  
    43. //TRISA = 0x00;                                     //Set PORTA to outputs
    44. TRISB = 0xFF;                                       //Set PORTB to inputs
    45. TRISC = 0x00;                                       //Set PORTC to outputs
    46. RBPU = 0;
    47. //ANSEL = 0;
    48. //ANSELH = 0;
    49. //CM1CON0 = 0;
    50. //CM2CON0 = 0;
    51.  
    52. //OPTION = 0b01010101;
    53.  
    54. lcd_init();
    55.  
    56. char key;
    57.  
    58. while (key_detect() == 1)                           //Loops When Key is Pressed
    59. continue;
    60. {
    61.  
    62. key = key_detect();                                 //Referring to key_detect() function.  Returns key pressed.
    63.  
    64. switch (key)
    65.     {
    66.         case 0b00100001:                            //Column 1 Row 4--Switch 12
    67.             RA0 = 1;
    68.             lcd_goto(0x00);
    69.             lcd_puts("Switch 1");
    70.             DelayS(1);
    71.             lcd_clear();
    72.             break;
    73.         case 0b01000001:                            //Column 1 Row 3--Switch 11
    74.             //RA1 = 1;
    75.             lcd_goto(0x00);
    76.             lcd_puts("Switch 2");
    77.             DelayS(1);
    78.             lcd_clear();
    79.             break;
    80.         case 0b00010001:                            //Column 1 Row 2--Switch 10
    81.             //RA2 = 1;
    82.             lcd_goto(0x00);
    83.             lcd_puts("Switch 3");
    84.             DelayS(1);
    85.             lcd_clear();
    86.             break;
    87.         case 0b00001001:                            //Column 1 Row 1--Switch 9
    88.             lcd_goto(0x00);
    89.             lcd_puts("Switch 4");
    90.             DelayS(1);
    91.             lcd_clear();
    92.             break;
    93.         case 0b00100010:                            //Column 2 Row 4--Switch 8
    94.             lcd_goto(0x00);
    95.             lcd_puts("Switch 5");
    96.             DelayS(1);
    97.             lcd_clear();
    98.             break;
    99.         case 0b01000010:                            //Column 2 Row 3--Switch 7
    100.             lcd_goto(0x00);
    101.             lcd_puts("Switch 6");
    102.             DelayS(1);
    103.             lcd_clear();
    104.             break;
    105.         case 0b00010010:                            //Column 2 Row 2--Switch 6
    106.             lcd_goto(0x00);
    107.             lcd_puts("Switch 7");
    108.             DelayS(1);
    109.             lcd_clear();
    110.             break;
    111.         case 0b00001010:                            //Column 2 Row 1--Switch 5
    112.             lcd_goto(0x00);
    113.             lcd_puts("Switch 8");
    114.             DelayS(1);
    115.             lcd_clear();
    116.             break;
    117.         case 0b10100000:                            //Column 3 Row 4--Switch 4
    118.             lcd_goto(0x00);
    119.             lcd_puts("Switch 9");
    120.             DelayS(1);
    121.             lcd_clear();
    122.             break;
    123.         case 0b11000000:                            //Column 3 Row 3--Switch 3
    124.             lcd_goto(0x00);
    125.             lcd_puts("Switch 10");
    126.             DelayS(1);
    127.             lcd_clear();
    128.             break;
    129.         case 0b10010000:                            //Column 3 Row 2--Switch 2
    130.             lcd_goto(0x00);
    131.             lcd_puts("Switch 11");
    132.             DelayS(1);
    133.             lcd_clear();
    134.             break;
    135.         case 0b10001000:                            //Column 3 Row 1--Switch 1
    136.             lcd_goto(0x00);
    137.             lcd_puts("Switch 12");
    138.             DelayS(1);
    139.             lcd_clear();
    140.             break;
    141.     }
    142.  
    143.  
    144. }
    145. }
    146.  
    Here is my LCD code
    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>
    3. #include "lcd.h"
    4.  
    5. void pause(unsigned short usvalue);
    6.  
    7. #define     LCD_RS      RC1
    8. #define     LCD_RW      RC2                                    
    9. #define     LCD_EN      RC0
    10. #define     LCD_DATA    PORTC
    11. #define     LCD_STROBE()    ((LCD_EN = 1),(LCD_EN = 0))
    12.  
    13. void
    14. lcd_write (unsigned char c)
    15. {
    16.                                         //(cccc) = clearing bit - this is what is LCD_STROBE()
    17.                                         //(kkkk) = keeping bit - this is kept because of LCD_RS, LCD_RW, LCD_EN
    18.  
    19.    
    20.                         //(& 0bcccckkkk) clearing top nibble of LCD_DATA and keeping bottom nibble of LCD_DATA
    21.                     //(| 0bkkkkcccc) filling top nibble of LCD_DATA with top nibble of c
    22.                             //sending top nibble to LCD
    23.                         //clearing top nibble of LCD_DATA and keeping bottom nibble of LCD_DATA
    24.             //moving bottom nibble of c to high bits of LCD_DATA
    25.                             //sending top nibble of LCD_DATA to LCD
    26.  
    27.     pause(1);
    28.     LCD_DATA = (LCD_DATA & 0x0F) + (c & 0xF0);                             
    29.     LCD_STROBE();
    30.     LCD_DATA = (LCD_DATA & 0x0F) + ((c & 0x0F) << 4);                              
    31.     LCD_STROBE();
    32.  
    33.  
    34. }
    35.  
    36. void
    37. lcd_clear(void)
    38. {
    39.     LCD_RS = 0;
    40.     lcd_write (0x1);
    41.     pause(2);
    42. }
    43.  
    44. void
    45. lcd_puts(const char *s)
    46. {
    47.     LCD_RS = 1;
    48.     while (*s)
    49.         lcd_write(*s++);
    50. }
    51.  
    52. void
    53. lcd_putch(char c)
    54. {
    55.     LCD_RS = 1;
    56.     lcd_write(c);
    57. }
    58.  
    59. void
    60. lcd_goto(unsigned char pos)
    61. {
    62.     LCD_RS = 0;
    63.     lcd_write (0x80 + pos);
    64. }
    65.  
    66. void
    67. lcd_init()
    68. {
    69.     TRISC = 0;
    70.     LCD_EN = 0;                                                  
    71.     LCD_RS = 0;
    72.     LCD_RW = 0;
    73.  
    74.     pause(15);
    75.    
    76.     LCD_DATA &=0x0F;
    77.     LCD_DATA = 0x30;
    78.     LCD_STROBE();
    79.    
    80.     pause(5);
    81.     LCD_STROBE();
    82.    
    83.     pause(1);
    84.     LCD_STROBE();
    85.    
    86.     pause(5);
    87.     LCD_DATA &= 0x0F;
    88.     LCD_DATA = 0x20;
    89.     LCD_STROBE();
    90.    
    91.  
    92.     lcd_write (0x28);                                      
    93.     lcd_write (0x0F);                                      
    94.     lcd_clear();                   
    95.     lcd_write(0x06);                                           
    96. }
    97.  
     
  7. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    Yes that is definitely a good sign, it is even a good sign that your code for initializing it is working, since usually before initializing it for a 2 line display, it will only show 1 row of "boxes"....

    Try adding a line of code after initializing it to test it if is working properly :
    Code ( (Unknown Language)):
    1. lcd_write("Hello World!");
     
  8. beeson76

    Thread Starter Member

    Apr 19, 2010
    185
    1
    Should I have DB0 to DB3 tied to ground? I am using the LCD in 4 bit mode therefore I have nothing on DB0 to DB3.
     
  9. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    yes just connect those to ground....
     
  10. beeson76

    Thread Starter Member

    Apr 19, 2010
    185
    1
    Well I guess I made a mistake. I am not getting 2 rows of black boxes. It is still at one row of black boxes. So does that mean that the LCD is not inializing.
     
  11. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    yes, that is correct. Usually when the LCD is first powered up its controller defaults to a 1 line display, after you initialize it for a 2 line then it will recognize the 2nd line....

    If I remember correctly from your other post, you were trying to swap the 4 bits the LCD was actually connected to, did you wire the LCD data bits D4 to D7 to those I/O pins you are using in the code?? Normally in a standard configuration, the LCD will use the upper nibble of the port, D4 to D7 will connect to that ports D4 to D7

    and also I believe you are using the other 4 data bits to control the LCD's control pins, which I believe will be affected anytime data is written to the port. In my experience, I always use those other four IO lines as inputs, then they are not affected by writing to the port, and I just use another port to control the LCD's RS, R/W and E control lines.....

    I believe if you try this, you will see what I am talking about:

    Set the control line RS to high and write anything to the port, you will see after writing to the port that RS will now be low, since half of the ports data is masked with 0000, which will turn all outputs on those 4 I/O lines to go low........

    B. Morse
     
    Last edited: Jul 8, 2010
  12. beeson76

    Thread Starter Member

    Apr 19, 2010
    185
    1
    Here are a few pictures of what I took. Hopefully they help. If you need more, please let me know.
     
  13. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    Ok, after looking at your pics, and if I remember correctly, you had swapped nibbles on the port and moved all the control lines from PortA to the lower nibble of PortC which is also the data for the LCD.....

    The way you have it wired up, (except for the control lines) is exactly how the program should work without the modifications you did to swapping the upper and lower nibble, you need to revert back to the original LCD code and move your control lines to another port, and then just use those pins as inputs so they are not affected by the write to the port.

    B. Morse
     
  14. beeson76

    Thread Starter Member

    Apr 19, 2010
    185
    1
    To answer your first question. I am using d4-d7 on my upper nibble of my port (Port C Pins 4-7). If you can see from the picture I have D4-7 from my LCD (colors red, blue, yellow, and black) going to my Port C Pins 4-7. Again I am using the PIC16F886. I have simulated the code with Real Pic Simulator 1.1 and everything appears to be fine. I sure hope that isn't the problem. I guess I got in over my head trying to keep all the data and control lines on the same Port.

    Back to the pictures all pink wires are normally ground and gray are 5 volt. My Pins 4(brown) ,5(purple) ,6(light brown) on my LCD are hooked to pins RC0-RC2 of my chip.
     
  15. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    I tried to do the same with a PIC16F887 and a pic16F628A, and I ran into that write to port issue where it kept changing my control lines to low, so I moved the control lines to another port and it worked fine with Hi-Techs LCD demo code without modifications to the actual code....

    B. Morse
     
  16. beeson76

    Thread Starter Member

    Apr 19, 2010
    185
    1
    Thanks for the replies BMorse. I appreciate it. If I am understanding you correctly, if I would revert back to my old code and have PortA as my control lines and PortC as my data lines, I would need to make set PortA as inputs? Is that correct? PortC would be outputs wouldn't it?
     
  17. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234

    You only have to set the pins you are using on PORTA as outputs (the control lines (RS, R/W,E) are inputs to the LCD, so they should be outputs on the PIC), and only have to set the upper 4 bits of PORTC as outputs to write data to the LCD (the ones connected to D4 to D7 of the LCD), you can do what ever you want with the rest of the pins on those ports......


    B. Morse
     
  18. beeson76

    Thread Starter Member

    Apr 19, 2010
    185
    1
    I am going to try to use your suggestions. Do you think I should keep the modified LCD code and just change RS/RW/E to PORTA Pins 0,1,2. If I do that then I should not have to change anything except just rewiring from the Pins 0,1,2 of PORTC to PORTA. Is that correct?
     
  19. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234

    That could work, but didn't you also swap the nibbles being sent to the LCD port? If you did, it still may not work....

    B. Morse
     
  20. rajbex

    New Member

    Nov 2, 2009
    22
    0
Loading...