Tamagawaseiki IMU2 Ver2

Dependencies:   mbed

Fork of AQM0802_test by Yoshihiro TSUBOI

Committer:
ytsuboi
Date:
Wed Jun 19 15:58:03 2013 +0000
Revision:
0:0543430a6bd1
Child:
1:bbac27fb013c
Revision 1.0 19-June-2013; First release.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ytsuboi 0:0543430a6bd1 1 /*
ytsuboi 0:0543430a6bd1 2 * LCD module "AQM0802A-RN-GBW" sample program
ytsuboi 0:0543430a6bd1 3 *
ytsuboi 0:0543430a6bd1 4 * AQM0802A-RN-GBW is an I2C text LCD panel (Controller: Sitronix ST7032i)
ytsuboi 0:0543430a6bd1 5 * I bought this from AKIZUKI DENSHI TSUSHO CO.,LTD.
ytsuboi 0:0543430a6bd1 6 * http://akizukidenshi.com/catalog/g/gP-06669/ (Online shop page (Japanese))
ytsuboi 0:0543430a6bd1 7 * http://akizukidenshi.com/download/ds/sitronix/st7032.pdf (datasheet of the chip)
ytsuboi 0:0543430a6bd1 8 *
ytsuboi 0:0543430a6bd1 9 * Original Arduino version was developed by
ytsuboi 0:0543430a6bd1 10 * Copyright (c) 2013 Masahiro WAKAYAMA at SWITCH SCIENCE
ytsuboi 0:0543430a6bd1 11 *
ytsuboi 0:0543430a6bd1 12 * Copyright (c) 2013 Yoshihiro TSUBOI
ytsuboi 0:0543430a6bd1 13 *
ytsuboi 0:0543430a6bd1 14 * Released under the MIT License: http://mbed.org/license/mit
ytsuboi 0:0543430a6bd1 15 *
ytsuboi 0:0543430a6bd1 16 * revision 1.0 19-June-2013 a. 1st release
ytsuboi 0:0543430a6bd1 17 *
ytsuboi 0:0543430a6bd1 18 */
ytsuboi 0:0543430a6bd1 19
ytsuboi 0:0543430a6bd1 20 #include "mbed.h"
ytsuboi 0:0543430a6bd1 21
ytsuboi 0:0543430a6bd1 22 I2C i2c(p28, p27); // sda, scl
ytsuboi 0:0543430a6bd1 23 const int AQCM0802_addr = 0x7C;
ytsuboi 0:0543430a6bd1 24
ytsuboi 0:0543430a6bd1 25 unsigned char mode;
ytsuboi 0:0543430a6bd1 26 unsigned char contrast = 0; // 0-63
ytsuboi 0:0543430a6bd1 27 unsigned char contrastFlag = false;
ytsuboi 0:0543430a6bd1 28 int CGcounter;
ytsuboi 0:0543430a6bd1 29 int FADEcounter;
ytsuboi 0:0543430a6bd1 30
ytsuboi 0:0543430a6bd1 31 void lcd_cmd(char x) {
ytsuboi 0:0543430a6bd1 32 char data[2];
ytsuboi 0:0543430a6bd1 33 data[0] = 0x00; // CO = 0,RS = 0
ytsuboi 0:0543430a6bd1 34 data[1] = x;
ytsuboi 0:0543430a6bd1 35 i2c.write(AQCM0802_addr, data, 2);
ytsuboi 0:0543430a6bd1 36 }
ytsuboi 0:0543430a6bd1 37
ytsuboi 0:0543430a6bd1 38 void lcd_contdata(char x) {
ytsuboi 0:0543430a6bd1 39 char data[2];
ytsuboi 0:0543430a6bd1 40 data[0] = 0xC0; //0b11000000 CO = 1, RS = 1
ytsuboi 0:0543430a6bd1 41 data[1] = x;
ytsuboi 0:0543430a6bd1 42 i2c.write(AQCM0802_addr, data, 2);
ytsuboi 0:0543430a6bd1 43 }
ytsuboi 0:0543430a6bd1 44
ytsuboi 0:0543430a6bd1 45 void lcd_lastdata(char x) {
ytsuboi 0:0543430a6bd1 46 char data[2];
ytsuboi 0:0543430a6bd1 47 data[0] = 0x40; //0b11000000 CO = 0, RS = 1
ytsuboi 0:0543430a6bd1 48 data[1] = x;
ytsuboi 0:0543430a6bd1 49 i2c.write(AQCM0802_addr, data, 2);
ytsuboi 0:0543430a6bd1 50 }
ytsuboi 0:0543430a6bd1 51
ytsuboi 0:0543430a6bd1 52 void lcd_printStr(const char *s) {
ytsuboi 0:0543430a6bd1 53 while(*s) {
ytsuboi 0:0543430a6bd1 54 if(*(s + 1)) {
ytsuboi 0:0543430a6bd1 55 lcd_contdata(*s);
ytsuboi 0:0543430a6bd1 56 } else {
ytsuboi 0:0543430a6bd1 57 lcd_lastdata(*s);
ytsuboi 0:0543430a6bd1 58 }
ytsuboi 0:0543430a6bd1 59 s++;
ytsuboi 0:0543430a6bd1 60 }
ytsuboi 0:0543430a6bd1 61 }
ytsuboi 0:0543430a6bd1 62
ytsuboi 0:0543430a6bd1 63 void lcd_printHex(unsigned char num) {
ytsuboi 0:0543430a6bd1 64 lcd_contdata(num);
ytsuboi 0:0543430a6bd1 65 }
ytsuboi 0:0543430a6bd1 66
ytsuboi 0:0543430a6bd1 67 void lcd_init() {
ytsuboi 0:0543430a6bd1 68 wait(0.04);
ytsuboi 0:0543430a6bd1 69 // LCD initialize
ytsuboi 0:0543430a6bd1 70 lcd_cmd(0x38); // function set
ytsuboi 0:0543430a6bd1 71 lcd_cmd(0x39); // function set
ytsuboi 0:0543430a6bd1 72 lcd_cmd(0x04); // EntryModeSet
ytsuboi 0:0543430a6bd1 73 lcd_cmd(0x14); // interval osc
ytsuboi 0:0543430a6bd1 74 lcd_cmd(0x70 | (contrast & 0xF)); // contrast Low
ytsuboi 0:0543430a6bd1 75 lcd_cmd(0x5C | ((contrast >> 4) & 0x3)); // contast High/icon/power
ytsuboi 0:0543430a6bd1 76 lcd_cmd(0x6C); // follower control
ytsuboi 0:0543430a6bd1 77 wait(0.2);
ytsuboi 0:0543430a6bd1 78 lcd_cmd(0x38); // function set
ytsuboi 0:0543430a6bd1 79 lcd_cmd(0x0C); // Display On
ytsuboi 0:0543430a6bd1 80 lcd_cmd(0x01); // Clear Display
ytsuboi 0:0543430a6bd1 81 wait(0.2); // need additional wait to Clear Display
ytsuboi 0:0543430a6bd1 82 }
ytsuboi 0:0543430a6bd1 83
ytsuboi 0:0543430a6bd1 84 void lcd_setCursor(unsigned char x,unsigned char y) {
ytsuboi 0:0543430a6bd1 85 lcd_cmd(0x80 | (y * 0x40 + x));
ytsuboi 0:0543430a6bd1 86 }
ytsuboi 0:0543430a6bd1 87
ytsuboi 0:0543430a6bd1 88 unsigned char cg[13 * 8] = {
ytsuboi 0:0543430a6bd1 89 /*
ytsuboi 0:0543430a6bd1 90 0b00001111,0b00010000,0b00010000,0b00001110,0b00000001,0b00000001,0b00011110,0b00000000,
ytsuboi 0:0543430a6bd1 91 0b00010001,0b00010001,0b00010001,0b00010101,0b00010101,0b00010101,0b00001010,0b00000000,
ytsuboi 0:0543430a6bd1 92 0b00001110,0b00000100,0b00000100,0b00000100,0b00000100,0b00000100,0b00001110,0b00000000,
ytsuboi 0:0543430a6bd1 93 0b00011111,0b00000100,0b00000100,0b00000100,0b00000100,0b00000100,0b00000100,0b00000000,
ytsuboi 0:0543430a6bd1 94 0b00001110,0b00010001,0b00010000,0b00010000,0b00010000,0b00010001,0b00001110,0b00000000,
ytsuboi 0:0543430a6bd1 95 0b00010001,0b00010001,0b00010001,0b00011111,0b00010001,0b00010001,0b00010001,0b00000000,
ytsuboi 0:0543430a6bd1 96 */
ytsuboi 0:0543430a6bd1 97 0x0F,0x10,0x10,0x0E,0x01,0x01,0x1E,0x00, // S
ytsuboi 0:0543430a6bd1 98 0x11,0x11,0x11,0x15,0x15,0x15,0x0A,0x00, // W
ytsuboi 0:0543430a6bd1 99 0x0E,0x40,0x40,0x40,0x40,0x40,0x0E,0x00, // I
ytsuboi 0:0543430a6bd1 100 0x1F,0x40,0x40,0x40,0x40,0x40,0x40,0x00, // T
ytsuboi 0:0543430a6bd1 101 0x0E,0x11,0x10,0x10,0x10,0x11,0x0E,0x00, // C
ytsuboi 0:0543430a6bd1 102 0x11,0x11,0x11,0x1F,0x11,0x11,0x11,0x00, // H
ytsuboi 0:0543430a6bd1 103 /*
ytsuboi 0:0543430a6bd1 104 0b00001111,0b00010000,0b00010000,0b00001110,0b00000001,0b00000001,0b00011110,0b00000000,
ytsuboi 0:0543430a6bd1 105 0b00001110,0b00010001,0b00010000,0b00010000,0b00010000,0b00010001,0b00001110,0b00000000,
ytsuboi 0:0543430a6bd1 106 0b00001110,0b00000100,0b00000100,0b00000100,0b00000100,0b00000100,0b00001110,0b00000000,
ytsuboi 0:0543430a6bd1 107 0b00011111,0b00010000,0b00010000,0b00011110,0b00010000,0b00010000,0b00011111,0b00000000,
ytsuboi 0:0543430a6bd1 108 0b00010001,0b00010001,0b00011001,0b00010101,0b00010011,0b00010001,0b00010001,0b00000000,
ytsuboi 0:0543430a6bd1 109 0b00001110,0b00010001,0b00010000,0b00010000,0b00010000,0b00010001,0b00001110,0b00000000,
ytsuboi 0:0543430a6bd1 110 0b00011111,0b00010000,0b00010000,0b00011110,0b00010000,0b00010000,0b00011111,0b00000000,
ytsuboi 0:0543430a6bd1 111 */
ytsuboi 0:0543430a6bd1 112 0x0F,0x10,0x10,0x0E,0x01,0x01,0x1E,0x00, // S
ytsuboi 0:0543430a6bd1 113 0x0E,0x11,0x10,0x10,0x10,0x11,0x0E,0x00, // C
ytsuboi 0:0543430a6bd1 114 0x0E,0x40,0x40,0x40,0x40,0x40,0x0E,0x00, // I
ytsuboi 0:0543430a6bd1 115 0x1F,0x10,0x10,0x1E,0x10,0x10,0x1F,0x00, // E
ytsuboi 0:0543430a6bd1 116 0x11,0x11,0x19,0x15,0x13,0x11,0x11,0x00, // N
ytsuboi 0:0543430a6bd1 117 0x0E,0x11,0x10,0x10,0x10,0x11,0x0E,0x00, // C
ytsuboi 0:0543430a6bd1 118 0x1F,0x10,0x10,0x1E,0x10,0x10,0x1F,0x00 // E
ytsuboi 0:0543430a6bd1 119 };
ytsuboi 0:0543430a6bd1 120
ytsuboi 0:0543430a6bd1 121 void setCG(int src,int dst,int len) {
ytsuboi 0:0543430a6bd1 122 lcd_cmd(0x38);
ytsuboi 0:0543430a6bd1 123 lcd_cmd(0x40 + dst);
ytsuboi 0:0543430a6bd1 124 if (src >= 0) {
ytsuboi 0:0543430a6bd1 125 for (int i = 0;i < len;i++) lcd_printHex(cg[src + i]);
ytsuboi 0:0543430a6bd1 126 } else {
ytsuboi 0:0543430a6bd1 127 for (int i = 0;i < len;i++) lcd_printHex(0);
ytsuboi 0:0543430a6bd1 128 }
ytsuboi 0:0543430a6bd1 129 }
ytsuboi 0:0543430a6bd1 130
ytsuboi 0:0543430a6bd1 131 void setContrast(unsigned char c) {
ytsuboi 0:0543430a6bd1 132 lcd_cmd(0x39);
ytsuboi 0:0543430a6bd1 133 lcd_cmd(0x70 | (c & 0x0f)); // contrast Low
ytsuboi 0:0543430a6bd1 134 lcd_cmd(0x5C | ((c >> 4) & 0x03)); // contast High/icon/power
ytsuboi 0:0543430a6bd1 135 lcd_cmd(0x38);
ytsuboi 0:0543430a6bd1 136 }
ytsuboi 0:0543430a6bd1 137
ytsuboi 0:0543430a6bd1 138 int main() {
ytsuboi 0:0543430a6bd1 139 lcd_init();
ytsuboi 0:0543430a6bd1 140 while(1) {
ytsuboi 0:0543430a6bd1 141 switch(mode) {
ytsuboi 0:0543430a6bd1 142 case 0: // init
ytsuboi 0:0543430a6bd1 143 lcd_setCursor(0, 0);
ytsuboi 0:0543430a6bd1 144 for (int i = 0;i < 6;i++) lcd_printHex(i);
ytsuboi 0:0543430a6bd1 145 lcd_setCursor(1, 1);
ytsuboi 0:0543430a6bd1 146 lcd_printStr(" ");
ytsuboi 0:0543430a6bd1 147 setCG(-1,0,6 * 8);
ytsuboi 0:0543430a6bd1 148 contrast = 35;
ytsuboi 0:0543430a6bd1 149 setContrast(contrast);
ytsuboi 0:0543430a6bd1 150 CGcounter = 0;
ytsuboi 0:0543430a6bd1 151 mode++;
ytsuboi 0:0543430a6bd1 152 break;
ytsuboi 0:0543430a6bd1 153 case 1: // SWITCH
ytsuboi 0:0543430a6bd1 154 if(CGcounter <= (6 * 8)) {
ytsuboi 0:0543430a6bd1 155 setCG((CGcounter / 8) * 8,((CGcounter / 8) * 8) + 7 - (CGcounter % 8),CGcounter % 8);
ytsuboi 0:0543430a6bd1 156 CGcounter++;
ytsuboi 0:0543430a6bd1 157 } else {
ytsuboi 0:0543430a6bd1 158 lcd_setCursor(0, 0);
ytsuboi 0:0543430a6bd1 159 lcd_printStr("SWITCH");
ytsuboi 0:0543430a6bd1 160 setCG(-1,0,3 * 8);
ytsuboi 0:0543430a6bd1 161 lcd_setCursor(1, 1);
ytsuboi 0:0543430a6bd1 162 for (int i = 0;i < 3;i++) lcd_printHex(i);
ytsuboi 0:0543430a6bd1 163 CGcounter = 0;
ytsuboi 0:0543430a6bd1 164 mode++;
ytsuboi 0:0543430a6bd1 165 }
ytsuboi 0:0543430a6bd1 166 break;
ytsuboi 0:0543430a6bd1 167 case 2: // SCI
ytsuboi 0:0543430a6bd1 168 if(CGcounter <= (3 * 8)) {
ytsuboi 0:0543430a6bd1 169 setCG(((CGcounter / 8) + 6) * 8,((CGcounter / 8) * 8) + 7 - (CGcounter % 8),CGcounter % 8);
ytsuboi 0:0543430a6bd1 170 CGcounter++;
ytsuboi 0:0543430a6bd1 171 } else {
ytsuboi 0:0543430a6bd1 172 lcd_setCursor(1, 1);
ytsuboi 0:0543430a6bd1 173 lcd_printStr("SCI");
ytsuboi 0:0543430a6bd1 174 setCG(-1,0,4 * 8);
ytsuboi 0:0543430a6bd1 175 lcd_setCursor(4, 1);
ytsuboi 0:0543430a6bd1 176 for (int i = 0;i < 4;i++) lcd_printHex(i);
ytsuboi 0:0543430a6bd1 177 CGcounter = 0;
ytsuboi 0:0543430a6bd1 178 mode++;
ytsuboi 0:0543430a6bd1 179 }
ytsuboi 0:0543430a6bd1 180 break;
ytsuboi 0:0543430a6bd1 181 case 3: // ENCE
ytsuboi 0:0543430a6bd1 182 if(CGcounter <= (4 * 8)) {
ytsuboi 0:0543430a6bd1 183 setCG(((CGcounter / 8) + 9) * 8,((CGcounter / 8) * 8) + 7 - (CGcounter % 8),CGcounter % 8);
ytsuboi 0:0543430a6bd1 184 CGcounter++;
ytsuboi 0:0543430a6bd1 185 } else {
ytsuboi 0:0543430a6bd1 186 lcd_setCursor(4, 1);
ytsuboi 0:0543430a6bd1 187 lcd_printStr("ENCE");
ytsuboi 0:0543430a6bd1 188 FADEcounter = 0;
ytsuboi 0:0543430a6bd1 189 mode++;
ytsuboi 0:0543430a6bd1 190 }
ytsuboi 0:0543430a6bd1 191 break;
ytsuboi 0:0543430a6bd1 192 case 4:
ytsuboi 0:0543430a6bd1 193 if (contrastFlag == false) {
ytsuboi 0:0543430a6bd1 194 if (++contrast >= 54) contrastFlag = true;
ytsuboi 0:0543430a6bd1 195 } else {
ytsuboi 0:0543430a6bd1 196 if (--contrast <= 17) {
ytsuboi 0:0543430a6bd1 197 contrastFlag = false;
ytsuboi 0:0543430a6bd1 198 if(++FADEcounter >= 2) {
ytsuboi 0:0543430a6bd1 199 mode = 0;
ytsuboi 0:0543430a6bd1 200 }
ytsuboi 0:0543430a6bd1 201 }
ytsuboi 0:0543430a6bd1 202 }
ytsuboi 0:0543430a6bd1 203 setContrast(contrast);
ytsuboi 0:0543430a6bd1 204 break;
ytsuboi 0:0543430a6bd1 205 }
ytsuboi 0:0543430a6bd1 206 wait(0.05);
ytsuboi 0:0543430a6bd1 207 }
ytsuboi 0:0543430a6bd1 208 }