EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

AVR-GCC ADC example source

Started by Andrew Tweddle April 21, 2006
I have been looking on the web for AVR-GCC example sources on using the 
ADC converter in the ATmega32 or a related processor, to my 
disappointment, I have only found the Atmel AVR465: Energy meter 
example, are there any other examples both simple and complex on setting 
up and using this peripheral?

Andrew
In article <44484c48$1_4@news.chariot.net.au>,
Andrew Tweddle  <sarason_not_me@alphalink.com.au> wrote:
>I have been looking on the web for AVR-GCC example sources on using the >ADC converter in the ATmega32 or a related processor, to my >disappointment, I have only found the Atmel AVR465: Energy meter >example, are there any other examples both simple and complex on setting >up and using this peripheral?
This is from a simple application I use to decode the WWVB time codes. It is using the GCC AVR tool chain. It samples the amplified and filtered analog signal at 256 Hz to decode the time signal. This is part of the initialization code: // // Setup A/D, use AVCC for ref, ADC0 as input, left adjust value // Prescale CPU clock by 64 => 2,400 samp/sec max // ADMUX = _BV(REFS1) | _BV(REFS0) | _BV(ADLAR); ADCSRA = _BV(ADEN) | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0); Later on: void GetTime(void) { uint8_t prev; uint8_t s0; uint8_t s1; uint8_t s2; uint8_t s3; uint8_t sf; uint16_t t16; EdgeState estate; Write_P(PSTR("GetTime started\n")); // // Prime the pump // ADCSRA |= _BV(ADEN); ADCSRA |= _BV(ADSC); while (bit_is_set(ADCSRA, ADSC)) /**/; s0 = s1 = s2 = ADCH; prev = now = TCNT0; estate = EdgeDetect(EPrep, 0); while (estate != Done) { // // Wait for the next tick // while (prev == now) { now = TCNT0; } prev = now; // // Get the next sample // ADCSRA |= _BV(ADSC); while (bit_is_set(ADCSRA, ADSC)) /**/; s3 = ADCH; // // Filter the noise // t16 = s1 + 2 * s2 + s3; sf = t16 / 4; estate = EdgeDetect(estate, sf - s0); s0 = s1; s1 = s2; s2 = sf; } ADCSRA &= ~_BV(ADEN); }
Craig Ruff wrote:


> void > GetTime(void) > { > uint8_t prev; > uint8_t s0; > uint8_t s1; > uint8_t s2; > uint8_t s3; > uint8_t sf; > uint16_t t16; > EdgeState estate; >
Thankyou for the prompt reply! Next question. What is edgestate, a simple boolean value? Andrew
In article <44486405$1_1@news.chariot.net.au>,
Andrew Tweddle  <sarason_not_me@alphalink.com.au> wrote:
>Thankyou for the prompt reply! >Next question. What is edgestate, a simple boolean value?
typedef enum { EPrep, Rise, Fall, Done } EdgeState; Forgot to mention that this is running on a ATmega128. The WWVB time code is PWM encoded in the power level of the 60 KHz carrier signal. The codes represent a position marker 'P', a binary 0 and binary 1. The EdgeDetect routine is looking for the transitions of the filtered signal. The way the analog circuitry is wired, the default state of the A/D values is to be at a higher number, so I sync up by looking for the higher numbers, then watch for a falling edge whcih represents the start of the second and the code. The amount of time the signal is at a lower number represents which code is being sent: 0.2s = 0 0.5s = 1 0.8s = P A more detailed description of WWVB can be found at: http://tf.nist.gov/stations/wwvb.htm
Andrew: many examples and helpful friendly members at avrfreaks.net

BobG wrote:

> Andrew: many examples and helpful friendly members at avrfreaks.net >
The people might well be friendly but the interface drives me crazy!.It would have to be one of the worst web interfaces with all that redrawing every time you look at a new thread or message. Just a BG Bandwidth hog and absolutely no advantage to their advertising or thier user base, beats me? But thanks anyway, so endith the gripe. Andrew
What the regulars do is hit the 'maximize' button on the menu, and the
ads go away.

BobG wrote:

> What the regulars do is hit the 'maximize' button on the menu, and the > ads go away. >
Thankyou!

The 2024 Embedded Online Conference