--- Raspberry Pi に
LCD液晶を付けて時計を作ってみた ---
2014年11月09日(日)
Raspberry Pi にLCD液晶を付けて時計を作ってみた
LCD液晶の動作を確認するために時計のサンプルを作ってみた。
使用したLCD液晶は、トラ技2014年7月号に掲載されていたもの。

ストロベリー・リナックス http://strawberry-linux.com/

I2C低電圧キャラクタ液晶モジュール(16x2行) メーカー品番:SB1602B
http://strawberry-linux.com/catalog/items?code=27001

使い方は、ストロベリー・リナックスで公開されている資料と
使用されているコントローラー(ST7032i)のデータシートを参考に。



I2Cのアドレスを確認

--------------------------------------------
pi@raspberrypi ~ $ sudo i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- 3e --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
pi@raspberrypi ~ $
--------------------------------------------


そうそう、I2Cを使うのに、libi2c-dev だけではダメみたいで、i2c-tools もインストールしてた。

下のソースが時計のサンプル。
アイコン表示の動作を確認するために、26秒間だけ動作。

ソース

--------------------------------------------
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <wiringPiI2C.h>

#define LCD_ADDRESS 0x3e
#define LCD_CMDADRS 0x00
#define LCD_DATADRS 0x40

// Prototype
int lcd_init(int fd);
int lcd_write(int fd, char* buf, int line);
int lcd_clear(int fd);
int lcd_icon(int fd, int num, int disp);

/**
 * main
 *  Initialize ST7032i (LCD Controller)
 *  ICON Control
 *  Write DATE,TIME
 */
