/////////////////////////////More LCD ///////////////////////////////////////
//Sat AM Feb 22 added Line and Column GOTO
#include <p18F43k20.h>
#pragma config FOSC=INTIO67,BOREN=OFF,PWRT=ON,WDTEN=OFF,PBADEN=OFF,STVREN=OFF,LVP=OFF
//////////// Defines ////////////////////////////////////////////////////////
// The lower 4 bits of this port will be used
#define LCD_DATA LATD
#define LCD_EN LATDbits.LATD7
#define LCD_RS LATDbits.LATD4
#define TRIS_LCD_DATA TRISD
#define TRIS_LCD_EN TRISDbits.TRISD7
#define TRIS_LCD_RS TRISDbits.TRISD4
#define TRIS_LCD_PWR TRISEbits.TRISE2 //RE2 is for LCD power control through MOSFET
// Commands for Hitachi LCD
#define CLEAR_DISPLAY 0x01
#define FOUR_BIT 0b00101111 /* 4-bit Interface */
#define EIGHT_BIT 0b00111111 /* 8-bit Interface */
#define LINE_5X7 0b00110011 /* 5x7 characters, single line */
#define LINE_5X10 0b00110111 /* 5x10 characters */
#define LINES_5X7 0b00111011 /* 5x7 characters, multiple line */
#define DISPLAY_ON 0b00001111 /* Display on */
#define DISPLAY_OFF 0b00001011 /* Display off */
#define CURSOR_ON 0b00001111 /* Cursor on */
#define CURSOR_OFF 0b00001101 /* Cursor off */
#define BLINK_ON 0b00001111 /* Cursor Blink */
#define BLINK_OFF 0b00001110 /* Cursor No Blink */
#define UNDERLINE_ON 0b00001111
#define UNDERLINE_OFF 0b00001101
#define INCREMENT 0b00000111 /* Entry mode increment */
#define DECREMENT 0b00000101 /* Entry mode decrement */
#define SHIFT_ON 0b00000111 /* Display shift on */
#define SHIFT_OFF 0b00000110 /* Display shift off */
#define PulseEnable { LCD_EN = 1; Nop(); LCD_EN = 0; }
// Required prototypes.. each function needs to be declared
// if called BEFORE definition.
void LCDInit(void);
void LCDCmd(unsigned char); //RS is 0 for commands
void LCDChar(unsigned char); //RS is 1 for characters
void LCD_GOTO(unsigned char,unsigned char); //Sets position, line and column
void Delay5ms(void);
void Delay15ms(void);
void Delay100ms(void); //100 ms delay
void LCDStr(char *string);
char SimpleHello[13] = "Hello AGCB!\0";
unsigned char text1[] = {"Hello"}; ?????????????????
//////////////////////////// MAIN funtion ////////////////////////////////////
void main (void)
{
int index = 0;
OSCCON = 0xD0; /* Set internal oscillator at 4 Mhz */
ADCON0 = 0x00; /* Disable internal ADC */
TRISEbits.RE2 = 0; //turns on power to LCD via MOSFET ????????????????
TRISD = 0; //Make all bits on the Port B (LCD) output bits.
PORTD = 0; //Reset the LCD bits to 0
Delay15ms();
LCDInit();
LCDStr(SimpleHello);
while(1);
}
void Delay5ms(void) {
Delay1KTCYx(10); // Delay of 5ms
// Cycles = (TimeDelay * Fosc) / 4
// Cycles = (5ms * 8MHz) / 4
// Cycles = 10,000
return;
}
void Delay15ms(void) {
Delay1KTCYx(30); // Delay of 15ms
// Cycles = (TimeDelay * Fosc) / 4
// Cycles = (15ms * 8MHz) / 4
// Cycles = 30,000
return;
}
void Delay100ms(void)
{
Delay1KTCYx(100); //delay 100 ms
}
void LCDInit(void) {
PORTEbits.RE2 = 1; //turn on LCD power
Delay100ms(); //100 ms delay to power up LCD
// Set port directions
TRIS_LCD_DATA &= 0xf0; // Clear lower 4 bits
TRIS_LCD_EN = 0;
TRIS_LCD_RS = 0;
//LCDCmd(FOUR_BIT); // Nigel's code sends this before the next command?
LCDCmd(FOUR_BIT & LINES_5X7);
LCDCmd(INCREMENT & SHIFT_OFF);
LCDCmd(DISPLAY_ON & BLINK_OFF & UNDERLINE_OFF);
LCDCmd(CLEAR_DISPLAY);
Delay15ms();
}
void LCDCmd(unsigned char c) {
LCD_RS = 0;
LCD_DATA &= 0xf0;
LCD_DATA |= (c >> 4);
PulseEnable;
LCD_DATA &= 0xf0;
LCD_DATA |= (c & 0x0f);
PulseEnable;
Delay5ms();
}
void LCDChar(unsigned char c) {
LCD_RS = 1;
LCD_DATA &= 0xf0;
LCD_DATA |= (c >> 4);
PulseEnable;
LCD_DATA &= 0xf0;
LCD_DATA |= (c & 0x0f);
PulseEnable;
Delay5ms();
}
void LCD_GOTO(unsigned char line,unsigned char column)
{
unsigned char data = 0x80; // default to 1
if(line == 2)data = 0xc0; // change to 2
data += column; // add in column
LCDCmd(data);
Delay5ms();
}
void LCDStr(char *string)
{
while(*string != 0)
{
LCDChar(*string++);
}
}