Help regarding ADC calculations for MMA7260

General discussion on mikroC.
Post Reply
Author
Message
crocu
Posts: 71
Joined: 18 Jun 2008 09:28
Location: France, Macon

Help regarding ADC calculations for MMA7260

#1 Post by crocu » 15 Sep 2010 20:19

Hello

I'm novice with C programming and i need to some help to do some calculation written in C code.

I'm using an acceleration module MMA7260Q that delivers a voltage depending the gravity measurement.


I would like to display how many 'G' my module measures :
I'm able to display the voltage issued from Z axis with the following code : ( tested ok )

Code: Select all

// Added for AN reading :

unsigned char ch;
long tlong;
unsigned int z_read;

...

    Lcd_Out(2,1,("Z:"));

    z_read  = ADC_read(1);                 // get ADC value from 1st channel ( RA0 )
    IntToStr(z_read, val_adc);

    tlong = (long)z_read * 5000;           // convert adc reading to milivolts  ( 5000 if Pic Vcc = 5.00V  4970 for Pic Vcc = 4.97V )
    tlong = tlong / 1023;                  // 0..1023 -> 0-5000mV

    ch     = (tlong / 1000);               // extract volts digit   (* 4 is resistors divider bridge ratio)
    LCD_Chr(2,3,48+ch);                    // write ASCII digit at 4nd row, 16th column ( ecrit la valeur mesurée à partir de la 16ème colonne )

    LCD_Chr_CP('.');

    ch    = ((tlong / 100) % 10);          // extract 0.1 volts digit  (* 4 is resistors divider bridge ratio)
    LCD_Chr_CP(48+ch);                     // write ASCII digit at cursor point

    ch    = (tlong / 10) % 10;             // extract 0.01 volts digit
    LCD_Chr_CP(48+ch);                     // write ASCII digit at cursor point

For now, i would like to display how many 'G' this voltage represents but i don't know how to do the calculations :arrow:

In normal condition ( sensor not moved ) voltage is 2.54V this represents earth gravity = ' 1G '
when sensor records +1G, it adds +0.8V , when sensor records -1G it loose -0.8V to reference voltage : 2.54V

i would like to run a loop and display on a LCD how many 'G' (3 digits) the sensor reads.
ie : G = 1.00 or G = 1.55

If someone already made a project with that sensor or able to help to write the calculations i will really appreciate it . :)

Best Regards,

crocu
Posts: 71
Joined: 18 Jun 2008 09:28
Location: France, Macon

Re: Help regarding ADC calculations for MMA7260

#2 Post by crocu » 16 Sep 2010 20:28

Hello,

i'm still trying to write a code to perform calculations and display a gravity variable called "Gvalue"

The code needs to divide ADC value issue from sensor by 163. ( G_SENSITIVITY )
My code compiles properly but calculation shoud not be correct because "Gvalue" result always display0.00, can someone tell me what is wrong ? :roll:


Here is how i measure acceleration :

The PIC's ADC was set to 10bit resolution, with 5V as the reference voltage.
To get a rough estimate, I knew that the earth gravity (=1g) on the accelerometer output would be close to 5V / 2 = 2.5V.

In 10bit mode, this would correspond to (2.5V / 5V)*(2^10)= 512 ( #define CALIBRATED_1G will be set to 515 due to non perfect +5V power source )

The sensor is configured to read +- 6G , steps are 200mV/G, so ADC step is (0.2 / 5 ) * 1024 = 41

If sensor receives +1g, the voltage would be ~2.7V -> ADC will be 512 + 41 = 553
which would correspond to (2.7V / 5V)*(2^10)=553

Code: Select all

unsigned char ch;
long tlong;
unsigned int adc_rd;
unsigned int z_read=0;
int Gvalue=0;

#define CALIBRATED_1G  515             // ADC value when sensor is in normal condition : Earth gravity egals 1G
#define  G_SENSITIVITY 163             // has to be set = "41" if we use -+6G sensor configuration.
...

Code: Select all

    Lcd_Out(2,1,("Z:"));

    z_read  = ADC_read(1);                 // get ADC value from 1st channel ( RA0 )
    
    IntToStr(z_read, val_adc);
    Lcd_Out(3,12,val_adc);                  // display ADC value on LCD


    // Display ADC voltage issued from Z axis on LCD :
    
    tlong = (long)z_read * 5000;          // convert adc reading to milivolts  ( 5000 if Pic Vcc = 5.00V  4970 for Pic Vcc = 4.97V )
    tlong = tlong / 1023;                 // 0..1023 -> 0-5000mV

    ch     = (tlong / 1000);              // extract volts digit
    LCD_Chr(2,3,48+ch);               

    LCD_Chr_CP('.');

    ch    = ((tlong / 100) % 10);         // extract 0.1 volts digit
    LCD_Chr_CP(48+ch);                    // write ASCII digit at cursor point

    ch    = (tlong / 10) % 10;            // extract 0.01 volts digit
    LCD_Chr_CP(48+ch);                    // write ASCII digit at cursor point
    

    // G calculations :
    
    if( z_read >=+5 || z_read <= -5 )  // Perform a new calculation only measurement is different as before
                                                    // if measurement less than + or - 5 then ignore small changes to make display more readable.
      {
         Gvalue = z_read - CALIBRATED_1G; // Calibrated_1G is ADC value in normal conditions ( earth gravity ) = 515
         // Gvalue = Gvalue<<5;            // left shifting then dividing allows you to perform more precise multiplication/division  ( REMOVED Because it is not working well ! )
         Gvalue=Gvalue/G_SENSITIVITY;     // divide by 515
         tlong = (long)Gvalue;

    // Display G results on LCD :
    ch     = (tlong / 1000);               // extract volts digit
    LCD_Chr(2,15,48+ch);                    // write ASCII digit

    LCD_Chr_CP('.');

    ch    = ((tlong / 100) % 10);          // extract 0.1 volts digit
    LCD_Chr_CP(48+ch);                     // write ASCII digit at cursor point

    ch    = (tlong / 10) % 10;             // extract 0.01 volts digit
    LCD_Chr_CP(48+ch);                     // write ASCII digit at cursor point

     }

User avatar
slavisa.zlatanovic
mikroElektronika team
Posts: 1321
Joined: 07 Apr 2009 09:39

Re: Help regarding ADC calculations for MMA7260

#3 Post by slavisa.zlatanovic » 19 Sep 2010 13:12

Hi!

There are in our Measurement Boards offer (among other) also:
- Three-Axis Accelerometer Board
- Accel SPI Board

Currently, we provide the Three-Axis Accelerometer Board demo examples.
I'm sure these examples will help you get going with your project (although written for the ADXL330: 3-axis accelerometer).
Best regards
Slavisa

Post Reply

Return to “mikroC General”