Sending lower nibble to LCD in C

fm101

Newbie level 5
hi,

I am trying to write C code for AVR microcontroller to send lower nibble via port B lower 4 pins as shown in figure below. My code to send data is also below but its not working. Can somebody tell me what i am doing wrong?

thanks

C:
  void lcdchar(uint8_t data){

PORTB = (PORTB & 0xF0) | (data & 0x0F);  // send low nibble
PORTC &= ~RW;    //send 0 for write operation
PORTC |= RS;    //send 1 to select data register
latch();

PORTB = (PORTB & 0xF0) | (data>>4);  // send high nibble
latch();

}

betwixt

Super Moderator
Staff member
It depends on what values you have assigned to RW and RS.
If they have been assigned their bit position in PORTC, use these lines instead:

PORTC &= ~(1 << RW); // this makes the RW bit go low while leaving other bits as they were.
PORTC |= (1 << RS); // this makes the RS bit go high without changing other bits.

Make sure you change the bits back if necessary afterwards. I assume your latch() function just pulses 'E' low for a few mS.

Brian.

KlausST

Super Moderator
Staff member
Hi,

Are you sure you keep timing requirements of the LCD?

Klaus

fm101

Newbie level 5
if this part of the code :
PORTB = (PORTB & 0xF0) | (data & 0x0F);
is right then i should have no problem, because i have same working code but different port and pins(higher pins 7,6,5,4). but still nothing gets displayed on LCD!

FvM

Super Moderator
Staff member
The posted code snippet looks right, but there may be errors in other parts of your application, e.g. port setup, latch() function, definition of IO bits.

Also is your code failing in real hardware or Proteus simulation?
--- Updated ---

I just realized that you implement wrong order of 4-bit nibbles. Correct order is high nibble first low nibble second.

Last edited:

Aussie Susan

Also in what way is it "...not working..."?
No data displayed?
Wrong data displayed?
Susan

fm101

Newbie level 5
hi,
the solution was to send low nibble first...for anyone in future

thanks everybody, FvM post hinted out in soln direction......