Simple library for interfacing to Nokia 5110 LCD display (as found on the SparkFun website).
Fork of N5110 by
N5110.cpp@20:4145b7a59ef7, 2015-05-07 (annotated)
- Committer:
- qk2277
- Date:
- Thu May 07 16:12:53 2015 +0000
- Revision:
- 20:4145b7a59ef7
- Parent:
- 19:ba8addc061ea
the first version of my project
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eencae | 5:6ea180eef702 | 1 | /** |
eencae | 5:6ea180eef702 | 2 | @file N5110.cpp |
eencae | 5:6ea180eef702 | 3 | |
eencae | 5:6ea180eef702 | 4 | @brief Member functions implementations |
eencae | 5:6ea180eef702 | 5 | |
eencae | 5:6ea180eef702 | 6 | */ |
qk2277 | 20:4145b7a59ef7 | 7 | |
qk2277 | 20:4145b7a59ef7 | 8 | #include "N5110.h" |
eencae | 0:d563e74f0ae9 | 9 | #include "mbed.h" |
qk2277 | 20:4145b7a59ef7 | 10 | #include "BMP180.h" |
eencae | 0:d563e74f0ae9 | 11 | |
eencae | 2:e93021cfb0a9 | 12 | |
eencae | 1:df68f34cd32d | 13 | N5110::N5110(PinName pwrPin, PinName scePin, PinName rstPin, PinName dcPin, PinName mosiPin, PinName sclkPin, PinName ledPin) |
eencae | 0:d563e74f0ae9 | 14 | { |
eencae | 13:908644099648 | 15 | |
eencae | 0:d563e74f0ae9 | 16 | spi = new SPI(mosiPin,NC,sclkPin); // create new SPI instance and initialise |
eencae | 13:908644099648 | 17 | initSPI(); |
eencae | 13:908644099648 | 18 | |
eencae | 6:adb79338d40f | 19 | // set up pins as required |
eencae | 0:d563e74f0ae9 | 20 | led = new PwmOut(ledPin); |
eencae | 0:d563e74f0ae9 | 21 | pwr = new DigitalOut(pwrPin); |
eencae | 0:d563e74f0ae9 | 22 | sce = new DigitalOut(scePin); |
eencae | 0:d563e74f0ae9 | 23 | rst = new DigitalOut(rstPin); |
eencae | 0:d563e74f0ae9 | 24 | dc = new DigitalOut(dcPin); |
eencae | 0:d563e74f0ae9 | 25 | |
eencae | 0:d563e74f0ae9 | 26 | } |
eencae | 0:d563e74f0ae9 | 27 | |
eencae | 6:adb79338d40f | 28 | // initialise function - powers up and sends the initialisation commands |
eencae | 0:d563e74f0ae9 | 29 | void N5110::init() |
eencae | 0:d563e74f0ae9 | 30 | { |
eencae | 6:adb79338d40f | 31 | turnOn(); // power up |
eencae | 6:adb79338d40f | 32 | wait_ms(10); // small delay seems to prevent spurious pixels during mbed reset |
eencae | 6:adb79338d40f | 33 | reset(); // reset LCD - must be done within 100 ms |
eencae | 0:d563e74f0ae9 | 34 | |
eencae | 0:d563e74f0ae9 | 35 | // function set - extended |
eencae | 0:d563e74f0ae9 | 36 | sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE); |
eencae | 6:adb79338d40f | 37 | // Don't completely understand these parameters - they seem to work as they are |
eencae | 6:adb79338d40f | 38 | // Consult the datasheet if you need to change them |
eencae | 1:df68f34cd32d | 39 | sendCommand(CMD_VOP_7V38); // operating voltage - these values are from Chris Yan's Library |
eencae | 0:d563e74f0ae9 | 40 | sendCommand(CMD_TC_TEMP_2); // temperature control |
eencae | 0:d563e74f0ae9 | 41 | sendCommand(CMD_BI_MUX_48); // bias |
eencae | 0:d563e74f0ae9 | 42 | |
eencae | 0:d563e74f0ae9 | 43 | // function set - basic |
eencae | 0:d563e74f0ae9 | 44 | sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE); |
eencae | 1:df68f34cd32d | 45 | normalMode(); // normal video mode by default |
eencae | 0:d563e74f0ae9 | 46 | sendCommand(CMD_DC_NORMAL_MODE); // black on white |
eencae | 0:d563e74f0ae9 | 47 | |
eencae | 0:d563e74f0ae9 | 48 | // RAM is undefined at power-up so clear |
eencae | 0:d563e74f0ae9 | 49 | clearRAM(); |
eencae | 0:d563e74f0ae9 | 50 | |
eencae | 0:d563e74f0ae9 | 51 | } |
eencae | 13:908644099648 | 52 | |
eencae | 13:908644099648 | 53 | // sets normal video mode (black on white) |
eencae | 13:908644099648 | 54 | void N5110::normalMode() |
eencae | 13:908644099648 | 55 | { |
eencae | 13:908644099648 | 56 | sendCommand(CMD_DC_NORMAL_MODE); |
eencae | 13:908644099648 | 57 | |
eencae | 1:df68f34cd32d | 58 | } |
eencae | 1:df68f34cd32d | 59 | |
eencae | 13:908644099648 | 60 | // sets normal video mode (white on black) |
eencae | 13:908644099648 | 61 | void N5110::inverseMode() |
eencae | 13:908644099648 | 62 | { |
eencae | 13:908644099648 | 63 | sendCommand(CMD_DC_INVERT_VIDEO); |
eencae | 1:df68f34cd32d | 64 | } |
eencae | 0:d563e74f0ae9 | 65 | |
eencae | 0:d563e74f0ae9 | 66 | // function to power up the LCD and backlight |
eencae | 0:d563e74f0ae9 | 67 | void N5110::turnOn() |
eencae | 0:d563e74f0ae9 | 68 | { |
eencae | 0:d563e74f0ae9 | 69 | // set brightness of LED - 0.0 to 1.0 - default is 50% |
eencae | 0:d563e74f0ae9 | 70 | setBrightness(0.5); |
eencae | 0:d563e74f0ae9 | 71 | pwr->write(1); // apply power |
eencae | 0:d563e74f0ae9 | 72 | } |
eencae | 0:d563e74f0ae9 | 73 | |
eencae | 0:d563e74f0ae9 | 74 | // function to power down LCD |
eencae | 0:d563e74f0ae9 | 75 | void N5110::turnOff() |
eencae | 0:d563e74f0ae9 | 76 | { |
eencae | 0:d563e74f0ae9 | 77 | setBrightness(0.0); // turn backlight off |
eencae | 0:d563e74f0ae9 | 78 | clearRAM(); // clear RAM to ensure specified current consumption |
eencae | 10:6f3abb40202b | 79 | // send command to ensure we are in basic mode |
eencae | 0:d563e74f0ae9 | 80 | sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE); |
eencae | 6:adb79338d40f | 81 | // clear the display |
eencae | 0:d563e74f0ae9 | 82 | sendCommand(CMD_DC_CLEAR_DISPLAY); |
eencae | 6:adb79338d40f | 83 | // enter the extended mode and power down |
eencae | 0:d563e74f0ae9 | 84 | sendCommand(0x20 | CMD_FS_POWER_DOWN_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE); |
eencae | 6:adb79338d40f | 85 | // small delay and then turn off the power pin |
eencae | 6:adb79338d40f | 86 | wait_ms(10); |
eencae | 0:d563e74f0ae9 | 87 | pwr->write(0); |
eencae | 0:d563e74f0ae9 | 88 | |
eencae | 0:d563e74f0ae9 | 89 | } |
eencae | 0:d563e74f0ae9 | 90 | |
eencae | 0:d563e74f0ae9 | 91 | // function to change LED backlight brightness |
eencae | 0:d563e74f0ae9 | 92 | void N5110::setBrightness(float brightness) |
eencae | 0:d563e74f0ae9 | 93 | { |
eencae | 0:d563e74f0ae9 | 94 | // check whether brightness is within range |
eencae | 0:d563e74f0ae9 | 95 | if (brightness < 0.0) |
eencae | 0:d563e74f0ae9 | 96 | brightness = 0.0; |
eencae | 0:d563e74f0ae9 | 97 | if (brightness > 1.0) |
eencae | 0:d563e74f0ae9 | 98 | brightness = 1.0; |
eencae | 0:d563e74f0ae9 | 99 | // set PWM duty cycle |
eencae | 0:d563e74f0ae9 | 100 | led->write(brightness); |
eencae | 0:d563e74f0ae9 | 101 | } |
eencae | 0:d563e74f0ae9 | 102 | |
eencae | 0:d563e74f0ae9 | 103 | |
eencae | 0:d563e74f0ae9 | 104 | // pulse the active low reset line |
eencae | 0:d563e74f0ae9 | 105 | void N5110::reset() |
eencae | 0:d563e74f0ae9 | 106 | { |
eencae | 0:d563e74f0ae9 | 107 | rst->write(0); // reset the LCD |
eencae | 0:d563e74f0ae9 | 108 | rst->write(1); |
eencae | 0:d563e74f0ae9 | 109 | } |
eencae | 0:d563e74f0ae9 | 110 | |
eencae | 0:d563e74f0ae9 | 111 | // function to initialise SPI peripheral |
eencae | 0:d563e74f0ae9 | 112 | void N5110::initSPI() |
eencae | 0:d563e74f0ae9 | 113 | { |
eencae | 0:d563e74f0ae9 | 114 | spi->format(8,1); // 8 bits, Mode 1 - polarity 0, phase 1 - base value of clock is 0, data captured on falling edge/propagated on rising edge |
eencae | 0:d563e74f0ae9 | 115 | spi->frequency(4000000); // maximum of screen is 4 MHz |
eencae | 0:d563e74f0ae9 | 116 | } |
eencae | 0:d563e74f0ae9 | 117 | |
eencae | 6:adb79338d40f | 118 | // send a command to the display |
eencae | 0:d563e74f0ae9 | 119 | void N5110::sendCommand(unsigned char command) |
eencae | 0:d563e74f0ae9 | 120 | { |
eencae | 0:d563e74f0ae9 | 121 | dc->write(0); // set DC low for command |
eencae | 0:d563e74f0ae9 | 122 | sce->write(0); // set CE low to begin frame |
eencae | 0:d563e74f0ae9 | 123 | spi->write(command); // send command |
eencae | 0:d563e74f0ae9 | 124 | dc->write(1); // turn back to data by default |
eencae | 0:d563e74f0ae9 | 125 | sce->write(1); // set CE high to end frame (expected for transmission of single byte) |
eencae | 0:d563e74f0ae9 | 126 | |
eencae | 0:d563e74f0ae9 | 127 | } |
eencae | 0:d563e74f0ae9 | 128 | |
eencae | 6:adb79338d40f | 129 | // send data to the display at the current XY address |
eencae | 6:adb79338d40f | 130 | // dc is set to 1 (i.e. data) after sending a command and so should |
eencae | 6:adb79338d40f | 131 | // be the default mode. |
eencae | 0:d563e74f0ae9 | 132 | void N5110::sendData(unsigned char data) |
eencae | 0:d563e74f0ae9 | 133 | { |
eencae | 0:d563e74f0ae9 | 134 | sce->write(0); // set CE low to begin frame |
eencae | 0:d563e74f0ae9 | 135 | spi->write(data); |
eencae | 0:d563e74f0ae9 | 136 | sce->write(1); // set CE high to end frame (expected for transmission of single byte) |
eencae | 0:d563e74f0ae9 | 137 | } |
eencae | 0:d563e74f0ae9 | 138 | |
eencae | 0:d563e74f0ae9 | 139 | // this function writes 0 to the 504 bytes to clear the RAM |
eencae | 0:d563e74f0ae9 | 140 | void N5110::clearRAM() |
eencae | 0:d563e74f0ae9 | 141 | { |
eencae | 0:d563e74f0ae9 | 142 | int i; |
eencae | 0:d563e74f0ae9 | 143 | sce->write(0); //set CE low to begin frame |
eencae | 17:780a542d5f8b | 144 | for(i = 0; i < WIDTH * HEIGHT; i++) { // 48 x 84 bits = 504 bytes |
eencae | 0:d563e74f0ae9 | 145 | spi->write(0x00); // send 0's |
eencae | 0:d563e74f0ae9 | 146 | } |
eencae | 0:d563e74f0ae9 | 147 | sce->write(1); // set CE high to end frame |
eencae | 0:d563e74f0ae9 | 148 | |
eencae | 0:d563e74f0ae9 | 149 | } |
eencae | 0:d563e74f0ae9 | 150 | |
eencae | 13:908644099648 | 151 | // function to set the XY address in RAM for subsequenct data write |
eencae | 0:d563e74f0ae9 | 152 | void N5110::setXYAddress(int x, int y) |
eencae | 0:d563e74f0ae9 | 153 | { |
eencae | 17:780a542d5f8b | 154 | if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) { // check within range |
eencae | 17:780a542d5f8b | 155 | sendCommand(0x80 | x); // send addresses to display with relevant mask |
eencae | 17:780a542d5f8b | 156 | sendCommand(0x40 | y); |
eencae | 17:780a542d5f8b | 157 | } |
eencae | 0:d563e74f0ae9 | 158 | } |
eencae | 0:d563e74f0ae9 | 159 | |
eencae | 6:adb79338d40f | 160 | // These functions are used to set, clear and get the value of pixels in the display |
eencae | 6:adb79338d40f | 161 | // Pixels are addressed in the range of 0 to 47 (y) and 0 to 83 (x). The refresh() |
eencae | 6:adb79338d40f | 162 | // function must be called after set and clear in order to update the display |
eencae | 0:d563e74f0ae9 | 163 | void N5110::setPixel(int x, int y) |
eencae | 0:d563e74f0ae9 | 164 | { |
eencae | 17:780a542d5f8b | 165 | if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) { // check within range |
eencae | 17:780a542d5f8b | 166 | // calculate bank and shift 1 to required position in the data byte |
eencae | 17:780a542d5f8b | 167 | buffer[x][y/8] |= (1 << y%8); |
eencae | 17:780a542d5f8b | 168 | } |
eencae | 0:d563e74f0ae9 | 169 | } |
eencae | 0:d563e74f0ae9 | 170 | |
eencae | 0:d563e74f0ae9 | 171 | void N5110::clearPixel(int x, int y) |
eencae | 0:d563e74f0ae9 | 172 | { |
eencae | 17:780a542d5f8b | 173 | if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) { // check within range |
eencae | 17:780a542d5f8b | 174 | // calculate bank and shift 1 to required position (using bit clear) |
eencae | 17:780a542d5f8b | 175 | buffer[x][y/8] &= ~(1 << y%8); |
eencae | 17:780a542d5f8b | 176 | } |
eencae | 0:d563e74f0ae9 | 177 | } |
eencae | 0:d563e74f0ae9 | 178 | |
eencae | 7:3010f24e0a81 | 179 | int N5110::getPixel(int x, int y) |
eencae | 0:d563e74f0ae9 | 180 | { |
eencae | 17:780a542d5f8b | 181 | if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) { // check within range |
eencae | 17:780a542d5f8b | 182 | // return relevant bank and mask required bit |
eencae | 17:780a542d5f8b | 183 | return (int) buffer[x][y/8] & (1 << y%8); |
eencae | 19:ba8addc061ea | 184 | // note this does not necessarily return 1 - a non-zero number represents a pixel |
eencae | 17:780a542d5f8b | 185 | } else { |
eencae | 17:780a542d5f8b | 186 | return 0; |
eencae | 17:780a542d5f8b | 187 | } |
eencae | 0:d563e74f0ae9 | 188 | } |
eencae | 0:d563e74f0ae9 | 189 | |
eencae | 6:adb79338d40f | 190 | // function to refresh the display |
eencae | 6:adb79338d40f | 191 | void N5110::refresh() |
eencae | 0:d563e74f0ae9 | 192 | { |
eencae | 0:d563e74f0ae9 | 193 | int i,j; |
eencae | 13:908644099648 | 194 | |
eencae | 7:3010f24e0a81 | 195 | setXYAddress(0,0); // important to set address back to 0,0 before refreshing display |
eencae | 7:3010f24e0a81 | 196 | // address auto increments after printing string, so buffer[0][0] will not coincide |
eencae | 7:3010f24e0a81 | 197 | // with top-left pixel after priting string |
eencae | 13:908644099648 | 198 | |
eencae | 0:d563e74f0ae9 | 199 | sce->write(0); //set CE low to begin frame |
eencae | 0:d563e74f0ae9 | 200 | |
eencae | 17:780a542d5f8b | 201 | for(j = 0; j < BANKS; j++) { // be careful to use correct order (j,i) for horizontal addressing |
eencae | 17:780a542d5f8b | 202 | for(i = 0; i < WIDTH; i++) { |
eencae | 0:d563e74f0ae9 | 203 | spi->write(buffer[i][j]); // send buffer |
eencae | 0:d563e74f0ae9 | 204 | } |
eencae | 0:d563e74f0ae9 | 205 | } |
eencae | 0:d563e74f0ae9 | 206 | sce->write(1); // set CE high to end frame |
eencae | 0:d563e74f0ae9 | 207 | |
eencae | 0:d563e74f0ae9 | 208 | } |
eencae | 0:d563e74f0ae9 | 209 | |
eencae | 6:adb79338d40f | 210 | // fills the buffer with random bytes. Can be used to test the display. |
eencae | 6:adb79338d40f | 211 | // The rand() function isn't seeded so it probably creates the same pattern everytime |
eencae | 0:d563e74f0ae9 | 212 | void N5110::randomiseBuffer() |
eencae | 0:d563e74f0ae9 | 213 | { |
eencae | 0:d563e74f0ae9 | 214 | int i,j; |
eencae | 17:780a542d5f8b | 215 | for(j = 0; j < BANKS; j++) { // be careful to use correct order (j,i) for horizontal addressing |
eencae | 17:780a542d5f8b | 216 | for(i = 0; i < WIDTH; i++) { |
eencae | 0:d563e74f0ae9 | 217 | buffer[i][j] = rand()%256; // generate random byte |
eencae | 0:d563e74f0ae9 | 218 | } |
eencae | 0:d563e74f0ae9 | 219 | } |
eencae | 0:d563e74f0ae9 | 220 | |
eencae | 0:d563e74f0ae9 | 221 | } |
eencae | 0:d563e74f0ae9 | 222 | |
eencae | 0:d563e74f0ae9 | 223 | // function to print 5x7 font |
eencae | 13:908644099648 | 224 | void N5110::printChar(char c,int x,int y) |
eencae | 0:d563e74f0ae9 | 225 | { |
eencae | 19:ba8addc061ea | 226 | if (y>=0 && y<BANKS) { // check if printing in range of y banks |
eencae | 18:1af393359298 | 227 | |
eencae | 18:1af393359298 | 228 | for (int i = 0; i < 5 ; i++ ) { |
eencae | 18:1af393359298 | 229 | int pixel_x = x+i; |
eencae | 19:ba8addc061ea | 230 | if (pixel_x > WIDTH-1) // ensure pixel isn't outside the buffer size (0 - 83) |
eencae | 18:1af393359298 | 231 | break; |
eencae | 18:1af393359298 | 232 | buffer[pixel_x][y] = font5x7[(c - 32)*5 + i]; |
eencae | 18:1af393359298 | 233 | // array is offset by 32 relative to ASCII, each character is 5 pixels wide |
eencae | 18:1af393359298 | 234 | } |
eencae | 18:1af393359298 | 235 | |
eencae | 18:1af393359298 | 236 | refresh(); // this sends the buffer to the display and sets address (cursor) back to 0,0 |
eencae | 0:d563e74f0ae9 | 237 | } |
eencae | 0:d563e74f0ae9 | 238 | } |
eencae | 0:d563e74f0ae9 | 239 | |
eencae | 0:d563e74f0ae9 | 240 | // function to print string at specified position |
eencae | 0:d563e74f0ae9 | 241 | void N5110::printString(const char * str,int x,int y) |
eencae | 0:d563e74f0ae9 | 242 | { |
eencae | 19:ba8addc061ea | 243 | if (y>=0 && y<BANKS) { // check if printing in range of y banks |
eencae | 18:1af393359298 | 244 | |
eencae | 18:1af393359298 | 245 | int n = 0 ; // counter for number of characters in string |
eencae | 18:1af393359298 | 246 | // loop through string and print character |
eencae | 18:1af393359298 | 247 | while(*str) { |
eencae | 0:d563e74f0ae9 | 248 | |
eencae | 18:1af393359298 | 249 | // writes the character bitmap data to the buffer, so that |
eencae | 18:1af393359298 | 250 | // text and pixels can be displayed at the same time |
eencae | 18:1af393359298 | 251 | for (int i = 0; i < 5 ; i++ ) { |
eencae | 18:1af393359298 | 252 | int pixel_x = x+i+n*6; |
eencae | 19:ba8addc061ea | 253 | if (pixel_x > WIDTH-1) // ensure pixel isn't outside the buffer size (0 - 83) |
eencae | 18:1af393359298 | 254 | break; |
eencae | 18:1af393359298 | 255 | buffer[pixel_x][y] = font5x7[(*str - 32)*5 + i]; |
eencae | 18:1af393359298 | 256 | } |
eencae | 18:1af393359298 | 257 | |
eencae | 18:1af393359298 | 258 | str++; // go to next character in string |
eencae | 18:1af393359298 | 259 | |
eencae | 18:1af393359298 | 260 | n++; // increment index |
eencae | 18:1af393359298 | 261 | |
eencae | 9:7701f0126ba7 | 262 | } |
eencae | 13:908644099648 | 263 | |
eencae | 18:1af393359298 | 264 | refresh(); // this sends the buffer to the display and sets address (cursor) back to 0,0 |
eencae | 0:d563e74f0ae9 | 265 | } |
eencae | 0:d563e74f0ae9 | 266 | } |
eencae | 0:d563e74f0ae9 | 267 | |
eencae | 6:adb79338d40f | 268 | // function to clear the screen |
eencae | 0:d563e74f0ae9 | 269 | void N5110::clear() |
eencae | 0:d563e74f0ae9 | 270 | { |
eencae | 6:adb79338d40f | 271 | clearBuffer(); // clear the buffer then call the refresh function |
eencae | 6:adb79338d40f | 272 | refresh(); |
eencae | 0:d563e74f0ae9 | 273 | } |
eencae | 0:d563e74f0ae9 | 274 | |
eencae | 6:adb79338d40f | 275 | // function to clear the buffer |
eencae | 0:d563e74f0ae9 | 276 | void N5110::clearBuffer() |
eencae | 0:d563e74f0ae9 | 277 | { |
eencae | 0:d563e74f0ae9 | 278 | int i,j; |
eencae | 17:780a542d5f8b | 279 | for (i=0; i<WIDTH; i++) { // loop through the banks and set the buffer to 0 |
eencae | 17:780a542d5f8b | 280 | for (j=0; j<BANKS; j++) { |
eencae | 0:d563e74f0ae9 | 281 | buffer[i][j]=0; |
eencae | 0:d563e74f0ae9 | 282 | } |
eencae | 0:d563e74f0ae9 | 283 | } |
eencae | 8:40abe5736eca | 284 | } |
eencae | 8:40abe5736eca | 285 | |
eencae | 8:40abe5736eca | 286 | // function to plot array on display |
eencae | 13:908644099648 | 287 | void N5110::plotArray(float array[]) |
eencae | 13:908644099648 | 288 | { |
eencae | 13:908644099648 | 289 | |
eencae | 8:40abe5736eca | 290 | int i; |
eencae | 13:908644099648 | 291 | |
eencae | 17:780a542d5f8b | 292 | for (i=0; i<WIDTH; i++) { // loop through array |
eencae | 8:40abe5736eca | 293 | // elements are normalised from 0.0 to 1.0, so multiply |
eencae | 8:40abe5736eca | 294 | // by 47 to convert to pixel range, and subtract from 47 |
eencae | 8:40abe5736eca | 295 | // since top-left is 0,0 in the display geometry |
eencae | 9:7701f0126ba7 | 296 | setPixel(i,47 - int(array[i]*47.0)); |
eencae | 13:908644099648 | 297 | } |
eencae | 13:908644099648 | 298 | |
eencae | 8:40abe5736eca | 299 | refresh(); |
eencae | 13:908644099648 | 300 | |
eencae | 17:780a542d5f8b | 301 | } |
eencae | 13:908644099648 | 302 | |
eencae | 17:780a542d5f8b | 303 | // function to draw circle |
eencae | 17:780a542d5f8b | 304 | void N5110:: drawCircle(int x0,int y0,int radius,int fill) |
eencae | 17:780a542d5f8b | 305 | { |
eencae | 17:780a542d5f8b | 306 | // from http://en.wikipedia.org/wiki/Midpoint_circle_algorithm |
eencae | 17:780a542d5f8b | 307 | int x = radius; |
eencae | 17:780a542d5f8b | 308 | int y = 0; |
eencae | 17:780a542d5f8b | 309 | int radiusError = 1-x; |
eencae | 17:780a542d5f8b | 310 | |
eencae | 17:780a542d5f8b | 311 | while(x >= y) { |
eencae | 17:780a542d5f8b | 312 | |
eencae | 17:780a542d5f8b | 313 | // if transparent, just draw outline |
eencae | 17:780a542d5f8b | 314 | if (fill == 0) { |
eencae | 17:780a542d5f8b | 315 | setPixel( x + x0, y + y0); |
eencae | 17:780a542d5f8b | 316 | setPixel(-x + x0, y + y0); |
eencae | 17:780a542d5f8b | 317 | setPixel( y + x0, x + y0); |
eencae | 17:780a542d5f8b | 318 | setPixel(-y + x0, x + y0); |
eencae | 17:780a542d5f8b | 319 | setPixel(-y + x0, -x + y0); |
eencae | 17:780a542d5f8b | 320 | setPixel( y + x0, -x + y0); |
eencae | 17:780a542d5f8b | 321 | setPixel( x + x0, -y + y0); |
eencae | 17:780a542d5f8b | 322 | setPixel(-x + x0, -y + y0); |
eencae | 17:780a542d5f8b | 323 | } else { // drawing filled circle, so draw lines between points at same y value |
eencae | 17:780a542d5f8b | 324 | |
eencae | 17:780a542d5f8b | 325 | int type = (fill==1) ? 1:0; // black or white fill |
eencae | 17:780a542d5f8b | 326 | |
eencae | 17:780a542d5f8b | 327 | drawLine(x+x0,y+y0,-x+x0,y+y0,type); |
eencae | 17:780a542d5f8b | 328 | drawLine(y+x0,x+y0,-y+x0,x+y0,type); |
eencae | 17:780a542d5f8b | 329 | drawLine(y+x0,-x+y0,-y+x0,-x+y0,type); |
eencae | 17:780a542d5f8b | 330 | drawLine(x+x0,-y+y0,-x+x0,-y+y0,type); |
eencae | 17:780a542d5f8b | 331 | } |
eencae | 17:780a542d5f8b | 332 | |
eencae | 17:780a542d5f8b | 333 | |
eencae | 17:780a542d5f8b | 334 | y++; |
eencae | 17:780a542d5f8b | 335 | if (radiusError<0) { |
eencae | 17:780a542d5f8b | 336 | radiusError += 2 * y + 1; |
eencae | 17:780a542d5f8b | 337 | } else { |
eencae | 17:780a542d5f8b | 338 | x--; |
eencae | 17:780a542d5f8b | 339 | radiusError += 2 * (y - x) + 1; |
eencae | 17:780a542d5f8b | 340 | } |
eencae | 17:780a542d5f8b | 341 | } |
eencae | 17:780a542d5f8b | 342 | |
eencae | 17:780a542d5f8b | 343 | } |
eencae | 17:780a542d5f8b | 344 | |
eencae | 17:780a542d5f8b | 345 | void N5110::drawLine(int x0,int y0,int x1,int y1,int type) |
eencae | 17:780a542d5f8b | 346 | { |
eencae | 17:780a542d5f8b | 347 | int y_range = y1-y0; // calc range of y and x |
eencae | 17:780a542d5f8b | 348 | int x_range = x1-x0; |
eencae | 17:780a542d5f8b | 349 | int start,stop,step; |
eencae | 17:780a542d5f8b | 350 | |
eencae | 17:780a542d5f8b | 351 | // if dotted line, set step to 2, else step is 1 |
eencae | 17:780a542d5f8b | 352 | step = (type==2) ? 2:1; |
eencae | 17:780a542d5f8b | 353 | |
eencae | 17:780a542d5f8b | 354 | // make sure we loop over the largest range to get the most pixels on the display |
eencae | 17:780a542d5f8b | 355 | // for instance, if drawing a vertical line (x_range = 0), we need to loop down the y pixels |
eencae | 17:780a542d5f8b | 356 | // or else we'll only end up with 1 pixel in the x column |
eencae | 17:780a542d5f8b | 357 | if ( abs(x_range) > abs(y_range) ) { |
eencae | 17:780a542d5f8b | 358 | |
eencae | 17:780a542d5f8b | 359 | // ensure we loop from smallest to largest or else for-loop won't run as expected |
eencae | 17:780a542d5f8b | 360 | start = x1>x0 ? x0:x1; |
eencae | 17:780a542d5f8b | 361 | stop = x1>x0 ? x1:x0; |
eencae | 17:780a542d5f8b | 362 | |
eencae | 17:780a542d5f8b | 363 | // loop between x pixels |
eencae | 17:780a542d5f8b | 364 | for (int x = start; x<= stop ; x+=step) { |
eencae | 17:780a542d5f8b | 365 | // do linear interpolation |
eencae | 17:780a542d5f8b | 366 | int y = y0 + (y1-y0)*(x-x0)/(x1-x0); |
eencae | 17:780a542d5f8b | 367 | |
eencae | 17:780a542d5f8b | 368 | if (type == 0) // if 'white' line, turn off pixel |
eencae | 17:780a542d5f8b | 369 | clearPixel(x,y); |
eencae | 17:780a542d5f8b | 370 | else |
eencae | 17:780a542d5f8b | 371 | setPixel(x,y); // else if 'black' or 'dotted' turn on pixel |
eencae | 17:780a542d5f8b | 372 | } |
eencae | 17:780a542d5f8b | 373 | } else { |
eencae | 17:780a542d5f8b | 374 | |
eencae | 17:780a542d5f8b | 375 | // ensure we loop from smallest to largest or else for-loop won't run as expected |
eencae | 17:780a542d5f8b | 376 | start = y1>y0 ? y0:y1; |
eencae | 17:780a542d5f8b | 377 | stop = y1>y0 ? y1:y0; |
eencae | 17:780a542d5f8b | 378 | |
eencae | 17:780a542d5f8b | 379 | for (int y = start; y<= stop ; y+=step) { |
eencae | 17:780a542d5f8b | 380 | // do linear interpolation |
eencae | 17:780a542d5f8b | 381 | int x = x0 + (x1-x0)*(y-y0)/(y1-y0); |
eencae | 17:780a542d5f8b | 382 | |
eencae | 17:780a542d5f8b | 383 | if (type == 0) // if 'white' line, turn off pixel |
eencae | 17:780a542d5f8b | 384 | clearPixel(x,y); |
eencae | 17:780a542d5f8b | 385 | else |
eencae | 17:780a542d5f8b | 386 | setPixel(x,y); // else if 'black' or 'dotted' turn on pixel |
eencae | 17:780a542d5f8b | 387 | |
eencae | 17:780a542d5f8b | 388 | } |
eencae | 17:780a542d5f8b | 389 | } |
eencae | 17:780a542d5f8b | 390 | |
eencae | 17:780a542d5f8b | 391 | } |
eencae | 17:780a542d5f8b | 392 | |
eencae | 17:780a542d5f8b | 393 | void N5110::drawRect(int x0,int y0,int width,int height,int fill) |
eencae | 17:780a542d5f8b | 394 | { |
eencae | 17:780a542d5f8b | 395 | |
eencae | 17:780a542d5f8b | 396 | if (fill == 0) { // transparent, just outline |
eencae | 17:780a542d5f8b | 397 | drawLine(x0,y0,x0+width,y0,1); // top |
eencae | 17:780a542d5f8b | 398 | drawLine(x0,y0+height,x0+width,y0+height,1); // bottom |
eencae | 17:780a542d5f8b | 399 | drawLine(x0,y0,x0,y0+height,1); // left |
eencae | 17:780a542d5f8b | 400 | drawLine(x0+width,y0,x0+width,y0+height,1); // right |
eencae | 17:780a542d5f8b | 401 | } else { // filled rectangle |
eencae | 17:780a542d5f8b | 402 | int type = (fill==1) ? 1:0; // black or white fill |
eencae | 17:780a542d5f8b | 403 | for (int y = y0; y<= y0+height; y++) { // loop through rows of rectangle |
eencae | 17:780a542d5f8b | 404 | drawLine(x0,y,x0+width,y,type); // draw line across screen |
eencae | 17:780a542d5f8b | 405 | } |
eencae | 17:780a542d5f8b | 406 | } |
eencae | 17:780a542d5f8b | 407 | |
eencae | 17:780a542d5f8b | 408 | } |
qk2277 | 20:4145b7a59ef7 | 409 | |
qk2277 | 20:4145b7a59ef7 | 410 | |
qk2277 | 20:4145b7a59ef7 | 411 | |
qk2277 | 20:4145b7a59ef7 | 412 | BMP180::BMP180(PinName sdaPin, PinName sclPin) |
qk2277 | 20:4145b7a59ef7 | 413 | { |
qk2277 | 20:4145b7a59ef7 | 414 | i2c = new I2C(sdaPin,sclPin); // create new I2C instance and initialise |
qk2277 | 20:4145b7a59ef7 | 415 | i2c->frequency(400000); // I2C Fast Mode - 400kHz |
qk2277 | 20:4145b7a59ef7 | 416 | leds = new BusOut(LED4,LED3,LED2,LED1); |
qk2277 | 20:4145b7a59ef7 | 417 | } |
qk2277 | 20:4145b7a59ef7 | 418 | |
qk2277 | 20:4145b7a59ef7 | 419 | Measurement BMP180::readValues() |
qk2277 | 20:4145b7a59ef7 | 420 | { |
qk2277 | 20:4145b7a59ef7 | 421 | // algorithm for taking measurement is taken from datasheet |
qk2277 | 20:4145b7a59ef7 | 422 | int32_t UT = readUncompensatedTemperatureValue(); |
qk2277 | 20:4145b7a59ef7 | 423 | int32_t UP = readUncompensatedPressureValue(); |
qk2277 | 20:4145b7a59ef7 | 424 | // once you have the uncompensated T and P, you can calculate the true T and P |
qk2277 | 20:4145b7a59ef7 | 425 | // using the equations from the datasheet |
qk2277 | 20:4145b7a59ef7 | 426 | int32_t T = calcTrueTemperature(UT); |
qk2277 | 20:4145b7a59ef7 | 427 | int32_t P = calcTruePressure(UP); |
qk2277 | 20:4145b7a59ef7 | 428 | |
qk2277 | 20:4145b7a59ef7 | 429 | Measurement measurement; |
qk2277 | 20:4145b7a59ef7 | 430 | measurement.temperature = T*0.1; // scaled by 0.1 C |
qk2277 | 20:4145b7a59ef7 | 431 | measurement.pressure = P*0.01; // Put pressure in mb |
qk2277 | 20:4145b7a59ef7 | 432 | |
qk2277 | 20:4145b7a59ef7 | 433 | return measurement; |
qk2277 | 20:4145b7a59ef7 | 434 | } |
qk2277 | 20:4145b7a59ef7 | 435 | |
qk2277 | 20:4145b7a59ef7 | 436 | int32_t BMP180::readUncompensatedTemperatureValue() |
qk2277 | 20:4145b7a59ef7 | 437 | { |
qk2277 | 20:4145b7a59ef7 | 438 | // from algorithm in datasheet - p15 |
qk2277 | 20:4145b7a59ef7 | 439 | sendByteToRegister(0x2E,0xF4); |
qk2277 | 20:4145b7a59ef7 | 440 | wait_ms(5); // 4.5 ms delay for OSS = 1 |
qk2277 | 20:4145b7a59ef7 | 441 | char MSB = readByteFromRegister(0xF6); |
qk2277 | 20:4145b7a59ef7 | 442 | char LSB = readByteFromRegister(0xF7); |
qk2277 | 20:4145b7a59ef7 | 443 | // combine in 16-bit value |
qk2277 | 20:4145b7a59ef7 | 444 | int UT = (MSB << 8) | LSB; |
qk2277 | 20:4145b7a59ef7 | 445 | #ifdef DEBUG |
qk2277 | 20:4145b7a59ef7 | 446 | UT = 27898; // test data from datasheet |
qk2277 | 20:4145b7a59ef7 | 447 | printf("****DEBUG MODE****\nUT = %d\n",UT); |
qk2277 | 20:4145b7a59ef7 | 448 | #endif |
qk2277 | 20:4145b7a59ef7 | 449 | return UT; |
qk2277 | 20:4145b7a59ef7 | 450 | } |
qk2277 | 20:4145b7a59ef7 | 451 | |
qk2277 | 20:4145b7a59ef7 | 452 | int32_t BMP180::readUncompensatedPressureValue() |
qk2277 | 20:4145b7a59ef7 | 453 | { |
qk2277 | 20:4145b7a59ef7 | 454 | // from datasheet |
qk2277 | 20:4145b7a59ef7 | 455 | char byte = 0x34 + (oss << 6); |
qk2277 | 20:4145b7a59ef7 | 456 | sendByteToRegister(byte,0xF4); |
qk2277 | 20:4145b7a59ef7 | 457 | wait_ms(8); // 7.5 ms delay for OSS = 1 |
qk2277 | 20:4145b7a59ef7 | 458 | |
qk2277 | 20:4145b7a59ef7 | 459 | char MSB = readByteFromRegister(0xF6); |
qk2277 | 20:4145b7a59ef7 | 460 | char LSB = readByteFromRegister(0xF7); |
qk2277 | 20:4145b7a59ef7 | 461 | char XLSB = readByteFromRegister(0xF7); |
qk2277 | 20:4145b7a59ef7 | 462 | int UP = (MSB << 16 | LSB << 8 | XLSB) >> (8 - oss); |
qk2277 | 20:4145b7a59ef7 | 463 | |
qk2277 | 20:4145b7a59ef7 | 464 | #ifdef DEBUG |
qk2277 | 20:4145b7a59ef7 | 465 | UP = 23843; // test data from datasheet |
qk2277 | 20:4145b7a59ef7 | 466 | printf("UP = %d\n",UP); |
qk2277 | 20:4145b7a59ef7 | 467 | #endif |
qk2277 | 20:4145b7a59ef7 | 468 | return UP; |
qk2277 | 20:4145b7a59ef7 | 469 | } |
qk2277 | 20:4145b7a59ef7 | 470 | |
qk2277 | 20:4145b7a59ef7 | 471 | int32_t BMP180::calcTrueTemperature(int32_t UT) |
qk2277 | 20:4145b7a59ef7 | 472 | { |
qk2277 | 20:4145b7a59ef7 | 473 | // equations from data sheet |
qk2277 | 20:4145b7a59ef7 | 474 | X1 = ((UT - calibration.AC6)*calibration.AC5) >> 15; |
qk2277 | 20:4145b7a59ef7 | 475 | X2 = (calibration.MC << 11) / (X1 + calibration.MD); |
qk2277 | 20:4145b7a59ef7 | 476 | B5 = X1 + X2; |
qk2277 | 20:4145b7a59ef7 | 477 | int32_t T = (B5 + 8) >> 4; |
qk2277 | 20:4145b7a59ef7 | 478 | #ifdef DEBUG |
qk2277 | 20:4145b7a59ef7 | 479 | printf("****\nX1=%d\nX2=%d\nB5=%d\nT=%d\n",X1,X2,B5,T); |
qk2277 | 20:4145b7a59ef7 | 480 | #endif |
qk2277 | 20:4145b7a59ef7 | 481 | return T; |
qk2277 | 20:4145b7a59ef7 | 482 | } |
qk2277 | 20:4145b7a59ef7 | 483 | |
qk2277 | 20:4145b7a59ef7 | 484 | int32_t BMP180::calcTruePressure(int32_t UP) |
qk2277 | 20:4145b7a59ef7 | 485 | { |
qk2277 | 20:4145b7a59ef7 | 486 | // equations from data sheet |
qk2277 | 20:4145b7a59ef7 | 487 | B6 = B5 - 4000; |
qk2277 | 20:4145b7a59ef7 | 488 | X1 = (calibration.B2 * ((B6*B6) >> 12))>>11; |
qk2277 | 20:4145b7a59ef7 | 489 | X2 = (calibration.AC2*B6)>>11; |
qk2277 | 20:4145b7a59ef7 | 490 | X3 = X1 + X2; |
qk2277 | 20:4145b7a59ef7 | 491 | B3 = (((calibration.AC1*4 + X3) << oss)+2)/4; |
qk2277 | 20:4145b7a59ef7 | 492 | #ifdef DEBUG |
qk2277 | 20:4145b7a59ef7 | 493 | printf("*****\nB6=%d\nX1=%d\nX2=%d\nX3=%d\nB3=%d\n",B6,X1,X2,X3,B3); |
qk2277 | 20:4145b7a59ef7 | 494 | #endif |
qk2277 | 20:4145b7a59ef7 | 495 | X1 = (calibration.AC3*B6)>>13; |
qk2277 | 20:4145b7a59ef7 | 496 | X2 = (calibration.B1*((B6*B6)>>12))>>16; |
qk2277 | 20:4145b7a59ef7 | 497 | X3 = ((X1+X2)+2)/4; |
qk2277 | 20:4145b7a59ef7 | 498 | B4 = (calibration.AC4*(uint32_t)(X3+32768))>>15; |
qk2277 | 20:4145b7a59ef7 | 499 | #ifdef DEBUG |
qk2277 | 20:4145b7a59ef7 | 500 | printf("X1=%d\nX2=%d\nX3=%d\nB4=%u\n",X1,X2,X3,B4); |
qk2277 | 20:4145b7a59ef7 | 501 | #endif |
qk2277 | 20:4145b7a59ef7 | 502 | B7 = ((uint32_t)UP - B3)*(50000>>oss); |
qk2277 | 20:4145b7a59ef7 | 503 | #ifdef DEBUG |
qk2277 | 20:4145b7a59ef7 | 504 | printf("B7=%u\n",B7); |
qk2277 | 20:4145b7a59ef7 | 505 | #endif |
qk2277 | 20:4145b7a59ef7 | 506 | int32_t P; |
qk2277 | 20:4145b7a59ef7 | 507 | if (B7 < 0x80000000) |
qk2277 | 20:4145b7a59ef7 | 508 | P = (B7*2)/B4; |
qk2277 | 20:4145b7a59ef7 | 509 | else |
qk2277 | 20:4145b7a59ef7 | 510 | P = (B7/B4)*2; |
qk2277 | 20:4145b7a59ef7 | 511 | #ifdef DEBUG |
qk2277 | 20:4145b7a59ef7 | 512 | printf("P=%d\n",P); |
qk2277 | 20:4145b7a59ef7 | 513 | #endif |
qk2277 | 20:4145b7a59ef7 | 514 | X1 = (P>>8)*(P>>8); |
qk2277 | 20:4145b7a59ef7 | 515 | #ifdef DEBUG |
qk2277 | 20:4145b7a59ef7 | 516 | printf("X1=%d\n",X1); |
qk2277 | 20:4145b7a59ef7 | 517 | #endif |
qk2277 | 20:4145b7a59ef7 | 518 | X1 = (X1*3038)>>16; |
qk2277 | 20:4145b7a59ef7 | 519 | #ifdef DEBUG |
qk2277 | 20:4145b7a59ef7 | 520 | printf("X1=%d\n",X1); |
qk2277 | 20:4145b7a59ef7 | 521 | #endif |
qk2277 | 20:4145b7a59ef7 | 522 | X2 = (-7357*P)>>16; |
qk2277 | 20:4145b7a59ef7 | 523 | #ifdef DEBUG |
qk2277 | 20:4145b7a59ef7 | 524 | printf("X2=%d\n",X2); |
qk2277 | 20:4145b7a59ef7 | 525 | #endif |
qk2277 | 20:4145b7a59ef7 | 526 | P = P + (X1+X2+3791)/16; |
qk2277 | 20:4145b7a59ef7 | 527 | #ifdef DEBUG |
qk2277 | 20:4145b7a59ef7 | 528 | printf("P=%d\n",P); |
qk2277 | 20:4145b7a59ef7 | 529 | #endif |
qk2277 | 20:4145b7a59ef7 | 530 | |
qk2277 | 20:4145b7a59ef7 | 531 | return P; |
qk2277 | 20:4145b7a59ef7 | 532 | |
qk2277 | 20:4145b7a59ef7 | 533 | } |
qk2277 | 20:4145b7a59ef7 | 534 | |
qk2277 | 20:4145b7a59ef7 | 535 | // configure the barometer |
qk2277 | 20:4145b7a59ef7 | 536 | void BMP180::init() |
qk2277 | 20:4145b7a59ef7 | 537 | { |
qk2277 | 20:4145b7a59ef7 | 538 | i2c->frequency(400000); // set Fast Mode I2C frequency |
qk2277 | 20:4145b7a59ef7 | 539 | |
qk2277 | 20:4145b7a59ef7 | 540 | char data = readByteFromRegister(ID_REG); // Section 4 - datasheet |
qk2277 | 20:4145b7a59ef7 | 541 | if (data != 0x55) { // if correct ID not found, hang and flash error message |
qk2277 | 20:4145b7a59ef7 | 542 | error(); |
qk2277 | 20:4145b7a59ef7 | 543 | } |
qk2277 | 20:4145b7a59ef7 | 544 | |
qk2277 | 20:4145b7a59ef7 | 545 | readCalibrationData(); |
qk2277 | 20:4145b7a59ef7 | 546 | |
qk2277 | 20:4145b7a59ef7 | 547 | oss = 1; // standard power oversampling setting |
qk2277 | 20:4145b7a59ef7 | 548 | |
qk2277 | 20:4145b7a59ef7 | 549 | #ifdef DEBUG |
qk2277 | 20:4145b7a59ef7 | 550 | oss = 0; // used when testing data sheet example |
qk2277 | 20:4145b7a59ef7 | 551 | #endif |
qk2277 | 20:4145b7a59ef7 | 552 | |
qk2277 | 20:4145b7a59ef7 | 553 | |
qk2277 | 20:4145b7a59ef7 | 554 | } |
qk2277 | 20:4145b7a59ef7 | 555 | |
qk2277 | 20:4145b7a59ef7 | 556 | // Reads factory calibrated data |
qk2277 | 20:4145b7a59ef7 | 557 | void BMP180::readCalibrationData() |
qk2277 | 20:4145b7a59ef7 | 558 | { |
qk2277 | 20:4145b7a59ef7 | 559 | |
qk2277 | 20:4145b7a59ef7 | 560 | char eeprom[22]; |
qk2277 | 20:4145b7a59ef7 | 561 | |
qk2277 | 20:4145b7a59ef7 | 562 | readBytesFromRegister(EEPROM_REG_ADD,22,eeprom); |
qk2277 | 20:4145b7a59ef7 | 563 | // store calibration data in structure |
qk2277 | 20:4145b7a59ef7 | 564 | calibration.AC1 = (int16_t) (eeprom[0] << 8) | eeprom[1]; |
qk2277 | 20:4145b7a59ef7 | 565 | calibration.AC2 = (int16_t) (eeprom[2] << 8) | eeprom[3]; |
qk2277 | 20:4145b7a59ef7 | 566 | calibration.AC3 = (int16_t) (eeprom[4] << 8) | eeprom[5]; |
qk2277 | 20:4145b7a59ef7 | 567 | calibration.AC4 = (uint16_t) (eeprom[6] << 8) | eeprom[7]; |
qk2277 | 20:4145b7a59ef7 | 568 | calibration.AC5 = (uint16_t) (eeprom[8] << 8) | eeprom[9]; |
qk2277 | 20:4145b7a59ef7 | 569 | calibration.AC6 = (uint16_t) (eeprom[10] << 8) | eeprom[11]; |
qk2277 | 20:4145b7a59ef7 | 570 | calibration.B1 = (int16_t) (eeprom[12] << 8) | eeprom[13]; |
qk2277 | 20:4145b7a59ef7 | 571 | calibration.B2 = (int16_t) (eeprom[14] << 8) | eeprom[15]; |
qk2277 | 20:4145b7a59ef7 | 572 | calibration.MB = (int16_t) (eeprom[16] << 8) | eeprom[17]; |
qk2277 | 20:4145b7a59ef7 | 573 | calibration.MC = (int16_t) (eeprom[18] << 8) | eeprom[19]; |
qk2277 | 20:4145b7a59ef7 | 574 | calibration.MD = (int16_t) (eeprom[20] << 8) | eeprom[21]; |
qk2277 | 20:4145b7a59ef7 | 575 | |
qk2277 | 20:4145b7a59ef7 | 576 | // test data from data sheet |
qk2277 | 20:4145b7a59ef7 | 577 | #ifdef DEBUG |
qk2277 | 20:4145b7a59ef7 | 578 | calibration.AC1 = 408; |
qk2277 | 20:4145b7a59ef7 | 579 | calibration.AC2 = -72; |
qk2277 | 20:4145b7a59ef7 | 580 | calibration.AC3 = -14383; |
qk2277 | 20:4145b7a59ef7 | 581 | calibration.AC4 = 32741; |
qk2277 | 20:4145b7a59ef7 | 582 | calibration.AC5 = 32757; |
qk2277 | 20:4145b7a59ef7 | 583 | calibration.AC6 = 23153; |
qk2277 | 20:4145b7a59ef7 | 584 | calibration.B1 = 6190; |
qk2277 | 20:4145b7a59ef7 | 585 | calibration.B2 = 4; |
qk2277 | 20:4145b7a59ef7 | 586 | calibration.MB = -32768; |
qk2277 | 20:4145b7a59ef7 | 587 | calibration.MC = -8711; |
qk2277 | 20:4145b7a59ef7 | 588 | calibration.MD = 2868; |
qk2277 | 20:4145b7a59ef7 | 589 | printf("****EXAMPLE CALIBRATION DATA****\n"); |
qk2277 | 20:4145b7a59ef7 | 590 | printf("AC1=%d\nAC2=%d\nAC3=%d\nAC4=%u\nAC5=%u\nAC6=%u\nB1=%d\nB2=%d\nMB=%d\nMC=%d\nMD=%d\n", |
qk2277 | 20:4145b7a59ef7 | 591 | calibration.AC1,calibration.AC2,calibration.AC3,calibration.AC4,calibration.AC5,calibration.AC6, |
qk2277 | 20:4145b7a59ef7 | 592 | calibration.B1,calibration.B2,calibration.MB,calibration.MC,calibration.MD); |
qk2277 | 20:4145b7a59ef7 | 593 | #endif |
qk2277 | 20:4145b7a59ef7 | 594 | } |
qk2277 | 20:4145b7a59ef7 | 595 | |
qk2277 | 20:4145b7a59ef7 | 596 | |
qk2277 | 20:4145b7a59ef7 | 597 | // reads a byte from a specific register |
qk2277 | 20:4145b7a59ef7 | 598 | char BMP180::readByteFromRegister(char reg) |
qk2277 | 20:4145b7a59ef7 | 599 | { |
qk2277 | 20:4145b7a59ef7 | 600 | int nack = i2c->write(BMP180_W_ADDRESS,®,1,true); // send the register address to the slave |
qk2277 | 20:4145b7a59ef7 | 601 | if (nack) |
qk2277 | 20:4145b7a59ef7 | 602 | error(); // if we don't receive acknowledgement, flash error message |
qk2277 | 20:4145b7a59ef7 | 603 | |
qk2277 | 20:4145b7a59ef7 | 604 | char rx; |
qk2277 | 20:4145b7a59ef7 | 605 | nack = i2c->read(BMP180_W_ADDRESS,&rx,1); // read a byte from the register and store in buffer |
qk2277 | 20:4145b7a59ef7 | 606 | if (nack) |
qk2277 | 20:4145b7a59ef7 | 607 | error(); // if we don't receive acknowledgement, flash error message |
qk2277 | 20:4145b7a59ef7 | 608 | |
qk2277 | 20:4145b7a59ef7 | 609 | return rx; |
qk2277 | 20:4145b7a59ef7 | 610 | } |
qk2277 | 20:4145b7a59ef7 | 611 | |
qk2277 | 20:4145b7a59ef7 | 612 | // reads a series of bytes, starting from a specific register |
qk2277 | 20:4145b7a59ef7 | 613 | void BMP180::readBytesFromRegister(char reg,int numberOfBytes,char bytes[]) |
qk2277 | 20:4145b7a59ef7 | 614 | { |
qk2277 | 20:4145b7a59ef7 | 615 | int nack = i2c->write(BMP180_W_ADDRESS,®,1,true); // send the slave write address and the configuration register address |
qk2277 | 20:4145b7a59ef7 | 616 | |
qk2277 | 20:4145b7a59ef7 | 617 | if (nack) |
qk2277 | 20:4145b7a59ef7 | 618 | error(); // if we don't receive acknowledgement, flash error message |
qk2277 | 20:4145b7a59ef7 | 619 | |
qk2277 | 20:4145b7a59ef7 | 620 | nack = i2c->read(BMP180_W_ADDRESS,bytes,numberOfBytes); // read bytes |
qk2277 | 20:4145b7a59ef7 | 621 | if (nack) |
qk2277 | 20:4145b7a59ef7 | 622 | error(); // if we don't receive acknowledgement, flash error message |
qk2277 | 20:4145b7a59ef7 | 623 | |
qk2277 | 20:4145b7a59ef7 | 624 | } |
qk2277 | 20:4145b7a59ef7 | 625 | |
qk2277 | 20:4145b7a59ef7 | 626 | // sends a byte to a specific register |
qk2277 | 20:4145b7a59ef7 | 627 | void BMP180::sendByteToRegister(char byte,char reg) |
qk2277 | 20:4145b7a59ef7 | 628 | { |
qk2277 | 20:4145b7a59ef7 | 629 | char data[2]; |
qk2277 | 20:4145b7a59ef7 | 630 | data[0] = reg; |
qk2277 | 20:4145b7a59ef7 | 631 | data[1] = byte; |
qk2277 | 20:4145b7a59ef7 | 632 | // send the register address, followed by the data |
qk2277 | 20:4145b7a59ef7 | 633 | int nack = i2c->write(BMP180_W_ADDRESS,data,2); |
qk2277 | 20:4145b7a59ef7 | 634 | if (nack) |
qk2277 | 20:4145b7a59ef7 | 635 | error(); // if we don't receive acknowledgement, flash error message |
qk2277 | 20:4145b7a59ef7 | 636 | |
qk2277 | 20:4145b7a59ef7 | 637 | } |
qk2277 | 20:4145b7a59ef7 | 638 | |
qk2277 | 20:4145b7a59ef7 | 639 | void BMP180::error() |
qk2277 | 20:4145b7a59ef7 | 640 | { |
qk2277 | 20:4145b7a59ef7 | 641 | while(1) { |
qk2277 | 20:4145b7a59ef7 | 642 | leds->write(15); |
qk2277 | 20:4145b7a59ef7 | 643 | wait(0.1); |
qk2277 | 20:4145b7a59ef7 | 644 | leds->write(0); |
qk2277 | 20:4145b7a59ef7 | 645 | wait(0.1); |
qk2277 | 20:4145b7a59ef7 | 646 | } |
qk2277 | 20:4145b7a59ef7 | 647 | } |
qk2277 | 20:4145b7a59ef7 | 648 | |
qk2277 | 20:4145b7a59ef7 | 649 | |
qk2277 | 20:4145b7a59ef7 | 650 | |
qk2277 | 20:4145b7a59ef7 | 651 | |
qk2277 | 20:4145b7a59ef7 | 652 | N5110 lcd(p7,p8,p9,p10,p11,p13,p26);//The ports being connected of the mbed |
qk2277 | 20:4145b7a59ef7 | 653 | BusOut leds(LED4,LED3,LED2,LED1);//The ports being connected of the LCD |
qk2277 | 20:4145b7a59ef7 | 654 | BMP180 bmp180(p28,p27);//The ports being connected to the sensor |
qk2277 | 20:4145b7a59ef7 | 655 | Serial serial(USBTX,USBRX);//Timer set-up tool |
qk2277 | 20:4145b7a59ef7 | 656 | |
qk2277 | 20:4145b7a59ef7 | 657 | |
qk2277 | 20:4145b7a59ef7 | 658 | //Define the variable |
qk2277 | 20:4145b7a59ef7 | 659 | void serialISR();//ISR that is called when serial data is received |
qk2277 | 20:4145b7a59ef7 | 660 | void setTime();// function to set the UNIX time |
qk2277 | 20:4145b7a59ef7 | 661 | int setTimerFlag = 0;// flag for ISR |
qk2277 | 20:4145b7a59ef7 | 662 | char rxString[16];//Create a 16 chars row to display the data |
qk2277 | 20:4145b7a59ef7 | 663 | |
qk2277 | 20:4145b7a59ef7 | 664 | int main() |
qk2277 | 20:4145b7a59ef7 | 665 | { |
qk2277 | 20:4145b7a59ef7 | 666 | lcd.init(); |
qk2277 | 20:4145b7a59ef7 | 667 | bmp180.init();//Display the word before the sensor data |
qk2277 | 20:4145b7a59ef7 | 668 | lcd.printString("Weather",0,0);//At the location (0,0),display word "Weather" |
qk2277 | 20:4145b7a59ef7 | 669 | lcd.printString("Station",1,3);//At the location (1,3),display word "Station" |
qk2277 | 20:4145b7a59ef7 | 670 | wait(2.0);//The word above stay for 2s |
qk2277 | 20:4145b7a59ef7 | 671 | lcd.clear();//Clean the display for the continued work |
qk2277 | 20:4145b7a59ef7 | 672 | |
qk2277 | 20:4145b7a59ef7 | 673 | Measurement measurement; |
qk2277 | 20:4145b7a59ef7 | 674 | |
qk2277 | 20:4145b7a59ef7 | 675 | serial.attach(&serialISR);//attach serial ISR |
qk2277 | 20:4145b7a59ef7 | 676 | char t[30];//Create a 30 chars row to display the time |
qk2277 | 20:4145b7a59ef7 | 677 | while(1) { |
qk2277 | 20:4145b7a59ef7 | 678 | |
qk2277 | 20:4145b7a59ef7 | 679 | time_t seconds = time(NULL);//get current time |
qk2277 | 20:4145b7a59ef7 | 680 | // format time into a string (time and date) |
qk2277 | 20:4145b7a59ef7 | 681 | strftime(t, 30 , "%X %D",localtime(&seconds));// |
qk2277 | 20:4145b7a59ef7 | 682 | // print over serial |
qk2277 | 20:4145b7a59ef7 | 683 | serial.printf("Time = %s\n" ,t);//Display the timer |
qk2277 | 20:4145b7a59ef7 | 684 | lcd.printString(t,0,5);//The location of the timer |
qk2277 | 20:4145b7a59ef7 | 685 | |
qk2277 | 20:4145b7a59ef7 | 686 | if(setTimerFlag) {// if updated time has been sent |
qk2277 | 20:4145b7a59ef7 | 687 | setTimerFlag = 0;//clear flag |
qk2277 | 20:4145b7a59ef7 | 688 | setTime();// update time |
qk2277 | 20:4145b7a59ef7 | 689 | |
qk2277 | 20:4145b7a59ef7 | 690 | } |
qk2277 | 20:4145b7a59ef7 | 691 | |
qk2277 | 20:4145b7a59ef7 | 692 | measurement = bmp180.readValues();// |
qk2277 | 20:4145b7a59ef7 | 693 | char T[14];//Create a 14 chars row to display the temperature |
qk2277 | 20:4145b7a59ef7 | 694 | int length =sprintf(T,"T = %.2f C",measurement.temperature);//Set up "T = sensor data" as the thing will be shown |
qk2277 | 20:4145b7a59ef7 | 695 | if (length <= 14)//Judge the length of chars |
qk2277 | 20:4145b7a59ef7 | 696 | lcd.printString(T,0,1);//The location of the T will be shown |
qk2277 | 20:4145b7a59ef7 | 697 | char P[14];//Create a 14 chars row to display the pressure |
qk2277 | 20:4145b7a59ef7 | 698 | length = sprintf(P,"P = %.2f mb",measurement.pressure);//Set up "P = sensor data" as the thing will be shown |
qk2277 | 20:4145b7a59ef7 | 699 | lcd.printString(P,0,3); //The location of the P will be shown |
qk2277 | 20:4145b7a59ef7 | 700 | wait(1);//Repeat the circulate each 1s |
qk2277 | 20:4145b7a59ef7 | 701 | lcd.clear(); //Clear the data for next processing |
qk2277 | 20:4145b7a59ef7 | 702 | } |
qk2277 | 20:4145b7a59ef7 | 703 | } |
qk2277 | 20:4145b7a59ef7 | 704 | |
qk2277 | 20:4145b7a59ef7 | 705 | |
qk2277 | 20:4145b7a59ef7 | 706 | void setTime()//// print time for debugging |
qk2277 | 20:4145b7a59ef7 | 707 | { |
qk2277 | 20:4145b7a59ef7 | 708 | |
qk2277 | 20:4145b7a59ef7 | 709 | serial.printf("set_time - %s",rxString); |
qk2277 | 20:4145b7a59ef7 | 710 | //// atoi() converts a string to an integer |
qk2277 | 20:4145b7a59ef7 | 711 | int time = atoi(rxString); |
qk2277 | 20:4145b7a59ef7 | 712 | //update the time |
qk2277 | 20:4145b7a59ef7 | 713 | set_time(time); |
qk2277 | 20:4145b7a59ef7 | 714 | } |
qk2277 | 20:4145b7a59ef7 | 715 | |
qk2277 | 20:4145b7a59ef7 | 716 | void serialISR()// when a serial interrupt occurs, read rx string into buffer |
qk2277 | 20:4145b7a59ef7 | 717 | { |
qk2277 | 20:4145b7a59ef7 | 718 | |
qk2277 | 20:4145b7a59ef7 | 719 | serial.gets(rxString,16); |
qk2277 | 20:4145b7a59ef7 | 720 | //// set flag |
qk2277 | 20:4145b7a59ef7 | 721 | setTimerFlag = 1; |
qk2277 | 20:4145b7a59ef7 | 722 | } |