LCD interfacing with AVR microcontroller This tutorial is taken from the book
MY EXPERIENCE IN PROGRAMMING AVR MICROCONTROLLER
IN C USING WINAVR/AVRGCC.
you can download it free from the files
section(FILE-AVRBOOK.RAR) of yahoo group-booksbybibinhttp://groups.yahoo.com/group/booksbybibin/
comments about this book on
www.booksbybibin.blogspot.comINTERFACING LCD WITH AVR
LCD used: JHD162A
This is the simple schematic without using backlight and contrast pins. Here control pins are connected to PORTD and datapins to PORTA. See the figure below.
/*----------------------------------------------------------------
-----------------HEADER FILES-------------------------------------
-----------------------------------------------------------------*/
#include<avr/io.h>
/*----------------------------------------------------------------
-------------CONNECTION BETWEEN LCD AND ATMEGA32-----------------
-----------------------------------------------------------------*/
#define DATA_DDR DDRA
#define DATA_PORT PORTA
#define CONTROL_DDR DDRD
#define CONTROL_PORT PORTD
#define Enable_Pin 6
#define RegSelect_Pin 4
#define ReadWrite_Pin 5
#define CONTROL_MASK 0X70
/*----------------------------------------------------------------
-------------CONTROL BITS OF LCD --------------------------------
-----------------------------------------------------------------*/
#define Set_Enable CONTROL_PORT|=_BV(Enable_Pin)
#define Clear_Enable CONTROL_PORT&=~_BV(Enable_Pin)
#define Write_Lcd CONTROL_PORT&=~_BV(ReadWrite_Pin)
#define Read_Lcd CONTROL_PORT|=_BV(ReadWrite_Pin)
#define Select_InstructionRegister CONTROL_PORT&=~_BV(RegSelect_Pin)
#define Select_DataRegister CONTROL_PORT|=_BV(RegSelect_Pin)
#define Data_Lcd(a) DATA_PORT=a
#define delay(a) delay_ms(a)
/*----------------------------------------------------------------
-----------------FUNCTIONS---------------------------------------
-----------------------------------------------------------------*/
void Init_Ports(void);
void Init_Lcd(void);
void delay_ms(unsigned char time_ms);
void Lcd_Send(unsigned char a);
/*----------------------------------------------------------------
----------------MAIN FUNCTION--------------------------------------
-----------------------------------------------------------------*/
void main()
{
int len,i;
char string[] = {'B','I','B','I','N',' ','J','O','H','N'};
Init_Ports();
Init_Lcd();
len=10;
for (i=0; i< len;i++)
Lcd_Send(string[i]);
}
/*----------------------------------------------------------------
-----------------SEND A CHARACTER TO LCD-------------------------
-----------------------------------------------------------------*/
void Lcd_Send(unsigned char a)
{
Select_DataRegister;
Write_Lcd;
Data_Lcd(a);
Set_Enable;
delay(30);
Clear_Enable;
delay(30);
}
/*----------------------------------------------------------------
-----------------FUNCTIONS TO INITIALIZE PORTS--------------------
-----------------------------------------------------------------*/
void Init_Ports(void)
{
DATA_DDR=0XFF; //Setting data port for output
CONTROL_DDR=CONTROL_MASK;//setting selected pins of control port for output
CONTROL_PORT&=~(_BV(Enable_Pin)|_BV(RegSelect_Pin )|_BV(ReadWrite_Pin)); //setting values to 0 at starting
}
/*----------------------------------------------------------------
------------FUNCTION TO INITIATE LCD -----------------------------
-----------------------------------------------------------------*/
void Init_Lcd(void)
{
char init[10];
int i;
init[0] = 0x01;
init[1] = 0x38;
init[2] = 0x0c;
init[3] = 0x06;
init[4] = 0x80;
Select_InstructionRegister;
Write_Lcd;
for(i=0;i<5;i++)
{
Data_Lcd(init[i]);
Set_Enable;
delay(30);
Clear_Enable;
delay(30);
}
}
/*----------------------------------------------------------------
--------FUNCTION TO GENERATE DELAY IN MILLISECONDS----------------
-----------------------------------------------------------------*/
void delay_ms(unsigned char time_ms)
{
unsigned short delay_count = F_CPU / 400;
unsigned short cnt;
asm volatile ("\n"
"L_dl1%=:\n\t"
"mov %A0, %A2\n\t"
"mov %B0, %B2\n"
"L_dl2%=:\n\t"
"sbiw %A0, 1\n\t"
"brne L_dl2%=\n\t"
"dec %1\n\t" "brne L_dl1%=\n\t":"=&w" (cnt)
:"r"(time_ms), "r"((unsigned short) (delay_count))
);
}
Folder:lcd(yahoo group-booksbybibin, file-avrbook.rar)
/*----------------------------------------------------------------
-------------CONNECTION BETWEEN LCD AND ATMEGA32-----------------
-----------------------------------------------------------------*/
#define DATA_DDR DDRA
#define DATA_PORT PORTA
#define CONTROL_DDR DDRD
#define CONTROL_PORT PORTD
#define Enable_Pin 6
#define RegSelect_Pin 4
#define ReadWrite_Pin 5
#define CONTROL_MASK 0X70
Here are the declaration I am using. Here CONTROL_MASK is used so that remaining pins can be used for other purpose. Change the port settings here so that you can use same code for all ports. When you program you should program in such a way that port pins are not wasted and same program can be used irrespective of ports. So do this at the beginning of the program for better understanding of the program and portability. Practice this when you do programming. Rest program remain same as
that I had given my previous book for parallel port.
Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.