LCD programming question

Thread Starter

cktboy

Joined Apr 24, 2020
45
hello,
I have the following LCD code which works where the ATmega32 PD4 to PD7 pins are connected to LCD D4 to D7 pins.
Code:
 void lcdcmd(unsigned char cmd){
      PORTC = (PORTC & 0x0F) | (cmd & 0xF0);  // send high nibble
      PORTB &= ~RS;    //send 0 to select command register
      PORTB |= E;        //send high
      _delay_ms(5);        //wait
      PORTB &= ~E;        //send low
      _delay_ms(5);        //wait
      
      PORTC = (PORTC & 0x0F) | (cmd<<4);    //send low nibble
       PORTB |= E;        //send high
      _delay_ms(5);        //wait
      PORTB &= ~E;        //send low
      _delay_ms(5);        //wait
      }
the connection is as shown,
1.jpg

I wanted to change the code to work with ATmega328p pins PC0 to PC3 connected to D4 to D7 as shown below.

2.jpg

what changes do I have to make in the above code? I changed to PORTC = (PORTC & 0x0F) | (cmd & 0xF0); to PORTC = (PORTC & 0xF0) | (cmd & 0xF0); but this does not work.
 

Thread Starter

cktboy

Joined Apr 24, 2020
45
after logging later i saw the reply to post the solution, here is what worked:
Code:
 void lcdcmd(unsigned char cmd){
      unsigned char cmdHigh = (cmd & 0xF0);
      unsigned char cmdLow = (cmd & 0x0F);
      PORTC = (cmdHigh>>4);
      //PORTC = (PORTC & 0xF0) | (cmd & 0x0F);  // send high nibble
      PORTB &= ~RS;    //send 0 to select command register
      PORTB |= E;        //send high
      T2delayms(5);        //wait
      PORTB &= ~E;        //send low
      T2delayms(5);        //wait
        
      PORTC = cmdLow;
      //PORTC = (PORTC & 0xF0) | (cmd<<4);    //send low nibble
       PORTB |= E;        //send high
      T2delayms(5);        //wait
      PORTB &= ~E;        //send low
        T2delayms(5);        //wait
      }
 
Top