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.

Parents
  • #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.

Reply
  • #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.

Children