CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

receiving data on pic16F from an accelerometer MPU6050
Goto page 1, 2, 3, 4, 5, 6  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
AKA TENDO



Joined: 25 Jan 2023
Posts: 47

View user's profile Send private message

receiving data on pic16F from an accelerometer MPU6050
PostPosted: Wed Jan 25, 2023 4:37 pm     Reply with quote

Hi ! I'm currently working on a project on a pic16f877A and i must have data from an accelerometer mpu6050 on 3 axes (X,Y,Z). I'm receiving data in i2c burst mode but for some reasons, my data are not accurate and sometimes my i2c bus receive same numbers on 2 axes. I post my code here, pls don't focus on "write_slave()" this is for communication with my friend but this is not the problem I'm posting for ^^:

Code:

#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#use i2c(MASTER, sda=PIN_C4, scl=PIN_C3)

#define READ_ADDR      0xD1
#define WRITE_ADDR     0xD0

#define REG_GYRO_X1    0X43
#define REG_GYRO_X2    0X44
#define REG_GYRO_Y1    0X45
#define REG_GYRO_Y2    0X46
#define REG_GYRO_Z1    0X47
#define REG_GYRO_Z2    0X48

#define GYRO_CONFIG    0x1B
#define ACCEL_CONFIG   0x1C

#define PWR_MGMT_1     0x6B
#define POWER_MNGMNT   0x6B
#define CONFIG         0x1A
#define SAMPLE_DRATE   0x19
#define INT_ENABLE     0x38
#define FIFO_EN        0x6A
#define FIFO_CONFIG    0x23

#define REG_ACCEL_X1   0x3B
#define REG_ACCEL_X2   0x3C
#define REG_ACCEL_Y1   0x3D
#define REG_ACCEL_Y2   0x3E
#define REG_ACCEL_Z1   0x3F
#define REG_ACCEL_Z2   0x40
#define FIFO_BUFFER    0x74
#define WHO_AM_I       0x75
#define INT_PIN_CFG    0x37
#define INT_STATUS     0x3A

void write_slave(int val)
{
   printf("\n\r 1111 ");
   i2c_start();
   printf("\n\r 2222 ");
   i2c_write(0x10);
   printf("\n\r 3333 ");
   i2c_write(val);
   printf("\n\r 44444 ");
   i2c_stop();
   }

/////////////////////////////////////////////////////////
                   // WRITE//
////////////////////////////////////////////////////////

void write_reg(int Reg, int val)
{
   i2c_start();
   i2c_write(0xD0);
   i2c_write(Reg);
   i2c_write(val);
   i2c_stop();   
}

/////////////////////////////////////////////////////////
                    // READ//
////////////////////////////////////////////////////////

int read_reg(int Reg)
{
   //unsigned int data = 0;
   int buffer;
   
   i2c_start();
   i2c_write(0xD0);
   i2c_write(Reg);   
   i2c_start();
   i2c_write(0xD1); //start with the address in read mode
   buffer = i2c_read(0); // read and store the data in a variable
   i2c_stop();
   return buffer;
}

/////////////////////////////////////////////////////////
                     //INIT//
////////////////////////////////////////////////////////

void MPU6050_Init()
{
   
    write_reg(PWR_MGMT_1,  0x01);
    delay_ms(100);
    write_reg(CONFIG, 0x00);
    delay_ms(10);
    write_reg(GYRO_CONFIG, 0x18);  // Pourquoi configurer Gyro et pas AccĂ©
    delay_ms(10);
    write_reg(ACCEL_CONFIG, 0x00); // test 24/11
    delay_ms(10);
    write_reg(SAMPLE_DRATE, 0x07);
    delay_ms(10);
    write_reg(INT_ENABLE, 0x01);
    delay_ms(10);
    write_reg(FIFO_EN,  0x40);
    delay_ms(10);
    write_reg(FIFO_CONFIG, 0x08);
    delay_ms(10);
    write_reg(INT_PIN_CFG, 0x10);
    delay_ms(10);   
   
}

/////////////////////////////////////////////////////////
                     //MPU READ//
