I'm currently trying to neaten up some code I've written and make elements of it more portable. The code I've written controls an ADC card (measurment computing) which strips data from camera's we've designed.
We use the functions supplied by the card manufacturer to read the data into an array and when the frames have been grabbed then write the data to a file.
The way the code is now means there is a lot of repetition,
Code:
UDStat = cbAIn (BoardNum, Chan0, Gain, &DataValueA); //sample channel 0
UDStat = cbToEngUnits (BoardNum, Gain, DataValueA, &EngUnitsA); //convert raw data to voltage
if (photoNumber==0)//nothing to average - load the array
{
DataArrayA[row][column] = EngUnitsA;
}
else //start averaging - (old value + new value)
{
DataArrayA[row][column] = (DataArrayA[row][column] + EngUnitsA); //
}
this has to be repeated for every channel. Now what I want to do is create a function with this code and just pass the channel number etc to the function, which is no problem.
However I'm a bit unsure as to hae to treat the array. I'm currently usin 4 ADC channels, so I want to call the funtion 4 times which requires four arrays. Can I pass an array to a function, fill the array in the function and then have access to the data outside the function?
My memory of C is a bit rusty, but i suspect this is going to require the passing of pointers.......gulp!
I hope I've got my question across clearly(ish) enough.
Hi,
As far as I know, if arrays are defined outside all functions and the Main routine, they would be treated as Global variables and would be available to be used / updated by any function.
Regards, Laktronics
Hi Old Nick,
As laktronics has already pointed out, you can use global arrays which will be accessible to all the functions. In this case you need to differentiate which array to be updated (according to the channel no.) in the function which does the averaging.
Another way would be to pass a pointer to the array you need to be updated as an argument to the function which does the averaging. In this case, you don't need to pass the channel no. The function simply does the necessary averaging and updates in the locations pointed to by the argument passed.