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 support@ccsinfo.com

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



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Tue Jan 31, 2023 3:06 am     Reply with quote

You have a #USE RS232, and printf's shown in what you have posted.
AKA TENDO



Joined: 25 Jan 2023
Posts: 47

View user's profile Send private message

PostPosted: Tue Jan 31, 2023 3:31 am     Reply with quote

Ttelmah wrote:
You have a #USE RS232, and printf's shown in what you have posted.


Ok i see, so ERROR is only for my printf on terminal?
temtronic



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

View user's profile Send private message

PostPosted: Tue Jan 31, 2023 6:28 am     Reply with quote

simply said...

ERRORS is for incoming (receive) data To the PIC.
Without it, the PIC HW UART will 'freeze' (stop) the PIC from running
Think of the incoming buffer as your mailbox, that can only contain 5 letters. If YOU don't take at least ONE of those 5 letters out, then you can't get anymore mail.


If you code and compile a small program without and with 'ERRORS', then look at the listings, you'll see what the additional code is doing. Yes, it'll be in assembler but it's easy to decode.

Sadly it should be a default parameter but it's not.
AKA TENDO



Joined: 25 Jan 2023
Posts: 47

View user's profile Send private message

PostPosted: Wed Feb 01, 2023 2:24 pm     Reply with quote

temtronic wrote:
simply said...

ERRORS is for incoming (receive) data To the PIC.
Without it, the PIC HW UART will 'freeze' (stop) the PIC from running
Think of the incoming buffer as your mailbox, that can only contain 5 letters. If YOU don't take at least ONE of those 5 letters out, then you can't get anymore mail.


If you code and compile a small program without and with 'ERRORS', then look at the listings, you'll see what the additional code is doing. Yes, it'll be in assembler but it's easy to decode.

Sadly it should be a default parameter but it's not.


Okay i will add the options. I have noticed something yesterday, my code use currently 67% of ROM, and i don't know from what value PIC can have problems and what problems can appear?
temtronic



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

View user's profile Send private message

PostPosted: Wed Feb 01, 2023 3:13 pm     Reply with quote

If the program compiles, then you can fill the ROM with 'program'. Technically you will not get problems. Your program fits into the ROM.

Normally what happens is, depending on the PIC, you can get a 'segment too large' error(so will NOT compile) which indicates that a 'section' of your code (say a large or complicated function ) is too long(big) to fit into a segment or 'section' of the ROM. Now, providing there IS physical room for the code, you can usually 'juggle' or rearrange the order of the functions, or use other 'tricks' to get the program to fit.
AKA TENDO



Joined: 25 Jan 2023
Posts: 47

View user's profile Send private message

PostPosted: Mon Feb 20, 2023 2:48 am     Reply with quote

Hello, i hope you are all going well. I am back on project and i wanna try to use better the "atan" function in purpose to reduce the space used. One of you told me there is exemple in code library but i don't find it. Could someone help me? Best regards.
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Mon Feb 20, 2023 3:19 am     Reply with quote

It's an equivalent to atan2, not atan.

[url]
http://www.ccsinfo.com/forum/viewtopic.php?t=56264
[/url]

atan2, is generally what you need for things like this. Two measurements,
X and Y, and you want the angle this corresponds to.
AKA TENDO



Joined: 25 Jan 2023
Posts: 47

View user's profile Send private message

PostPosted: Mon Feb 20, 2023 10:02 am     Reply with quote

Ttelmah wrote:
It's an equivalent to atan2, not atan.

[url]
http://www.ccsinfo.com/forum/viewtopic.php?t=56264
[/url]

atan2, is generally what you need for things like this. Two measurements,
X and Y, and you want the angle this corresponds to.


ok, so i've checked your link, i just don't understand properly those lines :

Code:

 //Now approximation for atan from -1 to +1, giving an answer in degrees.

   aival = fAbs(ival);
   oval = 45 * ival - ival * (aival - 1) * (14.02 + 3.79 * aival);


they are replacing atan2 right?
but what does aival, ivaland oval mean?
AKA TENDO



Joined: 25 Jan 2023
Posts: 47

View user's profile Send private message

PostPosted: Mon Feb 20, 2023 10:03 am     Reply with quote

Ttelmah wrote:
It's an equivalent to atan2, not atan.

[url]
http://www.ccsinfo.com/forum/viewtopic.php?t=56264
[/url]

atan2, is generally what you need for things like this. Two measurements,
X and Y, and you want the angle this corresponds to.


ok, so i've checked your link, i just don't understand properly those lines :

Code:

 //Now approximation for atan from -1 to +1, giving an answer in degrees.

   aival = fAbs(ival);
   oval = 45 * ival - ival * (aival - 1) * (14.02 + 3.79 * aival);


they are replacing atan2 right?
but what does aival, ival and oval mean?
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Mon Feb 20, 2023 10:42 am     Reply with quote

They are internal variables in the function.
Declared at the top of the function.
A search online will find the algorithm this uses. Remember the factors
are tweaked to give a result in degrees, not radians, since most
applications for sensors like yours will actually want degrees as a result.
AKA TENDO



Joined: 25 Jan 2023
Posts: 47

View user's profile Send private message

