This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

MMA8451 accelerometer

i am using p89v51rd2bn microcontroller and have interfaced MMA8451 accelerometer with it using i2c bus.
The problem i am facing is that same value is being displayed again and again even on tilting the sensor,it only changes when the power is switched off and then again turned on and then the new value is being continuously displayed until it is again switched off and then turned on.

  • P89V51RD2 does not have on-chip i2c.
    which indicates that you have implemented the i2c protocol code.

    Post your i2c driver code.

  • #include "twi.h"

    sbit SDA = P1^7;
    sbit SCL = P1^5;

    void I2CInit(void)
    { SDA = 1; SCL = 1;
    }

    void I2CStart(void)
    { SDA = 1; _nop_(); _nop_(); SCL = 1; _nop_(); _nop_(); SDA = 0; _nop_(); _nop_(); SCL = 0; _nop_(); _nop_();
    }

    void I2CRestart(void)
    { SDA = 1; _nop_(); _nop_(); SCL = 1; _nop_(); _nop_(); SDA = 0; _nop_(); _nop_(); SCL = 0; _nop_(); _nop_();
    }

    void I2CStop(void)
    { SDA = 0; _nop_(); _nop_(); SCL = 1; _nop_(); _nop_(); SDA = 1; _nop_(); _nop_();
    }

    void I2CAck(void)
    { SDA = 0; _nop_(); _nop_(); SCL = 1; _nop_(); _nop_(); SCL = 0; _nop_(); _nop_();
    }

    void I2CNak(void)
    { SDA = 1; _nop_(); _nop_(); SCL = 1; _nop_(); _nop_(); SCL = 0; _nop_(); _nop_();
    }

    unsigned char I2CSend(unsigned char Data)
    { unsigned char i, ack_bit;

    for(i=0;i<8;i++) { SCL = 0; _nop_(); _nop_(); if ((Data & 0x80) == 0) SDA = 0; else SDA = 1;

    _nop_(); _nop_(); SCL = 1; _nop_(); _nop_(); Data<<=1; } SCL = 0; _nop_(); _nop_(); SDA = 1; SCL = 1; _nop_(); _nop_(); ack_bit = SDA; _nop_(); _nop_(); SCL = 0; _nop_(); _nop_(); return !ack_bit;
    }

    unsigned char I2CRead(void)
    { unsigned char i, Data=0;

    for(i=0;i<8;i++) { SDA = 1; SCL = 0; _nop_(); _nop_(); SCL = 1; _nop_(); _nop_(); if(SDA) Data |=1; if(i<7) Data<<=1; } SCL = 0; SDA = 1; return Data;
    }

  • did this look usable for someone willing to help you in preview?
    since it did not, did you try to find out why?
    did you, in that process happen to see the instructions for posting

    I guess no, no, no

    Erik

  • #include "twi.h"
    
    sbit    SDA = P1^7;
    sbit    SCL = P1^5;
    
    
    void I2CInit(void)
    {
            SDA = 1;
            SCL = 1;
    }
    
    
    void I2CStart(void)
    {
            SDA = 1;
            _nop_();
            _nop_();
            SCL = 1;
            _nop_();
            _nop_();
            SDA = 0;
            _nop_();
            _nop_();
            SCL = 0;
            _nop_();
            _nop_();
    }
    
    
    void I2CRestart(void)
    {
            SDA = 1;
            _nop_();
            _nop_();
            SCL = 1;
            _nop_();
            _nop_();
            SDA = 0;
            _nop_();
            _nop_();
            SCL = 0;
            _nop_();
            _nop_();
    }
    
    
    void I2CStop(void)
    {
            SDA = 0;
            _nop_();
            _nop_();
            SCL = 1;
            _nop_();
            _nop_();
            SDA = 1;
            _nop_();
            _nop_();
    }
    
    
    void I2CAck(void)
    {
            SDA = 0;
            _nop_();
            _nop_();
            SCL = 1;
            _nop_();
            _nop_();
            SCL = 0;
            _nop_();
            _nop_();
    }
    
    
    void I2CNak(void)
    {
            SDA = 1;
            _nop_();
            _nop_();
            SCL = 1;
            _nop_();
            _nop_();
            SCL = 0;
            _nop_();
            _nop_();
    }
    
    
    unsigned char I2CSend(unsigned char Data)
    {
            unsigned char i, ack_bit;
    
            for(i=0;i<8;i++)
            {
                    SCL = 0;
                    _nop_();
                    _nop_();
                    if ((Data & 0x80) == 0)
                            SDA = 0;
                    else
                            SDA = 1;
    
                    _nop_();
                    _nop_();
                    SCL = 1;
                    _nop_();
                    _nop_();
                    Data<<=1;
            }
            SCL = 0;
            _nop_();
            _nop_();
            SDA = 1;
            SCL = 1;
            _nop_();
            _nop_();
            ack_bit = SDA;
            _nop_();
            _nop_();
            SCL = 0;
            _nop_();
            _nop_();
            return !ack_bit;
    }
    
    
    unsigned char I2CRead(void)
    {
            unsigned char i, Data=0;
    
            for(i=0;i<8;i++)
            {
                    SDA = 1;
                    SCL = 0;
                    _nop_();
                    _nop_();
                    SCL = 1;
                    _nop_();
                    _nop_();
                    if(SDA)
                            Data |=1;
                    if(i<7)
                            Data<<=1;
            }
            SCL = 0;
            SDA = 1;
            return Data;
    }
    
    

  • with all those comments, I'm just eager to help you

  • i am sorry not to write the comments.
    will try to add comments and upload. thanks.

    till then kindly go through the code if possible.
    the _nop_() are added for small delay generation.

  • #include "twi.h"

    sbit SDA = P1^7;
    sbit SCL = P1^5;

    void I2CInit(void)
    {
            SDA = 1;
            SCL = 1;
    }
    
    
    void I2CStart(void)
    {
            SDA = 1;    //Start condition
            SCL = 1;
            SDA = 0;
            SCL = 0;
    }
    
    
    void I2CRestart(void)
    {
            SDA = 1;     //Transmit restart condition. I dont kno this is correct restart condition or not
            SCL = 1;
            SDA = 0;
            SCL = 0;
    }
    
    
    void I2CStop(void)
    {
            SDA = 0;    //stop condition at the end of transmission
            SCL = 1;
            SDA = 1;
    }
    
    
    void I2CAck(void)
    {
            SDA = 0;    //transmit ack
            SCL = 1;
            SCL = 0;
    }
    
    
    void I2CNak(void)
    {
            SDA = 1;    //transmitt nack
            SCL = 1;
            SCL = 0;
    }
    
    
    unsigned char I2CSend(unsigned char Data)
    {
            unsigned char i, ack_bit;
    
            for(i=0;i<8;i++)      //send data out on sda line with msb first
            {
                    SCL = 0;
                    if ((Data & 0x80) == 0)
                            SDA = 0;
                    else
                            SDA = 1;
    
                    SCL = 1;
                    Data<<=1;
            }
            SCL = 0;
            SDA = 1;              //configure as input pin. doubtful
            SCL = 1;
            ack_bit = SDA;        //read ack bit
            SCL = 0;
            return !ack_bit;     //i m doubtful about this
    }
    
    
    unsigned char I2CRead(void)
    {
            unsigned char i, Data=0;
    
            for(i=0;i<8;i++)
            {
                    SDA = 1;         //read the 8bit data
                    SCL = 0;
                    SCL = 1;
    
                    if(SDA)
                            Data |=1;
                    if(i<7)
                            Data<<=1;
            }
            SCL = 0;
            SDA = 1;
            return Data;
    }
    
    

    all the functions are named. so thought that comments may not be required.

  • MMA8451 is 3.3Volts. Are you connecting it directly to your uC and if so, what voltage is it running at?

  • MMA8451 is running at 3.3V and uC is running at 5V

    The mma8451 interface was successful. i can now read X-axis & Y-axis data continuously.

    I want to convert the values into angular positions expressed in degrees. how do i do that?
    the mma8451 is configured for 2g.

    also, depending upon 2g, 4g, 8g, what is the effect on the sensitivity (i mean the number of counts generated)?