1
\$\begingroup\$

I am new to programming. I have small doubt, I know this is simple question but I am confused. I have the following function:

void ReadAdConfReg(void)
{              
    SPCR = 0x5D;
    ADC_CS = 0;      
    while (MISO_PIN != 0) ;
    spi(0x50);
    adcConfig = spi(0xFF);    
    adcConfig = (adcConfig << 8) | spi(0xFF);  
    ADC_CS = 1;    
}

I have declared adcConfig as global variable: unsigned int adcConfig.

How can I print the adcConfig value. Because this is a void function, it doesn't return any value.

I have tried like this:

ReadAdConfReg();
printf("configreg:%d",adcConfig);

Is it wrong? Or how can I print adcConfig value.

Controller is ATMega32A, compiler CodeVisionAVR.

\$\endgroup\$
  • \$\begingroup\$ @stevenvh I am using ATmega32-A controller and CodeVisionAVR compiler. \$\endgroup\$ – verendra Oct 15 '12 at 12:10
  • \$\begingroup\$ Have you got any communications to a PC or VDU to display the value? \$\endgroup\$ – Dean Oct 15 '12 at 12:14
  • \$\begingroup\$ @Dean yes I am printing values using usart communication(PuTTy). \$\endgroup\$ – verendra Oct 15 '12 at 12:29
0
\$\begingroup\$

If need the function to return a value it can't be declared void. As you have it now you have to call the function first, then pass the global variable into the printf function. So what you are doing should work.

To use the function with a return value, do something like this:

unsigned int ReadAdConfReg(void)
{        
    unsigned int retVal;
    SPCR = 0x5D;
    ADC_CS = 0;      
    while (MISO_PIN != 0) ;
    spi(0x50);
    adcConfig = spi(0xFF);    
    adcConfig = (adcConfig << 8) | spi(0xFF);  
    ADC_CS = 1;
    return acdConfig;    
}

(note - the previous edit used a local retVal, whilst leaving the adcConfig variable present which may have been confusing. You can pass the adcConfig directly as it's the correct type. If you want the function to be independent of the adcConfig though, use the local retVal instead in place of the adcConfig - this is probably better than using a global variable)

Then you can do:

printf("configreg:%d", ReadAdcConfigReg());
\$\endgroup\$
  • \$\begingroup\$ You're not going to be able to return a 16-bit value as an unsigned char. \$\endgroup\$ – Dave Tweed Oct 15 '12 at 12:07
  • \$\begingroup\$ @DaveTweed - true, I thought it was an 8-bit value looking at only the first spi(0xFF) value :-) Will alter it now. \$\endgroup\$ – Oli Glaser Oct 15 '12 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.