Code: Select all
//E on A7, RS on A6, D4-7 on A0-3.
#include <p18f1220.h>
#pragma config WDT=OFF , OSC=INTIO2 , PWRT = ON, LVP=OFF, MCLRE = OFF
#include <delays.h>
#define CLEAR_SCREEN 0b00000001
#define FOUR_BIT 0b00101100
#define LINES_5X7 0b00111000
#define CURSOR_BLINK 0b00001111
#define CURSOR_RIGHT 0b00000110
#define DATA_PORT PORTA
#define RS_PIN PORTAbits.RA6
#define E_PIN PORTAbits.RA7
void Delay20TCY() //20 clock cycles, 2.5mS
{
Delay10TCYx(2);
}
void SetAddr(unsigned char DDaddr)
{
DATA_PORT &= 0xf0; // Write upper nibble
DATA_PORT |= (((DDaddr | 0b10000000)>>4) & 0x0f);
RS_PIN = 0; // Set control bit
Delay20TCY();
E_PIN = 1; // Clock the cmd and address in
Delay20TCY();
E_PIN = 0;
DATA_PORT &= 0xf0; // Write lower nibble
DATA_PORT |= (DDaddr&0x0f);
Delay20TCY();
E_PIN = 1; // Clock the cmd and address in
Delay20TCY();
E_PIN = 0;
}
void WriteCmd(unsigned char cmd)
{
DATA_PORT &= 0xf0;
DATA_PORT |= (cmd>>4)&0x0f;
RS_PIN = 0; // Set control signals for command
Delay20TCY();
E_PIN = 1; // Clock command in
Delay20TCY();
E_PIN = 0;
// Lower nibble interface
DATA_PORT &= 0xf0;
DATA_PORT |= cmd&0x0f;
Delay20TCY();
E_PIN = 1; // Clock command in
Delay20TCY();
E_PIN = 0;
}
void WriteChar(char data)
{
DATA_PORT &= 0xf0;
DATA_PORT |= ((data>>4)&0x0f);
RS_PIN = 1; // Set control bits
Delay20TCY();
E_PIN = 1; // Clock nibble into LCD
Delay20TCY();
E_PIN = 0;
DATA_PORT &= 0xf0; // Lower nibble interface
DATA_PORT |= (data&0x0f);
Delay20TCY();
E_PIN = 1; // Clock nibble into LCD
Delay20TCY();
E_PIN = 0;
}
void WriteString(const rom char *buffer)
{
while(*buffer) // Write data to LCD up to null
{
Delay20TCY();
WriteChar( *buffer); // Write character to LCD
buffer++; // Increment buffer
}
return;
}
void main (void)
{
//SET UP
// OSCCON defaults to 31kHz. So no need to alter it.
ADCON1 = 0x7F; //all IO are digital or 0b01111111 in binary
TRISA = 0b00100000; //sets PORTA
PORTA = 0b00000000; //turns off PORTA outputs
TRISB = 0b01000000; //sets PORTB as all outputs
PORTB = 0b00000000; //turns off PORTB outputs, good start position
// this code configures the display
WriteCmd ( 0x02 ); // sets 4bit operation
WriteCmd ( FOUR_BIT & LINES_5X7 ); // sets 5x7 font and multiline operation.
WriteCmd ( CURSOR_BLINK ); // blinks cursor
WriteCmd ( CURSOR_RIGHT ); // moves cursor right
// Start of user program.
SetAddr (0x83);
WriteSting("Hello World");
while(1);
}
Does anyone know what to do?
I use Mplaba and c18 compiler
Thank you