This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

do yOU need LCD (2 by 16) sample program in keil in C

Hi i'm kinda new here but this is my code for 16x2 Lcd
It was made with Keil uVision, Enjoy

#include <REGX52.H>

#define LCD_data P2
#define LCD_rs P1_0
#define LCD_rw P1_1
#define LCD_en P1_2


void LCD_busy(void);
void LCD_poweron(void);
void LCD_command(unsigned char var);
void LCD_senddata(unsigned char var);
void LCD_sendstring(unsigned char *var);
void LCD_init(void);

void main(void)
{
        unsigned char msg[] ="Learning";
        LCD_poweron();    // 15ms delay
                LCD_init();
                LCD_command (0x80);     // Set CGRAM adress,data is Avaible from uC
        LCD_sendstring(msg);
                while(1);
}

void LCD_busy()
{
    unsigned char i,j;
        for(i=0;i<50;i++)
                for(j=0;j<255;j++);
}
void LCD_poweron()
{
unsigned int i;
for (i=0;i<22500; i++);
}
void LCD_command(unsigned char var)
{
     LCD_data  = var;      //Function set: 2 Line, 8-bit, 5x8 dots
     LCD_rs   = 0;        //Selected command register
     LCD_rw   = 0;        //We are writing in instruction register
     LCD_en   = 1;        //Enable H->L
     LCD_en   = 0;
     LCD_busy();          //Wait for LCD to process the command
}

void LCD_sendstring(unsigned char *var)
{
     while(*var)              //till string ends
       LCD_senddata(*var++);  //send characters one by one
}

void LCD_senddata(unsigned char var)
{
     P2  = var;      //Function set: 2 Line, 8-bit, 5x7 dots
     LCD_rs   = 1;        //Selected data register
     LCD_rw   = 0;        //We are writing
     LCD_en   = 1;        //Enable H->L
     LCD_en   = 0;
     LCD_busy();          //Wait for LCD to process the command
}

void LCD_init(void)
{
     LCD_data = 0x38;     //Function set: 2 Line, 8-bit, 5x8 dots
     LCD_rs   = 0;        //Selected command register
     LCD_rw   = 0;        //We are writing in data register
     LCD_en   = 1;        //Enable H->L
     LCD_en   = 0;
     LCD_busy();          //Wait for LCD to process the command
     LCD_data  = 0x0F;     //Display on, Curson blinking command
     LCD_rs   = 0;        //Selected command register
     LCD_rw   = 0;        //We are writing in data register
     LCD_en   = 1;        //Enable H->L
     LCD_en   = 0;
     LCD_busy();          //Wait for LCD to process the command
     LCD_data  = 0x01;     //Clear LCD
     LCD_rs   = 0;        //Selected command register
     LCD_rw   = 0;        //We are writing in data register
     LCD_en   = 1;        //Enable H->L
     LCD_en   = 0;
     LCD_busy();          //Wait for LCD to process the command
     LCD_data  = 0x06;     //Entry mode, auto increment with no shift
     LCD_rs   = 0;        //Selected command register
     LCD_rw   = 0;        //We are writing in data register
     LCD_en   = 1;        //Enable H->L
         LCD_en   = 0;        //Enable H->L
     LCD_busy();
}

