aitendo C128X64SPI-12P-M with FRDM KL25Z

Dependencies:   mbed

/media/uploads/masato/c128x64spi-12p_2.jpg

aitendo の FSTN液晶モジュール(128x64/SPI)[C128X64SPI-12P-M] を KL25Z につないでみました。今のところ、ドット、ライン操作までしか用意できていません。コントローラ仕様によるとデバイスからデータを取得することができるのですが、シリアル(SPI)ではできないことになっているため、フレームバッファは自前で持っています。テスト用にドラゴン曲線を表示させてみました。

ところで aitendo の商品説明ページ中の FPCケーブル取り付け例の写真は誤っていました(修正された模様)。また、キャリーボードでの LCD とバックライトの GND と電源は直結されているようです。ついでにサンプルのコードでのフレームバッファの使い方にも誤りがありますからドット単位で操作する場合には注意が必要です。

2014.03.12
q61.org さんの Chibimo 表示器として使えるようにしてみました。ドラゴン曲線を描いた後、ホストからの Chibimo データを処理します。KL05 とトラ技 ARM ライタ基板でも動作を確認していますが、Chibimo として使うには USB シリアルの使える KL* が良いでしょう。

/media/uploads/masato/12876787564_6915e1f553_z.jpg

Committer:
masato
Date:
Mon Mar 03 03:28:29 2014 +0000
Revision:
1:84b2d36d57f0
Parent:
0:b06afbefd350
supports Chibimo; http://q61.org/chibimo/build/

Who changed what in which revision?

