4 bit LCD interfacing with atmega 32

Status
Not open for further replies.

anjumawasthi

New Member
plzz help me in LCD interfacing with atmega 32. i have attached my code. it is displaying some garbage not the content i have given. kindly help me to rectify the problem..
hardware is:
rs->PORTd7
RW->gnd(0)
E->PORTD6
DB7-DB4->PD5-PD2

Code:
// Program to interface LCD in 4 bit mode with AVR microcontroller
#define F_CPU 12000000UL
#include<avr/io.h>
#include<util/delay.h>
#include<inttypes.h>

#define rs PD7

#define en PD6

void lcd_init();
void dis_cmd(char);
void dis_data(char);
void lcdcmd(char);
void lcddata(char);

int main(void)
{
unsigned char data0[15]="hello world";
unsigned char data1[15]="AVR ATMEGA32";

int i=0;
DDRD=0xFF;
lcd_init();

while(data0!='\0')
{
dis_data(data0);
_delay_ms(200);
i++;
}

dis_cmd(0xC5);

i=0;
while(data1!='\0')
{
dis_data(data1);
_delay_ms(200);
i++;
}

while(1);
}



void lcd_init() // fuction for intialize
{
dis_cmd(0x20); // to initialize LCD in 4-bit mode.
dis_cmd(0x28); //to initialize LCD in 2 lines, 5X7 dots and 4bit mode.
dis_cmd(0x01);
dis_cmd(0x0C);
dis_cmd(0x06);
dis_cmd(0x80);
}

void dis_cmd(char cmd_value)//function to mask the upper and lower nibble
//DB7 DB6 DB% DB4 is connected to PORTD(5,4,3,2)PINS
{
char cmd_value1;

cmd_value1 = cmd_value & 0xF0; //mask lower nibble because PA4-PA7 pins are used.
cmd_value1=(cmd_value1>>2);
lcdcmd(cmd_value1); // send to LCD

cmd_value1 = ((cmd_value<<4) & 0xF0); //shift 4-bit and mask
cmd_value1=(cmd_value1>>2);
lcdcmd(cmd_value1); // send to LCD
}


void dis_data(char data_value)
{
char data_value1;

data_value1=data_value&0xF0;

data_value1=(data_value1>>2);

lcddata(data_value1);

data_value1=((data_value<<4)&0xF0);
data_value1=(data_value1>>2);
lcddata(data_value1);
}

void lcdcmd(char cmdout)//sending commands to lcd
{
PORTD&=~(1<<rs);
PORTD=cmdout;
PORTD|=(1<<en);
_delay_ms(1);
PORTD&=~(1<<en);
}

void lcddata(char dataout)//sending data to lcd
{
PORTD|=(1<<rs);
PORTD=dataout;


PORTD|=(1<<en);
_delay_ms(1);
PORTD&=~(1<<en);
}
 
Try the source code from our project here
**broken link removed** (at the end of the page you can download the source code)

We do it with 4 bits, works like a charm on many many LCDs.

best of luck
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…