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(); }
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?
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]); }
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; ...
it works 150%
No, it's not - it's lost all the indentaion!
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.
plz give example with comments and details a better delay
have you tried to search for the word "delay" in the forum...?
See: www.8052.com/.../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.
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
good code .just what i need . we use hw_delay() for delays. but remember to include hw_delay.h what editor did u use?
good code
Oh, yeah.
i put this in the code
#include < hw_delay.h > but it did not work. what is wrong?
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?
In what way, precisely, did it "not work"?
"what is wrong?"
You have not supplied nearly enough information to even guess!
#include < hw_delay.h >
s/< /</ s/ >/>/