The Godfather talking
Share your stuff or I will make you regret it.
Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 04, 2016, 05:27:54 17:27


Login with username, password and session length


Pages: [1]
Print
Author Topic: need LCD (2 by 16) sample program in keil in C  (Read 2990 times)
0 Members and 1 Guest are viewing this topic.
glenndr_15
Active Member
***
Offline Offline

Posts: 138

Thank You
-Given: 19
-Receive: 72



« on: June 14, 2009, 01:08:47 01:08 »

Greetings!
I tried to program the LCD sample n the book of mazidi, but it's not working. can someone share a program for LCD 2 by 16 written in C using keil for 8051?

best regards,
glenndr_15
« Last Edit: June 14, 2009, 01:41:23 01:41 by glenndr_15 » Logged
nambinh
Newbie
*
Offline Offline

Posts: 9

Thank You
-Given: 24
-Receive: 6


« Reply #1 on: June 14, 2009, 01:51:45 01:51 »

Code:
#ifndef __LCD_H__
#define __LCD_H__

void lcd_init(void);
void delay();
void control_word(unsigned char ch);
void data_word(unsigned char ch);
void wrt_value(unsigned char x);
void cursorxy(unsigned char x, unsigned char y);
void disp_str(unsigned char msg[]);
void wrt_string(unsigned char *string);
void wrt_long(unsigned long a);
void wrt_char(unsigned char a);
void wrt_int(unsigned int a);
void wrt_dec_char(unsigned char x);
void wrt_dec_long(unsigned long a);
void wrt_dec_int(unsigned int a);
void wrt_2dec_place(unsigned char a);
void wrt_float(float a);

#endif



#include <reg51.h>
#define LCD P2 // lcd connected to this port

void delay()
{
unsigned char i;
for (i=0; i<255 ;i++);
}

void control_word(unsigned char ch)
{
unsigned char copy;

copy  = ch; //save a copy of it
ch = ch >> 4; //get the higher nibble
ch = ch & 0x0f; // -- do --
ch = ch | 0x20; // add E(bit 6) &  RS (bit 5)
LCD = ch; //out to lcd data port HIGHER nibble with E=1, RS=0

ch = ch & 0x0f; // make E=0, RS=0 with higher nibble
LCD = ch; //out to lcd data port higher nibble with E=0, RS=0

ch = copy; //get original byte
ch = ch & 0x0f; //to get lower nibble
ch = ch | 0x20; // E=1, RS=0
LCD = ch; //out to lcd data port LOWER nibble with E=1, RS=0

ch = ch & 0x0f; // make E=0, RS=0
LCD = ch; //out to lcd data port lower nibble with E=0, RS=0

delay(); // give some delay here
//
}

void data_word(unsigned char ch)
{
unsigned char copy;
copy  = ch; //save a copy of it
ch = ch >> 4; //get the higher nibble
ch = ch & 0x0f; // -- do --
ch = ch | 0x30; // add E(bit 6) &  RS (bit 5)
LCD = ch; //out to lcd data port HIGHER nibble with E=1, RS=1

ch = ch & 0x1f; // make E=0, RS=1 with higher nibble
LCD = ch; //out to lcd data port higher nibble with E=0, RS=1

ch = copy; //get original byte
ch = ch & 0x0f; //to get lower nibble
ch = ch | 0x30; // E=1, RS=1
LCD = ch; //out to lcd data port LOWER nibble with E=1, RS=1

ch = ch & 0x1f; // make E=0, RS=1
LCD = ch; //out to lcd data port lower nibble with E=0, RS=1

delay(); // give some delay here
}


void lcd_init(void)
{
control_word(0x28);// 4bit, 2lines, 5x7
control_word(0x28);// again
control_word(0x0E);// disp On, cursor On, no blink
control_word(0x01);// clear LCD
control_word(0x06);// CUR_RIGHT
control_word(0x80);// line 1 cur. position 0
}