////////////////////////////////////////////////////////

 void Read_Sensor (int8 REG_add)
{
int MSB1;
int LSB1;
int MSB2;
int LSB2;
int MSB3;
int LSB3;
int16 AxeX;
int16 AxeY;
int16 AxeZ;
float  fAxeX, fAxeY, fAxeZ;

   i2c_start();
   i2c_write(0xD0);
   i2c_write(REG_add);   
   i2c_start();
   i2c_write(0xD1); //start with the address in read mode
   
   MSB1 = i2c_read();
   LSB1 = i2c_read();
 
   MSB2 = i2c_read();
   LSB2 = i2c_read();
     
   MSB3 = i2c_read();
   LSB3 = i2c_read(0);
   read_reg(INT_STATUS);
   
   AxeX = make16(MSB1, LSB1);
   AxeY = make16(MSB2, LSB2);
   AxeZ = make16(MSB3, LSB3);
   
   i2c_stop();
     
   fAxeX = (float)AxeX/16384.0;
   fAxeY = (float)AxeY/16384.0;
   fAxeZ = (float)AxeZ/16384.0;
   
   printf("\n\r axe X: %f",fAxeX);
   printf("\n\r axe Y: %f",fAxeY);
   printf("\n\r axe Z: %f",fAxeZ);
   
}

/////////////////////////////////////////////////////////
                     //Check addr//
////////////////////////////////////////////////////////

/*int check_addr (int reg_addr) //check if the slave's address is right
{
   //read_reg(0xD0,buffer_Receive); //0x75 is the REG: WHO_I_AM
   
   if (buffer_Receive == 0x68)
   {
      return (1);
   }
   
  else
  {
      return (0);
  }
}
*/



void main (void)
{

int TestRS = 0;


   MPU6050_Init();

   do
   {
   Read_Sensor(FIFO_BUFFER);
   write_slave(0x15);
   
   
   
         
   
                 
                           
   
   
   
   
      output_d(testRS);
      delay_ms(500);
      TestRS=TestRS+1;
      delay_ms(500);
    }
   
      while(true);
 
}



all ideas are welcome, thanks.
temtronic



Joined: 01 Jul 2010
Posts: 9232
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Jan 25, 2023 4:41 pm     Reply with quote

are you running a 5 volt PIC and a 3 volt sensor ?
AKA TENDO



Joined: 25 Jan 2023
Posts: 47

View user's profile Send private message

PostPosted: Wed Jan 25, 2023 4:45 pm     Reply with quote

They're both on 5V.
temtronic



Joined: 01 Jul 2010
Posts: 9232
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Jan 25, 2023 5:33 pm     Reply with quote

Try running the I2C port as 'slow' ? I don't use the MPU6050 but it has a maximum speed, 'somewhere' on it's datasheet.

What value are the I2C bus pullup resistors ? They have to be selected on BOTH VDD and speed.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jan 25, 2023 7:53 pm     Reply with quote

I notice you're using the MPU6050's fifo.

Here is some code ported from Github that does not use the fifo. It works:
http://www.ccsinfo.com/forum/viewtopic.php?t=52866&start=29
AKA TENDO



Joined: 25 Jan 2023
Posts: 47

View user's profile Send private message

PostPosted: Thu Jan 26, 2023 2:32 am     Reply with quote

PCM programmer wrote:
I notice you're using the MPU6050's fifo.

Here is some code ported from Github that does not use the fifo. It works:
http://www.ccsinfo.com/forum/viewtopic.php?t=52866&start=29


ok thank you i'lln try but i don't have the math.h library, have you got a sure link where i can take it?
AKA TENDO



Joined: 25 Jan 2023
Posts: 47

View user's profile Send private message

PostPosted: Thu Jan 26, 2023 2:35 am     Reply with quote

temtronic wrote:
Try running the I2C port as 'slow' ? I don't use the MPU6050 but it has a maximum speed, 'somewhere' on it's datasheet.

What value are the I2C bus pullup resistors ? They have to be selected on BOTH VDD and speed.


