]> jspc29.x-matter.uni-frankfurt.de Git - avr.git/commitdiff
9ch ADC board: add interrupt for periodical sampling
authorOle Artz <ole.artz@t-online.de>
Fri, 19 May 2017 07:32:40 +0000 (09:32 +0200)
committerOle Artz <ole.artz@t-online.de>
Fri, 19 May 2017 07:32:40 +0000 (09:32 +0200)
atmega32u4/adc_9channel/adc_9ch_1/adc_9ch_1.ino [new file with mode: 0644]

diff --git a/atmega32u4/adc_9channel/adc_9ch_1/adc_9ch_1.ino b/atmega32u4/adc_9channel/adc_9ch_1/adc_9ch_1.ino
new file mode 100644 (file)
index 0000000..830af53
--- /dev/null
@@ -0,0 +1,56 @@
+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<<COM3A0); // Toggle OC3A on compare match
+  TCCR3B |= (1<<WGM32); // turn on CTC-Mode // Mode 4: CTC-Mode mit OCR3A als TOP
+  TCCR3B |= (1<<CS31) | (1<<CS30); // Pre-Scaler N=64
+  TIMSK3 |= (1<<OCIE3A); // Output Compare A Match Interrupt Enable
+   
+  // 62.5 Hz  --  OCR3A -->  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;
+}