Project C accelerometer
Fork of N5110 by
N5110.cpp@20:4f843aa9cea1, 2015-05-09 (annotated)
- Committer:
- XuTianli
- Date:
- Sat May 09 20:23:19 2015 +0000
- Revision:
- 20:4f843aa9cea1
- Parent:
- 19:ba8addc061ea
ELEC 2645 Embedded System 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 | */ |
eencae | 0:d563e74f0ae9 | 7 | #include "mbed.h" |
eencae | 0:d563e74f0ae9 | 8 | #include "N5110.h" |
XuTianli | 20:4f843aa9cea1 | 9 | #include "MMA8452.h" |
eencae | 2:e93021cfb0a9 | 10 | |
eencae | 1:df68f34cd32d | 11 | N5110::N5110(PinName pwrPin, PinName scePin, PinName rstPin, PinName dcPin, PinName mosiPin, PinName sclkPin, PinName ledPin) |
eencae | 0:d563e74f0ae9 | 12 | { |
eencae | 13:908644099648 | 13 | |
eencae | 0:d563e74f0ae9 | 14 | spi = new SPI(mosiPin,NC,sclkPin); // create new SPI instance and initialise |
eencae | 13:908644099648 | 15 | initSPI(); |
eencae | 13:908644099648 | 16 | |
eencae | 6:adb79338d40f | 17 | // set up pins as required |
eencae | 0:d563e74f0ae9 | 18 | led = new PwmOut(ledPin); |
eencae | 0:d563e74f0ae9 | 19 | pwr = new DigitalOut(pwrPin); |
eencae | 0:d563e74f0ae9 | 20 | sce = new DigitalOut(scePin); |
eencae | 0:d563e74f0ae9 | 21 | rst = new DigitalOut(rstPin); |
eencae | 0:d563e74f0ae9 | 22 | dc = new DigitalOut(dcPin); |
eencae | 0:d563e74f0ae9 | 23 | |
eencae | 0:d563e74f0ae9 | 24 | } |
eencae | 0:d563e74f0ae9 | 25 | |
eencae | 6:adb79338d40f | 26 | // initialise function - powers up and sends the initialisation commands |
eencae | 0:d563e74f0ae9 | 27 | void N5110::init() |
eencae | 0:d563e74f0ae9 | 28 | { |
eencae | 6:adb79338d40f | 29 | turnOn(); // power up |
eencae | 6:adb79338d40f | 30 | wait_ms(10); // small delay seems to prevent spurious pixels during mbed reset |
eencae | 6:adb79338d40f | 31 | reset(); // reset LCD - must be done within 100 ms |
eencae | 0:d563e74f0ae9 | 32 | |
eencae | 0:d563e74f0ae9 | 33 | // function set - extended |
eencae | 0:d563e74f0ae9 | 34 | sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE); |
eencae | 6:adb79338d40f | 35 | // Don't completely understand these parameters - they seem to work as they are |
eencae | 6:adb79338d40f | 36 | // Consult the datasheet if you need to change them |
eencae | 1:df68f34cd32d | 37 | sendCommand(CMD_VOP_7V38); // operating voltage - these values are from Chris Yan's Library |
eencae | 0:d563e74f0ae9 | 38 | sendCommand(CMD_TC_TEMP_2); // temperature control |
eencae | 0:d563e74f0ae9 | 39 | sendCommand(CMD_BI_MUX_48); // bias |
eencae | 0:d563e74f0ae9 | 40 | |
eencae | 0:d563e74f0ae9 | 41 | // function set - basic |
eencae | 0:d563e74f0ae9 | 42 | sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE); |
eencae | 1:df68f34cd32d | 43 | normalMode(); // normal video mode by default |
eencae | 0:d563e74f0ae9 | 44 | sendCommand(CMD_DC_NORMAL_MODE); // black on white |
eencae | 0:d563e74f0ae9 | 45 | |
eencae | 0:d563e74f0ae9 | 46 | // RAM is undefined at power-up so clear |
eencae | 0:d563e74f0ae9 | 47 | clearRAM(); |
eencae | 0:d563e74f0ae9 | 48 | |
eencae | 0:d563e74f0ae9 | 49 | } |
eencae | 13:908644099648 | 50 | |
eencae | 13:908644099648 | 51 | // sets normal video mode (black on white) |
eencae | 13:908644099648 | 52 | void N5110::normalMode() |
eencae | 13:908644099648 | 53 | { |
eencae | 13:908644099648 | 54 | sendCommand(CMD_DC_NORMAL_MODE); |
eencae | 13:908644099648 | 55 | |
eencae | 1:df68f34cd32d | 56 | } |
eencae | 1:df68f34cd32d | 57 | |
eencae | 13:908644099648 | 58 | // sets normal video mode (white on black) |
eencae | 13:908644099648 | 59 | void N5110::inverseMode() |
eencae | 13:908644099648 | 60 | { |
eencae | 13:908644099648 | 61 | sendCommand(CMD_DC_INVERT_VIDEO); |
eencae | 1:df68f34cd32d | 62 | } |
eencae | 0:d563e74f0ae9 | 63 | |
eencae | 0:d563e74f0ae9 | 64 | // function to power up the LCD and backlight |
eencae | 0:d563e74f0ae9 | 65 | void N5110::turnOn() |
eencae | 0:d563e74f0ae9 | 66 | { |
eencae | 0:d563e74f0ae9 | 67 | // set brightness of LED - 0.0 to 1.0 - default is 50% |
eencae | 0:d563e74f0ae9 | 68 | setBrightness(0.5); |
eencae | 0:d563e74f0ae9 | 69 | pwr->write(1); // apply power |
eencae | 0:d563e74f0ae9 | 70 | } |
eencae | 0:d563e74f0ae9 | 71 | |
eencae | 0:d563e74f0ae9 | 72 | // function to power down LCD |
eencae | 0:d563e74f0ae9 | 73 | void N5110::turnOff() |
eencae | 0:d563e74f0ae9 | 74 | { |
eencae | 0:d563e74f0ae9 | 75 | setBrightness(0.0); // turn backlight off |
eencae | 0:d563e74f0ae9 | 76 | clearRAM(); // clear RAM to ensure specified current consumption |
eencae | 10:6f3abb40202b | 77 | // send command to ensure we are in basic mode |
eencae | 0:d563e74f0ae9 | 78 | sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE); |
eencae | 6:adb79338d40f | 79 | // clear the display |
eencae | 0:d563e74f0ae9 | 80 | sendCommand(CMD_DC_CLEAR_DISPLAY); |
eencae | 6:adb79338d40f | 81 | // enter the extended mode and power down |
eencae | 0:d563e74f0ae9 | 82 | sendCommand(0x20 | CMD_FS_POWER_DOWN_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE); |
eencae | 6:adb79338d40f | 83 | // small delay and then turn off the power pin |
eencae | 6:adb79338d40f | 84 | wait_ms(10); |
eencae | 0:d563e74f0ae9 | 85 | pwr->write(0); |
eencae | 0:d563e74f0ae9 | 86 | |
eencae | 0:d563e74f0ae9 | 87 | } |
eencae | 0:d563e74f0ae9 | 88 | |
eencae | 0:d563e74f0ae9 | 89 | // function to change LED backlight brightness |
eencae | 0:d563e74f0ae9 | 90 | void N5110::setBrightness(float brightness) |
eencae | 0:d563e74f0ae9 | 91 | { |
eencae | 0:d563e74f0ae9 | 92 | // check whether brightness is within range |
eencae | 0:d563e74f0ae9 | 93 | if (brightness < 0.0) |
eencae | 0:d563e74f0ae9 | 94 | brightness = 0.0; |
eencae | 0:d563e74f0ae9 | 95 | if (brightness > 1.0) |
eencae | 0:d563e74f0ae9 | 96 | brightness = 1.0; |
eencae | 0:d563e74f0ae9 | 97 | // set PWM duty cycle |
eencae | 0:d563e74f0ae9 | 98 | led->write(brightness); |
eencae | 0:d563e74f0ae9 | 99 | } |
eencae | 0:d563e74f0ae9 | 100 | |
eencae | 0:d563e74f0ae9 | 101 | |
eencae | 0:d563e74f0ae9 | 102 | // pulse the active low reset line |
eencae | 0:d563e74f0ae9 | 103 | void N5110::reset() |
eencae | 0:d563e74f0ae9 | 104 | { |
eencae | 0:d563e74f0ae9 | 105 | rst->write(0); // reset the LCD |
eencae | 0:d563e74f0ae9 | 106 | rst->write(1); |
eencae | 0:d563e74f0ae9 | 107 | } |
eencae | 0:d563e74f0ae9 | 108 | |
eencae | 0:d563e74f0ae9 | 109 | // function to initialise SPI peripheral |
eencae | 0:d563e74f0ae9 | 110 | void N5110::initSPI() |
eencae | 0:d563e74f0ae9 | 111 | { |
eencae | 0:d563e74f0ae9 | 112 | 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 | 113 | spi->frequency(4000000); // maximum of screen is 4 MHz |
eencae | 0:d563e74f0ae9 | 114 | } |
eencae | 0:d563e74f0ae9 | 115 | |
eencae | 6:adb79338d40f | 116 | // send a command to the display |
eencae | 0:d563e74f0ae9 | 117 | void N5110::sendCommand(unsigned char command) |
eencae | 0:d563e74f0ae9 | 118 | { |
eencae | 0:d563e74f0ae9 | 119 | dc->write(0); // set DC low for command |
eencae | 0:d563e74f0ae9 | 120 | sce->write(0); // set CE low to begin frame |
eencae | 0:d563e74f0ae9 | 121 | spi->write(command); // send command |
eencae | 0:d563e74f0ae9 | 122 | dc->write(1); // turn back to data by default |
eencae | 0:d563e74f0ae9 | 123 | sce->write(1); // set CE high to end frame (expected for transmission of single byte) |
eencae | 0:d563e74f0ae9 | 124 | |
eencae | 0:d563e74f0ae9 | 125 | } |
eencae | 0:d563e74f0ae9 | 126 | |
eencae | 6:adb79338d40f | 127 | // send data to the display at the current XY address |
eencae | 6:adb79338d40f | 128 | // dc is set to 1 (i.e. data) after sending a command and so should |
eencae | 6:adb79338d40f | 129 | // be the default mode. |
eencae | 0:d563e74f0ae9 | 130 | void N5110::sendData(unsigned char data) |
eencae | 0:d563e74f0ae9 | 131 | { |
eencae | 0:d563e74f0ae9 | 132 | sce->write(0); // set CE low to begin frame |
eencae | 0:d563e74f0ae9 | 133 | spi->write(data); |
eencae | 0:d563e74f0ae9 | 134 | sce->write(1); // set CE high to end frame (expected for transmission of single byte) |
eencae | 0:d563e74f0ae9 | 135 | } |
eencae | 0:d563e74f0ae9 | 136 | |
eencae | 0:d563e74f0ae9 | 137 | // this function writes 0 to the 504 bytes to clear the RAM |
eencae | 0:d563e74f0ae9 | 138 | void N5110::clearRAM() |
eencae | 0:d563e74f0ae9 | 139 | { |
eencae | 0:d563e74f0ae9 | 140 | int i; |
eencae | 0:d563e74f0ae9 | 141 | sce->write(0); //set CE low to begin frame |
eencae | 17:780a542d5f8b | 142 | for(i = 0; i < WIDTH * HEIGHT; i++) { // 48 x 84 bits = 504 bytes |
eencae | 0:d563e74f0ae9 | 143 | spi->write(0x00); // send 0's |
eencae | 0:d563e74f0ae9 | 144 | } |
eencae | 0:d563e74f0ae9 | 145 | sce->write(1); // set CE high to end frame |
eencae | 0:d563e74f0ae9 | 146 | |
eencae | 0:d563e74f0ae9 | 147 | } |
eencae | 0:d563e74f0ae9 | 148 | |
eencae | 13:908644099648 | 149 | // function to set the XY address in RAM for subsequenct data write |
eencae | 0:d563e74f0ae9 | 150 | void N5110::setXYAddress(int x, int y) |
eencae | 0:d563e74f0ae9 | 151 | { |
eencae | 17:780a542d5f8b | 152 | if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) { // check within range |
eencae | 17:780a542d5f8b | 153 | sendCommand(0x80 | x); // send addresses to display with relevant mask |
eencae | 17:780a542d5f8b | 154 | sendCommand(0x40 | y); |
eencae | 17:780a542d5f8b | 155 | } |
eencae | 0:d563e74f0ae9 | 156 | } |
eencae | 0:d563e74f0ae9 | 157 | |
eencae | 6:adb79338d40f | 158 | // These functions are used to set, clear and get the value of pixels in the display |
eencae | 6:adb79338d40f | 159 | // Pixels are addressed in the range of 0 to 47 (y) and 0 to 83 (x). The refresh() |
eencae | 6:adb79338d40f | 160 | // function must be called after set and clear in order to update the display |
eencae | 0:d563e74f0ae9 | 161 | void N5110::setPixel(int x, int y) |
eencae | 0:d563e74f0ae9 | 162 | { |
eencae | 17:780a542d5f8b | 163 | if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) { // check within range |
eencae | 17:780a542d5f8b | 164 | // calculate bank and shift 1 to required position in the data byte |
eencae | 17:780a542d5f8b | 165 | buffer[x][y/8] |= (1 << y%8); |
eencae | 17:780a542d5f8b | 166 | } |
eencae | 0:d563e74f0ae9 | 167 | } |
eencae | 0:d563e74f0ae9 | 168 | |
eencae | 0:d563e74f0ae9 | 169 | void N5110::clearPixel(int x, int y) |
eencae | 0:d563e74f0ae9 | 170 | { |
eencae | 17:780a542d5f8b | 171 | if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) { // check within range |
eencae | 17:780a542d5f8b | 172 | // calculate bank and shift 1 to required position (using bit clear) |
eencae | 17:780a542d5f8b | 173 | buffer[x][y/8] &= ~(1 << y%8); |
eencae | 17:780a542d5f8b | 174 | } |
eencae | 0:d563e74f0ae9 | 175 | } |
eencae | 0:d563e74f0ae9 | 176 | |
eencae | 7:3010f24e0a81 | 177 | int N5110::getPixel(int x, int y) |
eencae | 0:d563e74f0ae9 | 178 | { |
eencae | 17:780a542d5f8b | 179 | if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) { // check within range |
eencae | 17:780a542d5f8b | 180 | // return relevant bank and mask required bit |
eencae | 17:780a542d5f8b | 181 | return (int) buffer[x][y/8] & (1 << y%8); |
eencae | 19:ba8addc061ea | 182 | // note this does not necessarily return 1 - a non-zero number represents a pixel |
eencae | 17:780a542d5f8b | 183 | } else { |
eencae | 17:780a542d5f8b | 184 | return 0; |
eencae | 17:780a542d5f8b | 185 | } |
eencae | 0:d563e74f0ae9 | 186 | } |
eencae | 0:d563e74f0ae9 | 187 | |
eencae | 6:adb79338d40f | 188 | // function to refresh the display |
eencae | 6:adb79338d40f | 189 | void N5110::refresh() |
eencae | 0:d563e74f0ae9 | 190 | { |
eencae | 0:d563e74f0ae9 | 191 | int i,j; |
eencae | 13:908644099648 | 192 | |
eencae | 7:3010f24e0a81 | 193 | setXYAddress(0,0); // important to set address back to 0,0 before refreshing display |
eencae | 7:3010f24e0a81 | 194 | // address auto increments after printing string, so buffer[0][0] will not coincide |
eencae | 7:3010f24e0a81 | 195 | // with top-left pixel after priting string |
eencae | 13:908644099648 | 196 | |
eencae | 0:d563e74f0ae9 | 197 | sce->write(0); //set CE low to begin frame |
eencae | 0:d563e74f0ae9 | 198 | |
eencae | 17:780a542d5f8b | 199 | for(j = 0; j < BANKS; j++) { // be careful to use correct order (j,i) for horizontal addressing |
eencae | 17:780a542d5f8b | 200 | for(i = 0; i < WIDTH; i++) { |
eencae | 0:d563e74f0ae9 | 201 | spi->write(buffer[i][j]); // send buffer |
eencae | 0:d563e74f0ae9 | 202 | } |
eencae | 0:d563e74f0ae9 | 203 | } |
eencae | 0:d563e74f0ae9 | 204 | sce->write(1); // set CE high to end frame |
eencae | 0:d563e74f0ae9 | 205 | |
eencae | 0:d563e74f0ae9 | 206 | } |
eencae | 0:d563e74f0ae9 | 207 | |
eencae | 6:adb79338d40f | 208 | // fills the buffer with random bytes. Can be used to test the display. |
eencae | 6:adb79338d40f | 209 | // The rand() function isn't seeded so it probably creates the same pattern everytime |
eencae | 0:d563e74f0ae9 | 210 | void N5110::randomiseBuffer() |
eencae | 0:d563e74f0ae9 | 211 | { |
eencae | 0:d563e74f0ae9 | 212 | int i,j; |
eencae | 17:780a542d5f8b | 213 | for(j = 0; j < BANKS; j++) { // be careful to use correct order (j,i) for horizontal addressing |
eencae | 17:780a542d5f8b | 214 | for(i = 0; i < WIDTH; i++) { |
eencae | 0:d563e74f0ae9 | 215 | buffer[i][j] = rand()%256; // generate random byte |
eencae | 0:d563e74f0ae9 | 216 | } |
eencae | 0:d563e74f0ae9 | 217 | } |
eencae | 0:d563e74f0ae9 | 218 | |
eencae | 0:d563e74f0ae9 | 219 | } |
eencae | 0:d563e74f0ae9 | 220 | |
eencae | 0:d563e74f0ae9 | 221 | // function to print 5x7 font |
eencae | 13:908644099648 | 222 | void N5110::printChar(char c,int x,int y) |
eencae | 0:d563e74f0ae9 | 223 | { |
eencae | 19:ba8addc061ea | 224 | if (y>=0 && y<BANKS) { // check if printing in range of y banks |
eencae | 18:1af393359298 | 225 | |
eencae | 18:1af393359298 | 226 | for (int i = 0; i < 5 ; i++ ) { |
eencae | 18:1af393359298 | 227 | int pixel_x = x+i; |
eencae | 19:ba8addc061ea | 228 | if (pixel_x > WIDTH-1) // ensure pixel isn't outside the buffer size (0 - 83) |
eencae | 18:1af393359298 | 229 | break; |
eencae | 18:1af393359298 | 230 | buffer[pixel_x][y] = font5x7[(c - 32)*5 + i]; |
eencae | 18:1af393359298 | 231 | // array is offset by 32 relative to ASCII, each character is 5 pixels wide |
eencae | 18:1af393359298 | 232 | } |
eencae | 18:1af393359298 | 233 | |
eencae | 18:1af393359298 | 234 | refresh(); // this sends the buffer to the display and sets address (cursor) back to 0,0 |
eencae | 0:d563e74f0ae9 | 235 | } |
eencae | 0:d563e74f0ae9 | 236 | } |
eencae | 0:d563e74f0ae9 | 237 | |
eencae | 0:d563e74f0ae9 | 238 | // function to print string at specified position |
eencae | 0:d563e74f0ae9 | 239 | void N5110::printString(const char * str,int x,int y) |
eencae | 0:d563e74f0ae9 | 240 | { |
eencae | 19:ba8addc061ea | 241 | if (y>=0 && y<BANKS) { // check if printing in range of y banks |
eencae | 18:1af393359298 | 242 | |
eencae | 18:1af393359298 | 243 | int n = 0 ; // counter for number of characters in string |
eencae | 18:1af393359298 | 244 | // loop through string and print character |
eencae | 18:1af393359298 | 245 | while(*str) { |
eencae | 0:d563e74f0ae9 | 246 | |
eencae | 18:1af393359298 | 247 | // writes the character bitmap data to the buffer, so that |
eencae | 18:1af393359298 | 248 | // text and pixels can be displayed at the same time |
eencae | 18:1af393359298 | 249 | for (int i = 0; i < 5 ; i++ ) { |
eencae | 18:1af393359298 | 250 | int pixel_x = x+i+n*6; |
eencae | 19:ba8addc061ea | 251 | if (pixel_x > WIDTH-1) // ensure pixel isn't outside the buffer size (0 - 83) |
eencae | 18:1af393359298 | 252 | break; |
eencae | 18:1af393359298 | 253 | buffer[pixel_x][y] = font5x7[(*str - 32)*5 + i]; |
eencae | 18:1af393359298 | 254 | } |
eencae | 18:1af393359298 | 255 | |
eencae | 18:1af393359298 | 256 | str++; // go to next character in string |
eencae | 18:1af393359298 | 257 | |
eencae | 18:1af393359298 | 258 | n++; // increment index |
eencae | 18:1af393359298 | 259 | |
eencae | 9:7701f0126ba7 | 260 | } |
eencae | 13:908644099648 | 261 | |
eencae | 18:1af393359298 | 262 | refresh(); // this sends the buffer to the display and sets address (cursor) back to 0,0 |
eencae | 0:d563e74f0ae9 | 263 | } |
eencae | 0:d563e74f0ae9 | 264 | } |
eencae | 0:d563e74f0ae9 | 265 | |
eencae | 6:adb79338d40f | 266 | // function to clear the screen |
eencae | 0:d563e74f0ae9 | 267 | void N5110::clear() |
eencae | 0:d563e74f0ae9 | 268 | { |
eencae | 6:adb79338d40f | 269 | clearBuffer(); // clear the buffer then call the refresh function |
eencae | 6:adb79338d40f | 270 | refresh(); |
eencae | 0:d563e74f0ae9 | 271 | } |
eencae | 0:d563e74f0ae9 | 272 | |
eencae | 6:adb79338d40f | 273 | // function to clear the buffer |
eencae | 0:d563e74f0ae9 | 274 | void N5110::clearBuffer() |
eencae | 0:d563e74f0ae9 | 275 | { |
eencae | 0:d563e74f0ae9 | 276 | int i,j; |
eencae | 17:780a542d5f8b | 277 | for (i=0; i<WIDTH; i++) { // loop through the banks and set the buffer to 0 |
eencae | 17:780a542d5f8b | 278 | for (j=0; j<BANKS; j++) { |
eencae | 0:d563e74f0ae9 | 279 | buffer[i][j]=0; |
eencae | 0:d563e74f0ae9 | 280 | } |
eencae | 0:d563e74f0ae9 | 281 | } |
eencae | 8:40abe5736eca | 282 | } |
eencae | 8:40abe5736eca | 283 | |
eencae | 8:40abe5736eca | 284 | // function to plot array on display |
eencae | 13:908644099648 | 285 | void N5110::plotArray(float array[]) |
eencae | 13:908644099648 | 286 | { |
eencae | 13:908644099648 | 287 | |
eencae | 8:40abe5736eca | 288 | int i; |
eencae | 13:908644099648 | 289 | |
eencae | 17:780a542d5f8b | 290 | for (i=0; i<WIDTH; i++) { // loop through array |
eencae | 8:40abe5736eca | 291 | // elements are normalised from 0.0 to 1.0, so multiply |
eencae | 8:40abe5736eca | 292 | // by 47 to convert to pixel range, and subtract from 47 |
eencae | 8:40abe5736eca | 293 | // since top-left is 0,0 in the display geometry |
eencae | 9:7701f0126ba7 | 294 | setPixel(i,47 - int(array[i]*47.0)); |
eencae | 13:908644099648 | 295 | } |
eencae | 13:908644099648 | 296 | |
eencae | 8:40abe5736eca | 297 | refresh(); |
eencae | 13:908644099648 | 298 | |
eencae | 17:780a542d5f8b | 299 | } |
eencae | 13:908644099648 | 300 | |
eencae | 17:780a542d5f8b | 301 | // function to draw circle |
eencae | 17:780a542d5f8b | 302 | void N5110:: drawCircle(int x0,int y0,int radius,int fill) |
eencae | 17:780a542d5f8b | 303 | { |
eencae | 17:780a542d5f8b | 304 | // from http://en.wikipedia.org/wiki/Midpoint_circle_algorithm |
eencae | 17:780a542d5f8b | 305 | int x = radius; |
eencae | 17:780a542d5f8b | 306 | int y = 0; |
eencae | 17:780a542d5f8b | 307 | int radiusError = 1-x; |
eencae | 17:780a542d5f8b | 308 | |
eencae | 17:780a542d5f8b | 309 | while(x >= y) { |
eencae | 17:780a542d5f8b | 310 | |
eencae | 17:780a542d5f8b | 311 | // if transparent, just draw outline |
eencae | 17:780a542d5f8b | 312 | if (fill == 0) { |
eencae | 17:780a542d5f8b | 313 | setPixel( x + x0, y + y0); |
eencae | 17:780a542d5f8b | 314 | setPixel(-x + x0, y + y0); |
eencae | 17:780a542d5f8b | 315 | setPixel( y + x0, x + y0); |
eencae | 17:780a542d5f8b | 316 | setPixel(-y + x0, x + y0); |
eencae | 17:780a542d5f8b | 317 | setPixel(-y + x0, -x + y0); |
eencae | 17:780a542d5f8b | 318 | setPixel( y + x0, -x + y0); |
eencae | 17:780a542d5f8b | 319 | setPixel( x + x0, -y + y0); |
eencae | 17:780a542d5f8b | 320 | setPixel(-x + x0, -y + y0); |
eencae | 17:780a542d5f8b | 321 | } else { // drawing filled circle, so draw lines between points at same y value |
eencae | 17:780a542d5f8b | 322 | |
eencae | 17:780a542d5f8b | 323 | int type = (fill==1) ? 1:0; // black or white fill |
eencae | 17:780a542d5f8b | 324 | |
eencae | 17:780a542d5f8b | 325 | drawLine(x+x0,y+y0,-x+x0,y+y0,type); |
eencae | 17:780a542d5f8b | 326 | drawLine(y+x0,x+y0,-y+x0,x+y0,type); |
eencae | 17:780a542d5f8b | 327 | drawLine(y+x0,-x+y0,-y+x0,-x+y0,type); |
eencae | 17:780a542d5f8b | 328 | drawLine(x+x0,-y+y0,-x+x0,-y+y0,type); |
eencae | 17:780a542d5f8b | 329 | } |
eencae | 17:780a542d5f8b | 330 | |
eencae | 17:780a542d5f8b | 331 | |
eencae | 17:780a542d5f8b | 332 | y++; |
eencae | 17:780a542d5f8b | 333 | if (radiusError<0) { |
eencae | 17:780a542d5f8b | 334 | radiusError += 2 * y + 1; |
eencae | 17:780a542d5f8b | 335 | } else { |
eencae | 17:780a542d5f8b | 336 | x--; |
eencae | 17:780a542d5f8b | 337 | radiusError += 2 * (y - x) + 1; |
eencae | 17:780a542d5f8b | 338 | } |
eencae | 17:780a542d5f8b | 339 | } |
eencae | 17:780a542d5f8b | 340 | |
eencae | 17:780a542d5f8b | 341 | } |
eencae | 17:780a542d5f8b | 342 | |
eencae | 17:780a542d5f8b | 343 | void N5110::drawLine(int x0,int y0,int x1,int y1,int type) |
eencae | 17:780a542d5f8b | 344 | { |
eencae | 17:780a542d5f8b | 345 | int y_range = y1-y0; // calc range of y and x |
eencae | 17:780a542d5f8b | 346 | int x_range = x1-x0; |
eencae | 17:780a542d5f8b | 347 | int start,stop,step; |
eencae | 17:780a542d5f8b | 348 | |
eencae | 17:780a542d5f8b | 349 | // if dotted line, set step to 2, else step is 1 |
eencae | 17:780a542d5f8b | 350 | step = (type==2) ? 2:1; |
eencae | 17:780a542d5f8b | 351 | |
eencae | 17:780a542d5f8b | 352 | // make sure we loop over the largest range to get the most pixels on the display |
eencae | 17:780a542d5f8b | 353 | // for instance, if drawing a vertical line (x_range = 0), we need to loop down the y pixels |
eencae | 17:780a542d5f8b | 354 | // or else we'll only end up with 1 pixel in the x column |
eencae | 17:780a542d5f8b | 355 | if ( abs(x_range) > abs(y_range) ) { |
eencae | 17:780a542d5f8b | 356 | |
eencae | 17:780a542d5f8b | 357 | // ensure we loop from smallest to largest or else for-loop won't run as expected |
eencae | 17:780a542d5f8b | 358 | start = x1>x0 ? x0:x1; |
eencae | 17:780a542d5f8b | 359 | stop = x1>x0 ? x1:x0; |
eencae | 17:780a542d5f8b | 360 | |
eencae | 17:780a542d5f8b | 361 | // loop between x pixels |
eencae | 17:780a542d5f8b | 362 | for (int x = start; x<= stop ; x+=step) { |
eencae | 17:780a542d5f8b | 363 | // do linear interpolation |
eencae | 17:780a542d5f8b | 364 | int y = y0 + (y1-y0)*(x-x0)/(x1-x0); |
eencae | 17:780a542d5f8b | 365 | |
eencae | 17:780a542d5f8b | 366 | if (type == 0) // if 'white' line, turn off pixel |
eencae | 17:780a542d5f8b | 367 | clearPixel(x,y); |
eencae | 17:780a542d5f8b | 368 | else |
eencae | 17:780a542d5f8b | 369 | setPixel(x,y); // else if 'black' or 'dotted' turn on pixel |
eencae | 17:780a542d5f8b | 370 | } |
eencae | 17:780a542d5f8b | 371 | } else { |
eencae | 17:780a542d5f8b | 372 | |
eencae | 17:780a542d5f8b | 373 | // ensure we loop from smallest to largest or else for-loop won't run as expected |
eencae | 17:780a542d5f8b | 374 | start = y1>y0 ? y0:y1; |
eencae | 17:780a542d5f8b | 375 | stop = y1>y0 ? y1:y0; |
eencae | 17:780a542d5f8b | 376 | |
eencae | 17:780a542d5f8b | 377 | for (int y = start; y<= stop ; y+=step) { |
eencae | 17:780a542d5f8b | 378 | // do linear interpolation |
eencae | 17:780a542d5f8b | 379 | int x = x0 + (x1-x0)*(y-y0)/(y1-y0); |
eencae | 17:780a542d5f8b | 380 | |
eencae | 17:780a542d5f8b | 381 | if (type == 0) // if 'white' line, turn off pixel |
eencae | 17:780a542d5f8b | 382 | clearPixel(x,y); |
eencae | 17:780a542d5f8b | 383 | else |
eencae | 17:780a542d5f8b | 384 | setPixel(x,y); // else if 'black' or 'dotted' turn on pixel |
eencae | 17:780a542d5f8b | 385 | |
eencae | 17:780a542d5f8b | 386 | } |
eencae | 17:780a542d5f8b | 387 | } |
eencae | 17:780a542d5f8b | 388 | |
eencae | 17:780a542d5f8b | 389 | } |
eencae | 17:780a542d5f8b | 390 | |
eencae | 17:780a542d5f8b | 391 | void N5110::drawRect(int x0,int y0,int width,int height,int fill) |
eencae | 17:780a542d5f8b | 392 | { |
eencae | 17:780a542d5f8b | 393 | |
eencae | 17:780a542d5f8b | 394 | if (fill == 0) { // transparent, just outline |
eencae | 17:780a542d5f8b | 395 | drawLine(x0,y0,x0+width,y0,1); // top |
eencae | 17:780a542d5f8b | 396 | drawLine(x0,y0+height,x0+width,y0+height,1); // bottom |
eencae | 17:780a542d5f8b | 397 | drawLine(x0,y0,x0,y0+height,1); // left |
eencae | 17:780a542d5f8b | 398 | drawLine(x0+width,y0,x0+width,y0+height,1); // right |
eencae | 17:780a542d5f8b | 399 | } else { // filled rectangle |
eencae | 17:780a542d5f8b | 400 | int type = (fill==1) ? 1:0; // black or white fill |
eencae | 17:780a542d5f8b | 401 | for (int y = y0; y<= y0+height; y++) { // loop through rows of rectangle |
eencae | 17:780a542d5f8b | 402 | drawLine(x0,y,x0+width,y,type); // draw line across screen |
eencae | 17:780a542d5f8b | 403 | } |
eencae | 17:780a542d5f8b | 404 | } |
eencae | 17:780a542d5f8b | 405 | |
eencae | 17:780a542d5f8b | 406 | } |
XuTianli | 20:4f843aa9cea1 | 407 | |
XuTianli | 20:4f843aa9cea1 | 408 | |
XuTianli | 20:4f843aa9cea1 | 409 | MMA8452:: MMA8452(PinName sdaPin, PinName sclPin) |
XuTianli | 20:4f843aa9cea1 | 410 | { |
XuTianli | 20:4f843aa9cea1 | 411 | i2c = new I2C(sdaPin,sclPin); // create new I2C instance and initialise |
XuTianli | 20:4f843aa9cea1 | 412 | i2c->frequency(400000); // I2C Fast Mode - 400kHz |
XuTianli | 20:4f843aa9cea1 | 413 | leds = new BusOut(LED4,LED3,LED2,LED1); // for debug |
XuTianli | 20:4f843aa9cea1 | 414 | } |
XuTianli | 20:4f843aa9cea1 | 415 | |
XuTianli | 20:4f843aa9cea1 | 416 | void MMA8452::init() |
XuTianli | 20:4f843aa9cea1 | 417 | { |
XuTianli | 20:4f843aa9cea1 | 418 | |
XuTianli | 20:4f843aa9cea1 | 419 | i2c->frequency(400000); // set Fast Mode I2C frequency (5.10 datasheet) |
XuTianli | 20:4f843aa9cea1 | 420 | |
XuTianli | 20:4f843aa9cea1 | 421 | char data = readByteFromRegister(WHO_AM_I); // p18 datasheet |
XuTianli | 20:4f843aa9cea1 | 422 | if (data != 0x2A) { // if correct ID not found, hand and flash error message |
XuTianli | 20:4f843aa9cea1 | 423 | error(); |
XuTianli | 20:4f843aa9cea1 | 424 | } |
XuTianli | 20:4f843aa9cea1 | 425 | |
XuTianli | 20:4f843aa9cea1 | 426 | // put into STANDBY while configuring |
XuTianli | 20:4f843aa9cea1 | 427 | data = readByteFromRegister(CTRL_REG1); // get current value of register |
XuTianli | 20:4f843aa9cea1 | 428 | data &= ~(1<<0); // clear bit 0 (p37 datasheet) |
XuTianli | 20:4f843aa9cea1 | 429 | sendByteToRegister(data,CTRL_REG1); |
XuTianli | 20:4f843aa9cea1 | 430 | |
XuTianli | 20:4f843aa9cea1 | 431 | // Set output data rate, default is 800 Hz, will set to 100 Hz (clear b5, set b4/b3 - p37 datasheet) |
XuTianli | 20:4f843aa9cea1 | 432 | data = readByteFromRegister(CTRL_REG1); |
XuTianli | 20:4f843aa9cea1 | 433 | data &= ~(1<<5); |
XuTianli | 20:4f843aa9cea1 | 434 | data |= (1<<4); |
XuTianli | 20:4f843aa9cea1 | 435 | data |= (1<<3); |
XuTianli | 20:4f843aa9cea1 | 436 | sendByteToRegister(data,CTRL_REG1); |
XuTianli | 20:4f843aa9cea1 | 437 | |
XuTianli | 20:4f843aa9cea1 | 438 | //// Can also change default 2g range to 4g or 8g (p22 datasheet) |
XuTianli | 20:4f843aa9cea1 | 439 | data = readByteFromRegister(XYZ_DATA_CFG); |
XuTianli | 20:4f843aa9cea1 | 440 | data |= (1<<0); // set bit 0 - 4g range |
XuTianli | 20:4f843aa9cea1 | 441 | sendByteToRegister(data,XYZ_DATA_CFG); |
XuTianli | 20:4f843aa9cea1 | 442 | |
XuTianli | 20:4f843aa9cea1 | 443 | // set ACTIVE |
XuTianli | 20:4f843aa9cea1 | 444 | data = readByteFromRegister(CTRL_REG1); |
XuTianli | 20:4f843aa9cea1 | 445 | data |= (1<<0); // set bit 0 in CTRL_REG1 |
XuTianli | 20:4f843aa9cea1 | 446 | sendByteToRegister(data,CTRL_REG1); |
XuTianli | 20:4f843aa9cea1 | 447 | |
XuTianli | 20:4f843aa9cea1 | 448 | } |
XuTianli | 20:4f843aa9cea1 | 449 | |
XuTianli | 20:4f843aa9cea1 | 450 | // read acceleration data from device |
XuTianli | 20:4f843aa9cea1 | 451 | Acceleration MMA8452::readValues() |
XuTianli | 20:4f843aa9cea1 | 452 | { |
XuTianli | 20:4f843aa9cea1 | 453 | // acceleration data stored in 6 registers (0x01 to 0x06) |
XuTianli | 20:4f843aa9cea1 | 454 | // device automatically increments register, so can read 6 bytes starting from OUT_X_MSB |
XuTianli | 20:4f843aa9cea1 | 455 | char data[6]; |
XuTianli | 20:4f843aa9cea1 | 456 | readBytesFromRegister(OUT_X_MSB,6,data); |
XuTianli | 20:4f843aa9cea1 | 457 | |
XuTianli | 20:4f843aa9cea1 | 458 | char x_MSB = data[0]; // extract MSB and LSBs for x,y,z values |
XuTianli | 20:4f843aa9cea1 | 459 | char x_LSB = data[1]; |
XuTianli | 20:4f843aa9cea1 | 460 | char y_MSB = data[2]; |
XuTianli | 20:4f843aa9cea1 | 461 | char y_LSB = data[3]; |
XuTianli | 20:4f843aa9cea1 | 462 | char z_MSB = data[4]; |
XuTianli | 20:4f843aa9cea1 | 463 | char z_LSB = data[5]; |
XuTianli | 20:4f843aa9cea1 | 464 | |
XuTianli | 20:4f843aa9cea1 | 465 | // [0:7] of MSB are 8 MSB of 12-bit value , [7:4] of LSB are 4 LSB's of 12-bit value |
XuTianli | 20:4f843aa9cea1 | 466 | // need to type-cast as numbers are in signed (2's complement) form (p20 datasheet) |
XuTianli | 20:4f843aa9cea1 | 467 | int x = (int16_t) (x_MSB << 8) | x_LSB; // combine bytes |
XuTianli | 20:4f843aa9cea1 | 468 | x >>= 4; // are left-aligned, so shift 4 places right to right-align |
XuTianli | 20:4f843aa9cea1 | 469 | int y = (int16_t) (y_MSB << 8) | y_LSB; |
XuTianli | 20:4f843aa9cea1 | 470 | y >>= 4; |
XuTianli | 20:4f843aa9cea1 | 471 | int z = (int16_t) (z_MSB << 8) | z_LSB; |
XuTianli | 20:4f843aa9cea1 | 472 | z >>= 4; |
XuTianli | 20:4f843aa9cea1 | 473 | |
XuTianli | 20:4f843aa9cea1 | 474 | // sensitivity is 1024 counts/g in 2g mode (pg 9 datasheet) |
XuTianli | 20:4f843aa9cea1 | 475 | // " " 512 " 4g " |
XuTianli | 20:4f843aa9cea1 | 476 | // " " 256 " 8g " |
XuTianli | 20:4f843aa9cea1 | 477 | Acceleration acc; |
XuTianli | 20:4f843aa9cea1 | 478 | |
XuTianli | 20:4f843aa9cea1 | 479 | acc.x = x/512.0; |
XuTianli | 20:4f843aa9cea1 | 480 | acc.y = y/512.0; |
XuTianli | 20:4f843aa9cea1 | 481 | acc.z = z/512.0; |
XuTianli | 20:4f843aa9cea1 | 482 | |
XuTianli | 20:4f843aa9cea1 | 483 | return acc; |
XuTianli | 20:4f843aa9cea1 | 484 | } |
XuTianli | 20:4f843aa9cea1 | 485 | |
XuTianli | 20:4f843aa9cea1 | 486 | // reads a byte from a specific register |
XuTianli | 20:4f843aa9cea1 | 487 | char MMA8452::readByteFromRegister(char reg) |
XuTianli | 20:4f843aa9cea1 | 488 | { |
XuTianli | 20:4f843aa9cea1 | 489 | int nack = i2c->write(MMA8452_W_ADDRESS,®,1,true); // send the register address to the slave |
XuTianli | 20:4f843aa9cea1 | 490 | // true as need to send repeated start condition (5.10.1 datasheet) |
XuTianli | 20:4f843aa9cea1 | 491 | // http://www.i2c-bus.org/repeated-start-condition/ |
XuTianli | 20:4f843aa9cea1 | 492 | if (nack) |
XuTianli | 20:4f843aa9cea1 | 493 | error(); // if we don't receive acknowledgement, flash error message |
XuTianli | 20:4f843aa9cea1 | 494 | |
XuTianli | 20:4f843aa9cea1 | 495 | char rx; |
XuTianli | 20:4f843aa9cea1 | 496 | nack = i2c->read(MMA8452_R_ADDRESS,&rx,1); // read a byte from the register and store in buffer |
XuTianli | 20:4f843aa9cea1 | 497 | if (nack) |
XuTianli | 20:4f843aa9cea1 | 498 | error(); // if we don't receive acknowledgement, flash error message |
XuTianli | 20:4f843aa9cea1 | 499 | |
XuTianli | 20:4f843aa9cea1 | 500 | return rx; |
XuTianli | 20:4f843aa9cea1 | 501 | } |
XuTianli | 20:4f843aa9cea1 | 502 | |
XuTianli | 20:4f843aa9cea1 | 503 | // reads a series of bytes, starting from a specific register |
XuTianli | 20:4f843aa9cea1 | 504 | void MMA8452::readBytesFromRegister(char reg,int numberOfBytes,char bytes[]) |
XuTianli | 20:4f843aa9cea1 | 505 | { |
XuTianli | 20:4f843aa9cea1 | 506 | |
XuTianli | 20:4f843aa9cea1 | 507 | int nack = i2c->write(MMA8452_W_ADDRESS,®,1,true); // send the slave write address and the configuration register address |
XuTianli | 20:4f843aa9cea1 | 508 | // true as need to send repeated start condition (5.10.1 datasheet) |
XuTianli | 20:4f843aa9cea1 | 509 | // http://www.i2c-bus.org/repeated-start-condition/ |
XuTianli | 20:4f843aa9cea1 | 510 | |
XuTianli | 20:4f843aa9cea1 | 511 | if (nack) |
XuTianli | 20:4f843aa9cea1 | 512 | error(); // if we don't receive acknowledgement, flash error message |
XuTianli | 20:4f843aa9cea1 | 513 | |
XuTianli | 20:4f843aa9cea1 | 514 | nack = i2c->read(MMA8452_R_ADDRESS,bytes,numberOfBytes); // read bytes |
XuTianli | 20:4f843aa9cea1 | 515 | if (nack) |
XuTianli | 20:4f843aa9cea1 | 516 | error(); // if we don't receive acknowledgement, flash error message |
XuTianli | 20:4f843aa9cea1 | 517 | |
XuTianli | 20:4f843aa9cea1 | 518 | } |
XuTianli | 20:4f843aa9cea1 | 519 | |
XuTianli | 20:4f843aa9cea1 | 520 | // sends a byte to a specific register |
XuTianli | 20:4f843aa9cea1 | 521 | void MMA8452::sendByteToRegister(char byte,char reg) |
XuTianli | 20:4f843aa9cea1 | 522 | { |
XuTianli | 20:4f843aa9cea1 | 523 | char data[2]; |
XuTianli | 20:4f843aa9cea1 | 524 | data[0] = reg; |
XuTianli | 20:4f843aa9cea1 | 525 | data[1] = byte; |
XuTianli | 20:4f843aa9cea1 | 526 | // send the register address, followed by the data |
XuTianli | 20:4f843aa9cea1 | 527 | int nack = i2c->write(MMA8452_W_ADDRESS,data,2); |
XuTianli | 20:4f843aa9cea1 | 528 | if (nack) |
XuTianli | 20:4f843aa9cea1 | 529 | error(); // if we don't receive acknowledgement, flash error message |
XuTianli | 20:4f843aa9cea1 | 530 | |
XuTianli | 20:4f843aa9cea1 | 531 | } |
XuTianli | 20:4f843aa9cea1 | 532 | |
XuTianli | 20:4f843aa9cea1 | 533 | void MMA8452::error() |
XuTianli | 20:4f843aa9cea1 | 534 | { |
XuTianli | 20:4f843aa9cea1 | 535 | while(1) { |
XuTianli | 20:4f843aa9cea1 | 536 | leds->write(15); |
XuTianli | 20:4f843aa9cea1 | 537 | wait(0.1); |
XuTianli | 20:4f843aa9cea1 | 538 | leds->write(0); |
XuTianli | 20:4f843aa9cea1 | 539 | wait(0.1); |
XuTianli | 20:4f843aa9cea1 | 540 | } |
XuTianli | 20:4f843aa9cea1 | 541 | } |
XuTianli | 20:4f843aa9cea1 | 542 | |
XuTianli | 20:4f843aa9cea1 | 543 | |
XuTianli | 20:4f843aa9cea1 | 544 | |
XuTianli | 20:4f843aa9cea1 | 545 | N5110 lcd(p7,p8,p9,p10,p11,p13,p26); |
XuTianli | 20:4f843aa9cea1 | 546 | MMA8452 mma8452(p28,p27); |
XuTianli | 20:4f843aa9cea1 | 547 | Serial serial(USBTX,USBRX); |
XuTianli | 20:4f843aa9cea1 | 548 | PwmOut buzzer(p21); |
XuTianli | 20:4f843aa9cea1 | 549 | DigitalOut led(p24); |
XuTianli | 20:4f843aa9cea1 | 550 | float frequency = 500; |
XuTianli | 20:4f843aa9cea1 | 551 | |
XuTianli | 20:4f843aa9cea1 | 552 | int main() { |
XuTianli | 20:4f843aa9cea1 | 553 | Acceleration acceleration; |
XuTianli | 20:4f843aa9cea1 | 554 | mma8452.init(); |
XuTianli | 20:4f843aa9cea1 | 555 | lcd.init(); |
XuTianli | 20:4f843aa9cea1 | 556 | |
XuTianli | 20:4f843aa9cea1 | 557 | while(1) |
XuTianli | 20:4f843aa9cea1 | 558 | { |
XuTianli | 20:4f843aa9cea1 | 559 | lcd.printString("Accelerometer",1,0); |
XuTianli | 20:4f843aa9cea1 | 560 | acceleration = mma8452.readValues();//read value of acceleration |
XuTianli | 20:4f843aa9cea1 | 561 | char buffer[14]; |
XuTianli | 20:4f843aa9cea1 | 562 | int length = sprintf(buffer,"X = %.2f g",acceleration.x); // print formatted data to buffer |
XuTianli | 20:4f843aa9cea1 | 563 | char buffer2[14]; |
XuTianli | 20:4f843aa9cea1 | 564 | length = sprintf(buffer2,"Y = %.2f g",acceleration.y); |
XuTianli | 20:4f843aa9cea1 | 565 | char buffer3[14]; |
XuTianli | 20:4f843aa9cea1 | 566 | length = sprintf(buffer3,"Z = %.2f g",acceleration.z); |
XuTianli | 20:4f843aa9cea1 | 567 | char buffer4[14]; |
XuTianli | 20:4f843aa9cea1 | 568 | length = sprintf(buffer4,"X=%.0fdegree",acceleration.x*90); |
XuTianli | 20:4f843aa9cea1 | 569 | char buffer5[14]; |
XuTianli | 20:4f843aa9cea1 | 570 | length = sprintf(buffer5,"Y=%.0fdegree",acceleration.y*90); |
XuTianli | 20:4f843aa9cea1 | 571 | |
XuTianli | 20:4f843aa9cea1 | 572 | if (length <= 14) // if string will fit on display |
XuTianli | 20:4f843aa9cea1 | 573 | lcd.printString(buffer,3,1); |
XuTianli | 20:4f843aa9cea1 | 574 | lcd.printString(buffer2,3,2); |
XuTianli | 20:4f843aa9cea1 | 575 | lcd.printString(buffer3,3,3); |
XuTianli | 20:4f843aa9cea1 | 576 | lcd.printString(buffer4,3,4); |
XuTianli | 20:4f843aa9cea1 | 577 | lcd.printString(buffer5,3,5); |
XuTianli | 20:4f843aa9cea1 | 578 | |
XuTianli | 20:4f843aa9cea1 | 579 | wait(2.0); |
XuTianli | 20:4f843aa9cea1 | 580 | lcd.clear(); |
XuTianli | 20:4f843aa9cea1 | 581 | if (acceleration.x>=0.8||acceleration.y>=0.8) |
XuTianli | 20:4f843aa9cea1 | 582 | { |
XuTianli | 20:4f843aa9cea1 | 583 | buzzer.period(1/frequency); |
XuTianli | 20:4f843aa9cea1 | 584 | buzzer=0.5; |
XuTianli | 20:4f843aa9cea1 | 585 | |
XuTianli | 20:4f843aa9cea1 | 586 | } |
XuTianli | 20:4f843aa9cea1 | 587 | else |
XuTianli | 20:4f843aa9cea1 | 588 | { |
XuTianli | 20:4f843aa9cea1 | 589 | buzzer.period(1/frequency); |
XuTianli | 20:4f843aa9cea1 | 590 | buzzer=0; |
XuTianli | 20:4f843aa9cea1 | 591 | } |
XuTianli | 20:4f843aa9cea1 | 592 | if (acceleration.x<=0.09&&acceleration.x>=-0.09) |
XuTianli | 20:4f843aa9cea1 | 593 | { |
XuTianli | 20:4f843aa9cea1 | 594 | led = 1; |
XuTianli | 20:4f843aa9cea1 | 595 | } |
XuTianli | 20:4f843aa9cea1 | 596 | else |
XuTianli | 20:4f843aa9cea1 | 597 | { |
XuTianli | 20:4f843aa9cea1 | 598 | led = 0; |
XuTianli | 20:4f843aa9cea1 | 599 | } |
XuTianli | 20:4f843aa9cea1 | 600 | |
XuTianli | 20:4f843aa9cea1 | 601 | |
XuTianli | 20:4f843aa9cea1 | 602 | |
XuTianli | 20:4f843aa9cea1 | 603 | } |
XuTianli | 20:4f843aa9cea1 | 604 | } |
XuTianli | 20:4f843aa9cea1 | 605 | |
XuTianli | 20:4f843aa9cea1 | 606 | |
XuTianli | 20:4f843aa9cea1 | 607 | |
XuTianli | 20:4f843aa9cea1 | 608 |