Discussion Forum

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

Next Thread | Thread List | Previous Thread Start a Thread | Settings

DetailsMessage
Read-Only
Author
tsk cable
Posted
18-Jun-2009 07:20 GMT
Toolset
C51
New! 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();
}
Read-Only
Author
erik malund
Posted
18-Jun-2009 15:02 GMT
Toolset
C51
New! worthless

1) there is a delay in C
2) no mentioning of processor/clock/optimization
3) no mention which brand of LCD

i.e. 95% of those that try it will not have it working

Erik

PS it IS nice to see someone posting properly formatted and indented code

Read-Only
Author
Dr khaksar
Posted
3-Jul-2009 07:17 GMT
Toolset
C51
New! RE: worthless

Nice to look on this code , i am try use it this project.
http://microcontroller51.blogspot.com/2009/02/interface-of-lcd-with-at89c51.html

Read-Only
Author
anand c
Posted
14-Aug-2009 15:45 GMT
Toolset
C51
New! RE: do yOU need LCD (2 by 16) sample program in keil in C

to the guy who posted the code...

the code looks fine..but its not working.i am not able to tfind the mistake.i tried using the 44780 lcd.is ur code lcd specific??

have u checked ur code using a uc?

Read-Only
Author
devesh shamea
Posted
19-Aug-2009 09:37 GMT
Toolset
C51
New! RE: do yOU need LCD (2 by 16) sample program in keil in C

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]);

}
Read-Only
Author
Per Westermark
Posted
19-Aug-2009 09:42 GMT
Toolset
C51
New! RE: do yOU need LCD (2 by 16) sample program in keil in C

This is better???

The use of the goto below indicates a lack of understanding of the conditional structures available in the language.

    ...
    if (status) {
        status = 0;
        goto a;
    }
    RS = 0;
a:
    RW = 0;
    ...
Read-Only
Author
devesh shamea
Posted
19-Aug-2009 09:45 GMT
Toolset
C51
New! RE: do yOU need LCD (2 by 16) sample program in keil in C

it works 150%

Read-Only
Author
erik malund
Posted
19-Aug-2009 11:36 GMT
Toolset
C51
New! correcting verbiage

it works 150%
should be
as far as I know it works with my chip, my LCD, my oscillator frequency, my wiring, my ... and if it does not work for you, so what, I saw no reason to help you by commenting.

Erik

Read-Only
Author
Andy Neil
Posted
19-Aug-2009 09:48 GMT
Toolset
None
New! RE: this is better.

No, it's not - it's lost all the indentaion!

Read-Only
Author
Per Westermark
Posted
19-Aug-2009 10:44 GMT
Toolset
None
New! RE: this is better.

Replacing one implementation that is using a broken delay function in C with another implementation that is also using a broken delay function in C isn't an improvement.

The function of the code will still depend on the processor speed, the used compiler (manufacturer + version) and optimization options.

Read-Only
Author
devesh shamea
Posted
19-Aug-2009 11:01 GMT
Toolset
None
New! RE: this is better.

plz give example with comments and details a better delay

Read-Only
Author
Tamir Michael
Posted
19-Aug-2009 11:10 GMT
Toolset
None
New! RE: this is better.

have you tried to search for the word "delay" in the forum...?

Read-Only
Author
Andy Neil
Posted
19-Aug-2009 11:10 GMT
Toolset
C51
New! example with comments and details a better delay

See: http://www.8052.com/forum/read/162556

In particular, the "And here's how" link.

Note that it still requires you to document the precise processor & clock speed used...

Note also that some processors can be configured into different clocking modes; eg "12-Clock", "6-Clock", etc. In such cases, that option would also need to be documented.

Read-Only
Author
anan c
Posted
19-Aug-2009 11:58 GMT
Toolset
C51
New! RE: example with comments and details a better delay

good code .just what i need . we use hw_delay() for delays. but remember to include hw_delay.h
what editor did u use?

Read-Only
Author
Tamir Michael
Posted
19-Aug-2009 12:01 GMT
Toolset
C51
New! RE: example with comments and details a better delay

good code

Oh, yeah.

Read-Only
Author
devesh shamea
Posted
19-Aug-2009 12:46 GMT
Toolset
C51
New! RE: example with comments and details a better delay

i put this in the code


#include < hw_delay.h >

but it did not work.

what is wrong?


Read-Only
Author
Tamir Michael
Posted
19-Aug-2009 12:49 GMT
Toolset
C51
New! RE: example with comments and details a better delay

the problem is that you are a student (I hope!!!) that does not know the basic of the C language. Did you cheat in your last C exam, too?

Read-Only
Author
Andy Neil
Posted
19-Aug-2009 12:50 GMT
Toolset
None
New! RE: it did not work

