液晶文字表示モジュール(LCD)

| ピン | 信号名 | 意味 |
| 1 | VCC | 5V |
| 2 | GND | GND |
| 3 | VO | コントラスト |
| 4 | RS | 1:制御、0:データ |
| 5 | R/W | 1で読み、0で書き込み |
| 6 | E(stb) | データ同期用パルス |
| 7−10 | BD0-DB3 | 下位データ |
| 11−14 | BD4-BD7 | 上位データ |



#define rs PIN_A2 //chip select #define rw PIN_A1 //read/write #define stb PIN_A0 //strobe
| lcd_clear(); | lcdをクリアする |
| lcd_cmd(cmd) | コマンド cmd を送る |
| lcd_data(<文字列>); | 文字列を表示する |
#define Bmode 0x0F #define rs PIN_A2 //chip select #define rw PIN_A1 //read/write #define stb PIN_A0 //strobe #include <lcd_lib.c> //include lcd library
fun(char c[])として呼び出すことで、c[] の各要素Cに fun(char C ) の処理を適用します。lcd_lib.c では、lcd_data() を1文字の表示を行う関数として定義しています。文字列への拡張は、CCSコンパイラが自動的に行います。
printf(lcd_data,"max=%4u long=%ld",max,ld);で、液晶に max と ld の値を書式指定して表示できます。ここで、 lcd_data はLCD に1文字を送り出す関数です。%4u は符号なし整数を4桁で表示するC言語標準の書式です。
//LCD display
//PB4>LCD.11(DB4), PB5> LCD.12, PB6> LCD.13, PB7> LCD.14
//PA0>LCD.6(STB), PA1>LCD.5(RW),PA2>LCD.4(RS)
//LCD.<7:11> GND
//LCD 1>VCC(4.2V) 2> GND 3>GND
//RC0>LED チェック用
//#include <16f648a.h>
//#fuses INTRC_IO,NOWDT,NOLVP,NOMCLR //内部クロック、WDT,LVPなし
//#use delay(CLOCK=4000000)
#include <16f873A.h>//16F873を利用する場合上の3行を以下の3行に置き換えます
#fuses HS,NOWDT,NOLVP //内部クロック、WDT,LVPなし
#use delay(CLOCK=20000000)
#byte port_b = 6 //port Bレジスタのアドレス
//for LCD control
//define port and Pin
#byte db=6
#define Bmode 0x0F
#define rs PIN_A2 //chip select
#define rw PIN_A1 //read/write
#define stb PIN_A0 //strobe
#include <lcd_lib.c> //include lcd library
///////////////////////////////////////////////
// LCD test program
///////////////////////////////////////////////
int max;
long ld;
main(){
set_tris_a(0xF0); //mode set of port
set_tris_b(0X0F); //lower is input
lcd_init(); //initialize LCD
while(1){
output_toggle(PIN_C0);//チェック用
lcd_clear(); //clear display
lcd_data("Hello");
delay_ms(1000); //wait 1sec
lcd_clear();
lcd_data("from PIC");//最初の表示
delay_ms(1000);
lcd_clear();
lcd_data("1234567890");//次の表示
delay_ms(1000);
lcd_clear();
lcd_data("abcdefghijklmnop");//2行に分けて表示
lcd_cmd(0xC0); //第2行に移動
max=31;ld=12345;
printf(lcd_data,"max=%3u long=%6ld",max,ld);
delay_ms(3000);
}
return 0;
}
///////////////////////////////////////////////
// LCD control Library
// lcd_init()-------- initialize
// lcd_ready()------- busy check
// lcd_cmd(cmd)------ send command
// lcd_data(string)-- display string
// lcd_clear() ------ clear display
///////////////////////////////////
/// lcd ready check function
int lcd_ready(){
int high,low;
set_tris_b(Bmode | 0xF0); //upper is input
output_low(rs);
output_high(rw); //read mode
output_high(stb);
high=input_b()&0xF0; //input upper
output_low(stb);
output_high(stb);
low=input_b() & 0xF0; //input lower
output_low(stb);
set_tris_b(Bmode);
return(high | (low>>4)); //end check
}
////////// lcd display data function
void lcd_data(int asci){
//db = asci; //set upper data
output_b(asci);
output_low(rw); //set write
output_high(rs); //set rs high
output_high(stb); //strobe
output_low(stb);
asci=asci<<4;
output_b(asci);
//db = asci; //set lower data
output_high(stb); //strobe
output_low(stb);
//delay_ms(10);
while(bit_test(lcd_ready(),7));
}
////////// lcd command out function
void cmdout(int cmd){
output_b(cmd);
output_low(rw); //set write
output_low(rs); //set rs low
output_high(stb); //strobe
output_low(stb);
cmd=cmd<<4;
output_b(cmd);
output_high(stb); //strobe
output_low(stb);
}
void lcd_cmd(int cmd){
cmdout(cmd);
while(bit_test(lcd_ready(),7)); //end check
}
///// lcd display clear function
void lcd_clear(){
lcd_cmd(1); //initialize command
delay_ms(100);
}
///////// lcd initialize function
void lcd_incmd(int cmd){
//db = cmd; //mode command
output_b(cmd);
output_low(rw); //set write
output_low(rs); //set rs low
output_high(stb); //strobe
output_low(stb);
delay_ms(10);
}
void lcd_init(){
delay_ms(15);
lcd_incmd(0x30); //8bit mode set
delay_ms(5);
lcd_incmd(0x30); //8bit mode set
delay_ms(5);
lcd_incmd(0x30); //8bit mode set
delay_ms(5);
lcd_incmd(0x20); //4bit mode set
delay_ms(5);
lcd_cmd(0x2C); //DL=0 4bit mode
lcd_cmd(0x08); //disolay off C=D=B=0
lcd_cmd(0x0C); //display on C=D=1 B=0
lcd_cmd(0x06); //entry I/D=1 S=0
}
void cmdout(int cmd){
//db = cmd; //set upper data
output_b(cmd);
output_low(rw); //set write
output_low(rs); //set rs low
output_high(stb); //strobe
output_low(stb);
cmd=cmd<<4;
output_b(cmd);
//db = cmd; //set lower data
output_high(stb); //strobe
output_low(stb);
}
| 0000 0001 | クリア |
| 0000 0010 | カーソルを先頭に戻す |
| 0000 01ds | sが1なら文字入力でカーソルを移動 d=1なら次、0なら前に移動 |
| 0000 1dcb | d=1で表示ON 、c=1でカーソル表示、 b=1で、カーソルの文字をブリンク |
| 001D NF00 | D=1で8ビット、0で4ビットモード N=1で1行、0で2行表示 F=1 で5*10ドット、0で5*7ドット表示 |
| 01aaaaaa | CGRAMアドレス |
| 1aaaaaaa | DDRAM(文字表示)アドレス |