shenzhi xu / Mbed 2 deprecated temperature

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers N5110.cpp Source File

N5110.cpp

Go to the documentation of this file.
00001 /**
00002 @file N5110.cpp
00003 
00004 @brief Member functions implementations
00005 
00006 */
00007 #include "mbed.h"
00008 #include "N5110.h"
00009 #include "BMP180.h"
00010 #include "beep.h"
00011 
00012 
00013 N5110::N5110(PinName pwrPin, PinName scePin, PinName rstPin, PinName dcPin, PinName mosiPin, PinName sclkPin, PinName ledPin)
00014 {
00015 
00016     spi = new SPI(mosiPin,NC,sclkPin); // create new SPI instance and initialise
00017     initSPI();
00018 
00019     // set up pins as required
00020     led = new PwmOut(ledPin);
00021     pwr = new DigitalOut(pwrPin);
00022     sce = new DigitalOut(scePin);
00023     rst = new DigitalOut(rstPin);
00024     dc = new DigitalOut(dcPin);
00025 
00026 }
00027 
00028 // initialise function - powers up and sends the initialisation commands
00029 void N5110::init()
00030 {
00031     turnOn();     // power up
00032     wait_ms(10);  // small delay seems to prevent spurious pixels during mbed reset
00033     reset();      // reset LCD - must be done within 100 ms
00034 
00035     // function set - extended
00036     sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE);
00037     // Don't completely understand these parameters - they seem to work as they are
00038     // Consult the datasheet if you need to change them
00039     sendCommand(CMD_VOP_7V38);    // operating voltage - these values are from Chris Yan's Library
00040     sendCommand(CMD_TC_TEMP_2);   // temperature control
00041     sendCommand(CMD_BI_MUX_48);   // bias
00042 
00043     // function set - basic
00044     sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE);
00045     normalMode();  // normal video mode by default
00046     sendCommand(CMD_DC_NORMAL_MODE);  // black on white
00047 
00048     // RAM is undefined at power-up so clear
00049     clearRAM();
00050 
00051 }
00052 
00053 // sets normal video mode (black on white)
00054 void N5110::normalMode()
00055 {
00056     sendCommand(CMD_DC_NORMAL_MODE);
00057 
00058 }
00059 
00060 // sets normal video mode (white on black)
00061 void N5110::inverseMode()
00062 {
00063     sendCommand(CMD_DC_INVERT_VIDEO);
00064 }
00065 
00066 // function to power up the LCD and backlight
00067 void N5110::turnOn()
00068 {
00069     // set brightness of LED - 0.0 to 1.0 - default is 50%
00070     setBrightness(0.5);
00071     pwr->write(1);  // apply power
00072 }
00073 
00074 // function to power down LCD
00075 void N5110::turnOff()
00076 {
00077     setBrightness(0.0);  // turn backlight off
00078     clearRAM();   // clear RAM to ensure specified current consumption
00079     // send command to ensure we are in basic mode
00080     sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE);
00081     // clear the display
00082     sendCommand(CMD_DC_CLEAR_DISPLAY);
00083     // enter the extended mode and power down
00084     sendCommand(0x20 | CMD_FS_POWER_DOWN_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE);
00085     // small delay and then turn off the power pin
00086     wait_ms(10);
00087     pwr->write(0);
00088 
00089 }
00090 
00091 // function to change LED backlight brightness
00092 void N5110::setBrightness(float brightness)
00093 {
00094     // check whether brightness is within range
00095     if (brightness < 0.0)
00096         brightness = 0.0;
00097     if (brightness > 1.0)
00098         brightness = 1.0;
00099     // set PWM duty cycle
00100     led->write(brightness);
00101 }
00102 
00103 
00104 // pulse the active low reset line
00105 void N5110::reset()
00106 {
00107     rst->write(0);  // reset the LCD
00108     rst->write(1);
00109 }
00110 
00111 // function to initialise SPI peripheral
00112 void N5110::initSPI()
00113 {
00114     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
00115     spi->frequency(4000000);  // maximum of screen is 4 MHz
00116 }
00117 
00118 // send a command to the display
00119 void N5110::sendCommand(unsigned char command)
00120 {
00121     dc->write(0);  // set DC low for command
00122     sce->write(0); // set CE low to begin frame
00123     spi->write(command);  // send command
00124     dc->write(1);  // turn back to data by default
00125     sce->write(1); // set CE high to end frame (expected for transmission of single byte)
00126 
00127 }
00128 
00129 // send data to the display at the current XY address
00130 // dc is set to 1 (i.e. data) after sending a command and so should
00131 // be the default mode.
00132 void N5110::sendData(unsigned char data)
00133 {
00134     sce->write(0);   // set CE low to begin frame
00135     spi->write(data);
00136     sce->write(1);  // set CE high to end frame (expected for transmission of single byte)
00137 }
00138 
00139 // this function writes 0 to the 504 bytes to clear the RAM
00140 void N5110::clearRAM()
00141 {
00142     int i;
00143     sce->write(0);  //set CE low to begin frame
00144     for(i = 0; i < WIDTH * HEIGHT; i++) { // 48 x 84 bits = 504 bytes
00145         spi->write(0x00);  // send 0's
00146     }
00147     sce->write(1); // set CE high to end frame
00148 
00149 }
00150 
00151 // function to set the XY address in RAM for subsequenct data write
00152 void N5110::setXYAddress(int x, int y)
00153 {
00154     if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) {  // check within range
00155         sendCommand(0x80 | x);  // send addresses to display with relevant mask
00156         sendCommand(0x40 | y);
00157     }
00158 }
00159 
00160 // These functions are used to set, clear and get the value of pixels in the display
00161 // Pixels are addressed in the range of 0 to 47 (y) and 0 to 83 (x).  The refresh()
00162 // function must be called after set and clear in order to update the display
00163 void N5110::setPixel(int x, int y)
00164 {
00165     if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) {  // check within range
00166         // calculate bank and shift 1 to required position in the data byte
00167         buffer[x][y/8] |= (1 << y%8);
00168     }
00169 }
00170 
00171 void N5110::clearPixel(int x, int y)
00172 {
00173     if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) {  // check within range
00174         // calculate bank and shift 1 to required position (using bit clear)
00175         buffer[x][y/8] &= ~(1 << y%8);
00176     }
00177 }
00178 
00179 int N5110::getPixel(int x, int y)
00180 {
00181     if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) {  // check within range
00182         // return relevant bank and mask required bit
00183         return (int) buffer[x][y/8] & (1 << y%8);
00184         // note this does not necessarily return 1 - a non-zero number represents a pixel
00185     } else {
00186         return 0;
00187     }
00188 }
00189 
00190 // function to refresh the display
00191 void N5110::refresh()
00192 {
00193     int i,j;
00194 
00195     setXYAddress(0,0);  // important to set address back to 0,0 before refreshing display
00196     // address auto increments after printing string, so buffer[0][0] will not coincide
00197     // with top-left pixel after priting string
00198 
00199     sce->write(0);  //set CE low to begin frame
00200 
00201     for(j = 0; j < BANKS; j++) {  // be careful to use correct order (j,i) for horizontal addressing
00202         for(i = 0; i < WIDTH; i++) {
00203             spi->write(buffer[i][j]);  // send buffer
00204         }
00205     }
00206     sce->write(1); // set CE high to end frame
00207 
00208 }
00209 
00210 // fills the buffer with random bytes.  Can be used to test the display.
00211 // The rand() function isn't seeded so it probably creates the same pattern everytime
00212 void N5110::randomiseBuffer()
00213 {
00214     int i,j;
00215     for(j = 0; j < BANKS; j++) {  // be careful to use correct order (j,i) for horizontal addressing
00216         for(i = 0; i < WIDTH; i++) {
00217             buffer[i][j] = rand()%256;  // generate random byte
00218         }
00219     }
00220 
00221 }
00222 
00223 // function to print 5x7 font
00224 void N5110::printChar(char c,int x,int y)
00225 {
00226     if (y>=0 && y<BANKS) {  // check if printing in range of y banks
00227 
00228         for (int i = 0; i < 5 ; i++ ) {
00229             int pixel_x = x+i;
00230             if (pixel_x > WIDTH-1)  // ensure pixel isn't outside the buffer size (0 - 83)
00231                 break;
00232             buffer[pixel_x][y] = font5x7[(c - 32)*5 + i];
00233             // array is offset by 32 relative to ASCII, each character is 5 pixels wide
00234         }
00235 
00236         refresh();  // this sends the buffer to the display and sets address (cursor) back to 0,0
00237     }
00238 }
00239 
00240 // function to print string at specified position
00241 void N5110::printString(const char * str,int x,int y)
00242 {
00243     if (y>=0 && y<BANKS) {  // check if printing in range of y banks
00244 
00245         int n = 0 ; // counter for number of characters in string
00246         // loop through string and print character
00247         while(*str) {
00248 
00249             // writes the character bitmap data to the buffer, so that
00250             // text and pixels can be displayed at the same time
00251             for (int i = 0; i < 5 ; i++ ) {
00252                 int pixel_x = x+i+n*6;
00253                 if (pixel_x > WIDTH-1) // ensure pixel isn't outside the buffer size (0 - 83)
00254                     break;
00255                 buffer[pixel_x][y] = font5x7[(*str - 32)*5 + i];
00256             }
00257 
00258             str++;  // go to next character in string
00259 
00260             n++;    // increment index
00261 
00262         }
00263 
00264         refresh();  // this sends the buffer to the display and sets address (cursor) back to 0,0
00265     }
00266 }
00267 
00268 // function to clear the screen
00269 void N5110::clear()
00270 {
00271     clearBuffer();  // clear the buffer then call the refresh function
00272     refresh();
00273 }
00274 
00275 // function to clear the buffer
00276 void N5110::clearBuffer()
00277 {
00278     int i,j;
00279     for (i=0; i<WIDTH; i++) {  // loop through the banks and set the buffer to 0
00280         for (j=0; j<BANKS; j++) {
00281             buffer[i][j]=0;
00282         }
00283     }
00284 }
00285 
00286 // function to plot array on display
00287 void N5110::plotArray(float array[])
00288 {
00289 
00290     int i;
00291 
00292     for (i=0; i<WIDTH; i++) {  // loop through array
00293         // elements are normalised from 0.0 to 1.0, so multiply
00294         // by 47 to convert to pixel range, and subtract from 47
00295         // since top-left is 0,0 in the display geometry
00296         setPixel(i,47 - int(array[i]*47.0));
00297     }
00298 
00299     refresh();
00300 
00301 }
00302 
00303 // function to draw circle
00304 void N5110:: drawCircle(int x0,int y0,int radius,int fill)
00305 {
00306     // from http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
00307     int x = radius;
00308     int y = 0;
00309     int radiusError = 1-x;
00310 
00311     while(x >= y) {
00312 
00313         // if transparent, just draw outline
00314         if (fill == 0) {
00315             setPixel( x + x0,  y + y0);
00316             setPixel(-x + x0,  y + y0);
00317             setPixel( y + x0,  x + y0);
00318             setPixel(-y + x0,  x + y0);
00319             setPixel(-y + x0, -x + y0);
00320             setPixel( y + x0, -x + y0);
00321             setPixel( x + x0, -y + y0);
00322             setPixel(-x + x0, -y + y0);
00323         } else {  // drawing filled circle, so draw lines between points at same y value
00324 
00325             int type = (fill==1) ? 1:0;  // black or white fill
00326 
00327             drawLine(x+x0,y+y0,-x+x0,y+y0,type);
00328             drawLine(y+x0,x+y0,-y+x0,x+y0,type);
00329             drawLine(y+x0,-x+y0,-y+x0,-x+y0,type);
00330             drawLine(x+x0,-y+y0,-x+x0,-y+y0,type);
00331         }
00332 
00333 
00334         y++;
00335         if (radiusError<0) {
00336             radiusError += 2 * y + 1;
00337         } else {
00338             x--;
00339             radiusError += 2 * (y - x) + 1;
00340         }
00341     }
00342 
00343 }
00344 
00345 void N5110::drawLine(int x0,int y0,int x1,int y1,int type)
00346 {
00347     int y_range = y1-y0;  // calc range of y and x
00348     int x_range = x1-x0;
00349     int start,stop,step;
00350 
00351     // if dotted line, set step to 2, else step is 1
00352     step = (type==2) ? 2:1;
00353 
00354     // make sure we loop over the largest range to get the most pixels on the display
00355     // for instance, if drawing a vertical line (x_range = 0), we need to loop down the y pixels
00356     // or else we'll only end up with 1 pixel in the x column
00357     if ( abs(x_range) > abs(y_range) ) {
00358 
00359         // ensure we loop from smallest to largest or else for-loop won't run as expected
00360         start = x1>x0 ? x0:x1;
00361         stop =  x1>x0 ? x1:x0;
00362 
00363         // loop between x pixels
00364         for (int x = start; x<= stop ; x+=step) {
00365             // do linear interpolation
00366             int y = y0 + (y1-y0)*(x-x0)/(x1-x0);
00367 
00368             if (type == 0)   // if 'white' line, turn off pixel
00369                 clearPixel(x,y);
00370             else
00371                 setPixel(x,y);  // else if 'black' or 'dotted' turn on pixel
00372         }
00373     } else {
00374 
00375         // ensure we loop from smallest to largest or else for-loop won't run as expected
00376         start = y1>y0 ? y0:y1;
00377         stop =  y1>y0 ? y1:y0;
00378 
00379         for (int y = start; y<= stop ; y+=step) {
00380             // do linear interpolation
00381             int x = x0 + (x1-x0)*(y-y0)/(y1-y0);
00382 
00383             if (type == 0)   // if 'white' line, turn off pixel
00384                 clearPixel(x,y);
00385             else
00386                 setPixel(x,y);  // else if 'black' or 'dotted' turn on pixel
00387 
00388         }
00389     }
00390 
00391 }
00392 
00393 void N5110::drawRect(int x0,int y0,int width,int height,int fill)
00394 {
00395 
00396     if (fill == 0) { // transparent, just outline
00397         drawLine(x0,y0,x0+width,y0,1);  // top
00398         drawLine(x0,y0+height,x0+width,y0+height,1);  // bottom
00399         drawLine(x0,y0,x0,y0+height,1);  // left
00400         drawLine(x0+width,y0,x0+width,y0+height,1);  // right
00401     } else { // filled rectangle
00402         int type = (fill==1) ? 1:0;  // black or white fill
00403         for (int y = y0; y<= y0+height; y++) {  // loop through rows of rectangle
00404             drawLine(x0,y,x0+width,y,type);  // draw line across screen
00405         }
00406     }
00407 
00408 }
00409 
00410 
00411 
00412 
00413 
00414 
00415 BMP180::BMP180(PinName sdaPin, PinName sclPin)
00416 {
00417     i2c = new I2C(sdaPin,sclPin); // create new I2C instance and initialise
00418     i2c->frequency(400000);       // I2C Fast Mode - 400kHz
00419     leds = new BusOut(LED4,LED3,LED2,LED1);
00420 }
00421 
00422 Measurement BMP180::readValues()
00423 {
00424     // algorithm for taking measurement is taken from datasheet
00425     int32_t UT = readUncompensatedTemperatureValue();
00426     int32_t UP = readUncompensatedPressureValue();
00427     // once you have the uncompensated T and P, you can calculate the true T and P
00428     // using the equations from the datasheet
00429     int32_t T = calcTrueTemperature(UT);
00430     int32_t P = calcTruePressure(UP);
00431 
00432     Measurement measurement;
00433     measurement.temperature = T*0.1;  // scaled by 0.1 C
00434     measurement.pressure = P*0.01;    // Put pressure in mb
00435 
00436     return measurement;
00437 }
00438 
00439 int32_t BMP180::readUncompensatedTemperatureValue()
00440 {
00441     // from algorithm in datasheet - p15
00442     sendByteToRegister(0x2E,0xF4);
00443     wait_ms(5);  // 4.5 ms delay for OSS = 1
00444     char MSB = readByteFromRegister(0xF6);
00445     char LSB = readByteFromRegister(0xF7);
00446     // combine in 16-bit value
00447     int UT = (MSB << 8) | LSB;
00448 #ifdef DEBUG
00449     UT = 27898;  // test data from datasheet
00450     printf("****DEBUG MODE****\nUT = %d\n",UT);
00451 #endif
00452     return UT;
00453 }
00454 
00455 int32_t BMP180::readUncompensatedPressureValue()
00456 {
00457     // from datasheet
00458     char byte = 0x34 + (oss << 6);
00459     sendByteToRegister(byte,0xF4);
00460     wait_ms(8);  // 7.5 ms delay for OSS = 1
00461 
00462     char MSB = readByteFromRegister(0xF6);
00463     char LSB = readByteFromRegister(0xF7);
00464     char XLSB = readByteFromRegister(0xF7);
00465     int UP = (MSB << 16 | LSB << 8 | XLSB) >> (8 - oss);
00466 
00467 #ifdef DEBUG
00468     UP = 23843;   // test data from datasheet
00469     printf("UP = %d\n",UP);
00470 #endif
00471     return UP;
00472 }
00473 
00474 int32_t BMP180::calcTrueTemperature(int32_t UT)
00475 {
00476     // equations from data sheet
00477     X1 = ((UT - calibration.AC6)*calibration.AC5) >> 15;
00478     X2 = (calibration.MC << 11) / (X1 + calibration.MD);
00479     B5 = X1 + X2;
00480     int32_t T = (B5 + 8) >> 4;
00481 #ifdef DEBUG
00482     printf("****\nX1=%d\nX2=%d\nB5=%d\nT=%d\n",X1,X2,B5,T);
00483 #endif
00484     return T;
00485 }
00486 
00487 int32_t BMP180::calcTruePressure(int32_t UP)
00488 {
00489     // equations from data sheet
00490     B6 = B5 - 4000;
00491     X1 = (calibration.B2 * ((B6*B6) >> 12))>>11;
00492     X2 = (calibration.AC2*B6)>>11;
00493     X3 = X1 + X2;
00494     B3 = (((calibration.AC1*4 + X3) << oss)+2)/4;
00495 #ifdef DEBUG
00496     printf("*****\nB6=%d\nX1=%d\nX2=%d\nX3=%d\nB3=%d\n",B6,X1,X2,X3,B3);
00497 #endif
00498     X1 = (calibration.AC3*B6)>>13;
00499     X2 = (calibration.B1*((B6*B6)>>12))>>16;
00500     X3 = ((X1+X2)+2)/4;
00501     B4 = (calibration.AC4*(uint32_t)(X3+32768))>>15;
00502 #ifdef DEBUG
00503     printf("X1=%d\nX2=%d\nX3=%d\nB4=%u\n",X1,X2,X3,B4);
00504 #endif
00505     B7 = ((uint32_t)UP - B3)*(50000>>oss);
00506 #ifdef DEBUG
00507     printf("B7=%u\n",B7);
00508 #endif
00509     int32_t P;
00510     if (B7 < 0x80000000)
00511         P = (B7*2)/B4;
00512     else
00513         P = (B7/B4)*2;
00514 #ifdef DEBUG
00515     printf("P=%d\n",P);
00516 #endif
00517     X1 = (P>>8)*(P>>8);
00518 #ifdef DEBUG
00519     printf("X1=%d\n",X1);
00520 #endif
00521     X1 = (X1*3038)>>16;
00522 #ifdef DEBUG
00523     printf("X1=%d\n",X1);
00524 #endif
00525     X2 = (-7357*P)>>16;
00526 #ifdef DEBUG
00527     printf("X2=%d\n",X2);
00528 #endif
00529     P = P + (X1+X2+3791)/16;
00530 #ifdef DEBUG
00531     printf("P=%d\n",P);
00532 #endif
00533 
00534     return P;
00535 
00536 }
00537 
00538 // configure the barometer
00539 void BMP180::init()
00540 {
00541     i2c->frequency(400000); // set Fast Mode I2C frequency
00542 
00543     char data = readByteFromRegister(ID_REG);  // Section 4 - datasheet
00544     if (data != 0x55) { // if correct ID not found, hang and flash error message
00545         error();
00546     }
00547 
00548     readCalibrationData();
00549 
00550     oss = 1;  // standard power oversampling setting
00551 
00552 #ifdef DEBUG
00553     oss = 0;  // used when testing data sheet example
00554 #endif
00555 
00556 
00557 }
00558 
00559 // Reads factory calibrated data
00560 void BMP180::readCalibrationData()
00561 {
00562 
00563     char eeprom[22];
00564 
00565     readBytesFromRegister(EEPROM_REG_ADD,22,eeprom);
00566     // store calibration data in structure
00567     calibration.AC1 = (int16_t) (eeprom[0] << 8) | eeprom[1];
00568     calibration.AC2 = (int16_t) (eeprom[2] << 8) | eeprom[3];
00569     calibration.AC3 = (int16_t) (eeprom[4] << 8) | eeprom[5];
00570     calibration.AC4 = (uint16_t) (eeprom[6] << 8) | eeprom[7];
00571     calibration.AC5 = (uint16_t) (eeprom[8] << 8) | eeprom[9];
00572     calibration.AC6 = (uint16_t) (eeprom[10] << 8) | eeprom[11];
00573     calibration.B1 = (int16_t) (eeprom[12] << 8) | eeprom[13];
00574     calibration.B2 = (int16_t) (eeprom[14] << 8) | eeprom[15];
00575     calibration.MB = (int16_t) (eeprom[16] << 8) | eeprom[17];
00576     calibration.MC = (int16_t) (eeprom[18] << 8) | eeprom[19];
00577     calibration.MD = (int16_t) (eeprom[20] << 8) | eeprom[21];
00578 
00579     // test data from data sheet
00580 #ifdef DEBUG
00581     calibration.AC1 = 408;
00582     calibration.AC2 = -72;
00583     calibration.AC3 = -14383;
00584     calibration.AC4 = 32741;
00585     calibration.AC5 = 32757;
00586     calibration.AC6 = 23153;
00587     calibration.B1 = 6190;
00588     calibration.B2 = 4;
00589     calibration.MB = -32768;
00590     calibration.MC = -8711;
00591     calibration.MD = 2868;
00592     printf("****EXAMPLE CALIBRATION DATA****\n");
00593     printf("AC1=%d\nAC2=%d\nAC3=%d\nAC4=%u\nAC5=%u\nAC6=%u\nB1=%d\nB2=%d\nMB=%d\nMC=%d\nMD=%d\n",
00594            calibration.AC1,calibration.AC2,calibration.AC3,calibration.AC4,calibration.AC5,calibration.AC6,
00595            calibration.B1,calibration.B2,calibration.MB,calibration.MC,calibration.MD);
00596 #endif
00597 }
00598 
00599 
00600 // reads a byte from a specific register
00601 char BMP180::readByteFromRegister(char reg)
00602 {
00603     int nack = i2c->write(BMP180_W_ADDRESS,&reg,1,true);  // send the register address to the slave
00604     if (nack)
00605         error();  // if we don't receive acknowledgement, flash error message
00606 
00607     char rx;
00608     nack = i2c->read(BMP180_W_ADDRESS,&rx,1);  // read a byte from the register and store in buffer
00609     if (nack)
00610         error();  // if we don't receive acknowledgement, flash error message
00611 
00612     return rx;
00613 }
00614 
00615 // reads a series of bytes, starting from a specific register
00616 void BMP180::readBytesFromRegister(char reg,int numberOfBytes,char bytes[])
00617 {
00618     int nack = i2c->write(BMP180_W_ADDRESS,&reg,1,true);  // send the slave write address and the configuration register address
00619 
00620     if (nack)
00621         error();  // if we don't receive acknowledgement, flash error message
00622 
00623     nack = i2c->read(BMP180_W_ADDRESS,bytes,numberOfBytes);  // read bytes
00624     if (nack)
00625         error();  // if we don't receive acknowledgement, flash error message
00626 
00627 }
00628 
00629 // sends a byte to a specific register
00630 void BMP180::sendByteToRegister(char byte,char reg)
00631 {
00632     char data[2];
00633     data[0] = reg;
00634     data[1] = byte;
00635     // send the register address, followed by the data
00636     int nack = i2c->write(BMP180_W_ADDRESS,data,2);
00637     if (nack)
00638         error();  // if we don't receive acknowledgement, flash error message
00639 
00640 }
00641 
00642 void BMP180::error()
00643 {
00644     while(1) {
00645         leds->write(15);
00646         wait(0.1);
00647         leds->write(0);
00648         wait(0.1);
00649     }
00650 }
00651 
00652 
00653 
00654  
00655 using namespace mbed;
00656  // constructor
00657  /** Create a Beep object connected to the specified PwmOut pin
00658   *
00659   * @param pin PwmOut pin to connect to 
00660   */
00661     
00662 Beep::Beep(PinName pin) : _pwm(pin) {
00663     _pwm.write(0.0);     // after creating it have to be off
00664 }
00665 
00666  /** stop the beep instantaneous 
00667   * usually not used 
00668   */
00669 void Beep::nobeep() {
00670     _pwm.write(0.0);
00671 }
00672 
00673 /** Beep with given frequency and duration.
00674  *
00675  * @param frequency - the frequency of the tone in Hz
00676  * @param time - the duration of the tone in seconds
00677  */
00678      
00679 void Beep::beep(float freq, float time) {
00680 
00681     _pwm.period(1.0/freq);
00682     _pwm.write(0.5);            // 50% duty cycle - beep on
00683     toff.attach(this,&Beep::nobeep, time);   // time to off
00684 }
00685 
00686 
00687 
00688 
00689 
00690 
00691 
00692 
00693 
00694 BusOut leds(LED4,LED3,LED2,LED1);
00695 
00696 N5110 lcd(p7,p8,p9,p10,p11,p13,p26);
00697 
00698 Beep buzzer(p21);
00699 
00700 BMP180 bmp180(p28,p27);
00701 
00702 InterruptIn button(p16);
00703 
00704 DigitalOut led(p24);
00705 
00706 
00707 #define centigrade 0
00708 #define kelvin 1
00709 #define fahrenheit 2
00710 //define k and state.
00711 int k = 0;
00712 int state = 0;
00713 
00714 int buttonFlag = 0;
00715 
00716 //this funtion is used to judge the action the project will operate.
00717 void buttonPressed(){
00718    
00719    k++;
00720    state = k%3;
00721    
00722 }
00723     
00724 
00725 
00726 
00727 int main(){
00728     
00729     
00730     lcd.init();//initialize nokia 5110 lcd.
00731     bmp180.init();//initialize sensor.
00732     
00733     
00734     lcd.printString("Temperature",1,1);//display a word "temperature" at the begining
00735     wait(1.0);
00736     lcd.clear();
00737     buzzer.beep(1000,1.0);//make buzzer beep at the begining for 1 second.
00738     led = 1;//led is on.
00739     Measurement measurement;
00740     button.rise(&buttonPressed);//when button is pressed, the unit of temperature will be changed.
00741     
00742     
00743         
00744     while(1){
00745         measurement = bmp180.readValues();
00746         
00747         
00748         char t[14];
00749         int length = sprintf(t,"T = %.2f C",measurement.temperature);
00750         
00751         //then unit of temperature will change into kelvin.
00752         char kel[14];
00753         float k = measurement.temperature+273;
00754         length = sprintf(kel,"K = %.2f K",k);
00755         //the unit of temperature will change into fahrenheit
00756         char fah[14];
00757         float f = (measurement.temperature*1.8)+32;
00758         length = sprintf(fah,"F = %.2f F",f);
00759 
00760         
00761         char p[14];
00762         length = sprintf(p,"P = %.2f mb",measurement.pressure);
00763         
00764         
00765         wait(1.0);
00766         lcd.clear();
00767         //when the temperature is higher than 30 centigrade, the buzzed will beep.
00768         if (measurement.temperature> 30)
00769         {
00770             buzzer.beep(2000,1.0);   
00771         }
00772         //judge the state.
00773         switch(state)
00774         {   
00775             case centigrade://when the project judges it displays centigrade, what action the project will operate.
00776                 state = 0;
00777                 if (length <= 14);
00778                 lcd.printString(t,0,1);
00779                 lcd.printString(p,0,3);
00780                 break;
00781             case kelvin://when the project judges it displays kelvin, what action the project will operate.
00782                 state = 1;
00783                 if (length <= 14);
00784                 lcd.printString(kel,0,1);
00785                 lcd.printString(p,0,3);
00786                 break;
00787             case fahrenheit://when the project judges it displays fahrenheit, what action the project will operate.
00788                 state = 2;
00789                 if (length <= 14);
00790                 lcd.printString(fah,0,1);
00791                 lcd.printString(p,0,3);
00792                 break;
00793             default:
00794                 break;
00795         }
00796         
00797     }
00798     
00799 }
00800 
00801 
00802 
00803