In what way, precisely, did it "not work"?

"what is wrong?"

You have not supplied nearly enough information to even guess!

Read-Only
Author
devesh shamea
Posted
19-Aug-2009 13:10 GMT
Toolset
None
New! RE: it did not work

it did not work .there was nothin g on the display.

Read-Only
Author
Andy Neil
Posted
19-Aug-2009 15:02 GMT
Toolset
None
New! RE: there was nothin g on the display

Did the project build & download successfully with no errors and no warnings?

Read-Only
Author
devesh shamea
Posted
19-Aug-2009 15:11 GMT
Toolset
None
New! RE: there was nothin g on the display

no. i get somethin about cannot find file.

Read-Only
Author
Andy Neil
Posted
19-Aug-2009 15:21 GMT
Toolset
None
New! RE: i get somethin about cannot find file

Well, Obviousy the code isn't going to work if it didn't even build - is it?!

Read-Only
Author
Dan Henry
Posted
19-Aug-2009 15:27 GMT
Toolset
None
New! RE: there was nothin g on the display

"i get somethin about cannot find file."

Someone should make a post recommending how to make the file findable.

Read-Only
Author
Dan Henry
Posted
19-Aug-2009 13:03 GMT
Toolset
C51
New! RE: example with comments and details a better delay
#include < hw_delay.h >
s/< /</
s/ >/>/
Read-Only
Author
Andy Neil
Posted
19-Aug-2009 13:42 GMT
Toolset
C51
New! RE: example with comments and details a better delay

Surely, if the problem were with the syntax of the #include, that would give compiler errors?

Read-Only
Author
Dan Henry
Posted
19-Aug-2009 15:29 GMT
Toolset
C51
New! RE: example with comments and details a better delay

Or even better:

s/< /"/
s/ >/"/
Read-Only
Author
Per Westermark
Posted
19-Aug-2009 15:46 GMT
Toolset
C51
New! RE: example with comments and details a better delay

User-supplied header files should use

#include "myfile.h"


since the compiler isn't expected to look in the system-supplied include directories for the file.

Read-Only
Author
devesh shamea
Posted
19-Aug-2009 15:46 GMT
Toolset
C51
New! RE: example with comments and details a better delay

i dont under stand.

Read-Only
Author
devesh shamea
Posted
19-Aug-2009 15:48 GMT
Toolset
C51
New! RE: example with comments and details a better delay

i think it cannot find hw_delay.h. it is not on my c drive. where is it?

Read-Only
Author
Per Westermark
Posted
19-Aug-2009 15:53 GMT
Toolset
C51
New! RE: example with comments and details a better delay

Is there a reason why it should be on your C: drive?

If no one have placed it there, I would find it quite more likely that it isn't on your C: drive...

Read-Only
Author
Tamir Michael
Posted
19-Aug-2009 15:53 GMT
Toolset
C51
New! RE: example with comments and details a better delay

where is it?

I have it!
Hipi!

Read-Only
Author
devesh shamea
Posted
19-Aug-2009 15:55 GMT
Toolset
C51
New! RE: example with comments and details a better delay

you have it . plz send to me now at d.shamea@hotmail.com

this is urgend

Read-Only
Author
Andy Neil
Posted
19-Aug-2009 15:56 GMT
Toolset
None
New! Back to basics

OK, you really need to go right back to basics and start again at Chapter 1 of your 'C' textbook.

A decent 'C' textbook will explain how to construct a 'C' program.

You could try: http://publications.gbdirect.co.uk/c_book/

Or see: http://www.keil.com/books

Read-Only
Author
Nikunj Panchal
Posted
17-Sep-2009 07:30 GMT
Toolset
C51
New! RE: do yOU need LCD (2 by 16) sample program in keil in C

hi.i am a new guy in this field. this is really a nice code. it works 100%. i have tested it.

Read-Only
Author
RAMCHI PAG
Posted
17-Sep-2009 16:49 GMT
Toolset
C51
New! RE: do yOU need LCD (2 by 16) sample program in keil in C

YES THE CODE IS VERY VERY VERY GOOD. I TRIED IT AND I SAY IT WORKS 257%

VERY VERY GOOD.

Read-Only
Author
Tamir Michael
Posted
17-Sep-2009 16:53 GMT
Toolset
C51
New! RE: do yOU need LCD (2 by 16) sample program in keil in C

VERY VERY VERY GOOD...IT WORKS 257%

Oh, yeah.

Read-Only
Author
Sammi Tucker
Posted
17-Sep-2009 17:53 GMT
Toolset
C51
New! RE: do yOU need LCD (2 by 16) sample program in keil in C

Thx 4 t code.

I can confirm. It works. 1000%

Next Thread | Thread List | Previous Thread Start a Thread | Settings