i think this part doesn't have an issue, my teacher has the code who works on it ^^' he had put it in front of me and it works. so i think my problem isn't on hardware part.
Ttelmah



Joined: 11 Mar 2010
Posts: 19529

View user's profile Send private message

PostPosted: Thu Jan 26, 2023 2:37 am     Reply with quote

AKA TENDO wrote:
They're both on 5V.


Er. The MPU6050, is not a 5v device. 3.46v _max_ rating. If you
have run it off 5v, you have probably killed the chip.... Sad

If you are using a board from something like an Arduino, that has a 5v
regulator for the chip, you have the problem of signalling levels. The
Arduino accepts a 2.4v signal as 'high' for it's I2C connection. The PIC
does not. Needs 4v.
AKA TENDO



Joined: 25 Jan 2023
Posts: 47

View user's profile Send private message

PostPosted: Thu Jan 26, 2023 2:46 am     Reply with quote

Ttelmah wrote:
AKA TENDO wrote:
They're both on 5V.


Er. The MPU6050, is not a 5v device. 3.46v _max_ rating. If you
have run it off 5v, you have probably killed the chip.... Sad

If you are using a board from something like an Arduino, that has a 5v
regulator for the chip, you have the problem of signalling levels. The
Arduino accepts a 2.4v signal as 'high' for it's I2C connection. The PIC
does not. Needs 4v.


I use this sensor :

https://www.hotmcu.com/gy521-mpu6050-3axis-acceleration-gyroscope-6dof-module-p-83.html

and it accepts 5V.

And how could i solve the problem that you are talking about?
Do you mean that i should try to put my i2c in SLOW MODE?
Ttelmah



Joined: 11 Mar 2010
Posts: 19529

View user's profile Send private message

PostPosted: Thu Jan 26, 2023 2:58 am     Reply with quote

That is what I guessed. That has a 3.3v regulator, so you won't have
damaged the chip, but the problem is that your PIC I2C peripheral wants
4v for a logic high, so will be intermittent on receiving things.
Try changing the I2C setup and add 'SMBUS' to this. This should turn
the input voltage required down low enough to work.

#use i2c(MASTER, sda=PIN_C4, scl=PIN_C3, SMBUS)
AKA TENDO



Joined: 25 Jan 2023
Posts: 47

View user's profile Send private message

PostPosted: Thu Jan 26, 2023 2:58 am     Reply with quote

Is normal that the math.h doesn't compile? Did i forget something?
[/img]
AKA TENDO



Joined: 25 Jan 2023
Posts: 47

View user's profile Send private message

PostPosted: Thu Jan 26, 2023 3:03 am     Reply with quote

Ttelmah wrote:
That is what I guessed. That has a 3.3v regulator, so you won't have
damaged the chip, but the problem is that your PIC I2C peripheral wants
4v for a logic high, so will be intermittent on receiving things.
Try changing the I2C setup and add 'SMBUS' to this. This should turn
the input voltage required down low enough to work.

#use i2c(MASTER, sda=PIN_C4, scl=PIN_C3, SMBUS)


ok thank you very much i will try later but first i would try the code from pcm prgrammer. if it works i would use it for my project and continue to work on my own code as well.
Can you help me about the math.h library?
Ttelmah



Joined: 11 Mar 2010
Posts: 19529

View user's profile Send private message

PostPosted: Thu Jan 26, 2023 3:08 am     Reply with quote

Switch the setup to SMBUS anyway. It needs this setting to work with this
chip.
AKA TENDO



Joined: 25 Jan 2023
Posts: 47

View user's profile Send private message

PostPosted: Thu Jan 26, 2023 3:17 am     Reply with quote

Ttelmah wrote:
Switch the setup to SMBUS anyway. It needs this setting to work with this
chip.


i tried but it doesn't change anything..
AKA TENDO



Joined: 25 Jan 2023
Posts: 47

View user's profile Send private message

PostPosted: Thu Jan 26, 2023 4:02 am     Reply with quote

Anybody has a solution to compile math.h?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2, 3, 4, 5, 6  Next
Page 1 of 6

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group