initialization lcd 44780 avr

Discussion in 'Embedded Systems and Microcontrollers' started by demsp, Jun 28, 2018.

  1. demsp

    Thread Starter New Member

    Jun 28, 2018
    2
    0
    Can i initialize 44780 like so
    Code (Text):
    1. #include <avr/io.h>
    2. #define F_CPU 16000000UL //16MHz
    3. #include <util/delay.h>
    4.  
    5. int main(void) {
    6. DDRD = 0b11111100;
    7. PORTD = 0b00000000;
    8. _delay_ms(20);   //delay 20ms
    9. PORTD = 0b0011 0000;   //PD4=1; PD5=1;
    10. PORTD = 0b0000 1000;   //E=1;
    11.  //_delay_us(10);
    12. PORTD = 0b0000 0000;   //E=0;
    13. _delay_us(40);   //delay 40us
    14. PORTD = 0b0000 1000;   //E=1;
    15. PORTD = 0b0000 0000;   //E=0;
    16. _delay_us(40);   //delay 40us
    17. PORTD = 0b0000 1000;   //E=1;
    18. PORTD = 0b0000 0000;   //E=0;
    19. _delay_us(40);   //delay 40us
    20. PORTD = 0b0010 0000;   //PD5=1;
    21. PORTD = 0b0000 1000;   //E=1;
    22. PORTD = 0b0000 0000;   //E=0;
    23. _delay_us(40);      
    24. // ...
    25. }
    A=PD2
    RS=PD3
    DB4=PD4
    DB5=PD5
    DB6=PD6
    DB7=PD7
     
  2. MrChips

    Moderator

    Oct 2, 2009
    19,138
    6,150
    Maybe, but that is very poor design.
    You need to do this many times, hence put it into a procedure.

    Write out in pencil, in plain language what needs to be done. Forget about writing code for now.
    Here is an example.

    Set R/W to 0
    Set RS to 0
    Send 4-bit data
    Send E = 1
    Send E = 0
    (Remember to do this without disturbing all other bits that have already been set to 0 or 1.)

    Now put all of this into a function that accepts an 8-bit input.
     
  3. demsp

    Thread Starter New Member

    Jun 28, 2018
    2
    0
    Thanks, but i have no R/W and RS, i just want to nitialize lcd, so is it right (in 4bit mode)?
    Code (Text):
    1.  
    2. int main(void) {
    3. DDRD = 0b11111100;
    4. PORTD = 0b00000000;
    5.  
    6. // at first write PD4=1 and PD5=1
    7. _delay_ms(15);         // delay 15ms (after power on)
    8. PORTD = 0b0000 1000;   // E=1;
    9. _delay_ms(4);         // delay 4ms
    10. PORTD = 0b0011 1000;   // PD4=1; PD5=1; E=1;
    11. _delay_ms(4);        // delay 4ms
    12. PORTD = 0b0011 0000;   //PD4=1; PD5=1; E=0; (write)
    13. _delay_ms(4);        // delay 4ms
    14. PORTD = 0b0000 0000;  //PD4=0; PD5=0;
    15.  
    16. //than write PD4=1 and PD5=1 again
    17. _delay_ms(4);        // delay 4ms
    18. PORTD = 0b0000 1000;   // E=1;
    19. _delay_ms(4);         // delay 4ms
    20. PORTD = 0b0011 1000;   // PD4=1; PD5=1; E=1;
    21. _delay_ms(4);        // delay 4ms
    22. PORTD = 0b0011 0000;   //PD4=1; PD5=1; E=0; (write)
    23. _delay_ms(4);        // delay 4ms
    24. PORTD = 0b0000 0000;  //PD4=0; PD5=0;
    25. //and so on...
    26.  
     
  4. jayanthd

    Active Member

    Jul 4, 2015
    902
    79
    4-bit HD44780 or caompatible Lcd Driver. Adjust strobe delay as needed.

    Code (Text):
    1.  
    2. #define _LCD_FIRST_ROW          0x80     //Move cursor to the 1st row
    3. #define _LCD_SECOND_ROW         0xC0     //Move cursor to the 2nd row
    4. #define _LCD_THIRD_ROW          0x94     //Move cursor to the 3rd row
    5. #define _LCD_FOURTH_ROW         0xD4     //Move cursor to the 4th row
    6. #define _LCD_CLEAR              0x01     //Clear display
    7. #define _LCD_RETURN_HOME        0x02     //Return cursor to home position, returns a
    8.                                          //shifted display to its original position.
    9.                                          //Display data RAM is unaffected.
    10. #define _LCD_CURSOR_OFF         0x0C     //Turn off cursor
    11. #define _LCD_UNDERLINE_ON       0x0E     //Underline cursor on
    12. #define _LCD_BLINK_CURSOR_ON    0x0F     //Blink cursor on
    13. #define _LCD_MOVE_CURSOR_LEFT   0x10     //Move cursor left without changing
    14.                                          //display data RAM
    15. #define _LCD_MOVE_CURSOR_RIGHT  0x14     //Move cursor right without changing
    16.                                          //display data RAM
    17. #define _LCD_TURN_ON            0x0C     //Turn Lcd display on
    18. #define _LCD_TURN_OFF           0x08     //Turn Lcd display off
    19. #define _LCD_SHIFT_LEFT         0x18     //Shift display left without changing
    20.                                          //display data RAM
    21. #define _LCD_SHIFT_RIGHT        0x1E     //Shift display right without changing
    22.                                          //display data RAM
    23.  
    24. #define EN_DELAY 300
    25. #define LCD_STROBE {LCD_EN = 1; Delay_us(EN_DELAY); LCD_EN = 0; Delay_us(EN_DELAY);};
    26.  
    27. #define LCD_RS at LATBbits.LATB0
    28. #define LCD_EN at LATBbits.LATB2
    29. #define LCD_D4 at LATBbits.LATB4
    30. #define LCD_D5 at LATBbits.LATB3
    31. #define LCD_D6 at LATBbits.LATB7
    32. #define LCD_D7 at LATBbits.LATB1
    33.  
    34. #define LCD_RS_Direction TRISBbits.TRISB0
    35. #define LCD_EN_Direction TRISBbits.TRISB2
    36. #define LCD_D4_Direction TRISBbits.TRISB4
    37. #define LCD_D5_Direction TRISBbits.TRISB3
    38. #define LCD_D6_Direction TRISBbits.TRISB7
    39. #define LCD_D7_Direction TRISBbits.TRISB1
    40.  
    41. void LCD_Cmd(char out_char) {
    42.  
    43.     LCD_RS = 0;
    44.  
    45.     LCD_D4 = (out_char & 0x10)?1:0;
    46.     LCD_D5 = (out_char & 0x20)?1:0;
    47.     LCD_D6 = (out_char & 0x40)?1:0;
    48.     LCD_D7 = (out_char & 0x80)?1:0;
    49.     LCD_STROBE
    50.     LCD_D4 = (out_char & 0x01)?1:0;
    51.     LCD_D5 = (out_char & 0x02)?1:0;
    52.     LCD_D6 = (out_char & 0x04)?1:0;
    53.     LCD_D7 = (out_char & 0x08)?1:0;
    54.     LCD_STROBE
    55.  
    56.     if(out_char == 0x01)Delay_ms(2);
    57. }
    58.  
    59. void LCD_Chr(char row, char column, char out_char) {
    60.  
    61.     switch(row){
    62.  
    63.         case 1:
    64.         LCD_Cmd(0x80 + (column - 1));
    65.         break;
    66.         case 2:
    67.         LCD_Cmd(0xC0 + (column - 1));
    68.         break;
    69.         case 3:
    70.         LCD_Cmd(0x94 + (column - 1));
    71.         break;
    72.         case 4:
    73.         LCD_Cmd(0xD4 + (column - 1));
    74.         break;
    75.     }
    76.  
    77.     LCD_RS = 1;
    78.  
    79.     LCD_D4 = (out_char & 0x10)?1:0;
    80.     LCD_D5 = (out_char & 0x20)?1:0;
    81.     LCD_D6 = (out_char & 0x40)?1:0;
    82.     LCD_D7 = (out_char & 0x80)?1:0;
    83.     LCD_STROBE
    84.  
    85.     LCD_D4 = (out_char & 0x01)?1:0;
    86.     LCD_D5 = (out_char & 0x02)?1:0;
    87.     LCD_D6 = (out_char & 0x04)?1:0;
    88.     LCD_D7 = (out_char & 0x08)?1:0;  
    89.     LCD_STROBE
    90. }
    91.  
    92. void LCD_Chr_Cp(char out_char) {
    93.  
    94.     LCD_RS = 1;
    95.  
    96.     LCD_D4 = (out_char & 0x10)?1:0;
    97.     LCD_D5 = (out_char & 0x20)?1:0;
    98.     LCD_D6 = (out_char & 0x40)?1:0;
    99.     LCD_D7 = (out_char & 0x80)?1:0;
    100.     LCD_STROBE
    101.  
    102.     LCD_D4 = (out_char & 0x01)?1:0;
    103.     LCD_D5 = (out_char & 0x02)?1:0;
    104.     LCD_D6 = (out_char & 0x04)?1:0;
    105.     LCD_D7 = (out_char & 0x08)?1:0;  
    106.     LCD_STROBE
    107. }
    108.  
    109. void LCD_Init() {
    110.  
    111.     Delay_ms(200);
    112.  
    113.     LCD_RS_Direction = 0;
    114.     LCD_EN_Direction = 0;
    115.     LCD_D4_Direction = 0;
    116.     LCD_D5_Direction = 0;
    117.     LCD_D6_Direction = 0;
    118.     LCD_D7_Direction = 0;
    119.    
    120.     LCD_RS = 0;
    121.     LCD_EN = 0;
    122.     LCD_D4 = 0;
    123.     LCD_D5 = 0;
    124.     LCD_D6 = 0;
    125.     LCD_D7 = 0;  
    126.    
    127.     Delay_ms(30);
    128.  
    129.     LCD_D4 = 1;
    130.     LCD_D5 = 1;
    131.     LCD_D6 = 0;
    132.     LCD_D7 = 0;
    133.  
    134.     LCD_STROBE
    135.  
    136.     Delay_ms(30);
    137.  
    138.     LCD_D4 = 1;
    139.     LCD_D5 = 1;
    140.     LCD_D6 = 0;
    141.     LCD_D7 = 0;
    142.  
    143.     LCD_STROBE
    144.  
    145.     Delay_ms(30);
    146.  
    147.     LCD_D4 = 1;
    148.     LCD_D5 = 1;
    149.     LCD_D6 = 0;
    150.     LCD_D7 = 0;
    151.  
    152.     LCD_STROBE
    153.  
    154.     Delay_ms(30);
    155.  
    156.     LCD_D4 = 0;
    157.     LCD_D5 = 1;
    158.     LCD_D6 = 0;
    159.     LCD_D7 = 0;
    160.  
    161.     LCD_STROBE
    162.  
    163.     Delay_ms(30);
    164.  
    165.     LCD_Cmd(0x28);
    166.     LCD_Cmd(0x06);
    167. }
    168.  
    169. void LCD_Out(char row, char col, char *text) {
    170.     while(*text)
    171.          LCD_Chr(row, col++, *text++);
    172. }
    173.  
    174. void LCD_Out_Cp(char *text) {
    175.     while(*text)
    176.          LCD_Chr_Cp(*text++);
    177. }
    178.  
    179. void main() {
    180.  
    181.     LCD_Init();
    182.     LCD_Cmd(_LCD_CURSOR_OFF);
    183.     LCD_Cmd(_LCD_CLEAR);
    184.     LCD_Out(1,1,"LCD 20X4");  
    185.     LCD_Out(2,1,"MCU ?");
    186.     LCD_Out(3,1,"PIC ?");
    187.     LCD_Out(4,1,"Working For You ?");
    188.  
    189.     while(1) {
    190.  
    191.     }
    192. }
    193.  
     
    demsp likes this.
  5. MrChips

    Moderator

    Oct 2, 2009
    19,138
    6,150
    The code you have is not going to work. You have too many errors and too many inconsistencies.
    You can get away with R/W = 0 but you will need RS.

    You are modifying DATA bits while trying to pulse E.
    You should assume that you might want to change pin assignments at some point.
    It is better to define pin assignments so that you can easily port your code to a different application.

    For example:

    Code (Text):
    1. #define LCD_PORT PORTD
    2. #define E PORTD_bits.BIT3
    3.  
    4. //Then you can write
    5.  
    6. LCD_PORT = data;
    7. E = 1;
    8. E = 0;
    Edit: The code posted by @jayanthd above is a good example on how to program for any pin assignments.
     
    demsp likes this.
Loading...