Parents
  • this is better.

    // LCD header file with standard functions to drive 16*2 alphanumeric LCD
    
    #define LCDPORT P2
    
    #define RS P2_0
    
    #define RW P2_1
    
    #define E  P2_2
    
    bit  status=0
    #define lcd_delay 60
    
    void delay(unsigned int j)
    
    {
    
    unsigned int i,k;
    
    for(i=0;i<j;i++)
    
    {
    
    for(k=0;k<100;k++);
    
    }
    
    }
    
    void _lcd_init_write(unsigned char a)
    
    {
    
    RS = 0;
    
    RW = 0;
    
    LCDPORT=a;
    
    E=1;
    
    delay(lcd_delay);
    
    E=0;
    
    }
    
    void lcd_com(unsigned char a){
    
    unsigned char temp;
    
    if(status){
    
    status=0;
    
     goto a;
    
     }
    
    RS=0;
    
    a:
    
    RW=0;
    
    temp=a;
    
    temp&=0xF0;
    
    LCDPORT&=0x0F;
    
    LCDPORT|=temp;
    
    E=1;
    
    delay(lcd_delay);
    
    E=0;
    
    temp=a<<4;
    
    temp&=0xF0;
    
    LCDPORT&=0x0F;
    
    LCDPORT|=temp;
    
    E=1;
    
    delay(lcd_delay);
    
    E=0;
    
    }
    
    void lcd_data(unsigned char a){
    
    status=1;
    
    RS=1;
    
    lcd_com(a);
    
    }
    
    void lcd_init(void){
    
    delay(lcd_delay);
    
    _lcd_init_write(0x30);
    
    delay(lcd_delay);
    
    _lcd_init_write(0x30);
    
    delay(lcd_delay);
    
    _lcd_init_write(0x30);
    
    delay(lcd_delay);
    
    _lcd_init_write(0x20);
    
    delay(lcd_delay);
    
    lcd_com(0x28);
    
    delay(lcd_delay);
    
    lcd_com(4);
    
    delay(lcd_delay);
    
    lcd_com(0x85);
    
    delay(lcd_delay);
    
    lcd_com(6);
    
    delay(lcd_delay);
    
    lcd_com(1);
    
    delay(lcd_delay);
    
    }
    
    void lcd_puts(char *aaa)
    
    {
    
    unsigned int i=0;
    
    for(;aaa[i]!=0;i++)lcd_data(aaa[i]);
    
    }
    

Reply
  • this is better.

    // LCD header file with standard functions to drive 16*2 alphanumeric LCD
    
    #define LCDPORT P2
    
    #define RS P2_0
    
    #define RW P2_1
    
    #define E  P2_2
    
    bit  status=0
    #define lcd_delay 60
    
    void delay(unsigned int j)
    
    {
    
    unsigned int i,k;
    
    for(i=0;i<j;i++)
    
    {
    
    for(k=0;k<100;k++);
    
    }
    
    }
    
    void _lcd_init_write(unsigned char a)
    
    {
    
    RS = 0;
    
    RW = 0;
    
    LCDPORT=a;
    
    E=1;
    
    delay(lcd_delay);
    
    E=0;
    
    }
    
    void lcd_com(unsigned char a){
    
    unsigned char temp;
    
    if(status){
    
    status=0;
    
     goto a;
    
     }
    
    RS=0;
    
    a:
    
    RW=0;
    
    temp=a;
    
    temp&=0xF0;
    
    LCDPORT&=0x0F;
    
    LCDPORT|=temp;
    
    E=1;
    
    delay(lcd_delay);
    
    E=0;
    
    temp=a<<4;
    
    temp&=0xF0;
    
    LCDPORT&=0x0F;
    
    LCDPORT|=temp;
    
    E=1;
    
    delay(lcd_delay);
    
    E=0;
    
    }
    
    void lcd_data(unsigned char a){
    
    status=1;
    
    RS=1;
    
    lcd_com(a);
    
    }
    
    void lcd_init(void){
    
    delay(lcd_delay);
    
    _lcd_init_write(0x30);
    
    delay(lcd_delay);
    
    _lcd_init_write(0x30);
    
    delay(lcd_delay);
    
    _lcd_init_write(0x30);
    
    delay(lcd_delay);
    
    _lcd_init_write(0x20);
    
    delay(lcd_delay);
    
    lcd_com(0x28);
    
    delay(lcd_delay);
    
    lcd_com(4);
    
    delay(lcd_delay);
    
    lcd_com(0x85);
    
    delay(lcd_delay);
    
    lcd_com(6);
    
    delay(lcd_delay);
    
    lcd_com(1);
    
    delay(lcd_delay);
    
    }
    
    void lcd_puts(char *aaa)
    
    {
    
    unsigned int i=0;
    
    for(;aaa[i]!=0;i++)lcd_data(aaa[i]);
    
    }
    

Children