//#include <mega128.h> // ATmega128 MCU
#include <avr/io.h>
//#include <delay.h> // Delay functions
#include <util/delay.h>
#include <stdio.h> // Standard Input/Output functions
#include "LCD.h"
extern char lcdbuf[16+1]; // LCD Display Buffer
/*******************************/
/* Initial LCD 4-Bit Interface */
/*******************************/
void init_lcd(void)
{
PORTD &= 0b01111111; // Start LCD Control EN=0 (PD7)
_delay_ms(20); // Wait LCD Ready
write_ins(0x33); // Initial (Set DL=1 3 Time, Reset DL=0 1 Time)
write_ins(0x32);
write_ins(0x28); // Function Set (DL=0 4-Bit,N=1 2 Line,F=0 5X7)
write_ins(0x0C); // Display on/off Control (Entry Display,Cursor off,Cursor not Blink)
write_ins(0x06); // Entry Mode Set (I/D=1 Increment,S=0 Cursor Shift)
write_ins(0x01); // Clear Display (Clear Display,Set DD RAM Address=0)
_delay_ms(20); // Wait Initial Complete
return;
}
/******************/
/* Set LCD Cursor */
/******************/
void gotolcd(unsigned char i)
{
i |= 0x80; // Set DD-RAM Address Command
write_ins(i);
return;
}
/****************************/
/* Write Instruction to LCD */
/****************************/
void write_ins(unsigned char i)
{
PORTG &= 0b11101111; // Instruction Select RS=0(PG4)
PORTG &= 0xF0; // Clear old LCD Data (Bit[3..0])
PORTG |= (i>>4) & 0x0F; // Strobe High Nibble Command
PORTD |= 0b10000000; // Enable ON EN=1(PD7)
_delay_ms(20);
PORTD &= 0b01111111; // Enable OFF EN=0(PD7)
PORTG &= 0xF0; // Clear old LCD Data (Bit[3..0])
PORTG |= i & 0x0F; // Strobe Low Nibble Command
PORTD |= 0b10000000; // Enable ON EN=1(PD7)
_delay_ms(20);
PORTD &= 0b01111111; // Enable OFF EN=0(PD7)
_delay_ms(20); // Wait LCD Busy
return;
}
/****************************/
/* Write Data(ASCII) to LCD */
/****************************/
void write_data(unsigned char i)
{
PORTG |= 0b00010000; // Instruction Select RS=1(PG4)
PORTG &= 0xF0; // Clear old LCD Data (Bit[3..0])
PORTG |= (i>>4) & 0x0F; // Strobe High Nibble Command
PORTD |= 0b10000000; // Enable ON EN=1(PD7)
_delay_ms(20);
PORTD &= 0b01111111; // Enable OFF EN=0(PD7)
PORTG &= 0xF0; // Clear old LCD Data (Bit[3..0])
PORTG |= i & 0x0F; // Strobe Low Nibble Command
PORTD |= 0b10000000; // Enable ON EN=1(PD7)
_delay_ms(20);
PORTD &= 0b01111111; // Enable OFF EN=0(PD7)
_delay_ms(20); // Wait LCD Busy
return;
}
/****************************/
/* Print Data(ASCII) to LCD */
/****************************/
void printlcd(void)
{
char *p;
p = lcdbuf;
do // Get ASCII & Write to LCD Until null
{
write_data(*p); // Write ASCII to LCD
p++; // Next ASCII
}
while(*p != '\0'); // End of ASCII (null)
return;
}