LCD 4 Bit Interfacing with AVR -Atmega16
After interfacing with 8051 and PIC16f877 am providing AVR interfacing to LCD ,with a light changing in the previous code (ClikHere) .
/*
* LCD4bit.c
*
* Created: 16-Sep-15 10:22:26 PM
* Author: Krishna
*/
#define F_CPU 12000000UL
#include <avr/io.h>
#include<util/delay.h>
#define LCDPORT PORTD // Renaming the PORTD to LCDPORT
#define RS PD2 // Renaming the RS pin to 2
#define E PD3 // Renaming the E to number 3
#define LINE2 lcd_cmd(0xc0);
void latch(void) // used to a high to low pulse the pin E
{
PORTD |= ~(1<<E); // here we give a high to PORTD.3
_delay_ms(1);
PORTD |= (1<<E); // Here we give a LOW to PORTD.#
}
void lcd_cmd(unsigned char c) // used to send the command / Instruction to the lcd port
{
PORTD |= ~(1<<RS); // send a '0' value to select to send command
_delay_ms(1);
LCDPORT = c & 0xf0; // send the command c only 4 bit by masking the lower bit
latch();
_delay_ms(1);
LCDPORT = (c << 4); // giving the lowerbit by shifting the 4 bit to left
latch();
}
void gotoxy(unsigned char x,unsigned char y)
{
if(x<40)
{
if(y) x|=0b01000000;
x|=0b10000000;
lcd_cmd(x);
}
void lcd_data(unsigned char c)
{
PORTD |= (1<<RS); // send 1 to send data
_delay_ms(1);
LCDPORT = c & 0xf0| 0x4; //send the data only 4 bit by masking the lower bit and also making the RS pin high by giving 0x04 .
_delay_ms(1);
latch();
LCDPORT = (c << 4)| 0x4; ; // giving the lower bit by shifting the 4 bit to left
latch();
}
void lcd_init()
{
_delay_ms(20);
lcd_cmd(0x30); //as per data sheet
_delay_ms(20);
lcd_cmd(0x30); //as per data sheet
_delay_ms(4);
lcd_cmd(0x32); //as per data sheet
_delay_ms(4);
lcd_cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels)
lcd_cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels)
lcd_cmd(0x0c); // Make cursorinvisible
lcd_cmd(0x6); // Set entry Mode(auto increment of cursor)
}
void string(const char *q) // used to send single charcter to display the lcd
{
while (*q) {
lcd_data(*q++);
}
}
void main(){
DDRD = 0xFF;
lcd_init();
string("Lcd Testing ");
LINE2
string("its displaying ");
while(1);
}
Circuit Diagram
Downloa here the Full project including Proteus Click here