//LCD Testing
#pragma config OSC = INTIO2
#include "p18f2220.h"
#include "delays.h"
// LCD display ports etc
#define LCDdata LATA // data port
#define LCDstatus LATBbits // control/status port
#define RS LATB0 // read/write bit in LCDstatus
#define E LATB1 // enable bit in LCDstatus
void lcd_delay() { Delay1KTCYx(40); } // if LCD does not work make this longer
// Write a nibble to the LCD
void lcdNibble(int n)
{
LCDdata = ((n & 0x0f)); // send out lower Nibble
LCDstatus.E=1; // take clock E high
lcd_delay();
LCDstatus.E=0;
lcd_delay();
}
// Write a Control Command to the LCD
// This is written as two nibbles
void lcdCmd(int c)
{
LCDstatus.RS=0; // Take RS pin low for command
lcdNibble(c >>4); // Makeup Upper Nibble
lcdNibble(c); // Makeup Lower Nibble
}
// write a data byte to LCD
int lcdPutchar(int d)
{
LCDstatus.RS=1; // Take RS pin high for data
lcdNibble(d >>4); // Makeup Upper Nibble
lcdNibble(d); // Makeup Lower Nibble
return 1;
}
// Initialise the LCD in 4bit Mode
void lcdInit()
{
LATA = 0;
LATB = 0;
ADCON1 = 0x0F;
TRISBbits.TRISB0=0; // set RS and E bits output
TRISBbits.TRISB1=0; // set RS and E bits output
TRISA &= 0xf0; // set bits 0-3 output for data
LCDstatus.RS=0; // Take RS pin low for command
lcdNibble(0x3); // This put the LCD into Soft Reset
lcdNibble(0x3);
lcdNibble(0x3);
lcdNibble(0x2);
lcdCmd(0x28); // 2 line, 4 bit mode
lcdCmd(0x6); // increment cursor after each write
lcdCmd(0x1); // clear display
lcdCmd(0x2); // home
lcdCmd(0xF); // turn disply on
}
void main(void)
{
OSCCON = 0x72; //8MHz Internal Clock
Delay1KTCYx(50);
lcdInit();
lcdPutchar('a');
}