PostPosted: Fri Nov 17, 2023 2:19 pm     Reply with quote

Hi everybody. First, i would say thank you to all of you guys who helped me for my project. It kind or worked i just had other problems but i have mainly reached my goal. I just realised that i forgot to say thank you and post my final code. So, i ll do it now. I hope this is the good code, in other way it should still help people with same problem :



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)

#include <MATH.h>

#define MPU6050_SLAVE_WRT  0xD0
#define MPU6050_SLAVE_RD   0xD1

// Register addresses
#define MPU6050_WHO_AM_I     0x05
#define MPU6050_SMPLRT_DIV   0x19
#define MPU6050_CONFIG       0x1A
#define MPU6050_GYRO_CONFIG  0x1B
#define MPU6050_PWR_MGMT_1   0x6B

#define ACCEL_XOUT_H  0x3B
#define ACCEL_XOUT_L  0x3C
#define ACCEL_YOUT_H  0x3D
#define ACCEL_YOUT_L  0x3E
#define ACCEL_ZOUT_H  0x3F
#define ACCEL_ZOUT_L  0x40



// Global variables
 
int order = 0;                       
signed int16 accel_xout;
signed int16 accel_yout;
signed int16 accel_zout;

float accel_xangle;
float accel_yangle;

/////////////////////////////////////////////////////
               //WRITE SLAVE//   
/////////////////////////////////////////////////////

void write_slave(int add, int val)
{
   i2c_start();
   i2c_write(add);
   i2c_write(val);
   i2c_stop();
   delay_ms(100);
}

/////////////////////////////////////////////////////
               //Slave_Handler//   
/////////////////////////////////////////////////////

void Slave_Handler()                     
{

//int valCompare1, valCompare2, valCompare3;

   //Angle_X//

if (accel_xangle <= (-5))
   {
     order = 1;
   }

if (accel_xangle >= (-3))
   {
     order = 2;
   }
 

if ((accel_xangle <= (-3)) && (accel_xangle >= (-5)))
   {
     order = 0;
   }
   printf("order= %d \n\r", order);

}

   //Angle_Y//

/*if (accel_yangle <= (-3))
   {
     order = 1;
   }

if (accel_yangle >= 0)
   {
     order = 2;
   }
}
*/
/////////////////////////////////////////////////////
               //READ WORD//   
/////////////////////////////////////////////////////

int16 MPU6050_read_word(int8 reg_addr)
{
int8 lsb, msb;
int16 retval;

i2c_start();
i2c_write(MPU6050_SLAVE_WRT);
i2c_write(reg_addr);
i2c_start();
i2c_write(MPU6050_SLAVE_RD);
msb = i2c_read();   // Read MSB byte first.
lsb = i2c_read(0);  // Do a NACK on the last read
i2c_stop();   

retval = make16(msb, lsb);

return(retval);
}

/////////////////////////////////////////////////////
               //ACCEL VALUES//   
/////////////////////////////////////////////////////

void Get_Accel_Values()
{
accel_xout = MPU6050_read_word(ACCEL_XOUT_H);
accel_yout = MPU6050_read_word(ACCEL_YOUT_H);
accel_zout = MPU6050_read_word(ACCEL_ZOUT_H);
}   

/////////////////////////////////////////////////////
               //GET ANGLES//   
/////////////////////////////////////////////////////
void Get_Accel_Angles()
{
accel_xangle = 57.295*atan((float)accel_yout/ sqrt(pow((float)accel_zout,2)+pow((float)accel_xout,2)));
accel_yangle = 57.295*atan((float)-accel_xout/ sqrt(pow((float)accel_zout,2)+pow((float)accel_yout,2)));
}   

/////////////////////////////////////////////////////
               //MPU INIT//   
/////////////////////////////////////////////////////

void Setup_mpu6050(void)
{
delay_ms(100);

i2c_start();
i2c_write(MPU6050_SLAVE_WRT);
i2c_write(MPU6050_PWR_MGMT_1);
i2c_write(0x00);
i2c_stop();

i2c_start();
i2c_write(MPU6050_SLAVE_WRT);
i2c_write(MPU6050_GYRO_CONFIG);
i2c_write(0x08);
i2c_stop();

i2c_start();
i2c_write(MPU6050_SLAVE_WRT);
i2c_write(MPU6050_SMPLRT_DIV);   
i2c_write(0x07);
i2c_stop();

delay_ms(100);  // Gyro start-up time is 30ms after power-up.
}

/////////////////////////////////////////////////////
               // MAIN //   
/////////////////////////////////////////////////////

void main()
{

Setup_mpu6050();   


while(TRUE)
  {
 
   Get_Accel_Values();
 
   Get_Accel_Angles();
   delay_ms(200);

   Slave_Handler();
   output_d(input_b());
   write_slave(0x17,order1);
   write_slave(0x21,order2);
     
   printf("accel_xangle = %7.0f \n\r", accel_xangle);
   printf("accel_yangle = %7.0f \n\r", accel_yangle);
   printf("\n\r");
   delay_ms(300);
  }
}


I know my code isn't very good but i hope it will help. Yo could at least use my functions. Best regards and thank you all.
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 Previous  1, 2, 3, 4, 5, 6
Page 6 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