rigomm
Joined: 17 Mar 2010 Posts: 13
|
Using 2D array float type as parameters in a function |
Posted: Wed Oct 20, 2010 5:09 am |
|
|
Hi! I did a simple program using arrays This will be the base of a more complex one.
I’m using 2D arrays and passing them as parameters into a function.
If the variables (arrays) are declared as INTEGERS, the program works perfect!
If I declare the arrays as float, the function simply cannot load the variables. I mean, the variables in the function become zero.
Below the code of both versions
USING INTEGERS
Code: |
#include "30f3012.h" //DEVICE
#device adc=12
//*****************CHANGED INITAL dsPIC parameter to work at higher speed using the PLL
#FUSES HS2_PLL8//The xtal freq is divided by 2 and multiplied by 8 (maximum using 20MHz xtal)
#FUSES NOWDT // NO Watch Dog Timer
#FUSES PR_PLL //Primary Oscillator
#FUSES NOCKSFSM //Clock Switching is disabled, fail Safe clock monitor is disabled
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOWRT //Program memory not write protected
#use delay(clock=80000000)// 20MHz/2*8=80MHz=80000000
#use rs232(baud=115200, xmit=PIN_B4, rcv=PIN_B5) //To transmite result over Bluetooth module
//********************************
#define LED PIN_D0
//=========================================================
int bandpass(int r[1][2],int r2[1][2]);
//**************************************************************************************
//////////////////
//*****MAIN*****//
//////////////////
void main()
{
printf ("start");
int step = 0;
int x[1][2]={1,2};
int y[1][2]={3,4};
int a[1][2]={0,0};
while(1)
{
step = bandpass(x,y);
a[0][1]=step+y[0][1] ;
printf(" Value %d \n \r", a[0][1]); //display
delay_ms(1000);
}
}
//=====================================================
int bandpass( int r[1][2] , int r2[1][2] )
{
int z=1; //
z = r[0][0]; //
printf(" Z %d \n \r", z); //display
return z;
} |
Using integers: Z=2; Value=6 That is correct.
USING FLOATING TYPE
Code: |
#include "30f3012.h" //DEVICE
#device adc=12
//*****************CHANGED INITAL dsPIC parameter to work at higher speed using the PLL
#FUSES HS2_PLL8//The xtal freq is divided by 2 and multiplied by 8 (maximum using 20MHz xtal)
#FUSES NOWDT // NO Watch Dog Timer
#FUSES PR_PLL //Primary Oscillator
#FUSES NOCKSFSM //Clock Switching is disabled, fail Safe clock monitor is disabled
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOWRT //Program memory not write protected
#use delay(clock=80000000)// 20MHz/2*8=80MHz=80000000
#use rs232(baud=115200, xmit=PIN_B4, rcv=PIN_B5) //To transmite result over Bluetooth module
//********************************
#define LED PIN_D0
//=========================================================
float bandpass(float r[1][2],float r2[1][2]);
//**************************************************************************************
//////////////////
//*****MAIN*****//
//////////////////
void main()
{
printf ("start");
float step = 0;
float x[1][2]={1,2};
float y[1][2]={3,4};
float a[1][2]={0,0};
while(1)
{
step = bandpass(x,y);
a[0][1]=step+y[0][1] ;
printf(" Value %f \n \r", a[0][1]); //display
delay_ms(1000);
}
}
//=====================================================
float bandpass( float r[1][2] , float r2[1][2] )
{
float z=1; //
z = r[0][0]; //
printf(" Z %f \n \r", z); //display
return z;
} |
Using float type
Z=0, Value =4 That is incorrect.
Is not possible to use float type arrays as arguments in a function?
There is a way to make it work?
Please, any idea is welcome! |
|