returing a value from a routine

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

I have a routine I use to read different ADCs. Right now I'm storeing the result in a variable and using the variable afterwards in the Main section.


uint16_t adcval = 0;
uint16_t adc1 = 0;
uint16_t adc2 = 0;


void readADC(uint8_t newadmux)
{
  ADMUX = newadmux;
  // read ADC several times
  // adding the values of each read.
  adcval = [summed and averaged ADC values];
}

main()
{

  readADC(0x03);

  adc1 = adcval;

  readADC(0x82);

  adc2 = adcval;

  // remainder of code
}

Is there a better way to return the result of the readADC() routine?

Also, do I need to declare the adc1 and adc2 variables as globals or should it more properly be done in the main() section like below.

 uint16_t adc1 = adcval;

Thanks for your help. I'm rubbish when it comes to C.

Jim M., Rank amateur AVR guy.

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

grab yourself a copy of K&R's C programming.
See the tutorials here by Smiley and in the last few issues of "Nuts 'n Volts" magazine

Consider this

uint16_t readADC(uint8_t newadmux)
{
  uint16_t ret;

  ADMUX = newadmux;
  // read ADC several times
  // adding the values of each read.
  ret = [summed and averaged ADC values];
  return(ret);
}

void main(void)  {
  uint16_t adc;

  adc = readADC(3);
  // more code
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Also, do I need to declare the adc1 and adc2 variables as globals or should it more properly be done in the main() section like below.

Declare them where it is convenient for you.

Regards,
Steve A.

The Board helps those that help themselves.

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

Thanks, both.

I'll have a look at those references.

Jim M., Rank amateur AVR guy.