void disp_str(unsigned char msg[])
{
unsigned char i;
while(msg[i] != '\0')
{
data_word(msg[i++]);
}
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Bringing Cursor to (X,Y) location of LCD
X -> 1,2
Y -> 1,16
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void cursorxy(unsigned char x, unsigned char y)
{
if((x<1||x>2)&&(y<1||y>16))
{
x=1;
y=1;
}
if(x == 1)
control_word(0x7F+y);
else
control_word(0xBF+y);
}

void wrt_value(unsigned char x)
{
unsigned char temp;
temp = x/0x10;
if(temp>9)
return;
else
data_word(temp+0x30);
temp = x%0x10;
if(temp>9)
return;
else
data_word(temp+0x30);
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Writing string to LCD
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void wrt_string(unsigned char *string)
{
delay();
while(*string)
data_word(*string++);
}
//-----------------------------------------------

void wrt_long(unsigned long a)
{
unsigned char i;
unsigned char b,c[8]={48,48,48,48,48,48,48,48};
for(i = 7;a > 0;i--)
{
b = a % 16;
if(b <= 9)
b = b + 48;
else
b = b + 55;
a =  a / 16;
c[i] = b;
}
for(i =0; i<=7;i++)
data_word(c[i]);
}

//-----------------------------------------------
void wrt_int(unsigned int a)
{
unsigned char i;
unsigned char b,c[4]={48,48,48,48};
for(i = 3;a > 0;i--)
{
b = a % 16;
if(b <= 9)
b = b + 48;
else
b = b + 55;
a =  a / 16;
c[i] = b;
}
for(i =0; i<=3;i++)
data_word(c[i]);
}

//-----------------------------------------------
void wrt_char(unsigned char x)
{
unsigned char temp;
temp = x/0x10;
if(temp>9)
data_word(temp+55);
else
data_word(temp+48);
temp = x%0x10;
if(temp>9)
data_word(temp+55);
else
data_word(temp+48);
}
//------------------------------------
void wrt_dec_char(unsigned char a)
{
unsigned char i;
unsigned char b,c[3]={48,48,48};
for(i = 2;a > 0;i--)
{
b = a % 10;
b = b + 48;

a =  a / 10;
c[i] = b;
}
for(i =0; i<=2;i++)
data_word(c[i]);
}
//-----------------------------------------------
void wrt_dec_long(unsigned long a)
{
unsigned char i;
unsigned char b,c[10]={48,48,48,48,48,48,48,48,48,48};
for(i = 9;a > 0;i--)
{
b = a % 10;
b = b + 48;

a =  a / 10;
c[i] = b;
}
for(i =0; i<=9;i++)
data_word(c[i]);
}

//-----------------------------------------------
void wrt_dec_int(unsigned int a)
{
unsigned char i;
unsigned char b,c[5]={48,48,48,48,48};
for(i = 4;a > 0;i--)   
{
b = a % 10;
b = b + 48;

a =  a / 10;
c[i] = b;
}
for(i =0; i<=4;i++)
data_word(c[i]);
}
//------------------------------------
void wrt_2dec_place(unsigned char a)
/* function to write the fractional part after decimal upto
two decimal places
INPUT : the decimal part as unsigned char */
{
unsigned char i;
unsigned char b,c[2]={48,48};
for(i = 1;a > 0;i--)
{
b = a % 10;
b = b + 48;

a =  a / 10;
c[i] = b;
}
for(i =0; i<=1;i++)
data_word(c[i]);
}
//------------------------------------
void wrt_float(float a)
/* function to write the floating point number upto
two decimal places
INPUT : floating point number
OUTPUT : xxxxx.yy   on lcd at current cursor position */
{
unsigned int i;
unsigned char j;

i = (unsigned int)a;
wrt_dec_int(i);

wrt_string(".");
j = (unsigned char)((a - i) * 100);

wrt_2dec_place(j);
}
//-----------------------------------------------

USE THE INSERT CODE BUTTON WHEN UPLOADING CODE
« Last Edit: June 18, 2009, 04:40:28 04:40 by bbarney » Logged
tsk_cable
Guest
« Reply #2 on: June 17, 2009, 09:51:30 21:51 »

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

Code:
#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();
}
Logged
deveshsamaiya
Newbie
*
 Muted
Offline Offline

Posts: 28

Thank You
-Given: 15
-Receive: 13


« Reply #3 on: August 18, 2009, 04:09:32 16:09 »

Code:
// 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]);

}

Logged

embedded@iit
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC