![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
Ttelmah
Joined: 11 Mar 2010 Posts: 19358
|
|
Posted: Tue Jan 31, 2023 3:06 am |
|
|
You have a #USE RS232, and printf's shown in what you have posted. |
|
![](templates/subSilver/images/spacer.gif) |
AKA TENDO
Joined: 25 Jan 2023 Posts: 47
|
|
Posted: Tue Jan 31, 2023 3:31 am |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9168 Location: Greensville,Ontario
|
|
Posted: Tue Jan 31, 2023 6:28 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
AKA TENDO
Joined: 25 Jan 2023 Posts: 47
|
|
Posted: Wed Feb 01, 2023 2:24 pm |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9168 Location: Greensville,Ontario
|
|
Posted: Wed Feb 01, 2023 3:13 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
AKA TENDO
Joined: 25 Jan 2023 Posts: 47
|
|
Posted: Mon Feb 20, 2023 2:48 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19358
|
|
Posted: Mon Feb 20, 2023 3:19 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
AKA TENDO
Joined: 25 Jan 2023 Posts: 47
|
|
Posted: Mon Feb 20, 2023 10:02 am |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
AKA TENDO
Joined: 25 Jan 2023 Posts: 47
|
|
Posted: Mon Feb 20, 2023 10:03 am |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19358
|
|
Posted: Mon Feb 20, 2023 10:42 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
AKA TENDO
Joined: 25 Jan 2023 Posts: 47
|
|
Posted: Fri Nov 17, 2023 2:19 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|