Unable to pass 16 bit value from another C file

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have written code for ADC interfacing for mega8. I put ADC subroutines in separate file "ADC_routines_m8.c" & properly added to project. Put function prototypes in header & included in main too. I am using 10 bits of resolution with default Right-adjusted result.

Now, the problem I am getting is that whenever I call the function

ADC_convert(channel);  //channel:0..5

which is supposed to return 16 bit ADC value, It only returns 8 bit value! even though the return type is 16 bit declared everywhere.
Forget the ADC, even if I write:
return 0x1FE; , it prints only 0xFE on the LCD(in main).

The prob is not with the ADC, but with the return type. If I put the file's contents (& prototypes) in main, it works fine (10bit ADC or 0x1FE are printed).

What can be wrong? Any help would be greatly appreciated.

-
Regards,
Munish

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Since you are dead sure that it is not the setup of the ADC that is at fault, that leave the other parts of the code. Without us having seen it, the usual solution is to look at line 42 (some will point to line 47). :wink:

Show us the function prototype and the function definition (implementation).

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You might try reading Help, My "..." Doesn't Work! before posting again.

Stu

Engineering seems to boil down to: Cheap. Fast. Good. Choose two. Sometimes choose only one.

Newbie? Be sure to read the thread Newbie? Start here!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I suppose that was a glitch with AVR studio. The next day, I compiled the same program with no change than before, & it returned 16 bit value.

function prototype:

uint16_t ADC_convert(uint8_t channel);

function implementation:

/*--------------------------------------------------------------------------
	ADC_convert()	
	I/P param: channel: can be 0...7 (0 to 5 in mega8)
	O/P param: 10-bit ADC value.
---------------------------------------------------------------------------*/
uint16_t ADC_convert(uint8_t channel)
{
//	uint16_t result;
//	ADMUX |= (1<<ADLAR);	//LEFT ADJUST RESULT
	ADMUX=(ADMUX & 0xF0)|channel;
	ADCSRA |= (1<<ADSC);	//start conversaion, cleared automatically
	while(!(ADCSRA & (1<<ADIF)));	//wait for conversion to complete
//	result = ADCL;
//	result += (ADCH<<8);
	return ADC;  //result;
}

Quote:
You might try reading Help, My "..." Doesn't Work! before posting again.
Stu

Perhaps you have mistaken "Any help would be greatly appreciated" for "Help, My "..." Doesn't Work!" You should have read my whole post instead of just the last lines to be of any real help. Then you would have known that I was only asking why the function works only when included in main.c and not otherwise. I was asking if there were some special precautions/caveats while putting code in submodule files. AND if I were unsure about the function prototype & implementation, then I would have posted them in first place.

I think I should really have asked "has anyone experienced this before?" than "What can be wrong?" Ofcourse if I dont know what can be wrong, how can you? The only way one can know is if he has encountered/heard about it before. And that was all I asked. :?

-
Regards,
Munish

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Perhaps you have mistaken "Any help would be greatly appreciated" for "Help, My "..." Doesn't Work!" You should have read my whole post instead of just the last lines to be of any real help.
Perhaps you are mistaken as to the point of the "Help, My "..." Doesn't Work!" thread. The point of that thread is that if you don't include enough information in your question, then you are not likely to get an answer. You provided very little information in your post, so you got no answer. Lesson learned.
Quote:
AND if I were unsure about the function prototype & implementation
And since your so sure of your code, then I won't bother pointing out the error in it.

Regards,
Steve A.

The Board helps those that help themselves.