Prince Charming
Member level 2
Hello, I've struggled for hours but I couldn't manage to succeed. I need a good coders help.
ATtiny24A demo program:
Requests:
// Select ADC0 channel, Read ADC0
// Select ADC1 channel, Read ADC1
//Turn off ADC
// Turn on LED
// Wait as long as ADC0 value in milliseconds
// Turn off LED
// Wait as long as ADC1 value in milliseconds
// Turn on LED
// Wait as long as ADC0 value in milliseconds
// Turn off LED
// Wait as long as ADC1 value in milliseconds
// Wait for 3 seconds
--> Loop starts over from here
The code I've written so far is below. It reads pot for once but doesn't update in the next loops. Can you help me answer: Why does ADC is able to do only a single conversion?
my code:
ATtiny24A demo program:
Requests:
- There will be two potentiometers (ADC0 and ADC1) that is going to be read in an order for a single time, no constant conversions.
- This is a power saved system so the ADC will turn off after each conversion.
- In the "loop" part, potentiometers will be read for a single time. A led (on PA3 pin) will blink in the rate which is according to the read values.
- The sequence of loop:
// Select ADC0 channel, Read ADC0
// Select ADC1 channel, Read ADC1
//Turn off ADC
// Turn on LED
// Wait as long as ADC0 value in milliseconds
// Turn off LED
// Wait as long as ADC1 value in milliseconds
// Turn on LED
// Wait as long as ADC0 value in milliseconds
// Turn off LED
// Wait as long as ADC1 value in milliseconds
// Wait for 3 seconds
--> Loop starts over from here
The code I've written so far is below. It reads pot for once but doesn't update in the next loops. Can you help me answer: Why does ADC is able to do only a single conversion?
my code:
C:
#include <avr/io.h>
#include <avr/interrupt.h>
#define LEDButtonPin PA3
#define PotEnablePin PA2
#define pot_DEL_pin PA0
#define pot_DUR_pin PA1
volatile int analogResult=200;
void setup() {
}
void loop() {
PORTA |= (1 << LEDButtonPin);
delay(analogResult);
PORTA &= ~(1 << LEDButtonPin);
delay(analogResult);
PORTA |= (1 << LEDButtonPin);
delay(analogResult);
PORTA &= ~(1 << LEDButtonPin);
delay(3000);
readPots();
}
void readPots() {
DDRA &= ~(1 << pot_DUR_pin);
DDRA |= ((1 << LEDButtonPin) | (1 << PotEnablePin));
PORTA |= (1 << PotEnablePin); // power up POTs
delay(100);
ADMUX |= (1 << MUX0); // ADC1 (PA1) channel is selected
DIDR0 |= (1 << ADC1D); // Digital input disable to save power
ADCSRA |= (1 << ADPS1) | (1 << ADPS0); // lowers ADC clock by 1/8 of CPU clock
ADCSRA |= (1 << ADEN); // Turn on ADC
delay(100);
ADCSRA |= (1 << ADSC); //start convertion
while (ADCSRA & (1 << ADSC) ); //wait for conversion
analogResult = (ADCH << 8) | ADCL; // read val
ADCSRA &= ~(1 << ADEN); // Turn off ADC
ADCSRA &= ~(1 << ADIF); // clear flag
PORTA &= ~(1 << PotEnablePin); // power down POTs
}