Tell me more ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

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.

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

1 Answer

up vote 0 down vote accepted

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());
share|improve this answer
You're not going to be able to return a 16-bit value as an unsigned char. – Dave Tweed Oct 15 '12 at 12:07
@DaveTweed - true, I thought it was an 8-bit value looking at only the first spi(0xFF) value :-) Will alter it now. – Oli Glaser Oct 15 '12 at 12:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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