#include <mega32.h>
#include <delay.h>
#include<stdio.h>
typedef unsigned char bit_8;
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
LCD Port Configuration
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#define LCD_PORT PORTD
#define rs PORTB.0
#define rw PORTB.1
#define en PORTB.2
#define flag PIND.7
#define led PORTB.1
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
LCD Command MACROS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#define LCD_CLEAR 0x01
#define RETURN_HOME 0X02
#define DEC_CURSOR 0x04
#define INC_CURSOR 0x06
#define DISP_OFF_CUR_OFF 0x08
#define DISP_OFF_CUR_ON 0x0A
#define DISP_ON_CUR_OFF 0x0C
#define DISP_ON_CUR_BLINK 0x0E
#define SHIFT_CUR_LEFT 0x10
#define SHIFT_CUR_RIGHT 0x14
#define SHIFT_DISP_LEFT 0x18
#define SHIFT_DISP_RIGHT 0x1C
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function Prototypes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void lcd_init(void); // Initialize LCD
void busy(void); // Check Busy
void wrt_cmd(bit_8); // Sending Command
void wrt_data(bit_8); // Sending single character
void wrt_string(bit_8 *); // Sending String
void wrt_value(bit_8); // Sending 8-bit Decimal value
void cursorxy(bit_8,bit_8); // Bringing Cursor to (X,Y) location X -> 1,2 and Y -> 1-16
unsigned char *flash buf="Hello";
void lcd_init(void)
{
wrt_cmd(0x38);
wrt_cmd(0x06);
wrt_cmd(0x08);
wrt_cmd(0x01);
wrt_cmd(0x0F);
wrt_cmd(0x02);
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
**** Cheacking the busy flag of LCD
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void busy()
{
// delay_ms(1000);
//flag=1;
PORTD=0x00;
DDRD=0x00;
rs=0;
rw=1;
led=1;
while(flag!=0)
{
en=0;
delay_ms(2);
en=1;
}
led=0;
PORTD=0xFF;
DDRD=0xFF;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Writing command to LCD
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void wrt_cmd(bit_8 val)
{
busy();
LCD_PORT=val;
rs=0;
rw=0;
en=1;
delay_ms(2);
en=0;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Writing string to LCD
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void wrt_string(char *string)
{
busy();
while(*string)
wrt_data(*string++);
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Writing data to LCD
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void wrt_data(bit_8 ch)
{
busy();
LCD_PORT = ch;
rs=1;
rw=0;
en=1;
delay_ms(2);
en=0;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Writing 8-bit Value to LCD
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void wrt_value(bit_8 x)
{
bit_8 temp;
temp = x/0x10;
if(temp>9)
return;
else
wrt_data(temp+0x30);
temp = x%0x10;
if(temp>9)
return;
else
wrt_data(temp+0x30);
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Bringing Cursor to (X,Y) location of LCD
X -> 1,2
Y -> 1,16
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void cursorxy(bit_8 x, bit_8 y)
{
if((x<1||x>2)&&(y<1||y>16))
{
x=1;
y=1;
}
if(x == 1)
wrt_cmd(0x7F+y);
else
wrt_cmd(0xBF+y);
}
void main()
{
PORTB=0x00;
DDRB=0x07;
//PORTD=0x00;
//DDRD=0x00;
delay_ms(20);
lcd_init();
while(1)
{
delay_ms(1000);
cursorxy(1,1);
//wrt_cmd(0x80);
//delay_ms(100);
//buf="Hello";
//wrt_string(buf);
//delay_ms(100);
//wrt_cmd(0xC0);
//delay_ms(100);
wrt_data('A');
cursorxy(1,3);
//delay_ms(100);
wrt_string("HELLO");
delay_ms(1000);
}
}