秋月電子 I2C接続キャラクタLCDモジュール用にI2CLCDライブラリを修正しました。 Modified I2CLCD Lib for ACM1602NI character LCD module. http://akizukidenshi.com/catalog/g/gP-05693/

Dependencies:   mbed

Committer:
maro
Date:
Sun Oct 20 03:44:57 2013 +0000
Revision:
0:9bf010140a3f
Changed the waiting time of writeCommand and writeData

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maro 0:9bf010140a3f 1 //8x2 lcd
maro 0:9bf010140a3f 2 #include "I2cLCD.h"
maro 0:9bf010140a3f 3 #include "mbed.h"
maro 0:9bf010140a3f 4
maro 0:9bf010140a3f 5 #define I2CLCD_ADDR 0xa0//0x7C
maro 0:9bf010140a3f 6
maro 0:9bf010140a3f 7 #define RS_CMD 0x00
maro 0:9bf010140a3f 8 #define RS_DATA 0x80
maro 0:9bf010140a3f 9 const short WAIT1 = 10;
maro 0:9bf010140a3f 10
maro 0:9bf010140a3f 11 I2cLCD::I2cLCD(PinName sda, PinName scl) : _i2c( sda , scl ){
maro 0:9bf010140a3f 12 //_i2c.frequency(50000);
maro 0:9bf010140a3f 13 wait_ms(15);
maro 0:9bf010140a3f 14 writeCommand(0x01);
maro 0:9bf010140a3f 15 wait_ms(5);
maro 0:9bf010140a3f 16 writeCommand(0x38);
maro 0:9bf010140a3f 17 wait_ms(5);
maro 0:9bf010140a3f 18 writeCommand(0x0f);
maro 0:9bf010140a3f 19 wait_ms(5);
maro 0:9bf010140a3f 20 writeCommand(0x06);
maro 0:9bf010140a3f 21 wait_ms(5);
maro 0:9bf010140a3f 22 writeCommand(0x0c);
maro 0:9bf010140a3f 23 wait_ms(5);
maro 0:9bf010140a3f 24 }
maro 0:9bf010140a3f 25
maro 0:9bf010140a3f 26
maro 0:9bf010140a3f 27
maro 0:9bf010140a3f 28 void I2cLCD::character(int column, int row, int c) {
maro 0:9bf010140a3f 29 int a = address(column, row);
maro 0:9bf010140a3f 30 writeCommand(a);
maro 0:9bf010140a3f 31 writeData(c);
maro 0:9bf010140a3f 32 }
maro 0:9bf010140a3f 33
maro 0:9bf010140a3f 34 void I2cLCD::cls() {
maro 0:9bf010140a3f 35 writeCommand(0x01); // cls, and set cursor to 0
maro 0:9bf010140a3f 36 wait_ms(2);
maro 0:9bf010140a3f 37 locate(0, 0);
maro 0:9bf010140a3f 38 }
maro 0:9bf010140a3f 39
maro 0:9bf010140a3f 40 void I2cLCD::locate(int column, int row) {
maro 0:9bf010140a3f 41 _column = column;
maro 0:9bf010140a3f 42 _row = row;
maro 0:9bf010140a3f 43 }
maro 0:9bf010140a3f 44
maro 0:9bf010140a3f 45 int I2cLCD::_putc(int value) {
maro 0:9bf010140a3f 46 if (value == '\n') {
maro 0:9bf010140a3f 47 _column = 0;
maro 0:9bf010140a3f 48 _row++;
maro 0:9bf010140a3f 49 if (_row >= rows()) {
maro 0:9bf010140a3f 50 _row = 0;
maro 0:9bf010140a3f 51 }
maro 0:9bf010140a3f 52 } else {
maro 0:9bf010140a3f 53 character(_column, _row, value);
maro 0:9bf010140a3f 54 _column++;
maro 0:9bf010140a3f 55 if (_column >= columns()) {
maro 0:9bf010140a3f 56 _column = 0;
maro 0:9bf010140a3f 57 _row++;
maro 0:9bf010140a3f 58 if (_row >= rows()) {
maro 0:9bf010140a3f 59 _row = 0;
maro 0:9bf010140a3f 60 }
maro 0:9bf010140a3f 61 }
maro 0:9bf010140a3f 62 }
maro 0:9bf010140a3f 63 return value;
maro 0:9bf010140a3f 64 }
maro 0:9bf010140a3f 65
maro 0:9bf010140a3f 66 int I2cLCD::_getc() {
maro 0:9bf010140a3f 67 return -1;
maro 0:9bf010140a3f 68 }
maro 0:9bf010140a3f 69
maro 0:9bf010140a3f 70 void I2cLCD::writeCommand( int cmd )
maro 0:9bf010140a3f 71 {
maro 0:9bf010140a3f 72 char cmds[2];
maro 0:9bf010140a3f 73
maro 0:9bf010140a3f 74 cmds[0] = RS_CMD;
maro 0:9bf010140a3f 75 cmds[1] = cmd;
maro 0:9bf010140a3f 76
maro 0:9bf010140a3f 77 //_i2c.write(I2CLCD_ADDR, cmds, 2);
maro 0:9bf010140a3f 78 _i2c.start();
maro 0:9bf010140a3f 79 wait_us(9);
maro 0:9bf010140a3f 80 _i2c.write(I2CLCD_ADDR);
maro 0:9bf010140a3f 81 wait_us(WAIT1);
maro 0:9bf010140a3f 82 _i2c.write(cmds[0]);
maro 0:9bf010140a3f 83 wait_us(WAIT1);
maro 0:9bf010140a3f 84 _i2c.write(cmds[1]);
maro 0:9bf010140a3f 85 wait_us(WAIT1);
maro 0:9bf010140a3f 86 _i2c.stop();
maro 0:9bf010140a3f 87 }
maro 0:9bf010140a3f 88
maro 0:9bf010140a3f 89 void I2cLCD::writeData( int data )
maro 0:9bf010140a3f 90 {
maro 0:9bf010140a3f 91 char cmd[2];
maro 0:9bf010140a3f 92
maro 0:9bf010140a3f 93 cmd[0] = RS_DATA;
maro 0:9bf010140a3f 94 cmd[1] = data;
maro 0:9bf010140a3f 95
maro 0:9bf010140a3f 96 //_i2c.write(I2CLCD_ADDR, cmd, 2);
maro 0:9bf010140a3f 97 _i2c.start();
maro 0:9bf010140a3f 98 wait_us(9);
maro 0:9bf010140a3f 99 _i2c.write(I2CLCD_ADDR);
maro 0:9bf010140a3f 100 wait_us(WAIT1);
maro 0:9bf010140a3f 101 _i2c.write(cmd[0]);
maro 0:9bf010140a3f 102 wait_us(WAIT1);
maro 0:9bf010140a3f 103 _i2c.write(cmd[1]);
maro 0:9bf010140a3f 104 wait_us(WAIT1);
maro 0:9bf010140a3f 105 _i2c.stop();
maro 0:9bf010140a3f 106
maro 0:9bf010140a3f 107 }
maro 0:9bf010140a3f 108
maro 0:9bf010140a3f 109 int I2cLCD::address(int column, int row) {
maro 0:9bf010140a3f 110
maro 0:9bf010140a3f 111 return 0x80 + (row * 0x40) + column;
maro 0:9bf010140a3f 112 }
maro 0:9bf010140a3f 113
maro 0:9bf010140a3f 114 int I2cLCD::columns() {
maro 0:9bf010140a3f 115 return 16;
maro 0:9bf010140a3f 116 }
maro 0:9bf010140a3f 117
maro 0:9bf010140a3f 118 int I2cLCD::rows() {
maro 0:9bf010140a3f 119 return 2;
maro 0:9bf010140a3f 120 }