ADC / ADCW registers, true or myth ?

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

So I wrote the following function to get an ADC read in a program on an ATtiny13(a)

uint16_t adc_result (void) //return the current ADC value, the function will wait until the a conversion has ended.
{
	uint16_t result;
	
	while( ADCSRA & (1<<ADSC));   // Wait until ADC conversion is complete..
	
	result = ADCW;
		
	return result;
}

I'm not getting expected behavior from my program and I think the culprit is dodgy ADC values because of my use of ADCW. Now someone told me that ADC or ADCW was something built into the compiler that I could use to automatically get the 16 bit reading of both ADCH and ADCL registers in one 16 bit value. Is this the case or should i read the ADCL/H registers and add them myself ?

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

Any compiler that provides ADC/ADCW (I know both GCC and CodeVision do, maybe Imagecraft too? ) surely arrange to read in the datasheet dictated order? GCC does and it works well. Your routine waits on ADSC but never sets it in the first place?

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

yes I have updated the routine to include the setting of ADSC as that was one of my first errors. the project is now working

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

In ICC I've always used just ADC. Not sure if ADCW is defined.

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

I think that you will find that ADCW 'works' with GCC, Rowley, ImageCraft, IAR, CodeVision.

ADC does not work with every compiler.

Of course using 8-bit ADCH, ADCL is guaranteed to work every time but is unlikely to be as convenient as using the 16-bit ADC/ADCW.

David.

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

Both ADC and ADCW are declared and point to the same registers

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

In SOME compilers you mean?

You haven't said which one you use but I suspect it's avr-gcc you are talking about?

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

Err yes i think so, Atmel Studio 6.2 as far as i know it uses avr-gcc

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

I'm intrigued. How does anyone manage to use a compiler without knowing which one it is? Doesn't this make locating the user manual on the Internet a little tricky?

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

OK I'm sure it's GCC !

Oh and do i have to keep filling in this blasted catchpa ?

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

What is the problem?

Write a general purpose function to :

uint16_t adc_read(uint8_t channel);

Then you can simply re-use it whenever you want. Note that different models of AVR can use 8, 16, 32, ... channels. So you just need to set the ADMUX bits. Start the conversion. Wait for completion. Return ADCW.

David.