LCD PIC18f4550 help

Discussion in 'Embedded Systems and Microcontrollers' started by Alanayala, Nov 6, 2014.

  1. Alanayala

    Thread Starter New Member

    Apr 11, 2013
    2
    0
    Hi, i have been triying to initiate the lCD with the pic18f4550 but i can´t. I dont have and idea.
    I paste mi code, if anyone can help. Thanks
    Code (Text):
    1.  
    2. #include <p18f4550.h>
    3. #define E  PORTAbits.RA5
    4. #define RS PORTAbits.RA4
    5. #define D7 PORTAbits.RA0
    6. #define D6 PORTAbits.RA1
    7. #define D5 PORTAbits.RA2
    8. #define D4 PORTAbits.RA3
    9.  
    10. #define Clean_Display    0x01
    11. #define Go_Home        0x02
    12. #define Autoshift_right    0x07
    13. #define Autoshift_left    0x05
    14. #define All_On        0x0F
    15. #define Display_On    0x0C
    16. #define Cursor_On    0x0E
    17. #define _8Bit_2Lines    0x38
    18. #define _4Bit_2Lines    0x28
    19. #define _4bit_10dots    0x2C
    20. #define DDRAM_Home    0x80 //move cursor display
    21. #define DDRAM_Line2    0xA8 //move cursor display low bit
    22.  
    23. unsigned char line = 0;
    24.  
    25.  
    26.  
    27. void wait_ms(int tiempo_ms) //Función de esperar un segundo.
    28. {
    29.     int temp;
    30.     int x;
    31.     temp=tiempo_ms;
    32.     x=0; //variable utilizada para el tiempo
    33. //    INTCON2=0b00000100; //Option - configuracion del preescaler
    34. //    T0CON=0b00000001;
    35.     do
    36.         {
    37.         TMR0=6;         do{x=x+1;} while (x<550);
    38.         x=0;
    39.         temp=--temp;
    40.         }
    41.     while (temp!=0);
    42. }
    43.  
    44.  
    45. void LCD_Write_Nibble(unsigned char data) {
    46.     if (data & 8) {
    47.         D7 = 1;
    48.     } else {
    49.         D7 = 0;
    50.     }
    51.     if (data & 4) {
    52.         D6 = 1;
    53.     } else {
    54.         D6 = 0;
    55.     }
    56.     if (data & 2) {
    57.         D5 = 1;
    58.     } else {
    59.         D5 = 0;
    60.     }
    61.     if (data & 1) {
    62.         D4 = 1;
    63.     } else {
    64.         D4 = 0;
    65.     }
    66.     wait_ms(10);
    67.     E = 1;
    68.     wait_ms(20);
    69.     E = 0;
    70. }
    71.  
    72.  
    73. void LCD_Write_Byte (char data)
    74. {
    75.     LCD_Write_Nibble(data >> 4);
    76.     LCD_Write_Nibble(data);
    77. }
    78.  
    79. /*************************************************************************************************/
    80. /*********************                  exported Functions                    **********************/
    81. /*************************************************************************************************/
    82.  
    83. void LCD_Init(void) {
    84.     D4=0;
    85.     D5=0;
    86.     D6=0;
    87.     D7=0;
    88.     wait_ms(20);
    89.     E = 0;
    90.     RS = 0;
    91.     LCD_Write_Nibble(0x30);
    92.     wait_ms(30);
    93.     LCD_Write_Nibble(0x30);
    94.     wait_ms(30);
    95.     LCD_Write_Nibble(0x30);
    96.     wait_ms(30);
    97.     LCD_Write_Nibble(0x20);
    98.     LCD_Write_Byte(_4Bit_2Lines);     // Configure LDC for 4 bits
    99.     LCD_Write_Byte(Clean_Display);    // Clean display
    100.     LCD_Write_Byte(Cursor_On);        // Turn on display, cursor
    101.     LCD_Write_Byte(DDRAM_Home);        // Set the DDRAM position to
    102. }
    103.  
    104. void LCD_Skip_Line(void) {
    105.     RS = 0;
    106.     if(line) {
    107.         LCD_Write_Byte(DDRAM_Home);
    108.         line = 0;
    109.     } else {
    110.         LCD_Write_Byte(DDRAM_Line2);
    111.         line = 1;
    112.     }
    113. }
    114.  
    115. char LCD_Get_Line() {
    116.     return line;
    117. }
    118.  
    119. void LCD_Write_String(char data) {
    120.     RS = 1;
    121.     LCD_Write_Byte(data);
    122. }
    123.  
    124.  
    125. unsigned char mensaje[]={"Hola mundo"};
    126.  
    127. void main (void){
    128. ADCON1=0x0F;
    129. wait_ms(200);
    130. unsigned char i;
    131.     TRISD=0;
    132.     TRISB=0;
    133.     D4=0;
    134.     D5=0;
    135.     D6=0;
    136.     D7=0;
    137.     E=0;
    138.     RS=0;
    139.     wait_ms(5);
    140.     LCD_Init();
    141.     wait_ms(5);
    142.  
    143.         i=0;
    144.        do
    145.     {
    146.         LCD_Write_String(mensaje[i++]);
    147.     } while (mensaje[I]!=0);
    148. do{}
    149. while(1);
    150. }
    151.  
     
    Last edited by a moderator: Nov 7, 2014
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    Assuming the "lCD" means an alphanumeric display...

    These devices must be initialized before you use them, and you can't do that before they complete their internal init stuff.

    A very common error is to have the required delays during set up too short.

    This task is very tricky and probably best not to be attempted until you are more proficient at C. There is lots of sample code on the net to copy, (some good, some bad).

    The one section of code I noted will fail is
    Code (Text):
    1.  
    2. do
    3. {
    4.    LCD_Write_String(mensaje[i++]);
    5. } while (mensaje!=0);
    6.  
    as "while (mensaje!=0)" compares the address of the array to zero

    This is what you meant:
    Code (Text):
    1.  
    2. do
    3. {
    4.    LCD_Write_String(mensaje[i++]);
    5. } while (mensaje[i]!=0);
    6.  
    And this is how I would write the same thing with a "while" loop as opposed to a "do...while" loop:
    Code (Text):
    1.  
    2. while (mensaje[i]) LCD_Write_String(mensaje[i++]);
    3.  
     
  3. MrChips

    Moderator

    Oct 2, 2009
    12,452
    3,371
    The bracketed 'I' got interpreted as italics tag. Next time use code tags.

    Why not create a better LCD_Write_String( ) function that does the job:

    Code (Text):
    1.  
    2. void LCD_Write_String(char *s)
    3.   {
    4.     RS = 1;
    5.     while(*s) LCD_Write_Byte(*s++);
    6.   }
    7.  
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,452
    3,371
    Here is my LCD initialization sequence from my blog:

    Code (Text):
    1.  
    2. void LCD_init(void)
    3. {
    4.   RS = 0;
    5.   LCD_Write_Byte(0x28); // dual line, 4 bits
    6.   LCD_Write_Byte(0x06); // increment mode
    7.   LCD_Write_Byte(0x0C); // cursor turned off
    8.   LCD_Write_Byte(0x10); // cursor right
    9.   LCD_Write_Byte(0x01); // clear display
    10. }
    11.  
     
Loading...