Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of N5110 by
Revision 20:4f843aa9cea1, committed 2015-05-09
- Comitter:
- XuTianli
- Date:
- Sat May 09 20:23:19 2015 +0000
- Parent:
- 19:ba8addc061ea
- Commit message:
- ELEC 2645 Embedded System Project
Changed in this revision
N5110.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/N5110.cpp Thu Apr 23 18:57:52 2015 +0000 +++ b/N5110.cpp Sat May 09 20:23:19 2015 +0000 @@ -6,7 +6,7 @@ */ #include "mbed.h" #include "N5110.h" - +#include "MMA8452.h" N5110::N5110(PinName pwrPin, PinName scePin, PinName rstPin, PinName dcPin, PinName mosiPin, PinName sclkPin, PinName ledPin) { @@ -404,3 +404,205 @@ } } + + +MMA8452:: MMA8452(PinName sdaPin, PinName sclPin) +{ + i2c = new I2C(sdaPin,sclPin); // create new I2C instance and initialise + i2c->frequency(400000); // I2C Fast Mode - 400kHz + leds = new BusOut(LED4,LED3,LED2,LED1); // for debug +} + +void MMA8452::init() +{ + + i2c->frequency(400000); // set Fast Mode I2C frequency (5.10 datasheet) + + char data = readByteFromRegister(WHO_AM_I); // p18 datasheet + if (data != 0x2A) { // if correct ID not found, hand and flash error message + error(); + } + + // put into STANDBY while configuring + data = readByteFromRegister(CTRL_REG1); // get current value of register + data &= ~(1<<0); // clear bit 0 (p37 datasheet) + sendByteToRegister(data,CTRL_REG1); + + // Set output data rate, default is 800 Hz, will set to 100 Hz (clear b5, set b4/b3 - p37 datasheet) + data = readByteFromRegister(CTRL_REG1); + data &= ~(1<<5); + data |= (1<<4); + data |= (1<<3); + sendByteToRegister(data,CTRL_REG1); + + //// Can also change default 2g range to 4g or 8g (p22 datasheet) + data = readByteFromRegister(XYZ_DATA_CFG); + data |= (1<<0); // set bit 0 - 4g range + sendByteToRegister(data,XYZ_DATA_CFG); + + // set ACTIVE + data = readByteFromRegister(CTRL_REG1); + data |= (1<<0); // set bit 0 in CTRL_REG1 + sendByteToRegister(data,CTRL_REG1); + +} + +// read acceleration data from device +Acceleration MMA8452::readValues() +{ + // acceleration data stored in 6 registers (0x01 to 0x06) + // device automatically increments register, so can read 6 bytes starting from OUT_X_MSB + char data[6]; + readBytesFromRegister(OUT_X_MSB,6,data); + + char x_MSB = data[0]; // extract MSB and LSBs for x,y,z values + char x_LSB = data[1]; + char y_MSB = data[2]; + char y_LSB = data[3]; + char z_MSB = data[4]; + char z_LSB = data[5]; + + // [0:7] of MSB are 8 MSB of 12-bit value , [7:4] of LSB are 4 LSB's of 12-bit value + // need to type-cast as numbers are in signed (2's complement) form (p20 datasheet) + int x = (int16_t) (x_MSB << 8) | x_LSB; // combine bytes + x >>= 4; // are left-aligned, so shift 4 places right to right-align + int y = (int16_t) (y_MSB << 8) | y_LSB; + y >>= 4; + int z = (int16_t) (z_MSB << 8) | z_LSB; + z >>= 4; + + // sensitivity is 1024 counts/g in 2g mode (pg 9 datasheet) + // " " 512 " 4g " + // " " 256 " 8g " + Acceleration acc; + + acc.x = x/512.0; + acc.y = y/512.0; + acc.z = z/512.0; + + return acc; +} + +// reads a byte from a specific register +char MMA8452::readByteFromRegister(char reg) +{ + int nack = i2c->write(MMA8452_W_ADDRESS,®,1,true); // send the register address to the slave + // true as need to send repeated start condition (5.10.1 datasheet) + // http://www.i2c-bus.org/repeated-start-condition/ + if (nack) + error(); // if we don't receive acknowledgement, flash error message + + char rx; + nack = i2c->read(MMA8452_R_ADDRESS,&rx,1); // read a byte from the register and store in buffer + if (nack) + error(); // if we don't receive acknowledgement, flash error message + + return rx; +} + +// reads a series of bytes, starting from a specific register +void MMA8452::readBytesFromRegister(char reg,int numberOfBytes,char bytes[]) +{ + + int nack = i2c->write(MMA8452_W_ADDRESS,®,1,true); // send the slave write address and the configuration register address + // true as need to send repeated start condition (5.10.1 datasheet) + // http://www.i2c-bus.org/repeated-start-condition/ + + if (nack) + error(); // if we don't receive acknowledgement, flash error message + + nack = i2c->read(MMA8452_R_ADDRESS,bytes,numberOfBytes); // read bytes + if (nack) + error(); // if we don't receive acknowledgement, flash error message + +} + +// sends a byte to a specific register +void MMA8452::sendByteToRegister(char byte,char reg) +{ + char data[2]; + data[0] = reg; + data[1] = byte; + // send the register address, followed by the data + int nack = i2c->write(MMA8452_W_ADDRESS,data,2); + if (nack) + error(); // if we don't receive acknowledgement, flash error message + +} + +void MMA8452::error() +{ + while(1) { + leds->write(15); + wait(0.1); + leds->write(0); + wait(0.1); + } +} + + + +N5110 lcd(p7,p8,p9,p10,p11,p13,p26); +MMA8452 mma8452(p28,p27); +Serial serial(USBTX,USBRX); +PwmOut buzzer(p21); +DigitalOut led(p24); +float frequency = 500; + +int main() { + Acceleration acceleration; + mma8452.init(); + lcd.init(); + + while(1) + { + lcd.printString("Accelerometer",1,0); + acceleration = mma8452.readValues();//read value of acceleration + char buffer[14]; + int length = sprintf(buffer,"X = %.2f g",acceleration.x); // print formatted data to buffer + char buffer2[14]; + length = sprintf(buffer2,"Y = %.2f g",acceleration.y); + char buffer3[14]; + length = sprintf(buffer3,"Z = %.2f g",acceleration.z); + char buffer4[14]; + length = sprintf(buffer4,"X=%.0fdegree",acceleration.x*90); + char buffer5[14]; + length = sprintf(buffer5,"Y=%.0fdegree",acceleration.y*90); + + if (length <= 14) // if string will fit on display + lcd.printString(buffer,3,1); + lcd.printString(buffer2,3,2); + lcd.printString(buffer3,3,3); + lcd.printString(buffer4,3,4); + lcd.printString(buffer5,3,5); + + wait(2.0); + lcd.clear(); + if (acceleration.x>=0.8||acceleration.y>=0.8) + { + buzzer.period(1/frequency); + buzzer=0.5; + + } + else + { + buzzer.period(1/frequency); + buzzer=0; + } + if (acceleration.x<=0.09&&acceleration.x>=-0.09) + { + led = 1; + } + else + { + led = 0; + } + + + + } + } + + + + \ No newline at end of file