/****************************************************************
Project : LCD 8-bit interface Test
Author : kouchenan(amihomo) ([url]www.amihomo.persianblog.ir[/url])
Chip type : ATmega8535
Clock frequency : 4.000000 MHz
==========================================================
PORTC[0..7] ==> DB0..DB7 LCD data bus (pin 14-pin7)
LCD EN (pin 6) ==> PORTD.7
LCD RW (pin 5) ==> PORTD.6
LCD RS (pin 4) ==> PORTD.5
*******************************************************************/
#include <mega8535.h>
#include <string.h>
#include <delay.h>
#define EN PORTD.7
#define RW PORTD.6
#define RS PORTD.5
#define BF PINC.7 //busy flag
/*****************************************************************************/
/* LCD Commands ( Refer to LCD Data Sheet ) */
/* Standard command should work with most common devices */
/*****************************************************************************/
/* */
#define write_data 0x00 /* With RS = 1 */
#define clr_lcd 0x01 /* Clear Display */
#define ret_home 0x02 /* Cursor to Home position */
#define entry_mode 0x06 /* Normal entry mode */
#define entry_mode_sh 0x07 /* - with shift */
#define sys_set_4_bit 0x28 /* 4 bit data mode 2 line ( 5x7 font ) */
#define sys_set_8_bit 0x38 /* 8 bit data mode 2 line ( 5x7 font ) */
#define disp_on 0x0C /* Display ON */
#define disp_off 0x08 /* Cursor plus blink */
#define cursor_on 0x0E /* Switch Cursor ON */
#define cursor_on_blink 0x0F /* Switch Cursor ON ,Blink ON */
#define cursor_off 0x0C /* Switch Cursor OFF */
#define move_cursor_l 0x10 /* Move cursor one character left */
#define move_cursor_r 0x14 /* Move cursor one character right */
#define scrl_disp_l 0x18 /* Scroll display 1 character left (all lines)*/
#define scrl_disp_r 0x1E /* Scroll display 1 character left (all lines)*/
#define goto_line1 0x80 /* Line 1 position 1 */
#define goto_line2 0xC0 /* Line 2 position 1 */
/*****************************************************************************/
void waitLCD(void);
void writeLCD(unsigned char byte ,unsigned char rs); //rs=0 for commands, rs=1 for data
void putStr(unsigned char str[]);
void initLCD(void);
void gotoXY(unsigned char x ,unsigned char y);
//=====================================================
void main(void)
{
char msg[]="Hello World!This is an LCD test!";
DDRC=0xff;
PORTC=0xff; //Set all data pins to 1 initially
DDRD=0xff;
initLCD();
putStr(msg);
while (1)
{
//loop forever
}
}
void initLCD(void)
{
//delay_ms(45);
writeLCD(sys_set_8_bit,0);
writeLCD(cursor_on_blink,0);
writeLCD(clr_lcd,0);
writeLCD(entry_mode,0);
}
void writeLCD(unsigned char byte ,unsigned char rs)
{
DDRC=0xff;
PORTC=byte;
RS=rs; //rs=0 for commands, rs=1 for data
RW=0;
EN=1;
EN=0;
waitLCD();
}
//monitor the busy flag of LCD on its DB7
void waitLCD(void)
{
unsigned char busy=1;
//unsigned char cnt=0;
do
{
DDRC=0xff;
PORTC=0xff; //Set all data pins to 1 initially
DDRC=0x00; //configure all bits as inputs
EN=0; //Start LCD command
RS=0; //It's a command
RW=1; //It's a read command
EN=1;
busy=BF; //read the busy flag
//cnt++;
} while(busy==1);// && cnt<255);
EN=0; //Finish the command
RW=0; //Turn off RW for future commands
}
void putStr(unsigned char str[])
{
unsigned char i;
for (i=0;i<strlen(str);i++)
{
if (i>15)
gotoXY(1,i-16);
writeLCD(str[i],1);
}
}
void gotoXY(unsigned char x ,unsigned char y)
{
//x is 0 or 1
//y is between 0 and 0x0f
unsigned char pos;
pos=x ? (0x80+0x40+y):(0x80+y) ;
DDRC=0xff;
PORTC=pos;
RS=0;
RW=0;
EN=1;
EN=0;
waitLCD();
}