Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help to change this code to read on a LCD. It needs to display 03 80 FF. vo

ID: 3536272 • Letter: N

Question

Need help to change this code to read on a LCD. It needs to display 03 80 FF.


void init_lcd4(void);   // LCD routines

void cmdwrt_HI(char);

void cmdwrt(char cmd);

void delay(int del);

void lcd_puts(char*); // display a null-term ascii string to lcd

void lcd_putchar(char); // display a single ASCII-formatted char to lcd

void lcd_printd(int); // display a signed integer variable as decimal to lcd

/************ LCD routines **********/

void init_lcd4(void){ // on-board Seiko LCD in 4-bit mode, 24 MHz cpu

   int i;

   DDRK = 0xff; // write-only

   for(i=0;i<12;i++){

      cmdwrt_HI(init_commands[i]); // send commands to lcd, one-by-one

   }

}

void lcd_md(char *pt, int num){

    cmdwrt(0xC0); // cursor to beginning of 2nd line

    while(num){

        lcd_print8(*pt);   // print it (without 0x's)

        lcd_puts(" ");   // print spaces between

        pt++;

        num--;

    }

}

void lcd_print8(char var){ // displays char variable as hex to lcd (no 0x)

    char varcopy = var;

    var = var>>4;    // high nibble 1st

    var &= 0x0F; // mask off upper nibble to avoid arithmetic shift of sign bits

    if(var > 9)

       var += 0x37;

    else

       var += 0x30;

    lcd_putchar(var);

    varcopy &= 0x0F;   // low nibble 2nd

    if(varcopy > 9)

       varcopy += 0x37;

    else

       varcopy += 0x30;

    lcd_putchar(varcopy);

}

void lcd_putchar(char ascii){ // RS = 1, character write

    PORTK = (((ascii >> 2) & 0x3c) | 1); // high nibble, EN = 0, RS = 1

    PORTK |= 2; // EN = 1

    PORTK &= 0x3d; // EN = 0

    delay(1);

    PORTK = (((ascii << 2) & 0x3c) | 1); // low nibble, EN = 0, RS = 1

    PORTK |= 2; // EN = 1

    PORTK &= 0x3d; // EN = 0

    delay(1);

}

void cmdwrt(char cmd){ // RS = 0, command write

    PORTK = ((cmd >> 2) & 0x3c); // high nibble, EN = 0, RS = 0

    PORTK |= 2; // EN = 1

    PORTK &= 0x3c; // EN = 0

    delay(1);

    PORTK = ((cmd << 2) & 0x3c); // low nibble, EN = 0, RS = 0

    PORTK |= 2; // EN = 1

    PORTK &= 0x3c; // EN = 0

    delay(1);

}

void cmdwrt_HI(char initcmd){ // use for LCD initialization only

    PORTK = (initcmd >> 2) & 0x3c; // high nibble only, EN = 0, RS = 0

    PORTK |= 2; // EN = 1

    PORTK &= 0x3c; // EN = 0

    delay(5);   // extra delay when waking-up LCD

}

void delay(int del){ // delay for 'del' msecs

    int i,j;

      for(i=0; i<del; i++)

         for(j=0; j<2000; j++);

}

Explanation / Answer

void init_lcd4(void); // LCD routines

void cmdwrt_HI(char);

void cmdwrt(char);

void delay(int);

void lcd_puts(char*); // display null-term ascii string to lcd

void lcd_putchar(char);

void lcd_printd(int); // display signed integer variable as decimal to lcd


/************ LCD routines **********/


void init_lcd4(void){ // on-board Seiko LCD in 4-bit mode, 24 MHz cpu

int i;

DDRK = 0xff; // write-only

for(i=0;i<12;i++){

cmdwrt_HI(init_commands[i]); // send commands to lcd, one-by-one

}

}


void lcd_printd(int val){ // displays hex integer as decimal to lcd

int valcopy; // abbreviations: R. = remainder, Q. = quotient

char dig[5] = {0}; // storage for R.'s

int i = 0; // index into array called dig[ ]

if(val == 0)

lcd_putchar('0'); // statements below do nothing if val==0

if(val < 0){

val = ((~val)+1); // get hex magnitude if negative and...

lcd_putchar('-'); // ...display minus sign in front   

}

valcopy = val; // successive /10 to generate R.'s

while(val){ // keep looping until Q. drops to 0

val /= 10; // 1st Q., next Q., etc.

dig[i++] = (char)(valcopy % 10); // 1st R., store it, next R..etc.

valcopy = val; // copy of 1st Q., copy of next Q., etc.

}

while(i){ // convert & output remainders (digits), last to first

dig[--i] += 0x30; // convert to ASCII numeral and...

lcd_putchar(dig[i]); // send to lcd until dig[0] is sent out

}

}


void lcd_puts(char *ptr){ // string write

while(*ptr != 0){

lcd_putchar(*ptr);

ptr++;

}

}


void lcd_putchar(char ascii){ // RS = 1, character write


PORTK = (((ascii >> 2) & 0x3c) | 1); // high nibble, EN = 0, RS = 1

PORTK |= 2; // EN = 1

PORTK &= 0x3d; // EN = 0

delay(1);


PORTK = (((ascii << 2) & 0x3c) | 1); // low nibble, EN = 0, RS = 1

PORTK |= 2; // EN = 1

PORTK &= 0x3d; // EN = 0

delay(1);


}


void cmdwrt(char cmd){ // RS = 0, command write


PORTK = ((cmd >> 2) & 0x3c); // high nibble, EN = 0, RS = 0

PORTK |= 2; // EN = 1

PORTK &= 0x3c; // EN = 0

delay(1);


PORTK = ((cmd << 2) & 0x3c); // low nibble, EN = 0, RS = 0

PORTK |= 2; // EN = 1

PORTK &= 0x3c; // EN = 0

delay(1);


}


void cmdwrt_HI(char initcmd){ // use for LCD initialization only

PORTK = (initcmd >> 2) & 0x3c; // high nibble only, EN = 0, RS = 0

PORTK |= 2; // EN = 1

PORTK &= 0x3c; // EN = 0

delay(5); // extra delay when waking-up LCD

}


void delay(int del){ // delay for 'del' msecs

int i,j;

for(i=0; i<del; i++)

for(j=0; j<2000; j++);

}


/************* file end ****************/