pkipyy
Joined: 21 Nov 2013 Posts: 25
|
lcd 16*2 driver |
Posted: Sat Nov 30, 2013 7:26 am |
|
|
This is lcd driver for demo2 plus board.
If you a bit modify,you can use this.
Code: |
// As defined in the following structure the pin connection is as follows:
// D0 enable RD6
// D1 rs RD4
// D2 rw RD5
// D4 D4 RD0
// D5 D5 RD1
// D6 D6 RD2
// D7 D7 RD3
struct lcd_pin_map { // This structure is overlayed
boolean rd0;
boolean rd1;
boolean rd2;
boolean rd3;
boolean rs;
boolean rw;
boolean en; // on to an I/O port to gain
boolean pwr; // low order up. ENABLE will
} lcd;
struct lcd_pin_map trisd;
struct lcd_pin_map latd;
#byte lcd = 0x0f //PORTD
#byte trisd = 0x8f
#byte latd = 0x10f
#define INP 1
#define OUTP 0
#define ENABLE 1
#define DISABLE 0
#define READ 1
#define WRITE 0
#define CLEAR 0
#define SET 1
#define MAX_LINE 2
#define MAX_COL 16
void nop()
{
}
char read_byte()
{
int res,dat;
trisd.rd0=INP;
trisd.rd1=INP;
trisd.rd2=INP;
trisd.rd3=INP;
lcd.en=ENABLE;
nop();
nop();
dat=latd;
lcd.en=DISABLE;
res=dat<<4;
lcd.en=ENABLE;
nop();
nop();
dat=latd;
dat=dat&0x0f;
res=res|dat;
trisd.rd0=OUTP;
trisd.rd1=OUTP;
trisd.rd2=OUTP;
trisd.rd3=OUTP;
return res;
}
void wait_lcd()
{
char status;
lcd.rs=CLEAR;
lcd.rw=READ;
do{
status=read_byte();
}while(status&0x80);
}
void write_nibble(char cmd,char dat)
{
char buf;
if(cmd) lcd.rs=CLEAR;
else lcd.rs=SET;
lcd.rw=WRITE;
lcd.en=ENABLE;
buf=latd;
buf=buf &0xf0;
latd=buf |(dat&0x0f);
nop();
nop();
lcd.en=DISABLE;
}
void write_byte(char cmd,char dat)
{
write_nibble(cmd,dat>>4);
write_nibble(cmd,dat);
}
void lcd_put_char(char dat)
{
write_byte(FALSE,dat);
wait_lcd();
}
void lcd_put_str(str[])
{
int i=0;
while(str[i])
lcd_put_char(str[i++]);
}
void lcd_goto(char line,char col)
{
if((line >MAX_LINE-1)||(col>MAX_COL-1)) return;
if(line==1) write_byte(TRUE,0xc0|col);
if(line==0) write_byte(TRUE,0x80|col);
wait_lcd();
}
void lcd_clear()
{
write_byte(TRUE,0x01);
wait_lcd();
}
void init_lcd()
{
trisd=0;
lcd.pwr=0;
lcd.pwr=1;
lcd.rs=CLEAR;
lcd.rw=WRITE;
lcd.en=DISABLE;
lcd.en=ENABLE;
wait_lcd();
write_nibble(TRUE,0x02);
wait_lcd();
write_byte(TRUE,0x28);
wait_lcd();
write_byte(TRUE,0x01);
wait_lcd();
write_byte(TRUE,0x02);
wait_lcd();
write_byte(TRUE,0x0c);
wait_lcd();
wait_lcd();
}
|
Here is test program
Code: |
#include <16f1937.h>
#include "lcd.c"
#use delay(clock = 500000)
void main()
{
init_lcd();
while(1) {
lcd_goto(0,0);
lcd_put_char("a");
}
}
|
Happy to you. |
|