From 76115cb2f97d9e85ad21b7a795281724a42aca40 Mon Sep 17 00:00:00 2001 From: Ole Artz Date: Mon, 29 May 2017 11:04:43 +0200 Subject: [PATCH] Overwrite old ADC code with timer/interrupt based version --- atmega32u4/adc_9ch/adc_9ch.ino | 58 ++++++++++++------- .../adc_9channel/adc_9ch_1/adc_9ch_1.ino | 56 ------------------ 2 files changed, 36 insertions(+), 78 deletions(-) delete mode 100644 atmega32u4/adc_9channel/adc_9ch_1/adc_9ch_1.ino diff --git a/atmega32u4/adc_9ch/adc_9ch.ino b/atmega32u4/adc_9ch/adc_9ch.ino index 55261be..830af53 100644 --- a/atmega32u4/adc_9ch/adc_9ch.ino +++ b/atmega32u4/adc_9ch/adc_9ch.ino @@ -1,29 +1,39 @@ -// These constants won't change. They're used to give names -// to the pins used: -// const int analogInPin = A8; // Analog input pin that the potentiometer is attached to - const int numPins = 9; const int analogInPins[] = {A0, A1, A2, A3, A6, A7, A8, A9, A10}; -const float scaling[] = {4.267, 4.267, 4.267, 4.267, 4.267, 4.267, 4.267, 4.267, 4.267}; -/* -const int numPins = 1; -const int analogInPins[] = {A8}; -const float scaling[] = {4.267}; -*/ -int sensorValues[numPins]; // 0 - 1023 -float sensorVoltages[numPins]; // actual voltage on analog in pins -float scaledSensorVoltages[numPins]; // corrected (scaled) voltages before voltage divider +const float scaling[] = {4.267, 4.267, 4.267, 4.267, 4.267, 4.267, 4.267, 4.267, 4.267}; //potentional divider resistor factor +volatile int sensorValues[numPins]; // 0 - 1023 +float sensorVoltages[numPins]; // actual voltage on analog in pins +float scaledSensorVoltages[numPins]; // corrected (scaled) voltages before voltage divider +volatile int newValues = 0; // volatile: comand using to change variable by interupt void setup() { - // initialize serial communications at 9600 bps: - Serial.begin(9600); + // initialize serial communications at 11520 bps: + Serial.begin(115200); analogReference(INTERNAL); - } + // set clk/interrupt + cli(); // disable global interrupts + + TCCR3A = 0; // Timer/Counter3 Control Register A + TCCR3B = 0; // Timer/Counter3 Control Register B + TIMSK3 = 0; // Timer/Counter3 Interrupt Mask Register + + TCCR3A |= (1< 4000 + // 12.5 Hz -- OCR3A --> 20000 + // 5 Hz -- OCR3A --> 50000 + OCR3A = 50000; + + sei(); +} void loop() { - // read the analog in value: + while (newValues == 0) {} + newValues = 0; for (int i = 0; i < numPins; i++) { - sensorValues[i] = analogRead(analogInPins[i]); sensorVoltages[i] = sensorValues[i] * 2.56 / 1023.; scaledSensorVoltages[i] = scaling[i] * sensorVoltages[i]; //Serial.print(sensorValues[i]); @@ -34,9 +44,13 @@ void loop() { Serial.print(" "); } Serial.println(); +} - // wait 2 milliseconds before the next loop - // for the analog-to-digital converter to settle - // after the last reading: - delay(250); +ISR(TIMER3_COMPA_vect) +{ + for (int i = 0; i < numPins; i++) { + sensorValues[i] = analogRead(analogInPins[i]); + } + newValues = 1; } + diff --git a/atmega32u4/adc_9channel/adc_9ch_1/adc_9ch_1.ino b/atmega32u4/adc_9channel/adc_9ch_1/adc_9ch_1.ino deleted file mode 100644 index 830af53..0000000 --- a/atmega32u4/adc_9channel/adc_9ch_1/adc_9ch_1.ino +++ /dev/null @@ -1,56 +0,0 @@ -const int numPins = 9; -const int analogInPins[] = {A0, A1, A2, A3, A6, A7, A8, A9, A10}; -const float scaling[] = {4.267, 4.267, 4.267, 4.267, 4.267, 4.267, 4.267, 4.267, 4.267}; //potentional divider resistor factor -volatile int sensorValues[numPins]; // 0 - 1023 -float sensorVoltages[numPins]; // actual voltage on analog in pins -float scaledSensorVoltages[numPins]; // corrected (scaled) voltages before voltage divider -volatile int newValues = 0; // volatile: comand using to change variable by interupt - -void setup() { - // initialize serial communications at 11520 bps: - Serial.begin(115200); - analogReference(INTERNAL); - // set clk/interrupt - cli(); // disable global interrupts - - TCCR3A = 0; // Timer/Counter3 Control Register A - TCCR3B = 0; // Timer/Counter3 Control Register B - TIMSK3 = 0; // Timer/Counter3 Interrupt Mask Register - - TCCR3A |= (1< 4000 - // 12.5 Hz -- OCR3A --> 20000 - // 5 Hz -- OCR3A --> 50000 - OCR3A = 50000; - - sei(); -} - -void loop() { - while (newValues == 0) {} - newValues = 0; - for (int i = 0; i < numPins; i++) { - sensorVoltages[i] = sensorValues[i] * 2.56 / 1023.; - scaledSensorVoltages[i] = scaling[i] * sensorVoltages[i]; - //Serial.print(sensorValues[i]); - //Serial.print(" "); - //Serial.print(sensorVoltages[i], 4); - //Serial.print(" "); - Serial.print(scaledSensorVoltages[i], 3); - Serial.print(" "); - } - Serial.println(); -} - -ISR(TIMER3_COMPA_vect) -{ - for (int i = 0; i < numPins; i++) { - sensorValues[i] = analogRead(analogInPins[i]); - } - newValues = 1; -} - -- 2.43.0