#include #include "lcd.h" #define _XTAL_FREQ 1000000 // __delay_ms()用クロック定義 (1MHz駆動) //----------------------------- // PIC-LCDのピンアサイン //----------------------------- #define LCD_RS PORTAbits.RA6 //#define LCD_RW PORTAbits.RA1 // 未使用 #define LCD_ENABLE PORTAbits.RA7 #define LCD_DATA PORTB #define LCD_DATA_IS_HIGH // PortBの上位4bitをLCDのDB4-7に接続 // 内部関数のプロトタイプ宣言 static void lcd_write_cmd( unsigned char data ); static void lcd_write_databus( unsigned char data ); //**************************************************************************************** // Function : lcd_init // Description: LCD初期化処理(4bitモード) //**************************************************************************************** void lcd_init( void ) { //------------------- // 制御ピンの初期化 //------------------- LCD_RS = 0; LCD_ENABLE = 0; #ifdef LCD_DATA_IS_HIGH LCD_DATA = (LCD_DATA & 0x0f); #else LCD_DATA = (LCD_DATA & 0xf0); #endif __delay_ms( 300 ); // ウェイト300msにしてみる //------------------- // LCD初期化処理 //------------------- lcd_write_databus( 0x03 ); __delay_ms(20); // 4.1mSec以上ウェイト lcd_write_databus( 0x03 ); __delay_us(500); // 100uSec以上ウェイト lcd_write_databus( 0x03 ); __delay_ms(1); // 40uSec以上ウェイト // モードの指定 lcd_write_databus( 0x02 ); // 4bitモードに変更する __delay_ms( 2 ); // 40uSec以上ウェイト //-------------------- // 画面のクリア //-------------------- lcd_write_cmd( 0x28 ); // ファンクションセット(2行表示、5*7dot) __delay_ms( 2 ); // Display off lcd_write_cmd( 0x08 ); // ディスプレイ・カーソルの設定(カーソル非表示) __delay_ms( 2 ); lcd_write_cmd( 0x01 ); // clear display __delay_ms( 2 ); lcd_write_cmd( 0x06 ); // エントリモード(1:右にスクロール、0:シフトなし) __delay_ms( 2 ); // lcd_write_cmd( 0x0F ); // ディスプレイ・カーソルの設定(ディスプレイ:ON, カーソル:ON , カーソル点滅:ON) // lcd_write_cmd( 0x0E ); // ディスプレイ・カーソルの設定(ディスプレイ:ON, カーソル:ON , カーソル点滅:OFF) lcd_write_cmd( 0x0C ); // ディスプレイ・カーソルの設定(ディスプレイ:ON, カーソル:OFF, カーソル点滅:OFF) } //***************************************************************************** // Function : lcd_locate // Description : 印字位置の指定 //***************************************************************************** void lcd_locate( unsigned char x, unsigned char y ) { unsigned char writeData; writeData = 0x40 * x + y; // 表示位置を決定(2行目に出すときは,3bit目を立てる) writeData |= 0x80; // 最上位ビットを立てる lcd_write_cmd( writeData ); } //***************************************************************************** // Function : lcd_cls // Description : LCD画面クリア //***************************************************************************** void lcd_cls() { lcd_write_cmd( 0x01 ); // clear display __delay_ms( 3 ); // 1.64mSec以上ウェイト } //***************************************************************************** // Function : lcd_putc // Description : LCDに1文字出力 //***************************************************************************** void lcd_putc( char c ) { LCD_RS = 1; __delay_us( 1 ); // 40nSec以上 lcd_write_databus( (c & 0xf0) >> 4 ); lcd_write_databus( (c & 0x0f) ); } //***************************************************************************** // Function : lcd_puts // Description : LCDに文字列出力 //***************************************************************************** void lcd_puts( const char *p ) { while( *p != '\0' ) { lcd_putc( *p ); p++; } } //***************************************************************************** // Function : lcd_write_cmd // Description : 4bitモードでの命令書き込み(RS=LOW) // 命令は上位ビットから順に送る //***************************************************************************** static void lcd_write_cmd( unsigned char data ) { LCD_RS = 0; __delay_us( 1 ); // 40nSec以上 lcd_write_databus( (data & 0xf0) >> 4 ); lcd_write_databus( (data & 0x0f) ); } //***************************************************************************** // Function : lcd_write_databus // Description : dataで指定された下位4bit分のデータをDB4-7に書き込む //***************************************************************************** static void lcd_write_databus( unsigned char data ) { // まずEnableをactiveする LCD_ENABLE = 1; // 書き込むべきデータをセット #ifdef LCD_DATA_IS_HIGH LCD_DATA = (LCD_DATA & 0x0f) | ( (data<<4) & 0xf0); #else // 下位4bitにセットする LCD_DATA = (LCD_DATA & 0xf0) | (data & 0x0f); #endif __delay_us( 1 ); // 80nSec以上 // Enableをinactiveにする LCD_ENABLE = 0; __delay_us( 1 ); // 10nSec以上 // データをALL0でクリアしておく #ifdef LCD_DATA_IS_HIGH LCD_DATA = (LCD_DATA & 0x0f); // 上位4bitをクリア #else LCD_DATA = (LCD_DATA & 0xf0); // 下位4bitをクリア #endif }