LCD interface in 4 bit mode.

Thread Starter

ak52

Joined Oct 15, 2014
230
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:
#define EN LATBbits.LATB8
#define RS LATBbits.LATB9

#define D4 LATBbits.LATB10
#define D5 LATBbits.LATB11
#define D6 LATAbits.LATA0
#define D7 LATAbits.LATA1

void data(char ch)
{
   if(ch & 1)
     D4 = 1;
   else
     D4 = 0;

   if(ch & 2)
     D5 = 1;
   else
     D5 = 0;

   if(ch & 4)
     D6 = 1;
   else
     D6 = 0;

   if(ch & 8)
     D7 = 1;
   else
     D7 = 0;
}
I am stuck ,as the LCD is showing black square boxes only :(
 

ErnieM

Joined Apr 24, 2011
8,377
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.
 

Thread Starter

ak52

Joined Oct 15, 2014
230
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.
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.
 

ErnieM

Joined Apr 24, 2011
8,377
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:
#define EN TRISBbits.TRISB8
#define RS TRISBbits.TRISB9
 
#define D4 TRISBbits.TRISB10
#define D5 TRISBbits.TRISB11
#define D6 TRISAbits.TRISA0
#define D7 TRISAbits.TRISA1
 
 
// set all the latches low
LATBbits.LATB8 = 0;    //EN
LATBbits.LATB9 = 0;    //RS
 
LATBbits.LATB10 = 0;    //D4
LATBbits.LATB11 = 0;    //D5
LATAbits.LATA0 = 0;    //D6
LATAbits.LATA1 = 0;    //D7
 
 
// write to TRIS to set states
void data(char ch)
{
   if(ch & 1)
     D4 = 1;
   else
     D4 = 0;
 
   if(ch & 2)
     D5 = 1;
   else
     D5 = 0;
 
   if(ch & 4)
     D6 = 1;
   else
     D6 = 0;
 
   if(ch & 8)
     D7 = 1;
   else
     D7 = 0;
}
 

Thread Starter

ak52

Joined Oct 15, 2014
230
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:

ErnieM

Joined Apr 24, 2011
8,377
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.
 

Thread Starter

ak52

Joined Oct 15, 2014
230
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
 

Attachments

ErnieM

Joined Apr 24, 2011
8,377
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.
 

Thread Starter

ak52

Joined Oct 15, 2014
230
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.
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
 

ErnieM

Joined Apr 24, 2011
8,377
i recommend switching to this as it is far more advanced
And I recommend sticking to programs that you can expect everyone to have.

You mentioned some critical delays messing,could you please point out where exactly.
Asked and answered:
I do note your lcd_init() routine is missing the very critical delays before any command and between commands.
Check the spec for the display you are using as these can vary.
 

Thread Starter

ak52

Joined Oct 15, 2014
230
And I recommend sticking to programs that you can expect everyone to have.
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

Joined Apr 24, 2011
8,377
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!
 

MrChips

Joined Oct 2, 2009
30,821
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:
 
Top