#include <pic.h>
#define E RB4
#define RS RB5
#define D4 RB0
#define D5 RB1
#define D6 RB2
#define D7 RB3
void waitms(int ms)
{
volatile int i,j;
for (i=0;i<ms;i++)
for (j=0;j<60;j++);
}
void wait20us(int ms)
{
RB6=1;
volatile int i,j;
for (i=0;i<ms;i++)
for (j=0;j<2;j++);
RB6=0;
}
void E_pulse(void)
{
E=1;
wait20us(5);
E=0;
wait20us(5);
}
void LCD_cmd(int cmd)
{
int pv;
RS=0; // LCD command mode
pv = cmd>>4;
PORTB=pv; // high bits
E_pulse();
waitms(5);
pv = cmd&0x0f;
PORTB=pv; // low bits
E_pulse();
waitms(10);
}
void LCD_chr(char data[])
{
int i;
int pv;
int ch;
E=0;
RS=1; // LCD data mode
for(i = 0; data[i] != 0; i++)
{
ch=data[i];
pv=ch>>4;
PORTB=pv; // high bits
E_pulse();
waitms(5);
pv=ch&0x0f;
PORTB=pv; // low bits
E_pulse();
}
waitms(10);
}
void LCD_locate(int row, int column)
{
RS=0;
if(row == 1)
{
LCD_cmd(0x80 | (column - 1)&0x0f);
}
else
{
LCD_cmd(0xc0 | (column - 1)&0x0f);
}
waitms(5);
}
void LCD_init(void)
{
TRISB=0;
waitms(40);
RS=0;
PORTB=0x3;
E_pulse();
waitms(6);
PORTB=0x3;
E_pulse(); // init!
waitms(6);
PORTB=0x3;
E_pulse(); // init!
waitms(6);
PORTB=0x2;
E_pulse();
LCD_cmd(0x28);
LCD_cmd(0x08);
LCD_cmd(0x01);
waitms(5);
LCD_cmd(0x06);
LCD_cmd(0x0f);
waitms(1);
}
void main(){
LCD_init();
waitms(10);
LCD_chr("test");
while(1){
}
}