this class is meant to deal with the Freescale MMA7455L accelermoter through an SPI interface. it has limtid functionality, but it can be extended to contain more features
Revision 0:0d4a1401a9c5, committed 2012-02-05
- Comitter:
- hassan_ali
- Date:
- Sun Feb 05 21:03:49 2012 +0000
- Commit message:
- this class is not a prefect or complete class containing all features of the MMA7455L accelermoter, but it can be extended later.
Changed in this revision
diff -r 000000000000 -r 0d4a1401a9c5 MMA7455L_SPI.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MMA7455L_SPI.cpp Sun Feb 05 21:03:49 2012 +0000 @@ -0,0 +1,138 @@ +#include "MMA7455L_SPI.h" + +MMA7455L_SPI :: MMA7455L_SPI (PinName _mosi, PinName _miso, PinName _clk, PinName _cs):_CS(_cs),_spi(_mosi, _miso, _clk) +{ + /*initializing the main variables (or data members) of the accelermoter class*/ + Ax = 0; + Ay = 0; + Az = 0; + sensetivity = 0; + _CS = 1; /* just to disable the connection by default, and if we want to enable it _CS = 0 */ + _spi.format(8,0); + _spi.frequency(8000000); + set_mode(0x01); + set_interrupt_latch_reset_time(); + //set_offsets(char, char, char); + set_control_1(); + set_control_2(); + set_level_detection_thershold_limit_value(); + set_pulse_detection_thershold_limit_value(); + set_pulse_duration_value(); + set_latency_time_value(); +} + +signed char MMA7455L_SPI :: read(char adress) +{ + signed char value; + _CS = 0; + _spi.write(READ | (adress << 1) ); + value = _spi.write(0x00); /*writing a dummy char ito the register while returning the register + content because SPI is an exchange protocol*/ + _CS = 1; + return value; +} + +void MMA7455L_SPI :: write(char adress, char content) +{ + _CS = 0; + _spi.write(WRITE | (adress << 1) ); + _spi.write(content); + _CS = 1; +} + +void MMA7455L_SPI :: set_mode(char reg_mode) /*notice that we operate on measurment mode only*/ +{ + write(MODE_CONTROL, reg_mode); + char g_select_1 = reg_mode & 0x08; + char g_select_0 = reg_mode & 0x04; + if ( (g_select_1 == 0) && (g_select_0 == 0) ) /*means we are operating on 8g range*/ + { + sensetivity = 16; /*the units are "count/g" */ + } + else if ( (g_select_1 == 0) && (g_select_0 == 1) ) /*means we are operating on 2g range*/ + { + sensetivity = 64; + } + else if ( (g_select_1 == 1) && (g_select_0 == 0) ) /*means we are operating on 4g range*/ + { + sensetivity = 32; + } + +} + +void MMA7455L_SPI :: set_range_of_measurment(int GS1, int GS0) +{ + char g_select_1 = GS1 << 3; + char g_select_0 = GS0 << 2; + char reg_mode = g_select_1 | g_select_0; + set_mode(reg_mode); +} + +/* these functions may not be useful now, but may be of use if the class is to be extended*/ + + void MMA7455L_SPI :: set_interrupt_latch_reset_time(char value) + { + write(INTERRUPT_LATCH_RESET, value); + } + + void MMA7455L_SPI :: set_control_1(char value) + { + write(CONTROL_1, value); + } + void MMA7455L_SPI :: set_control_2(char value) + { + write(CONTROL_2, value); + } + + void MMA7455L_SPI :: set_level_detection_thershold_limit_value(char value) + { + write(LEVEL_DETECTION_THRESHOLD_LIMIT,value); + } + + void MMA7455L_SPI :: set_pulse_detection_thershold_limit_value(char value) + { + write(PULSE_DETECTION_THRESHOLD_LIMIT,value); + } + +void MMA7455L_SPI :: set_pulse_duration_value(char value) +{ + write(PULSE_DURATION_VALUE, value); +} + +void MMA7455L_SPI :: set_latency_time_value(char value) +{ + write(LATENCY_TIME_VALUE, value); +} + +void MMA7455L_SPI :: set_offsets (char offx, char offy, char offz) +{ + /*this function is not very necessary as the offset values were measured beore and sutracted + from the readings in the "update_acceleraton()" function ... if you want to know more info, you + can refer to Freescale MMA7455L data sheet page 24, and Freescale application note AN3745, which + describes a procedure for this calibration. */ +} + +signed char MMA7455L_SPI :: status() +{ + signed char value = 0; + value = read(STATUS); + return value; +} + +void MMA7455L_SPI :: update_acceleration() +{ + signed char x = 0,y = 0,z = 0; + if( status() & DATA_READY) + { + x = read(X_OUT); + Ax = (float)x / sensetivity + 0.1875; + + y = read(Y_OUT); + Ay = (float)y / sensetivity + 0.438; + + z = read(Z_OUT); + Az = (float)z / sensetivity + 1 - 0.562; + } +} + +
diff -r 000000000000 -r 0d4a1401a9c5 MMA7455L_SPI.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MMA7455L_SPI.h Sun Feb 05 21:03:49 2012 +0000 @@ -0,0 +1,80 @@ +/*This class is meant to interface the MMA7455L digital acceleromter with the mbed micro through a SPI interface. +This class deals mainly with acquiring measurment from the acceleromter, but doesn't deal with pulse detection +or interrupts, so it needs to be extended*/ + +#ifndef MMA7455L_SPI_H +#define MMA7455L_SPI_H + +#include "mbed.h" + +/*declaration of the registers of the MMA7455L accelermoter*/ + +#define X_OUT 0x06 +#define Y_OUT 0x07 +#define Z_OUT 0x08 +#define STATUS 0x09 +#define DETECTION_SOURCE_REGISTER 0x0A +#define X_OFF_LSB 0x10 +#define X_OFF_MSB 0x11 +#define Y_OFF_LSB 0x12 +#define Y_OFF_MSB 0x13 +#define Z_OFF_LSB 0x14 +#define Z_OFF_MSB 0x15 +#define MODE_CONTROL 0x16 +#define INTERRUPT_LATCH_RESET 0x17 +#define CONTROL_1 0x18 +#define CONTROL_2 0x19 +#define LEVEL_DETECTION_THRESHOLD_LIMIT 0x1A +#define PULSE_DETECTION_THRESHOLD_LIMIT 0x1B +#define PULSE_DURATION_VALUE 0x1C +#define LATENCY_TIME_VALUE 0x1D + +/*some useful charechters for writing, reading and checking readiness of data*/ +#define READ 0x00 +#define WRITE 0x80 +#define DATA_READY 0x01 + + + + + class MMA7455L_SPI{ + + public: + + float Ax,Ay,Az; + MMA7455L_SPI(PinName, PinName, PinName, PinName); /*mosi, miso, clk and cs*/ + void update_acceleration(); + void set_range_of_measurment(int GS1, int GS0); /*this function takes two bits, zeros ones + and they are used to determine the required char + to be sent to the mode control register*/ +//void set_mode_of_operation(int LSB1, int LSB2); + + + private: + + int sensetivity; + DigitalOut _CS; + SPI _spi; + + /*we declare some basic functions for reading data from accelermoter and writing to the accelermoter**/ + signed char read(char adress); + void write(char adress, char content); + + /*some basic functions that take a char that controls mode of operation and behaviour of the accelermoter*/ + void set_mode(char); + void set_interrupt_latch_reset_time(char value = 0x03); + void set_offsets(char, char, char); + void set_control_1(char value = 0x00); + void set_control_2(char value = 0x00); + void set_level_detection_thershold_limit_value(char value = 0x2F); + void set_pulse_detection_thershold_limit_value(char value = 0x2F); + void set_pulse_duration_value(char value = 0x2F); + void set_latency_time_value(char value =0x2F); + + /*some auxillary functions that describes its status*/ + signed char status(); + + + + }; + #endif \ No newline at end of file
diff -r 000000000000 -r 0d4a1401a9c5 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Feb 05 21:03:49 2012 +0000 @@ -0,0 +1,15 @@ +#include "mbed.h" +#include "MMA7455L_SPI.h" +DigitalOut myled(LED1); +Serial pc(USBTX, USBRX); +MMA7455L_SPI acc(p5,p6,p7,p14); +int main() { + while(1) { + acc.update_acceleration(); + pc.printf("%f %5.3f %5.3f\r\n", acc.Ax, acc.Ay, acc.Az); + myled = 1; + wait(0.02); + myled = 0; + wait(0.02); + } +}
diff -r 000000000000 -r 0d4a1401a9c5 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sun Feb 05 21:03:49 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/b4b9f287a47e