ks0713 SPI library. This library includes a small 5x5 ascii font and line function.

Committer:
muraguchi
Date:
Sun Feb 26 16:01:29 2012 +0000
Revision:
1:408627d73aa8
Parent:
0:7b044b89e49c
Updated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
muraguchi 0:7b044b89e49c 1 #include "mbed.h"
muraguchi 0:7b044b89e49c 2 #include "font5x5.h"
muraguchi 0:7b044b89e49c 3 #include "ks0713_spi.h"
muraguchi 0:7b044b89e49c 4
muraguchi 0:7b044b89e49c 5 KS0713_SPI::KS0713_SPI(PinName mosi, PinName sclk, PinName cs1b, PinName rs, PinName rstb)
muraguchi 0:7b044b89e49c 6 : _spi(mosi,NC,sclk), _cs1b(cs1b), _rs(rs), _rstb(rstb) {
muraguchi 0:7b044b89e49c 7 init();
muraguchi 0:7b044b89e49c 8 }
muraguchi 0:7b044b89e49c 9
muraguchi 0:7b044b89e49c 10
muraguchi 1:408627d73aa8 11 void KS0713_SPI::init() {
muraguchi 0:7b044b89e49c 12 _cs1b=0;
muraguchi 0:7b044b89e49c 13 _rs =0;
muraguchi 0:7b044b89e49c 14 _rstb=0;
muraguchi 0:7b044b89e49c 15
muraguchi 0:7b044b89e49c 16 _spi.format(8,0);
muraguchi 0:7b044b89e49c 17 _spi.frequency(5000000);
muraguchi 0:7b044b89e49c 18
muraguchi 0:7b044b89e49c 19
muraguchi 0:7b044b89e49c 20 wait(0.015); // wait for power up (reset low term must be greater than 900[ns])
muraguchi 1:408627d73aa8 21
muraguchi 0:7b044b89e49c 22 _rstb=1; // de-assert resetb
muraguchi 1:408627d73aa8 23
muraguchi 1:408627d73aa8 24
muraguchi 0:7b044b89e49c 25 writeCommand(0xA2); // LCD BIAS selection (1/65duty, 1/9bias)
muraguchi 0:7b044b89e49c 26 writeCommand(0xA1); // ADC selection (SEG128->SEG0)
muraguchi 0:7b044b89e49c 27 writeCommand(0xC0); // SHL selection (COM0->COM64)
muraguchi 0:7b044b89e49c 28 wait_ms(1);
muraguchi 0:7b044b89e49c 29
muraguchi 1:408627d73aa8 30 /*Power control
muraguchi 1:408627d73aa8 31 Displaytech screen uses built-in power circuits
muraguchi 1:408627d73aa8 32 Switch on in order as specified in data sheet
muraguchi 1:408627d73aa8 33 wait 1ms between each command
muraguchi 1:408627d73aa8 34 1st - Voltage converter ON = 0x2C
muraguchi 1:408627d73aa8 35 2nd - Voltage regulator ON = 0x2E
muraguchi 1:408627d73aa8 36 3rd - Voltage follower ON = 0x2F */
muraguchi 1:408627d73aa8 37 writeCommand(0x2C); //0x2C = Voltage converter ON
muraguchi 1:408627d73aa8 38 wait_ms(1);
muraguchi 1:408627d73aa8 39 writeCommand(0x2E); //0x2E = Voltage regulator ON
muraguchi 1:408627d73aa8 40 wait_ms(1);
muraguchi 1:408627d73aa8 41 writeCommand(0x2F); //0x2F = Voltage follower ON
muraguchi 1:408627d73aa8 42
muraguchi 0:7b044b89e49c 43
muraguchi 1:408627d73aa8 44 // Regulator Resistor Selection
muraguchi 1:408627d73aa8 45 /*Regulator resistor select
muraguchi 1:408627d73aa8 46 Sets the internal resistance ratio used in the internal voltage regulator
muraguchi 1:408627d73aa8 47 Refer to datasheet p.42
muraguchi 1:408627d73aa8 48 This works as a corse contrast control
muraguchi 1:408627d73aa8 49 0x20 = 1.9
muraguchi 1:408627d73aa8 50 0x21 = 2.19
muraguchi 1:408627d73aa8 51 0x22 = 2.55
muraguchi 1:408627d73aa8 52 0x23 = 3.02
muraguchi 1:408627d73aa8 53 0x24 = 3.61
muraguchi 1:408627d73aa8 54 0x25 = 4.35
muraguchi 1:408627d73aa8 55 0x26 = 5.29
muraguchi 1:408627d73aa8 56 0x27 = 6.48 */
muraguchi 1:408627d73aa8 57 writeCommand(0x23);
muraguchi 1:408627d73aa8 58
muraguchi 1:408627d73aa8 59 /*Set reference voltage register
muraguchi 1:408627d73aa8 60 Used as a fine contrast control
muraguchi 1:408627d73aa8 61 0x81 = Enter voltage register set mode
muraguchi 1:408627d73aa8 62 0x00 to 0x3F = 0 to 63 */
muraguchi 1:408627d73aa8 63 writeCommand(0x81); //0x81 = Enter voltage register set mode
muraguchi 1:408627d73aa8 64 writeCommand(0x20); //0x20 = Set ref voltage to 20
muraguchi 1:408627d73aa8 65
muraguchi 1:408627d73aa8 66 /*Initial display line
muraguchi 1:408627d73aa8 67 Specify DDRAM line for COM1
muraguchi 1:408627d73aa8 68 0x40 + display line */
muraguchi 1:408627d73aa8 69 writeCommand(0x40); //Set initial line to 0
muraguchi 0:7b044b89e49c 70
muraguchi 1:408627d73aa8 71 /*Set page address
muraguchi 1:408627d73aa8 72 Sets the initial page address to write to
muraguchi 1:408627d73aa8 73 0xB0 + page address 0 to 8 */
muraguchi 1:408627d73aa8 74 writeCommand(0xB0); //Initial page set to 0
muraguchi 1:408627d73aa8 75
muraguchi 1:408627d73aa8 76 /*Set column address
muraguchi 1:408627d73aa8 77 Sets the initial column to write to
muraguchi 1:408627d73aa8 78 for LSB (b3-b0) 0x00 + first nibble
muraguchi 1:408627d73aa8 79 for MSB (b7-b4) 0x10 + second nibble
muraguchi 1:408627d73aa8 80 0x00 to 0x83 = column 0 to 131 */
muraguchi 1:408627d73aa8 81 writeCommand(0x00); //Sets LSB to 0
muraguchi 1:408627d73aa8 82 writeCommand(0x10); //Sets MSB to 0 - column is now set to 0
muraguchi 1:408627d73aa8 83
muraguchi 1:408627d73aa8 84 /*Reverse display
muraguchi 1:408627d73aa8 85 Selects either a normal display or a reverse display
muraguchi 1:408627d73aa8 86 0xA6 = normal
muraguchi 1:408627d73aa8 87 0xA7 = reverse */
muraguchi 1:408627d73aa8 88 writeCommand(0xA6); //Sets display to normal
muraguchi 1:408627d73aa8 89
muraguchi 1:408627d73aa8 90 /*Set static indicator
muraguchi 1:408627d73aa8 91 Sets up a static indicator on the display
muraguchi 1:408627d73aa8 92 See datasheet p.42
muraguchi 1:408627d73aa8 93 This is a 2 instruction cycle
muraguchi 1:408627d73aa8 94 0xAC = static indicator ON
muraguchi 1:408627d73aa8 95 0xAD = static indicator OFF
muraguchi 1:408627d73aa8 96 Next instruction to set indicator type:
muraguchi 1:408627d73aa8 97 0x00 = OFF
muraguchi 1:408627d73aa8 98 0x01 = ON - 1 second blinking
muraguchi 1:408627d73aa8 99 0x02 = ON - 0.5 second blinking
muraguchi 1:408627d73aa8 100 0x03 = ON - always ON */
muraguchi 1:408627d73aa8 101 writeCommand(0xAD); //Static indicator OFF
muraguchi 1:408627d73aa8 102 writeCommand(0x00); //OFF - 0.5 second blinking
muraguchi 1:408627d73aa8 103
muraguchi 1:408627d73aa8 104 /*Display ON/OFF
muraguchi 1:408627d73aa8 105 Switched the display to on or off
muraguchi 1:408627d73aa8 106 0xAE = Display OFF
muraguchi 1:408627d73aa8 107 0xAF = Display ON */
muraguchi 1:408627d73aa8 108 writeCommand(0xAF);
muraguchi 1:408627d73aa8 109
muraguchi 1:408627d73aa8 110 clear();
muraguchi 1:408627d73aa8 111 update();
muraguchi 1:408627d73aa8 112
muraguchi 0:7b044b89e49c 113 _x=0;
muraguchi 0:7b044b89e49c 114 _y=0;
muraguchi 0:7b044b89e49c 115 }
muraguchi 0:7b044b89e49c 116
muraguchi 1:408627d73aa8 117 void KS0713_SPI::writeCommand(unsigned char data) {
muraguchi 0:7b044b89e49c 118 _cs1b=0;
muraguchi 0:7b044b89e49c 119 _rs =0;
muraguchi 0:7b044b89e49c 120 wait_us(1); // setup 55[ns]
muraguchi 1:408627d73aa8 121 _spi.write(data);
muraguchi 0:7b044b89e49c 122 wait_us(1); // hold 180[ns]
muraguchi 0:7b044b89e49c 123 _cs1b=1;
muraguchi 0:7b044b89e49c 124 }
muraguchi 0:7b044b89e49c 125
muraguchi 1:408627d73aa8 126 void KS0713_SPI::writeData(unsigned char data) {
muraguchi 0:7b044b89e49c 127 _cs1b=0;
muraguchi 0:7b044b89e49c 128 _rs =1;
muraguchi 0:7b044b89e49c 129 wait_us(1); // setup 55[ns]
muraguchi 1:408627d73aa8 130 _spi.write(data);
muraguchi 0:7b044b89e49c 131 wait_us(1); // hold 180[ns]
muraguchi 0:7b044b89e49c 132 _cs1b=1;
muraguchi 0:7b044b89e49c 133 }
muraguchi 0:7b044b89e49c 134
muraguchi 1:408627d73aa8 135 void KS0713_SPI::clear() {
muraguchi 1:408627d73aa8 136 for (int page=0; page<MBED_KS0713_SPI_HEIGHT/8; page++) {
muraguchi 1:408627d73aa8 137 for (int column=0; column<MBED_KS0713_SPI_WIDTH; column++) {
muraguchi 0:7b044b89e49c 138 vram[column][page]=0x00;
muraguchi 1:408627d73aa8 139 }
muraguchi 0:7b044b89e49c 140 }
muraguchi 0:7b044b89e49c 141 }
muraguchi 1:408627d73aa8 142 void KS0713_SPI::fill() {
muraguchi 1:408627d73aa8 143 for (int page=0; page<MBED_KS0713_SPI_HEIGHT/8; page++) {
muraguchi 1:408627d73aa8 144 for (int column=0; column<MBED_KS0713_SPI_WIDTH; column++) {
muraguchi 0:7b044b89e49c 145 vram[column][page]=0xFF;
muraguchi 1:408627d73aa8 146 }
muraguchi 0:7b044b89e49c 147 }
muraguchi 0:7b044b89e49c 148 }
muraguchi 1:408627d73aa8 149 void KS0713_SPI::update() {
muraguchi 1:408627d73aa8 150 for (int page=0; page<MBED_KS0713_SPI_HEIGHT/8; page++) {
muraguchi 0:7b044b89e49c 151 writeCommand(0xB0+page);
muraguchi 0:7b044b89e49c 152 writeCommand(0x10);
muraguchi 0:7b044b89e49c 153 writeCommand(0x00);
muraguchi 1:408627d73aa8 154 for (int column=0; column<100; column++) {
muraguchi 0:7b044b89e49c 155 writeData(vram[column][page]);
muraguchi 1:408627d73aa8 156 }
muraguchi 0:7b044b89e49c 157 }
muraguchi 0:7b044b89e49c 158 }
muraguchi 0:7b044b89e49c 159
muraguchi 1:408627d73aa8 160 void KS0713_SPI::locate(int x,int y) {
muraguchi 0:7b044b89e49c 161 _x=x;
muraguchi 0:7b044b89e49c 162 _y=y;
muraguchi 0:7b044b89e49c 163 }
muraguchi 0:7b044b89e49c 164
muraguchi 1:408627d73aa8 165 void KS0713_SPI::line(int x1, int y1, int x2, int y2) {
muraguchi 0:7b044b89e49c 166 int dist_x;
muraguchi 0:7b044b89e49c 167 int dist_y;
muraguchi 0:7b044b89e49c 168 int calc_point;
muraguchi 0:7b044b89e49c 169
muraguchi 1:408627d73aa8 170 if (x2>x1) {
muraguchi 0:7b044b89e49c 171 dist_x = x2-x1;
muraguchi 1:408627d73aa8 172 } else {
muraguchi 0:7b044b89e49c 173 dist_x = x1-x2;
muraguchi 0:7b044b89e49c 174 }
muraguchi 1:408627d73aa8 175 if (y2>y1) {
muraguchi 0:7b044b89e49c 176 dist_y = y2-y1;
muraguchi 1:408627d73aa8 177 } else {
muraguchi 0:7b044b89e49c 178 dist_y = y1-y2;
muraguchi 0:7b044b89e49c 179 }
muraguchi 1:408627d73aa8 180
muraguchi 0:7b044b89e49c 181
muraguchi 0:7b044b89e49c 182
muraguchi 1:408627d73aa8 183 if (dist_x>dist_y) {
muraguchi 0:7b044b89e49c 184 // x direction
muraguchi 1:408627d73aa8 185 if (x1>x2)
muraguchi 1:408627d73aa8 186 // step -1
muraguchi 0:7b044b89e49c 187 {
muraguchi 1:408627d73aa8 188 for (int x=x1; x>=x2; x--) {
muraguchi 0:7b044b89e49c 189 if (x<0)
muraguchi 0:7b044b89e49c 190 break;
muraguchi 0:7b044b89e49c 191 calc_point = y1+ ( (y2-y1) * (x-x1) / (x2-x1) );
muraguchi 0:7b044b89e49c 192 if (calc_point>=0 && calc_point<MBED_KS0713_SPI_HEIGHT)
muraguchi 1:408627d73aa8 193 vram[x][(calc_point>>3)] |= ( 0x1 <<(calc_point&0x7));
muraguchi 0:7b044b89e49c 194 }
muraguchi 1:408627d73aa8 195 } else
muraguchi 1:408627d73aa8 196 // step ++
muraguchi 0:7b044b89e49c 197 {
muraguchi 0:7b044b89e49c 198 // point 1 -> point 2
muraguchi 1:408627d73aa8 199 for (int x=x1; x<=x2; x++) {
muraguchi 0:7b044b89e49c 200 if (x>=MBED_KS0713_SPI_WIDTH)
muraguchi 0:7b044b89e49c 201 break;
muraguchi 0:7b044b89e49c 202 calc_point = y1+( (y2-y1) * (x-x1) / (x2-x1) );
muraguchi 0:7b044b89e49c 203 if (calc_point>=0 && calc_point<MBED_KS0713_SPI_HEIGHT)
muraguchi 0:7b044b89e49c 204 vram[x][(calc_point>>3)] |= ( 0x1 <<(calc_point&0x7));
muraguchi 0:7b044b89e49c 205 }
muraguchi 0:7b044b89e49c 206
muraguchi 0:7b044b89e49c 207 }
muraguchi 1:408627d73aa8 208 } else {
muraguchi 0:7b044b89e49c 209 // y direction
muraguchi 1:408627d73aa8 210 if (y1>y2)
muraguchi 1:408627d73aa8 211 // step -1
muraguchi 0:7b044b89e49c 212 {
muraguchi 1:408627d73aa8 213 for (int y=y1; y>=y2; y--) {
muraguchi 0:7b044b89e49c 214 if (y<0)
muraguchi 0:7b044b89e49c 215 break;
muraguchi 0:7b044b89e49c 216 calc_point = x1+( (x2-x1) * (y-y1) / (y2-y1) );
muraguchi 0:7b044b89e49c 217 if (calc_point>=0 && calc_point<MBED_KS0713_SPI_WIDTH)
muraguchi 1:408627d73aa8 218 vram[calc_point][y>>3] |= ( 0x1 <<(y&0x7));
muraguchi 0:7b044b89e49c 219 }
muraguchi 1:408627d73aa8 220 } else
muraguchi 1:408627d73aa8 221 // step ++
muraguchi 0:7b044b89e49c 222 {
muraguchi 0:7b044b89e49c 223 // point 1 -> point 2
muraguchi 1:408627d73aa8 224 for (int y=y1; y<=y2; y++) {
muraguchi 0:7b044b89e49c 225 if (y>=MBED_KS0713_SPI_HEIGHT)
muraguchi 0:7b044b89e49c 226 break;
muraguchi 0:7b044b89e49c 227 calc_point = x1+( (x2-x1) * (y-y1) / (y2-y1) );
muraguchi 0:7b044b89e49c 228 if (calc_point>=0 && calc_point<MBED_KS0713_SPI_WIDTH)
muraguchi 1:408627d73aa8 229 vram[calc_point][y>>3] |= ( 0x1 <<(y&0x7));
muraguchi 0:7b044b89e49c 230 }
muraguchi 0:7b044b89e49c 231 }
muraguchi 1:408627d73aa8 232 }
muraguchi 0:7b044b89e49c 233 }
muraguchi 0:7b044b89e49c 234
muraguchi 0:7b044b89e49c 235
muraguchi 1:408627d73aa8 236 int KS0713_SPI::_putc(int value) {
muraguchi 0:7b044b89e49c 237 unsigned char fontdata;
muraguchi 1:408627d73aa8 238 for (int cx=0; cx<5; cx++) {
muraguchi 1:408627d73aa8 239 if ( _x+cx>=MBED_KS0713_SPI_WIDTH ) {
muraguchi 0:7b044b89e49c 240 break;
muraguchi 0:7b044b89e49c 241 }
muraguchi 0:7b044b89e49c 242
muraguchi 0:7b044b89e49c 243 fontdata = font5x5[value-' '][cx];
muraguchi 1:408627d73aa8 244 for (int cy=0; cy<5; cy++) {
muraguchi 0:7b044b89e49c 245 if (_y+cy>=MBED_KS0713_SPI_HEIGHT) {
muraguchi 0:7b044b89e49c 246 break ;
muraguchi 0:7b044b89e49c 247 }
muraguchi 0:7b044b89e49c 248
muraguchi 0:7b044b89e49c 249 if (fontdata & 0x1) {
muraguchi 1:408627d73aa8 250 vram[_x+cx][(_y+cy)>>3] |= ( 0x1 <<((_y+cy)&0x7));
muraguchi 0:7b044b89e49c 251 } else {
muraguchi 1:408627d73aa8 252 vram[_x+cx][(_y+cy)>>3] &= ~( 0x1 <<((_y+cy)&0x7));
muraguchi 1:408627d73aa8 253
muraguchi 0:7b044b89e49c 254 }
muraguchi 0:7b044b89e49c 255 fontdata=fontdata>>1;
muraguchi 0:7b044b89e49c 256 }
muraguchi 0:7b044b89e49c 257 }
muraguchi 1:408627d73aa8 258
muraguchi 0:7b044b89e49c 259 _x+=6;
muraguchi 0:7b044b89e49c 260 return value;
muraguchi 0:7b044b89e49c 261 }
muraguchi 0:7b044b89e49c 262
muraguchi 1:408627d73aa8 263 int KS0713_SPI::_getc() {
muraguchi 0:7b044b89e49c 264 return -1;
muraguchi 0:7b044b89e49c 265 }