cleberalbert
Joined: 25 Feb 2014 Posts: 34 Location: Brazil
|
How to change int for #define |
Posted: Wed Mar 19, 2014 6:27 pm |
|
|
Hi everyone! could someone help me please??
I've tried to change some constants declared as int to #define but when I do it, a lot of error is shown when i try to compile. Error: A numeric expression must appear here.
every line where is used that constants appear these error. I think it's a bug, or not?
PS: before change for #define the program runs.
I'm using the CCS Compiler version 5.018
Thanks!!
Code: |
#include <18F4520.h>
#case
#FUSES HS
#FUSES PUT
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#use delay(crystal=20MHz)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,ERRORS)
#use i2c(master, fast, sda=PIN_C4, scl=PIN_C3)
#include <math.h>
//All these #define were int.
#define ADXL345_Address 0XA6; //0xA6 //0x53//0x3a
#define ACCEL_READ_ADDR 0XA7;//0xA7 //0x53//0x3b
#define ACCEL_PWRCTRL_ADDR 0x2d;
#define ACCEL_MEASURE_MODE 0x08;
#define DATA_FORMAT 0x31;
#define OUT_X0 0x32;
#define OUT_X1 0x33;
#define OUT_Y0 0x34;
#define OUT_Y1 0x35;
#define OUT_Z0 0x36;
#define OUT_Z1 0x37;
int accel_data[6];
signed int x, y, z;
unsigned int absx, absy, absz, absxyz;
float x1, y1, z1, R;
float teta=0, gama=0,fi=0;
float roll=0, pitch=0, yaw=0;
void initializingADXL345();
void accelerometer();
void cartesian_coordinates();
void spherical_coordinates();
void flight_coordinates();
void writeRegisterI2C(int8 deviceAddress, int8 address, int8 val);
int readRegisterI2C(int8 deviceAddress, int8 address);
void main()
{
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
initializingADXL345();// initialize
while(TRUE)
{
output_high(PIN_A0);
delay_ms (10);
accelerometer();
cartesian_coordinates();
// spherical_coordinates();
// flight_coordinates();
// printf("\n");
output_low(PIN_A0);
delay_ms (10);
}
}
void initializingADXL345(){
// Tell the accelerometer to wake up
writeRegisterI2C(ADXL345_Address, DATA_FORMAT, 0X01);
writeRegisterI2C(ADXL345_Address, ACCEL_PWRCTRL_ADDR, ACCEL_MEASURE_MODE);
}
void writeRegisterI2C(int8 deviceAddress, int8 address, int8 val){
i2c_start(); // start transmission to device
i2c_write(deviceAddress); // send device address
i2c_write(address); // send register address
i2c_write(val); // send value to write
i2c_stop(); // end transmission
}
int8 readRegisterI2C(int8 deviceAddress, int8 address){
int v;
i2c_start(); // start transmission to device
i2c_write(deviceAddress); // This is where you have to _write_ the register number you want
i2c_write(address); // register to read
i2c_start(); // restart - the bus is now set to _read_
i2c_write(deviceAddress + 1); // now turn the bus round
v = i2c_read(0);
i2c_stop(); // This will update x, y, and z with new values
return v;
}
void accelerometer(){
// Read data from the accel
accel_data[0] = readRegisterI2C(ADXL345_Address, OUT_X0);
accel_data[1] = readRegisterI2C(ADXL345_Address, OUT_X1);
accel_data[2] = readRegisterI2C(ADXL345_Address, OUT_Y0);
accel_data[3] = readRegisterI2C(ADXL345_Address, OUT_Y1);
accel_data[4] = readRegisterI2C(ADXL345_Address, OUT_Z0);
accel_data[5] = readRegisterI2C(ADXL345_Address, OUT_Z1);
//////////concatenar datos adxl345/////////////
x=(((accel_data[1])<<8)|accel_data[0]);
y=(((accel_data[3])<<8)|accel_data[2]);
z=(((accel_data[5])<<8)|accel_data[4]);
/*
//Low Pass Filter
x=x*alpha + (x*(1-alpha));
y=y*alpha + (y*(1-alpha));
z=z*alpha + (z*(1-alpha));
*/
printf("%d %d %d\n",x,y,z);
}
void cartesian_coordinates(){ //RETORNANDO VALORES ERRADOS AO CALCULAR O VERSOR
if(x<0) absx=x*(-1); else absx=x;
if(y<0) absy=y*(-1); else absy=y;
if(z<0) absz=z*(-1); else absz=z;
absxyz=(absx*absx)+(absy*absy)+(absz*absz);
R=sqrt(absxyz);
//printf("modulos: %d, %d, %d. soma dos quadrados: %d. raiz quadrada da soma: %.2f\n",absx, absy, absz, absx*absy,R);
x1=x/R;
y1=y/R;
z1=z/R;
//printf("%.2fx, %.2fy, %.2fz\n",x1,y1,z1);
}
void spherical_coordinates(){
teta=acos(x1/R)*180/PI;
gama=acos(y1/R)*180/PI;
fi=acos(z1/R)*180/PI;
//printf("%fº, %fº, %fº (coordenadas esfericas)\n",teta,gama,fi);
}
void flight_coordinates(){
roll = (atan2(-y, z)*180.0)/PI;
pitch = (atan2(x, sqrt(y*y + z*z))*180.0)/PI;
yaw=0;
if(roll<0) roll+=360;
if((fi>90)&&(fi<180)) pitch=(pitch-180)*(-1); //está no segundo ou terceiro quadrante. força range de 90 ate 270
else if((fi<90)&&(pitch<0)) pitch+=360; //está no quarto quadrante. força range de 270 ate 0
//printf("%fº, %fº, %fº (coordenadas de aviacao)\n",roll,pitch,yaw);
}
|
|
|