int main(void)
{
	int fd_lcd;           // ST7032i (LCD Controller)
	char disp_buf[17];    // Display Buffer
	int icon_num;         // ICON Number
	int icon_disp;        // ICON (1:display 0:clear)

	int loop;

	time_t timer;
	struct tm *local;
	char wday_buf[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

	/** Device open */
	if((fd_lcd = wiringPiI2CSetup(LCD_ADDRESS)) == -1) { return 1; }

	/** Initialize ST7032i (LCD Controller) */
	if (lcd_init(fd_lcd) == -1) { return 1; }

	icon_num  = 0;
	icon_disp = 1;               // ICON display
	for (loop=0; loop<26; loop++) {
		/** ICON */
		if (loop == 13) {        // 0-12:Display 13-:Clear
			icon_num  = 0;
			icon_disp = 0;       // ICON clear
		}
		lcd_icon(fd_lcd, icon_num, icon_disp);
		icon_num++;

		/** CLOCK */
		timer = time(NULL);
		local = localtime(&timer);
		sprintf(disp_buf,"%4d/%02d/%02d %s",
		        local->tm_year+1900, local->tm_mon+1, local->tm_mday, wday_buf[local->tm_wday]);
		lcd_write(fd_lcd, disp_buf, 1);

		sprintf(disp_buf,"%02d:%02d:%02d",
		        local->tm_hour, local->tm_min, local->tm_sec);
		lcd_write(fd_lcd, disp_buf, 2);

		sleep(1);        // 1sec
	}

	/** LCD clear */
	lcd_clear(fd_lcd);

	return 0;
}

/**
 * sub:lcd_init
 *  ex. ST7032i dataseet (Command , Initilaize flowchart)
 */
int lcd_init(int fd)
{
	delay(40);                                          // 40msec

	/** Function Set (8bit bus mode, 2-line mode,normal font,normal instruction mode) */
	wiringPiI2CWriteReg8(fd, LCD_CMDADRS, 0b00111000);
	delayMicroseconds(30);                              // 26.3usec

	/** Function Set (IS=1 : extension instruction mode) */
	wiringPiI2CWriteReg8(fd, LCD_CMDADRS, 0b00111001);
	delayMicroseconds(30);                              // 26.3usec

	/** Internal OSC frequency (BS=Low, Freq=183Hz, VDD=3.0V) */
	wiringPiI2CWriteReg8(fd, LCD_CMDADRS, 0b00010100);
	delayMicroseconds(30);                              // 26.3usec

	/** Contrast set (lower 4bit) */
	wiringPiI2CWriteReg8(fd, LCD_CMDADRS, 0b01111010);
	delayMicroseconds(30);                              // 26.3usec

	/** Power (ICON ON/Boost ON/Contrast higher 2bit) */
	wiringPiI2CWriteReg8(fd, LCD_CMDADRS, 0b01011111);
	delayMicroseconds(30);                              // 26.3usec

	/** Follower control (Fon ON/Rab1 ON) */
	wiringPiI2CWriteReg8(fd, LCD_CMDADRS, 0b01101010);
	delay(200);                                         // 200msec (for Power stable)

	/** Display ON/Cursor ON/Blink ON */
	wiringPiI2CWriteReg8(fd, LCD_CMDADRS, 0b00001111);
	delayMicroseconds(30);                              // 26.3usec

	/** Clear Display */
	wiringPiI2CWriteReg8(fd, LCD_CMDADRS, 0b00000001);
	delay(2);                                           // 2msec

	/** Entry Mode Set (Cursor moves to right) */
	wiringPiI2CWriteReg8(fd, LCD_CMDADRS, 0b00000110);
	delayMicroseconds(30);                              // 26.3usec

	return 0;
}

/**
 * sub:lcd_write
 */
int lcd_write(int fd, char* buf, int line)
{
	int cnt;
	char addr;

	/** Set DDRAM Address */
	if (line == 1) {
		/** Line 1 (0x00) */
		addr = 0b10000000;
	} else {
		/** Line 2 (0x40) */
		addr = 0b11000000;
	}
	wiringPiI2CWriteReg8(fd, LCD_CMDADRS, addr);

	/** Write Data To DDRAM */
	for (cnt=0; buf[cnt]!=0; cnt++) {
		wiringPiI2CWriteReg8(fd, LCD_DATADRS, buf[cnt]);
	}

	return 0;
}

/**
 * sub:lcd_clear
 */
int lcd_clear(int fd)
{
	/** Clear Display */
	wiringPiI2CWriteReg8(fd, LCD_CMDADRS, 0b00000001);
	delay(2);                                           // 2msec

	/** Return Home */
	wiringPiI2CWriteReg8(fd, LCD_CMDADRS, 0b00000011);
	delay(2);                                           // 2msec

	return 0;
}

/**
 * sub:lcd_icon
 */
int lcd_icon(int fd, int num, int disp)
{
	char icon;

	// ICON address table (Lower 4bit)      OFFSET : ADDR: BIT : ICON
	char icon_addr_buf[13] = { 0x00,          // 0 : 00H : S1  : ANT
	                           0x02,          // 1 : 02H : S11 : TEL
	                           0x04,          // 2 : 04H : S21 : SP
	                           0x06,          // 3 : 06H : S31 : CON
	                           0x07,          // 4 : 07H : S36 : UP
	                           0x07,          // 5 : 07H : S37 : DN
	                           0x09,          // 6 : 09H : S46 : KEY
	                           0x0b,          // 7 : 0BH : S56 : ?
	                           0x0d,          // 8 : 0DH : S66 : BAT
	                           0x0d,          // 9 : 0DH : S67 : BAT
	                           0x0d,          // A : 0DH : S68 : BAT
	                           0x0d,          // B : 0DH : S69 : BAT
	                           0x0f           // C : 0FH : S76 : ?
	                         };

	// ICON bits table (Lower 5bit)         OFFSET : ADDR: BIT : ICON
	char icon_bits_buf[13] = { 0b00010000,    // 0 : 00H : S1  : ANT
	                           0b00010000,    // 1 : 02H : S11 : TEL
	                           0b00010000,    // 2 : 04H : S21 : SP
	                           0b00010000,    // 3 : 06H : S31 : CON
	                           0b00010000,    // 4 : 07H : S36 : UP
	                           0b00001000,    // 5 : 07H : S37 : DN
	                           0b00010000,    // 6 : 09H : S46 : KEY
	                           0b00010000,    // 7 : 0BH : S56 : ?
	                           0b00010000,    // 8 : 0DH : S66 : BAT
	                           0b00001000,    // 9 : 0DH : S67 : BAT
	                           0b00000100,    // A : 0DH : S68 : BAT
	                           0b00000010,    // B : 0DH : S69 : BAT
	                           0b00010000     // C : 0FH : S76 : ?
	                         };

	if (num > 12) { return 1; }

	/** Function Set (IS=1 : extension instruction mode) */
	wiringPiI2CWriteReg8(fd, LCD_CMDADRS, 0b00111001);
	delayMicroseconds(30);                              // 26.3usec

	/** Set ICON Address */
	icon = 0b01000000 | icon_addr_buf[num];             // Lower 4bit
	wiringPiI2CWriteReg8(fd, LCD_CMDADRS, icon);
	delayMicroseconds(30);                              // 26.3usec

	/** Write Data To RAM */
	if (disp == 1) {
		/** ICON display */
		icon = icon_bits_buf[num];
	} else {
		/** ICON clear */
		icon = icon_bits_buf[num] ^ icon_bits_buf[num];
	}
	icon = 0b00000000 | icon;                           // Lower 5bit
	wiringPiI2CWriteReg8(fd, LCD_DATADRS, icon);

	/** Function Set (IS=0) */
	wiringPiI2CWriteReg8(fd, LCD_CMDADRS, 0b00111000);
	delayMicroseconds(30);                              // 26.3usec

	return 0;
}
--------------------------------------------

--------------------------------------------
gcc -o lcdsample lcdsample.c -l wiringPi
--------------------------------------------