UserRevisionLine numberNew contents of line
masato 0:b06afbefd350 1 #include "mbed.h"
masato 0:b06afbefd350 2 #include "c128x64spi.h"
masato 0:b06afbefd350 3
masato 0:b06afbefd350 4 #define PAGE_SEL 0xB0
masato 0:b06afbefd350 5 #define COL_SEL 0x10
masato 0:b06afbefd350 6
masato 0:b06afbefd350 7 const char init_cmd[] = {
masato 0:b06afbefd350 8 0xE2, // S/W RESET
masato 0:b06afbefd350 9 0xA3, // LCD bias
masato 0:b06afbefd350 10 // 0xAF, // Display ON
masato 0:b06afbefd350 11 0xA0, // segment direction
masato 0:b06afbefd350 12 0xC8, // Common direction
masato 0:b06afbefd350 13 0x22, // Regulation register select
masato 0:b06afbefd350 14 0x81, // EV select
masato 0:b06afbefd350 15 0x2F, // Select EV value
masato 0:b06afbefd350 16 0x2F, // Power control
masato 0:b06afbefd350 17 //
masato 0:b06afbefd350 18 0x40, // initialy display line 40
masato 0:b06afbefd350 19 0xB0, // set page address
masato 0:b06afbefd350 20 0x10, // set column addr MSB
masato 0:b06afbefd350 21 0x00, // set column addr LSB
masato 0:b06afbefd350 22 // 0xAF, // display ON
masato 0:b06afbefd350 23 0xA4, // A5 .normal display, all pixels OFF
masato 0:b06afbefd350 24 0xA6, // A7 .normal display (inverse pixel)
masato 0:b06afbefd350 25 0xAF, // display ON
masato 0:b06afbefd350 26 };
masato 0:b06afbefd350 27
masato 0:b06afbefd350 28 c128x64spi::c128x64spi(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName rs, PinName reset)
masato 0:b06afbefd350 29 : _spi(mosi, miso, sclk), _cs(cs), _rs(rs), _reset(reset) {
masato 0:b06afbefd350 30 int i, j;
masato 0:b06afbefd350 31
masato 0:b06afbefd350 32 for (i = 0; i < 8; i++)
masato 0:b06afbefd350 33 for (j = 0; j < 128; j++)
masato 0:b06afbefd350 34 gfx_buf[i][j] = 0;
masato 0:b06afbefd350 35
masato 0:b06afbefd350 36 // _spi.format(8, 3); // 8 bit spi mode 3
masato 1:84b2d36d57f0 37 _spi.frequency(16000000); // 8MHz SPI clock ..
masato 0:b06afbefd350 38
masato 0:b06afbefd350 39 _cs = 0;
masato 0:b06afbefd350 40 _reset = 0; // reset
masato 0:b06afbefd350 41 wait_ms(500);
masato 0:b06afbefd350 42 _reset = 1;
masato 0:b06afbefd350 43
masato 0:b06afbefd350 44 for (i = 0; i < sizeof(init_cmd); i++) {
masato 0:b06afbefd350 45 wr_cmd(init_cmd[i]);
masato 0:b06afbefd350 46 }
masato 0:b06afbefd350 47 }
masato 0:b06afbefd350 48
masato 0:b06afbefd350 49 void c128x64spi::hline(int x0, int x1, int y, int color) {
masato 0:b06afbefd350 50 for (int x = x0; x < x1; x++)
masato 0:b06afbefd350 51 pixel(x, y, color);
masato 0:b06afbefd350 52 }
masato 0:b06afbefd350 53
masato 0:b06afbefd350 54 void c128x64spi::vline(int x, int y0, int y1, int color) {
masato 0:b06afbefd350 55 for (int y = y0; y < y1; y++) {
masato 0:b06afbefd350 56 pixel(x, y, color);
masato 0:b06afbefd350 57 }
masato 0:b06afbefd350 58 }
masato 0:b06afbefd350 59
masato 0:b06afbefd350 60 void c128x64spi::line(int x0, int y0, int x1, int y1, int color) {
masato 0:b06afbefd350 61 #if 1
masato 0:b06afbefd350 62 int dx = 0, dy = 0;
masato 0:b06afbefd350 63 int dx_sym = 0, dy_sym = 0;
masato 0:b06afbefd350 64 int dx_x2 = 0, dy_x2 = 0;
masato 0:b06afbefd350 65 int di = 0;
masato 0:b06afbefd350 66
masato 0:b06afbefd350 67 dx = x1-x0;
masato 0:b06afbefd350 68 dy = y1-y0;
masato 0:b06afbefd350 69
masato 0:b06afbefd350 70 if (dx == 0) { /* vertical line */
masato 0:b06afbefd350 71 if (y1 > y0) vline(x0,y0,y1,color);
masato 0:b06afbefd350 72 else vline(x0,y1,y0,color);
masato 0:b06afbefd350 73 return;
masato 0:b06afbefd350 74 }
masato 0:b06afbefd350 75
masato 0:b06afbefd350 76 if (dx > 0) {
masato 0:b06afbefd350 77 dx_sym = 1;
masato 0:b06afbefd350 78 } else {
masato 0:b06afbefd350 79 dx_sym = -1;
masato 0:b06afbefd350 80 }
masato 0:b06afbefd350 81 if (dy == 0) { /* horizontal line */
masato 0:b06afbefd350 82 if (x1 > x0) hline(x0,x1,y0,color);
masato 0:b06afbefd350 83 else hline(x1,x0,y0,color);
masato 0:b06afbefd350 84 return;
masato 0:b06afbefd350 85 }
masato 0:b06afbefd350 86
masato 0:b06afbefd350 87 if (dy > 0) {
masato 0:b06afbefd350 88 dy_sym = 1;
masato 0:b06afbefd350 89 } else {
masato 0:b06afbefd350 90 dy_sym = -1;
masato 0:b06afbefd350 91 }
masato 0:b06afbefd350 92
masato 0:b06afbefd350 93 dx = dx_sym*dx;
masato 0:b06afbefd350 94 dy = dy_sym*dy;
masato 0:b06afbefd350 95
masato 0:b06afbefd350 96 dx_x2 = dx*2;
masato 0:b06afbefd350 97 dy_x2 = dy*2;
masato 0:b06afbefd350 98
masato 0:b06afbefd350 99 if (dx >= dy) {
masato 0:b06afbefd350 100 di = dy_x2 - dx;
masato 0:b06afbefd350 101 while (x0 != x1) {
masato 0:b06afbefd350 102
masato 0:b06afbefd350 103 pixel(x0, y0, color);
masato 0:b06afbefd350 104 x0 += dx_sym;
masato 0:b06afbefd350 105 if (di<0) {
masato 0:b06afbefd350 106 di += dy_x2;
masato 0:b06afbefd350 107 } else {
masato 0:b06afbefd350 108 di += dy_x2 - dx_x2;
masato 0:b06afbefd350 109 y0 += dy_sym;
masato 0:b06afbefd350 110 }
masato 0:b06afbefd350 111 }
masato 0:b06afbefd350 112 pixel(x0, y0, color);
masato 0:b06afbefd350 113 } else {
masato 0:b06afbefd350 114 di = dx_x2 - dy;
masato 0:b06afbefd350 115 while (y0 != y1) {
masato 0:b06afbefd350 116 pixel(x0, y0, color);
masato 0:b06afbefd350 117 y0 += dy_sym;
masato 0:b06afbefd350 118 if (di < 0) {
masato 0:b06afbefd350 119 di += dx_x2;
masato 0:b06afbefd350 120 } else {
masato 0:b06afbefd350 121 di += dx_x2 - dy_x2;
masato 0:b06afbefd350 122 x0 += dx_sym;
masato 0:b06afbefd350 123 }
masato 0:b06afbefd350 124 }
masato 0:b06afbefd350 125 pixel(x0, y0, color);
masato 0:b06afbefd350 126 }
masato 0:b06afbefd350 127 #else
masato 0:b06afbefd350 128 int dx, dy;
masato 0:b06afbefd350 129 int sx, sy;
masato 0:b06afbefd350 130 int chg;
masato 0:b06afbefd350 131 int e;
masato 0:b06afbefd350 132 int i;
masato 0:b06afbefd350 133 int tmp;
masato 0:b06afbefd350 134
masato 0:b06afbefd350 135 if (x0 > x1) {
masato 0:b06afbefd350 136 sx = -1; dx = x0 - x1;
masato 0:b06afbefd350 137 } else {
masato 0:b06afbefd350 138 sx = 1; dx = x1 - x0;
masato 0:b06afbefd350 139 }
masato 0:b06afbefd350 140 if (y0 > y1) {
masato 0:b06afbefd350 141 sy = -1; dy = y0 - y1;
masato 0:b06afbefd350 142 } else {
masato 0:b06afbefd350 143 sy = 1; dy = y1 - y0;
masato 0:b06afbefd350 144 }
masato 0:b06afbefd350 145
masato 0:b06afbefd350 146 if (dy > dx) {
masato 0:b06afbefd350 147 tmp = dx; dx = dy; dy = tmp;
masato 0:b06afbefd350 148 chg = 1;
masato 0:b06afbefd350 149 } else {
masato 0:b06afbefd350 150 chg = 0;
masato 0:b06afbefd350 151 }
masato 0:b06afbefd350 152
masato 0:b06afbefd350 153 e = (dy << 1) - dx;
masato 0:b06afbefd350 154 for (i = 0; i <= dx; i++) {
masato 0:b06afbefd350 155 pixel(x0, y0, color);
masato 0:b06afbefd350 156 if (e >= 0) {
masato 0:b06afbefd350 157 if (chg)
masato 0:b06afbefd350 158 x0 += sx;
masato 0:b06afbefd350 159 else
masato 0:b06afbefd350 160 y0 += sy;
masato 0:b06afbefd350 161 e -= (dx << 1);
masato 0:b06afbefd350 162 }
masato 0:b06afbefd350 163 if (chg)
masato 0:b06afbefd350 164 y0 += sy;
masato 0:b06afbefd350 165 else
masato 0:b06afbefd350 166 x0 += sx;
masato 0:b06afbefd350 167 e += (dy << 1);
masato 0:b06afbefd350 168 }
masato 0:b06afbefd350 169 #endif
masato 0:b06afbefd350 170 }
masato 0:b06afbefd350 171
masato 0:b06afbefd350 172 void c128x64spi::clr(int sw) {
masato 0:b06afbefd350 173 int x, y;
masato 0:b06afbefd350 174
masato 0:b06afbefd350 175 if (sw) sw = 0xff;
masato 0:b06afbefd350 176
masato 0:b06afbefd350 177 for (y = 0; y < 8; y++) {
masato 0:b06afbefd350 178 // int page = y / 8;
masato 0:b06afbefd350 179 // wr_cmd(PAGE_SEL | y);
masato 0:b06afbefd350 180 for (x = 0; x < 128; x++) {
masato 0:b06afbefd350 181 wr_cmd(PAGE_SEL | y);
masato 0:b06afbefd350 182 int col = ((x & 0xF0) >> 4) | 0x10;
masato 0:b06afbefd350 183 wr_cmd(COL_SEL | col);
masato 0:b06afbefd350 184 wr_cmd(x & 0xf);
masato 0:b06afbefd350 185
masato 0:b06afbefd350 186 gfx_buf[y][x] = sw;
masato 0:b06afbefd350 187 wr_dat(sw);
masato 0:b06afbefd350 188 }
masato 0:b06afbefd350 189 }
masato 0:b06afbefd350 190 }
masato 0:b06afbefd350 191
masato 0:b06afbefd350 192 void c128x64spi::pixel(int x, int y, int sw) {
masato 0:b06afbefd350 193 if ((x < 0) || (x >= 128)) return;
masato 0:b06afbefd350 194 if ((y < 0) || (y >= 64)) return;
masato 0:b06afbefd350 195
masato 0:b06afbefd350 196 int page, col;
masato 0:b06afbefd350 197 int row_in_page;
masato 0:b06afbefd350 198
masato 0:b06afbefd350 199 page = y / 8;
masato 0:b06afbefd350 200
masato 0:b06afbefd350 201 /* Selecting Page */
masato 0:b06afbefd350 202 wr_cmd(PAGE_SEL | page);
masato 0:b06afbefd350 203
masato 0:b06afbefd350 204 /* Selecting Column */
masato 0:b06afbefd350 205 col = ((x & 0xF0) >> 4) | 0x10;
masato 0:b06afbefd350 206 wr_cmd(COL_SEL|col);
masato 0:b06afbefd350 207 wr_cmd(x & 0xf);
masato 0:b06afbefd350 208
masato 0:b06afbefd350 209 /* Pixel location */
masato 0:b06afbefd350 210 row_in_page = y % 8;
masato 0:b06afbefd350 211
masato 0:b06afbefd350 212 if (sw)
masato 0:b06afbefd350 213 gfx_buf[page][x] |= (1 << row_in_page);
masato 0:b06afbefd350 214 else
masato 0:b06afbefd350 215 gfx_buf[page][x] &= ~(1 << row_in_page);
masato 0:b06afbefd350 216
masato 0:b06afbefd350 217 wr_dat(gfx_buf[page][x]);
masato 1:84b2d36d57f0 218 loc_x++;
masato 0:b06afbefd350 219 }
masato 0:b06afbefd350 220
masato 1:84b2d36d57f0 221 void c128x64spi::locate_x(int x) {
masato 1:84b2d36d57f0 222 /* Selecting Column */
masato 1:84b2d36d57f0 223 int col = ((x & 0xF0) >> 4) | 0x10;
masato 1:84b2d36d57f0 224 wr_cmd(COL_SEL|col);
masato 1:84b2d36d57f0 225 wr_cmd(x & 0xf);
masato 1:84b2d36d57f0 226
masato 1:84b2d36d57f0 227 loc_x = x;
masato 1:84b2d36d57f0 228 }
masato 1:84b2d36d57f0 229
masato 1:84b2d36d57f0 230 void c128x64spi::locate_y(int y) {
masato 1:84b2d36d57f0 231 /* Selecting Page */
masato 1:84b2d36d57f0 232 // y = y / 8;
masato 1:84b2d36d57f0 233 wr_cmd(PAGE_SEL | y);
masato 1:84b2d36d57f0 234 loc_y = y;
masato 1:84b2d36d57f0 235 }
masato 1:84b2d36d57f0 236
masato 1:84b2d36d57f0 237
masato 0:b06afbefd350 238 void c128x64spi::wr_cmd(int cmd) {
masato 0:b06afbefd350 239 _rs = 0; // rs low, cs low for transmitting command
masato 0:b06afbefd350 240 _cs = 0;
masato 0:b06afbefd350 241 _spi.write(cmd);
masato 0:b06afbefd350 242 // wait_ms(5);
masato 0:b06afbefd350 243 _cs = 1;
masato 0:b06afbefd350 244 }
masato 0:b06afbefd350 245
masato 0:b06afbefd350 246 void c128x64spi::wr_dat(int dat) {
masato 0:b06afbefd350 247 _rs = 1; // rs high, cs low for transmitting data
masato 0:b06afbefd350 248 _cs = 0;
masato 0:b06afbefd350 249 _spi.write(dat);
masato 0:b06afbefd350 250 // wait_ms(5);
masato 0:b06afbefd350 251 _cs = 1;
masato 0:b06afbefd350 252 }
masato 0:b06afbefd350 253