LCD interface in 4 bit mode.

Discussion in 'Embedded Systems and Microcontrollers' started by ak52, Feb 24, 2015.

  1. ak52

    Thread Starter Member

    Oct 15, 2014
    145
    4
    Hello everyone,
    I am trying to interface a JHD204A display in a dspic33 controller.
    I have a few hardware issues ,connecting the LCD

    Firstly the dspic33 operates with 3.3v.
    The operating voltage of the LCD is 5v.
    So is it ok if my Enable,RS,and data lines operates at 3.3v or should i level shift it upto 5v?
    I drew a rough sketch of the circuit which i am going to implement on a bread board.
    Capture.PNG

    Secondly my data bus is not in sequence,so i created a function to write each bit individually.Please have a look at the code and please point out if i am missing anything.
    Code (Text):
    1.  
    2. #define EN LATBbits.LATB8
    3. #define RS LATBbits.LATB9
    4.  
    5. #define D4 LATBbits.LATB10
    6. #define D5 LATBbits.LATB11
    7. #define D6 LATAbits.LATA0
    8. #define D7 LATAbits.LATA1
    9.  
    10. void data(char ch)
    11. {
    12.    if(ch & 1)
    13.      D4 = 1;
    14.    else
    15.      D4 = 0;
    16.  
    17.    if(ch & 2)
    18.      D5 = 1;
    19.    else
    20.      D5 = 0;
    21.  
    22.    if(ch & 4)
    23.      D6 = 1;
    24.    else
    25.      D6 = 0;
    26.  
    27.    if(ch & 8)
    28.      D7 = 1;
    29.    else
    30.      D7 = 0;
    31. }
    32.  
    I am stuck ,as the LCD is showing black square boxes only :(
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    When using a 3.3V controller and a 5V display it *may* work without level translation, but I prefer to do the translation.

    An emitter follower as you show for RW and E will not work as it lowers the voltage rather than raising it. A simple inverting grounded emitter transistor amp will work fine, you just have to remember to invert the data driving it.

    When presented with the same issue on a 3.3V PIC I put the display on 5V tolerant pins and just used a pull up resistor as the level shifter: the pin set as input is a one, or the pin set as zero output is a zero. So just load the latch with zeros and use the TRIS as the one/zero control.
     
  3. ak52

    Thread Starter Member

    Oct 15, 2014
    145
    4
    Hello Ernie,thank you for your reply,but a few things still confuse me,there is no emitter follower for the RW signal,i assume you mean RS signal.I have an extra OPA2314 ,is it ok if i use that opamp for the RS and Enable signals.?
    I will try to shift all the signals to 5v tolerant pins.
    Could you please share a code spinet ,it could give me an idea on the software part of it.
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    Yeah, RS not RW.

    An op amp seems too complicated though probably workable.

    I believe you can find level shifting chips that will do all 7 bits (8 actually) in one package.

    For pull ups on 5V tollerant pins:

    Code (Text):
    1. #define EN TRISBbits.TRISB8
    2. #define RS TRISBbits.TRISB9
    3.  
    4. #define D4 TRISBbits.TRISB10
    5. #define D5 TRISBbits.TRISB11
    6. #define D6 TRISAbits.TRISA0
    7. #define D7 TRISAbits.TRISA1
    8.  
    9.  
    10. // set all the latches low
    11. LATBbits.LATB8 = 0;    //EN
    12. LATBbits.LATB9 = 0;    //RS
    13.  
    14. LATBbits.LATB10 = 0;    //D4
    15. LATBbits.LATB11 = 0;    //D5
    16. LATAbits.LATA0 = 0;    //D6
    17. LATAbits.LATA1 = 0;    //D7
    18.  
    19.  
    20. // write to TRIS to set states
    21. void data(char ch)
    22. {
    23.    if(ch & 1)
    24.      D4 = 1;
    25.    else
    26.      D4 = 0;
    27.  
    28.    if(ch & 2)
    29.      D5 = 1;
    30.    else
    31.      D5 = 0;
    32.  
    33.    if(ch & 4)
    34.      D6 = 1;
    35.    else
    36.      D6 = 0;
    37.  
    38.    if(ch & 8)
    39.      D7 = 1;
    40.    else
    41.      D7 = 0;
    42. }
    43.  
     
  5. ak52

    Thread Starter Member

    Oct 15, 2014
    145
    4
    Thanks for this, i will give it a try without the level shifter first,if it is working correctly i can eliminate the level shifter.
    So as of now i will omit the two emitter followers and add pull ups for all the LCD pins
    I will post the corrected schematic in here before i proceed.

    Also i observed in many 3v3 MCU(freescale's tower boards comes to mind) that RS and EN are directly connected to LCD display,without any pull ups or level shifters on them.So i am guessing that level shifters/pull ups are to be used only on data lines?

    Why is an op-amp more complicated.It can be used as a simple comparator with inverting terminal grounded and the non inverting terminal with the signal from the MCU.(The opamp is rail to rail and operates from 0 to 5v)
     
    Last edited: Feb 24, 2015
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    I would check (and not "guess") the display's spec for input voltage range before committing to driving 5V logic with a 3.3V signal. It may appear to work but I expect you are pushing the signal noise immunity margins at minimum.

    An op amp as you describe will not work. You also need an appropriate reference voltage to determine if the logic signal is low or high.
     
  7. BobTPH

    Active Member

    Jun 5, 2013
    789
    114
    If the LCD expects TTL signals, no level translation is needed. 3.3V will be seen as high.

    Bob
     
    KLillie likes this.
  8. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    Has anyone actually checked the spec yet?

    [​IMG]

    So feel free to drive it direct.
     
  9. ak52

    Thread Starter Member

    Oct 15, 2014
    145
    4
    Well i tried my level best ,but i still cant get the dam thing to work.(with pulls and without pull ups)
    I will post the full lcd code in here with the schematics.
    If some one can spot any abnormality please share it with me.
    LCD connections:
    Capture.PNG
    My MCU
    Data lines are RB10 to RB13
    Enable :RB4
    RS:RB9

    Capture2.PNG

    LCD used is JHD 204A
     
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    Could you add carrage returns to your test file so it can be read in notepad? Moar peoples can read it that way.

    I do note your lcd_init() routine is missing the very critical delays before any command and between commands.
     
  11. ak52

    Thread Starter Member

    Oct 15, 2014
    145
    4
    The file looks all messed up in notepad.,:eek: ,i copied the text in notepad++,i recommend switching to this as it is far more advanced :)
    I'm not able to attach a .c file for some reason.After downloading the file ,just change the extension to .c,and it can be viewed with any c compiler
    I'll indent the file when i get back to work,
    You mentioned some critical delays messing,could you please point out where exactly.

    Regards,
    AK
     
  12. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    And I recommend sticking to programs that you can expect everyone to have.

    Asked and answered:
    Check the spec for the display you are using as these can vary.
     
  13. MrChips

    Moderator

    Oct 2, 2009
    12,452
    3,371
  14. ak52

    Thread Starter Member

    Oct 15, 2014
    145
    4
    Mr ErnieM, i meant no disrespect,just shared my experience of notepad++ with you.

    Good news, i finally got the LCD operating correctly.
    The problem was quiet silly,on the bread board i had interchanged the RS pin with the Enable pin.
    Once i corrected that it worked.
    I tried two displays ,JHD162A and JHD204A,Both are working fine.

    @MrChips,thank you for that blog post,it was very informative.

    Thank you all once again,
    AK
     
    ErnieM likes this.
  15. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    Thanks for the update. So many people just disappear when they solve the problem.

    My thing is swapping the clock and data programming lines. I'm no better then random chance in about 20 builds (breadboards and even one PCB).

    I didn't take offense at the Notepad+ thing, hey, I was trying top be friendly folksy by saying "moar peoples". I think I used Notepad++ a while back but I've settled on Programmer's Notepad when viewing orphan code. But in either case that is only when I am at home, at work there are such incredibly tight restrictions on installing software that unless it is absolutely necessary I just do not bother.

    Congrats on this and good luck with your projects!
     
  16. MrChips

    Moderator

    Oct 2, 2009
    12,452
    3,371
    It happens to the best of us. On one of my first PCB builds I had the D0-D7 data lines swapped on a batch of commercially made boards.:mad:
     
Loading...