I2cLCD library that can display cursor.

Dependents:   LcdClock

Fork of I2cLCD by Junichi Katsu

Committer:
jksoft
Date:
Mon Jun 28 14:20:58 2010 +0000
Revision:
0:b3cc6ae3dfd8
Child:
1:f973d1359998

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:b3cc6ae3dfd8 1 #include "I2cLCD.h"
jksoft 0:b3cc6ae3dfd8 2 #include "mbed.h"
jksoft 0:b3cc6ae3dfd8 3
jksoft 0:b3cc6ae3dfd8 4 #define I2CLCD_ADDR 0x7C
jksoft 0:b3cc6ae3dfd8 5
jksoft 0:b3cc6ae3dfd8 6 #define RS_CMD 0x00
jksoft 0:b3cc6ae3dfd8 7 #define RS_DATA 0x40
jksoft 0:b3cc6ae3dfd8 8
jksoft 0:b3cc6ae3dfd8 9 #define FUNC_SET1 0x38
jksoft 0:b3cc6ae3dfd8 10 #define FUNC_SET2 0x39
jksoft 0:b3cc6ae3dfd8 11 #define INT_OSC 0x14
jksoft 0:b3cc6ae3dfd8 12
jksoft 0:b3cc6ae3dfd8 13 #define CNTR_DEF 0x20
jksoft 0:b3cc6ae3dfd8 14
jksoft 0:b3cc6ae3dfd8 15 unsigned char icon_data[]=
jksoft 0:b3cc6ae3dfd8 16 {
jksoft 0:b3cc6ae3dfd8 17 0x00, 0x10,
jksoft 0:b3cc6ae3dfd8 18 0x02, 0x10,
jksoft 0:b3cc6ae3dfd8 19 0x04, 0x10,
jksoft 0:b3cc6ae3dfd8 20 0x06, 0x10,
jksoft 0:b3cc6ae3dfd8 21
jksoft 0:b3cc6ae3dfd8 22 0x07, 0x10,
jksoft 0:b3cc6ae3dfd8 23 0x07, 0x08,
jksoft 0:b3cc6ae3dfd8 24 0x09, 0x10,
jksoft 0:b3cc6ae3dfd8 25 0x0B, 0x10,
jksoft 0:b3cc6ae3dfd8 26
jksoft 0:b3cc6ae3dfd8 27 0x0D, 0x08,
jksoft 0:b3cc6ae3dfd8 28 0x0D, 0x04,
jksoft 0:b3cc6ae3dfd8 29 0x0D, 0x02,
jksoft 0:b3cc6ae3dfd8 30 0x0D, 0x10,
jksoft 0:b3cc6ae3dfd8 31
jksoft 0:b3cc6ae3dfd8 32 0x0F, 0x10,
jksoft 0:b3cc6ae3dfd8 33 };
jksoft 0:b3cc6ae3dfd8 34
jksoft 0:b3cc6ae3dfd8 35 I2cLCD::I2cLCD(PinName sda, PinName scl, PinName rp) : _rs( rp ) , _i2c( sda , scl ){
jksoft 0:b3cc6ae3dfd8 36
jksoft 0:b3cc6ae3dfd8 37 contrast = CNTR_DEF;
jksoft 0:b3cc6ae3dfd8 38 icon = 0;
jksoft 0:b3cc6ae3dfd8 39
jksoft 0:b3cc6ae3dfd8 40 wait(0.015);
jksoft 0:b3cc6ae3dfd8 41 // reset LOW->HIGH
jksoft 0:b3cc6ae3dfd8 42 _rs = 0;
jksoft 0:b3cc6ae3dfd8 43 wait(0.01);
jksoft 0:b3cc6ae3dfd8 44 _rs = 1;
jksoft 0:b3cc6ae3dfd8 45 wait(0.05);
jksoft 0:b3cc6ae3dfd8 46
jksoft 0:b3cc6ae3dfd8 47 writeCommand(FUNC_SET1);
jksoft 0:b3cc6ae3dfd8 48 writeCommand(FUNC_SET2);
jksoft 0:b3cc6ae3dfd8 49 writeCommand(INT_OSC);
jksoft 0:b3cc6ae3dfd8 50
jksoft 0:b3cc6ae3dfd8 51 writeCommand(0x70 | (contrast & 0xF));
jksoft 0:b3cc6ae3dfd8 52 writeCommand(0x5C | ((contrast >> 4) & 0x3));
jksoft 0:b3cc6ae3dfd8 53
jksoft 0:b3cc6ae3dfd8 54 writeCommand(0x6C);
jksoft 0:b3cc6ae3dfd8 55 wait(0.3);
jksoft 0:b3cc6ae3dfd8 56
jksoft 0:b3cc6ae3dfd8 57 writeCommand(0x38); // function set
jksoft 0:b3cc6ae3dfd8 58 writeCommand(0x0C); // Display On
jksoft 0:b3cc6ae3dfd8 59
jksoft 0:b3cc6ae3dfd8 60 cls(); // Clear Display
jksoft 0:b3cc6ae3dfd8 61
jksoft 0:b3cc6ae3dfd8 62 }
jksoft 0:b3cc6ae3dfd8 63
jksoft 0:b3cc6ae3dfd8 64
jksoft 0:b3cc6ae3dfd8 65
jksoft 0:b3cc6ae3dfd8 66 void I2cLCD::character(int column, int row, int c) {
jksoft 0:b3cc6ae3dfd8 67 int a = address(column, row);
jksoft 0:b3cc6ae3dfd8 68 writeCommand(a);
jksoft 0:b3cc6ae3dfd8 69 writeData(c);
jksoft 0:b3cc6ae3dfd8 70 }
jksoft 0:b3cc6ae3dfd8 71
jksoft 0:b3cc6ae3dfd8 72 void I2cLCD::cls() {
jksoft 0:b3cc6ae3dfd8 73 writeCommand(0x01); // cls, and set cursor to 0
jksoft 0:b3cc6ae3dfd8 74 wait(0.00164f); // This command takes 1.64 ms
jksoft 0:b3cc6ae3dfd8 75 locate(0, 0);
jksoft 0:b3cc6ae3dfd8 76 }
jksoft 0:b3cc6ae3dfd8 77
jksoft 0:b3cc6ae3dfd8 78 void I2cLCD::locate(int column, int row) {
jksoft 0:b3cc6ae3dfd8 79 _column = column;
jksoft 0:b3cc6ae3dfd8 80 _row = row;
jksoft 0:b3cc6ae3dfd8 81 }
jksoft 0:b3cc6ae3dfd8 82
jksoft 0:b3cc6ae3dfd8 83 int I2cLCD::_putc(int value) {
jksoft 0:b3cc6ae3dfd8 84 if (value == '\n') {
jksoft 0:b3cc6ae3dfd8 85 _column = 0;
jksoft 0:b3cc6ae3dfd8 86 _row++;
jksoft 0:b3cc6ae3dfd8 87 if (_row >= rows()) {
jksoft 0:b3cc6ae3dfd8 88 _row = 0;
jksoft 0:b3cc6ae3dfd8 89 }
jksoft 0:b3cc6ae3dfd8 90 } else {
jksoft 0:b3cc6ae3dfd8 91 character(_column, _row, value);
jksoft 0:b3cc6ae3dfd8 92 _column++;
jksoft 0:b3cc6ae3dfd8 93 if (_column >= columns()) {
jksoft 0:b3cc6ae3dfd8 94 _column = 0;
jksoft 0:b3cc6ae3dfd8 95 _row++;
jksoft 0:b3cc6ae3dfd8 96 if (_row >= rows()) {
jksoft 0:b3cc6ae3dfd8 97 _row = 0;
jksoft 0:b3cc6ae3dfd8 98 }
jksoft 0:b3cc6ae3dfd8 99 }
jksoft 0:b3cc6ae3dfd8 100 }
jksoft 0:b3cc6ae3dfd8 101 return value;
jksoft 0:b3cc6ae3dfd8 102 }
jksoft 0:b3cc6ae3dfd8 103
jksoft 0:b3cc6ae3dfd8 104 int I2cLCD::_getc() {
jksoft 0:b3cc6ae3dfd8 105 return -1;
jksoft 0:b3cc6ae3dfd8 106 }
jksoft 0:b3cc6ae3dfd8 107
jksoft 0:b3cc6ae3dfd8 108 void I2cLCD::writeCommand( int cmd )
jksoft 0:b3cc6ae3dfd8 109 {
jksoft 0:b3cc6ae3dfd8 110 char cmds[2];
jksoft 0:b3cc6ae3dfd8 111
jksoft 0:b3cc6ae3dfd8 112 cmds[0] = RS_CMD;
jksoft 0:b3cc6ae3dfd8 113 cmds[1] = cmd;
jksoft 0:b3cc6ae3dfd8 114
jksoft 0:b3cc6ae3dfd8 115 _i2c.write(I2CLCD_ADDR, cmds, 2);
jksoft 0:b3cc6ae3dfd8 116 }
jksoft 0:b3cc6ae3dfd8 117
jksoft 0:b3cc6ae3dfd8 118 void I2cLCD::writeData( int data )
jksoft 0:b3cc6ae3dfd8 119 {
jksoft 0:b3cc6ae3dfd8 120 char cmd[2];
jksoft 0:b3cc6ae3dfd8 121
jksoft 0:b3cc6ae3dfd8 122 cmd[0] = RS_DATA;
jksoft 0:b3cc6ae3dfd8 123 cmd[1] = data;
jksoft 0:b3cc6ae3dfd8 124
jksoft 0:b3cc6ae3dfd8 125 _i2c.write(I2CLCD_ADDR, cmd, 2);
jksoft 0:b3cc6ae3dfd8 126 }
jksoft 0:b3cc6ae3dfd8 127
jksoft 0:b3cc6ae3dfd8 128 int I2cLCD::address(int column, int row) {
jksoft 0:b3cc6ae3dfd8 129
jksoft 0:b3cc6ae3dfd8 130 return 0x80 + (row * 0x40) + column;
jksoft 0:b3cc6ae3dfd8 131 }
jksoft 0:b3cc6ae3dfd8 132
jksoft 0:b3cc6ae3dfd8 133 int I2cLCD::columns() {
jksoft 0:b3cc6ae3dfd8 134 return 16;
jksoft 0:b3cc6ae3dfd8 135 }
jksoft 0:b3cc6ae3dfd8 136
jksoft 0:b3cc6ae3dfd8 137 int I2cLCD::rows() {
jksoft 0:b3cc6ae3dfd8 138 return 2;
jksoft 0:b3cc6ae3dfd8 139 }
jksoft 0:b3cc6ae3dfd8 140
jksoft 0:b3cc6ae3dfd8 141 void I2cLCD::seticon(IconType type)
jksoft 0:b3cc6ae3dfd8 142 {
jksoft 0:b3cc6ae3dfd8 143 icon |= type;
jksoft 0:b3cc6ae3dfd8 144 puticon( icon );
jksoft 0:b3cc6ae3dfd8 145 }
jksoft 0:b3cc6ae3dfd8 146
jksoft 0:b3cc6ae3dfd8 147 void I2cLCD::clearicon(IconType type)
jksoft 0:b3cc6ae3dfd8 148 {
jksoft 0:b3cc6ae3dfd8 149 icon &= ~type;
jksoft 0:b3cc6ae3dfd8 150 puticon( icon );
jksoft 0:b3cc6ae3dfd8 151 }
jksoft 0:b3cc6ae3dfd8 152
jksoft 0:b3cc6ae3dfd8 153
jksoft 0:b3cc6ae3dfd8 154 void I2cLCD::puticon(int flg)
jksoft 0:b3cc6ae3dfd8 155 {
jksoft 0:b3cc6ae3dfd8 156 static unsigned char icon_buff[16];
jksoft 0:b3cc6ae3dfd8 157 unsigned char i;
jksoft 0:b3cc6ae3dfd8 158
jksoft 0:b3cc6ae3dfd8 159 for(i=0;i<sizeof(icon_data)/2;i++)
jksoft 0:b3cc6ae3dfd8 160 {
jksoft 0:b3cc6ae3dfd8 161 if(flg & (0x1000>>i))
jksoft 0:b3cc6ae3dfd8 162 {
jksoft 0:b3cc6ae3dfd8 163 icon_buff[icon_data[i*2]] |= icon_data[i*2+1];
jksoft 0:b3cc6ae3dfd8 164 }
jksoft 0:b3cc6ae3dfd8 165 else
jksoft 0:b3cc6ae3dfd8 166 {
jksoft 0:b3cc6ae3dfd8 167 icon_buff[icon_data[i*2]] &= ~icon_data[i*2+1];
jksoft 0:b3cc6ae3dfd8 168 }
jksoft 0:b3cc6ae3dfd8 169 }
jksoft 0:b3cc6ae3dfd8 170
jksoft 0:b3cc6ae3dfd8 171 for(i=0;i<16;i++){
jksoft 0:b3cc6ae3dfd8 172 writeCommand(0x39);
jksoft 0:b3cc6ae3dfd8 173 writeCommand(0x40+i);
jksoft 0:b3cc6ae3dfd8 174 writeData(icon_buff[i]);
jksoft 0:b3cc6ae3dfd8 175 }
jksoft 0:b3cc6ae3dfd8 176 }