+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
LCD.c******************************************************************
/***********************************************
* Desciption: Code to initialize and use the 16x2 Hitachi interface LCD display in 4bit
mode
*
* my configuration(Bi-Mode)
* LCD_PORT0 - DB4
* LCD_PORT1 - DB5
* LCD_PORT2 - DB6
* LCD_PORT3 - DB7
* LCD_PORT4 - RS = 0x10
* LCD_PORT5 - r/w = 0x20
* LCD_PORT6 - EN = 0x40
*
* LCD_PORT: 7 6 5 4 3 2 1 0
* LCD_Wire: - EN RW RS DB7 DB6 DB5 DB4
*
* LCD obtained from junun.org
*
*R/W: you can ground it if you want to save a pin on the port. in the code it is not used =
GND
*
* RS: Register Select
* 0 - Command Register
* 1 - Data Register
**************************************************/
//the includes
#include "lcd.h"
#include <inttypes.h>
#include <avr/io.h>

void LCD_setDDR(void)
{
	LCD_DDR = 0xff;
}

void LCD_init(void)
{
//initialize the DDR
LCD_setDDR();
LCD_delay();
//initilize Command Register
LCD_sendCommand(0x33); //enable 4-bit mode
LCD_sendCommand(0x32);
LCD_sendCommand(0x2c); //enable 2-line mode
LCD_sendCommand(0x0f); //display, cursor, blink
LCD_sendCommand(0x01); //clear home
}

void LCD_delay(void)
{
uint16_t time1;
//for(time1 = 0; time1 < 2000; time1++);
for(time1 = 0; time1 < 65000; time1++);
for(time1 = 0; time1 < 65000; time1++);
}

void LCD_delayLong(void)
{
uint16_t i;
uint16_t k;
uint16_t var1 = 0;
for (i = 0; i < 30000; i++)
{
for (k = 0; k < 30000; k++)
{
var1 = 0;
}
}
}

void LCD_delayShort(void)
{
uint16_t i;
uint16_t k;
uint16_t var1 = 0;
for (i = 0; i < 15000; i++)
{
for (k = 0; k < 15000; k++)
{
var1 = 0;
}
}
34
}

void LCD_sendCommand(uint8_t val)
{
uint8_t temp = val; // first do the 4bit in DB and then do
the (-,E,RW,RS) section
temp &= 0x0f; // &= is equivalent to logic AND
val >>= 4; // >>= is equivalent to logic right shift value by 4 spaces
LCD_PORT = val;
LCD_delay();
LCD_PORT |= ENABLE; // |= is equivalent to logic OR
LCD_PORT &= ~ENABLE; // ~ is equivalent to logic NOT (aka. complement)
LCD_delay();
LCD_PORT = temp; // the other half of the bits
LCD_delay();
LCD_PORT |= ENABLE;
LCD_PORT &= ~ENABLE;
LCD_delay();
}

//&& =logic AND
// || = or, as in checking if either or condition is true
void LCD_sendString(char *s)
{
while (*s) LCD_sendByte(*s++); // ++ is equivalent to increment
}

void LCD_sendByte(uint8_t val)
{
uint8_t temp = val; // saving it first
val >>= 4; // taking what is in DBs
val |= RS; // set data mode/
LCD_PORT = val;
LCD_delay();
LCD_PORT |= ENABLE;
LCD_PORT &= ~ENABLE;
temp &= 0x0f;
temp |= RS; // set data mode
LCD_PORT = temp;
LCD_delay();
LCD_PORT |= ENABLE;
LCD_PORT &= ~ENABLE;
LCD_delay();
}

void LCD_clearScreen(void)
{
LCD_delay();
LCD_sendCommand(0x01);
LCD_delay();
}
void LCD_home(void)//brings blinker 1space to the left = on top of the the previous char
{
LCD_sendCommand(0x10);
}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
LCD.h******************************************************************
/***********************************************
* Title: LCD.h
* Desciption: Code to initialize and use the 16x2 Hitachi interface LCD display in 4bit
mode
*
* LCD_PORT0 - DB4
* LCD_PORT1 - DB5
* LCD_PORT2 - DB6
* LCD_PORT3 - DB7
* LCD_PORT4 - RS = 0x10
* LCD_PORT5 - r/w = 0x20
* LCD_PORT6 - EN = 0x40
*
* LCD_PORT: 7 6 5 4 3 2 1 0
* LCD_Wire: - EN RW RS DB7 DB6 DB5 DB4
*
* LCD obtained from junun.org
*
*R/W: you can ground it if you want to save a pin on the port. in the code it is not used =
GND
*
* RS: Register Select
* 0 - Command Register
* 1 - Data Register
**************************************************/
/*..................Includes........................*/
#include <inttypes.h>
#include <avr/io.h>
/*..................end of Includes..................*/
36
/*..................Constants........................*/
#define LCD_PORT PORTA
#define LCD_DDR DDRA
#define RS 0x10 // RS Signal "0001 0000"
#define RW 0x20 // RW Signal
#define ENABLE 0x40 // ENABLE Signal
/*..................end of Constants.................*/
//Method Signatures
void LCD_setDDR(void);
void LCD_init(void);
void LCD_delay(void);
void LCD_delayLong(void);
void LCD_sendString(char *s);
void LCD_sendByte(uint8_t val);
void LCD_sendCommand(uint8_t val);
void LCD_home(void);
void LCD_clearScreen(void);
int main(void);
