]> jspc29.x-matter.uni-frankfurt.de Git - avr.git/commitdiff
Overwrite old ADC code with timer/interrupt based version
authorOle Artz <ole.artz@t-online.de>
Mon, 29 May 2017 09:04:43 +0000 (11:04 +0200)
committerOle Artz <ole.artz@t-online.de>
Mon, 29 May 2017 09:04:43 +0000 (11:04 +0200)
atmega32u4/adc_9ch/adc_9ch.ino
atmega32u4/adc_9channel/adc_9ch_1/adc_9ch_1.ino [deleted file]

index 55261be65ef4b07c447333a1510b604f3b3fb843..830af53ec3fbeefabdaeb3d6f276cccd3dd56a71 100644 (file)
@@ -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<<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() {
-  // 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 (file)
index 830af53..0000000
+++ /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<<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;
-}