sonu2die4
Joined: 10 Aug 2009 Posts: 9
|
function Variable Passing Between Multiple C files [Solved] |
Posted: Sat Feb 20, 2010 4:43 pm |
|
|
Hi,
I am building an obstacle avoiding robot. I have tested various functionality of the peripherals. However I am having a tough time to pass an array to the main function. I get weird values in the main. However it is getting printed correctly in the Relevant function. Please find my code below. Please provide me with your feedback about pointer/array passing across these two files.
Thank You
Vivek
main.c
Code: |
#include <Robot.h>
#include "C:\My PICC Programs\Robot\IR.h"
#include "C:\My PICC Programs\Robot\ObstacleAvoidance.h"
#include "C:\My PICC Programs\Robot\GlobalVariables.h"
#include <string.h>
extern unsigned char IRfar;
extern unsigned char IRnear;
extern unsigned char IRmid;
extern unsigned long *IRSendNear;
extern unsigned long *IRSendMid;
extern unsigned long *IRSendFar;
unsigned long *IRSendNearReceive;
unsigned long *IRSendMidReceive;
unsigned long *IRSendFarReceive;
void main(void)
{
IR_Setup();
while(1)
{
Monitor(&IRSendNearReceive,&IRSendMidReceive,&IRSendFarReceive);
printf("\n\rMain Near :%c , Mid :%c , Far : %c",IRSendNearReceive[0],IRSendMidReceive[0], IRSendFarReceive[0]);
printf("\n\rMain Near :%Lu , Mid :%Lu , Far : %Lu",IRSendNearReceive[1],IRSendMidReceive[1], IRSendFarReceive[1]);
}
}
|
Obstacle Avoidance.c
Code: |
#include "Robot.h"
#include "IR.h"
#include "GlobalVariables.h"
unsigned long IR_Sensor_A;
unsigned long IR_Sensor_B;
unsigned long IR_Sensor_C;
unsigned char IRfar;
unsigned char IRnear;
unsigned char IRmid;
unsigned long *IRSendNear;
unsigned long *IRSendMid;
unsigned long *IRSendFar;
void Monitor( unsigned long *IRSendNear,unsigned long *IRSendMid, unsigned long *IRSendFar)
{
IR_Sensor_A=IR_Read(IR_A); //Reads a long value from the sensor function .Working Fine.
IR_Sensor_B=IR_Read(IR_B); //Reads a long value from the sensor function .Working Fine.
IR_Sensor_C=IR_Read(IR_C); //Reads a long value from the sensor function .Working Fine.
IRSendNear=near(IR_Sensor_A , IR_Sensor_B , IR_Sensor_C); // This function returns an array[2]
IRSendMid=mid(IR_Sensor_A , IR_Sensor_B , IR_Sensor_C);// This function returns an array[2]
IRSendFar=far(IR_Sensor_A ,IR_Sensor_B , IR_Sensor_C);// This function returns an array[2]
printf("\n\rNear :%c , Mid :%c , Far : %c",IRSendNear[0],IRSendMid[0], IRSendFar[0]);
printf("\n\rNear :%Lu , Mid :%Lu , Far : %Lu",IRSendNear[1],IRSendMid[1], IRSendFar[1]);
}
|
The output:
Quote: |
Near :C , Mid :B , Far : A // This is the expected result . It comes from Obstacle Avoidance.c
Near :267 , Mid :150 , Far : 4 // This is the expected result . It comes from Obstacle Avoidance.c
Main Near : , Mid :– , Far : // This is the unexpected result . It comes from main.c
Main Near :6346 , Mid :267 , Far : 6374 // This is the unexpected result . It comes from main.c |
|
|