I2CLCDを使ってみる

左側は一行8文字のミニLCD、右側は1行16文字のLCDです。
左側の写真の上側右に見えている抵抗2つはプルアップ抵抗です。
I2CのポートはこのCPUでは一組のみです。P19(PB6, SCL, 白)。P18(PB7, SDA, 橙)。



ミニLCDのピン配置
リセット端子は使わないと思うので、Vddに繋ぎます。



コンデンサの配線


普通のLCDのピン配置
リセット端子は使わないと思うので、Vddに繋ぎます。



LCD表示テスト(main.cpp)



main.cpp
        public void main()
        {
            String str = "Mizuno Makoto\n";
            int i = 0;
            I2CLCD lcd = new I2CLCD(16);
            while (true)
            {
                lcd.write(str);
                CPU.delay(1000);
                lcd.clear();
                lcd.write("Hello");
                CPU.delay(500);
                lcd.clear();
                lcd.write(i++);
                CPU.delay(500);
                lcd.write("\n");
                lcd.write(i++, 6, true);
                CPU.delay(500);
                lcd.clear();
            }
        }
 1 Mainは別にある
 2 
 3 
 4 
 5 1行16文字で二行あるLCDタイプ
 6 文字数/行をコンストラクタに渡す
 7 
 8 
 9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 


I2CLCDクラス



I2CLCD.cs
#define GHIE
//#define SecretLabs

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
#if GHIE
using GHI.OSHW.Hardware;        // FEZCerberus使用のため追加、参照設定にも追加
#elif SecretLabs
using SecretLabs.NETMF.Hardware;
#endif
using BoeBotLib;
namespace BoeBotLib
{
    class I2CLCD
    {
        // Set Constants for Address and Clock Frequency
        const byte LCD_ADDRESS = 0x3E; 
        const int CLOCK_FREQ = 100;
        const int TIMEOUT = 1000;
        int chCount;
        int columnNumber = 16;

        //Write Buffer
        byte[] WriteBuffer = new byte[3];

        //Init I2C Device Config
        I2CDevice.Configuration config =
            new I2CDevice.Configuration(LCD_ADDRESS, CLOCK_FREQ);

        //Init I2C Device & LCD initialize
        I2CDevice i2c;

        public I2CLCD()
        {
            this.columnNumber = 16;
            i2c = new I2CDevice(config);
            init();
        }
        public I2CLCD(int columnNumber)
        {
            this.columnNumber = columnNumber;
            i2c = new I2CDevice(config);
            init();
        }

        public void write(byte control, byte data)
        {
            i2c.Execute(new I2CDevice.I2CTransaction[1] {
                I2CDevice.CreateWriteTransaction(new Byte[] { control, data }) }, TIMEOUT);
            //Thread.Sleep(5);
        }

        void wait_us(int tm)
        {
            BoeBotLib.Timer.delay(tm * 10);
        }
        void wait_ms(int tm)
        {
            BoeBotLib.CPU.delay(tm);
        }
        void init()
        {
            int Wait = 50;
            chCount = 0;
            wait_ms(40);
            // LCD_WRITE= 0x7c;
            write(0x0, 0x38);   //data8bit, numberOfLine=2
            wait_us(Wait);
            write(0x0, 0x39);   //data8bit, numberOfLine=2
            wait_us(Wait);
            write(0x0, 0x14);   //internal OSC frequency
            wait_us(Wait);
            write(0x0, 0x7A);   // contrast
            wait_us(Wait);
            write(0x0, 0x5F);   // power/icon/contrast control
            wait_us(Wait);
            write(0x0, 0x6a);   // follower control
            wait_ms(200);          // 200ms
            write(0x0, 0x0c);   // Display ON
            wait_us(Wait);

            // write(0x0, 0x0E);   // cursor on, blink
            // wait_us(Wait);

            write(0x0, 0x01);   // clear disply
            wait_us(2000);         // 2ms
            write(0x00, 0x06);   // entry mode set
            wait_us(Wait);
        }

        public void write(byte ch)
        {
            int x;
            //int cn2 = columnNumber / 2;
            if (ch == 0x0a)
            {
                x = chCount % (columnNumber * 2);
                if (x < columnNumber)
                {
                    write(0x00, 0xc0);
                    chCount = columnNumber;
                }
                else
                {
                    write(0x00, 0x01); // clear display
                    chCount = 0;
                    wait_ms(2);
                }
            }
            else
            {
                write(0x40, ch);
                x = ++chCount % (columnNumber * 2);
                if (x == columnNumber) write(0x00, 0xc0);
                else if (x == 0) home();
            }
        }

        public void write(String str)
        {
            foreach (byte ch in str) write(ch);
        }

        public void write(byte[] str, int n)
        {
            for (int i = 0; i < n; i++) write(str[i]);
        }

        public void clear()
        {
            write(0x00, 0x01); // clear display
            chCount = 0;
            wait_ms(2);
        }

        public void home()
        {
            write(0x00, 0x02); // return to home
            chCount = 0;
            wait_ms(2);
        }

