shenzhi xu / Mbed 2 deprecated temperature

Dependencies:   mbed

Committer:
xuszdd
Date:
Sun May 10 20:47:53 2015 +0000
Revision:
0:ee32d3554f6f
temperature and pressure

Who changed what in which revision?

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