Lcd

Discussion in 'Programmer's Corner' started by musica07, Jul 22, 2008.

  1. musica07

    Thread Starter Member

    May 28, 2008
    13
    0
    Hello. I have some problem with my LCD c codes. It is an 8x2 character display. The codes can be builded successfully in the MP lab program but the characters do not show on the LCD. I tried troubleshooting but could not find the problem. Below is my LCD codes. Any help is greatly appreciated:)

    #include<p18f2620.h>
    #include<delays.h>
    #include<string.h>

    void Init_LCD(void);
    void W_LCD(char,char);
    void Out_LCD(char rom near*);
    void Out_Dec_LCD(unsigned int i);
    void Out_Hex_LCD(unsigned int i);
    void Puts_LCD(char *);
    char Hex2ascii(char);
    void LCD_Intro(void);
    void disp_val(void);
    void Delay_msec(unsigned int);
    void Delay_sec(unsigned int);

    #define LCD_DATA PORTB
    #define LCD_E PORTBbits.RB1
    #define LCD_RS PORTBbits.RB0

    #define PB1 PORTAbits.RA4

    #define ROM_TYPE (char rom near*)
    #define OSC_FREQ 32000000
    #define DELAY1MS 8 // 1ms delay = OSC_FREQ/(4*1000*1000)
    #define DELAY1S 200 // 1s delay = OSC_FREQ/(4*10000*4)

    #define LCD_Line1 W_LCD(0x80,0)
    #define LCD_Line2 W_LCD(0xC0,0)
    #define CLEAR_LCD W_LCD(0x01,0)
    #define HOME_LCD W_LCD(0x02,0)

    unsigned char LCD_TEMP;
    unsigned int i,x,y;

    void main()
    {
    TRISA = 0b11111111;
    TRISB = 0b00001100; // PORTB=>LCD_DATA, RB0=>LCD_E, RB1=>LCD_RS;

    while(1)
    {
    if(PB1==1)
    {
    Init_LCD();
    LCD_Intro();

    // Prompt user to put on cuff
    W_LCD(0x81,0);
    Out_LCD(ROM_TYPE "Put on");
    W_LCD(0xC2,0);
    Out_LCD(ROM_TYPE "cuff");
    Delay_sec(5); // Wait for 5 seconds
    W_LCD(0b00000001,0);

    for(x=10; x>0; x--)
    {
    W_LCD(0x81,0);
    Out_LCD(ROM_TYPE "s");
    Delay_sec(1);
    W_LCD(0b00000001,0);
    }

    for(y=60; y>0; y--)
    {
    W_LCD(0x81,0);
    Out_LCD(ROM_TYPE "Taking");
    W_LCD(0xC1,0);
    Out_LCD(ROM_TYPE "your BP");
    Delay_msec(1000); // Wait for 60 seconds
    W_LCD(0b00000001,0);
    W_LCD(0x81,0);
    Out_LCD(ROM_TYPE " ");
    W_LCD(0x81,0);
    Out_LCD(ROM_TYPE " ");
    Delay_msec(1000);
    }
    W_LCD(0x83,0);
    Out_LCD(ROM_TYPE "BP");
    W_LCD(0xC2,0);
    Out_LCD(ROM_TYPE "Taken");
    Delay_sec(3); // Wait for 3 seconds
    W_LCD(0b00000001,0);
    disp_val(); // Display BP measurements
    }
    }
    }
    // Initialise LCD
    void Init_LCD() // LCD display Initialization
    {
    W_LCD(0x03 ,0);
    Delay_msec(15);
    W_LCD(0x02 ,0);
    Delay_msec(5);
    W_LCD(0x28 ,0);
    Delay_msec(1);
    W_LCD(0x0C ,0);
    Delay_msec(1);
    W_LCD(0x06 ,0);
    Delay_msec(1);
    W_LCD(0x01 ,0);
    Delay_msec(5);
    }
    void W_LCD(char x, char i)
    {
    LCD_RS = i;
    LCD_TEMP = x;
    LCD_TEMP &= 0xF0;
    LCD_E = 1;
    LCD_DATA = LCD_TEMP;
    Delay_msec(1);
    LCD_E = 0;
    Delay_msec(1);
    LCD_TEMP = x;
    LCD_TEMP <<= 4;
    LCD_E = 1;
    LCD_DATA = LCD_TEMP;
    Delay_msec(1);
    LCD_E = 0;
    Delay_msec(1);
    }
    // Display const message on LCD
    void Out_LCD(char rom near *message)
    {
    while (*message)
    {
    W_LCD(*message,1);
    message++;
    }
    }

    // Print decimal number to LCD
    void Out_Dec_LCD(unsigned int i)
    {
    char digit, zero, value;
    long denom;
    denom = 1000000000;
    zero = 1;
    if (!value)
    W_LCD('0',1);
    else
    {
    while (denom)
    {
    digit = value/denom;
    if (digit)
    {
    W_LCD(digit + '0',1);
    zero = 0;
    }
    else if (!zero)
    W_LCD('0',1);
    value -= digit*denom;
    denom /= 10;
    }
    }
    }

    // Print hexadecimal number to LCD
    void Out_Hex_LCD(unsigned int i)
    {
    unsigned char ctmp0, ctmp1, ctmp2, ctmp3;
    ctmp0 = 0x000f & i;
    i >>= 4;
    ctmp1 = 0x000f & i;
    i >>= 4;
    ctmp2 = 0x000f & i;
    i >>= 4;
    ctmp3 = 0x000f & i;
    W_LCD(Hex2ascii(ctmp3),1);
    W_LCD(Hex2ascii(ctmp2),1);
    W_LCD(Hex2ascii(ctmp1),1);
    W_LCD(Hex2ascii(ctmp0),1);
    }

    // Convert hexadecimal number to ASCII character
    char Hex2ascii(char c)
    {
    c &= 0x07;
    if(c < 0x0a)
    return (c + 0x30);
    else
    return (c + 0x37);
    }

    // Output variable string to LCD
    void Puts_LCD(char *message)
    {
    while (*message)
    {
    W_LCD(*message,1);
    message++;
    }
    }

    // Output initialisation messages
    void LCD_Intro(void)
    {
    CLEAR_LCD;
    W_LCD(0x80,0);
    Out_LCD(ROM_TYPE "TEMASEK");
    W_LCD(0xC2,0);
    Out_LCD(ROM_TYPE "POLY");
    Delay_sec(2);
    W_LCD(0b00000001,0);

    W_LCD(0x80,0);
    Out_LCD(ROM_TYPE "PORTABLE");
    W_LCD(0x80,0);
    Out_LCD(ROM_TYPE "BP METER");
    Delay_sec(2);
    W_LCD(0b00000001,0);
    }

    // Display user's bp measurement
    void disp_val()
    {
    while(1)
    {
    W_LCD(0x82,0);
    Out_LCD(ROM_TYPE "SYS");
    W_LCD(0xC0,0);
    Out_LCD(ROM_TYPE "mmHg");
    Delay_sec(4);
    W_LCD(0b00000001,0);
    W_LCD(0x82,0);
    Out_LCD(ROM_TYPE "DIAS");
    W_LCD(0xC0,0);
    Out_LCD(ROM_TYPE "mmHg");
    Delay_sec(4);
    W_LCD(0b00000001,0);
    W_LCD(0x81,0);
    Out_LCD(ROM_TYPE "PULSE");
    W_LCD(0xC0,0);
    Out_LCD(ROM_TYPE "bpm");
    Delay_sec(4);
    W_LCD(0b00000001,0);
    }
    }

    // Delay multiple number of msec
    void Delay_msec(unsigned int delayInMSEC)
    {
    while (delayInMSEC--)
    Delay1KTCYx(DELAY1MS);
    }

    // Delay multiple number of sec
    void Delay_sec(unsigned int delayInSEC)
    {
    while (delayInSEC--)
    {
    Delay10KTCYx(DELAY1S);
    Delay10KTCYx(DELAY1S);
    Delay10KTCYx(DELAY1S);
    Delay10KTCYx(DELAY1S);
    }
    }
     
  2. musica07

    Thread Starter Member

    May 28, 2008
    13
    0
    thx for any help:)
    my lcd can work already.
     
  3. AllVol

    AAC Fanatic!

    Nov 22, 2005
    55
    2
    #include <system.h> ??
     
  4. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    I don't see where you are initializing the LCD display?

    This must be done before you can display anything on it.

    Have a look at this sample code:
    Code ( (Unknown Language)):
    1. /*
    2.  *  LCD interface example
    3.  *  Uses routines from delay.c
    4.  *  This code will interface to a standard LCD controller
    5.  *  like the Hitachi HD44780. It uses it in 4 bit mode, with
    6.  *  the hardware connected as follows (the standard 14 pin
    7.  *  LCD connector is used):
    8.  * 
    9.  *  PORTD bits 0-3 are connected to the LCD data bits 4-7 (high nibble)
    10.  *  PORTA bit 3 is connected to the LCD RS input (register select)
    11.  *  PORTA bit 1 is connected to the LCD EN bit (enable)
    12.  * 
    13.  *  To use these routines, set up the port I/O (TRISA, TRISD) then
    14.  *  call lcd_init(), then other routines as required.
    15.  * 
    16.  */
    17.  
    18. #include    <pic.h>
    19. #include    "lcd.h"
    20. #include    "delay.h"
    21.  
    22. #define LCD_RS RA3
    23. #define LCD_RW RA2
    24. #define LCD_EN RA1
    25.  
    26. #define LCD_DATA    PORTD
    27.  
    28. #define LCD_STROBE()    ((LCD_EN = 1),(LCD_EN=0))
    29.  
    30. /* write a byte to the LCD in 4 bit mode */
    31.  
    32. void
    33. lcd_write(unsigned char c)
    34. {
    35.     DelayUs(40);
    36.     LCD_DATA = ( ( c >> 4 ) & 0x0F );
    37.     LCD_STROBE();
    38.     LCD_DATA = ( c & 0x0F );
    39.     LCD_STROBE();
    40. }
    41.  
    42. /*
    43.  *  Clear and home the LCD
    44.  */
    45.  
    46. void
    47. lcd_clear(void)
    48. {
    49.     LCD_RS = 0;
    50.     lcd_write(0x1);
    51.     DelayMs(2);
    52. }
    53.  
    54. /* write a string of chars to the LCD */
    55.  
    56. void
    57. lcd_puts(const char * s)
    58. {
    59.     LCD_RS = 1; // write characters
    60.     while(*s)
    61.         lcd_write(*s++);
    62. }
    63.  
    64. /* write one character to the LCD */
    65.  
    66. void
    67. lcd_putch(char c)
    68. {
    69.     LCD_RS = 1; // write characters
    70.     lcd_write( c );
    71. }
    72.  
    73.  
    74. /*
    75.  * Go to the specified position
    76.  */
    77.  
    78. void
    79. lcd_goto(unsigned char pos)
    80. {
    81.     LCD_RS = 0;
    82.     lcd_write(0x80+pos);
    83. }
    84.    
    85. /* initialise the LCD - put into 4 bit mode */
    86. void
    87. lcd_init()
    88. {
    89.     char init_value;
    90.  
    91.     ADCON1 = 0x06;  // Disable analog pins on PORTA
    92.  
    93.     init_value = 0x3;
    94.     TRISA=0;
    95.     TRISD=0;
    96.     LCD_RS = 0;
    97.     LCD_EN = 0;
    98.     LCD_RW = 0;
    99.    
    100.     DelayMs(15);    // wait 15mSec after power applied,
    101.     LCD_DATA     = init_value;
    102.     LCD_STROBE();
    103.     DelayMs(5);
    104.     LCD_STROBE();
    105.     DelayUs(200);
    106.     LCD_STROBE();
    107.     DelayUs(200);
    108.     LCD_DATA = 2;   // Four bit mode
    109.     LCD_STROBE();
    110.  
    111.     lcd_write(0x28); // Set interface length
    112.     lcd_write(0xF); // Display On, Cursor On, Cursor Blink
    113.     lcd_clear();    // Clear screen
    114.     lcd_write(0x6); // Set entry Mode
    115. }
    116.  
     
    Last edited: Aug 3, 2008
Loading...