        public void write(int x)
        {
            byte[] num = new byte[5];
            int n = 0;
            if (x < 0)
            {     // マイナスの値の場合
                write((byte)'-');
                x *= -1;
            }
            do
            {
                num[n++] = (byte)(x % 10 + (int)'0');
                x /= 10;
            } while (x != 0);
            while (--n >= 0) write(num[n]);
        }

        public void write(int x, int column, bool zeroPad)
        {
            byte[] Buffer = new byte[10];
            byte[] num = new byte[5];
            int i, j, n = 0;
            bool signFlag = false;
            if (x < 0)
            {     // マイナスの値の場合
                signFlag = true;
                x *= -1;
            }
            do
            {
                num[n++] = (byte)(x % 10);
                x /= 10;
            } while (x != 0);
            i = column - n;
            if (i > 0)
            {
                for (i = 0; i < 10; i++) Buffer[i] = (byte)' ';
                if (zeroPad) for (i = 0; i < column - n; i++) Buffer[i] = (byte)'0';
                Buffer[9] = 0;
                for (i = column - n, j = n - 1; i < column; i++, j--) 
                    Buffer[i] = (byte)(num[j] | '0');
                Buffer[i] = 0;
                if (signFlag) Buffer[column - n - 1] = (byte)'-';
            }
            else
            {
                j = 0;
                if (signFlag) Buffer[j++] = (byte)'-';
                for (i = --n; i >= 0; i--) Buffer[j++] = (byte)(num[i] | '0');
                Buffer[j] = 0;
            }
            write(Buffer, column);
        }

        public void setCG(bool mode, byte[] cgData)
        {
            int i, j;
            write(0x00, 0x38);     // Function set IS= 0
            write(0x00, 0x40);     // CGRAM set address= 0
            if (mode)
            {            // StringModeで呼ばれた時はデフォールトにセット
                for (i = 0; i < 8; i++)
                {
                    for (j = 0; j < 8; j++)
                    {
                        if (j < (7 - i)) write(0x40, 0x00);
                        else write(0x40, 0x1f);     // 横棒をセット
                    }
                }
            }
            else
            {
                for (i = 0; i < 64; i++) write(0x40, cgData[i]);
            }
        }


        public void setIcon(int x)
        {
            write(0x00, 0x39);    // Function set
            write(0x00, 0x40 | 0x0f);   // flower icon
            if ((x & 1)!=0) write(0x40, 0x10);
            else write(0x40, 0x00);

            write(0x00, 0x40 | 0x0d);   // Battery icon
            write(0x40, 0);
            if ((x & 0x0e)!=0)
            {
                write(0x00, 0x40 | 0x0d);   // Battery icon
                write(0x40, (byte)(((x & 0x0e) << 1) | 2));
            }

            write(0x00, 0x40 | 0x0b);   // no sound icon
            if ((x & 0x10) != 0) write(0x40, 0x10);
            else write(0x40, 0);

            write(0x00, 0x40 | 0x09);   // Key icon
            if ((x & 0x20) != 0) write(0x40, 0x10);
            else write(0x40, 0);

            write(0x00, 0x40 | 0x07);   // 矢印 icon
            write(0x40, 0);
            if ((x & 0xC0) != 0)
            {
                write(0x00, 0x40 | 0x07);   // 矢印 icon
                write(0x40, (byte)((x & 0xC0) >> 3));
            }

            write(0x00, 0x40 | 0x06);   // enter icon
            if ((x & 0x100) != 0) write(0x40, 0x10);
            else write(0x40, 0);
            write(0x00, 0x40 | 0x04);   // ring icon
            if ((x & 0x200) != 0) write(0x40, 0x10);
            else write(0x40, 0);
            write(0x00, 0x40 | 0x02);   // 電話 icon
            if ((x & 0x400) != 0) write(0x40, 0x10);
            else write(0x40, 0);
            write(0x00, 0x40);    // antena icon
            if ((x & 0x800) != 0) write(0x40, 0x10);
            else write(0x40, 0);
            write(0x00, 0x38);    // Function set
        }
    }
}
 1 GHI Electronics のクラスを用いる
 2 SecretLabs のクラスを用いる
 3 以下基本的にGHIEのプログラムになっている
 4 SecretLabsはひょっとしたら動く
 5 
 6 
 7 
 8 
 9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 一行16文字がデフォールト
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 一行16文字がデフォールト
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 カーソルを使用するときには
85 コメントをはずす
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 
101
102
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 
147 
148 
149 
150 
151 
152 
153 
154 
155 
156 
157 
158 
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 
170 
171 
172 
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
191 
192 
193 
194 
195 
196 
197 
198 
199 
200 
201 
202 
203 
204 
205 
206 
207 
208 
209 
210 
211 
212 
213 
214 
215 
216 
217 
218 
219 
220 
221 
222 ちゃんとしたテストはしていない
223 
224 
225 
226 
227 
228 
229 
230 
231 
232 
233 
234 
235 
236 
237 
238 
239 
240 
241 
242 
243 
244 
245 
246 
247 
248 
249 
250 
251 
252 
253 
254 
255 
256 
257 
258 
259 
260 
261 
262 
263 
264 
265 
266 
267 
268