1
1
/*
2
- This example shows how to use the ADC library to sample several
3
- channels/pins in one go.
2
+ This example shows how to use the ADC library to sample several channels/pins in one go.
4
3
This example attaches an interrupt to the DMA completion to notify the user.
5
- The DMA must be set together with the start conversion, so both setDMA and startConversion
6
- Must be called.
7
4
8
5
*/
9
6
#include < STM32ADC.h>
10
7
11
- #define BOARD_LED D33 // PB0
8
+ #define BOARD_LED PC13
12
9
13
- uint8 pins[] = {11 , 10 , 9 , 8 , 7 , 6 , 5 , 4 };
10
+ uint8 pins[] = {PA0,PA1,PA2,PA3,PA4,PA5,PA6,PA7 };
14
11
15
12
const int maxSamples = 8 ; // 8 channels
16
13
@@ -21,46 +18,55 @@ volatile static bool dma1_ch1_Active; //flag for interrupt
21
18
22
19
STM32ADC myADC (ADC1);
23
20
24
- void setup () {
25
- // start Serial
26
- Serial.begin (19200 );
27
- // Start up blink from Pig-O-Scope
28
- pinMode (BOARD_LED, OUTPUT);
29
- pinMode (D32, INPUT);
30
- digitalWrite (BOARD_LED, HIGH);
31
- delay (1000 );
32
- digitalWrite (BOARD_LED, LOW);
33
- delay (1000 );
34
- // calibrate ADC.
35
- myADC.calibrate ();
36
-
37
- myADC.setSampleRate (ADC_SMPR_1_5); // sample ratio
38
- myADC.setPins (pins, maxSamples); // pins to be converted
39
- myADC.setScanMode (); // Set the ADC in Scan Mode
21
+ // -----------------------------------------------------------------------------
22
+ // Interrupt handler.
23
+ // -----------------------------------------------------------------------------
24
+ static void DMA1_CH1_Event ()
25
+ {
26
+ dma1_ch1_Active = 0 ;
40
27
}
28
+ // -----------------------------------------------------------------------------
29
+ void setup ()
30
+ {
31
+ // start Serial
32
+ Serial.begin (19200 );
33
+ pinMode (BOARD_LED, OUTPUT);
34
+ digitalWrite (BOARD_LED, HIGH);
41
35
42
- void loop (){
43
- // start acquisition on button push.
44
- if (digitalRead (D32) == 1 ) {
45
- Serial.println (" begin" );
46
- dma1_ch1_Active = 1 ;
47
- myADC.setDMA (dataPoints, 8 , (DMA_MINC_MODE | DMA_TRNS_CMPLT), DMA1_CH1_Event);
48
- myADC.startConversion ();
49
- while (dma1_ch1_Active == 1 ); // wait for DMA to complete.
50
-
51
- for (unsigned int i = 0 ; i < maxSamples; i ++) {
52
- Serial.print (" sample[" );
53
- Serial.print (i);
54
- Serial.print (" ] = " );
55
- Serial.println (dataPoints[i]);
56
- }
57
- while (digitalRead (D32) == 1 ); // stay here. Another button push is required.
58
- }
59
- }; // end loop
36
+ while (!Serial); delay (10 );
60
37
61
- /*
62
- Interrupt handler. eh eh
63
- */
64
- static void DMA1_CH1_Event () {
65
- dma1_ch1_Active = 0 ;
38
+ Serial.println (" MultiChannelSingleConversion demo started\n " );
39
+ // calibrate ADC.
40
+ myADC.calibrate ();
41
+ // setup ADC
42
+ myADC.setSampleRate (ADC_SMPR_1_5); // sample ratio
43
+ myADC.setPins (pins, maxSamples); // pins to be converted
44
+ myADC.setScanMode (); // Set the ADC in Scan Mode
45
+ myADC.setDMA (dataPoints, (DMA_MINC_MODE | DMA_TRNS_CMPLT), DMA1_CH1_Event);
46
+ }
47
+ // -----------------------------------------------------------------------------
48
+ void loop ()
49
+ {
50
+ // blink
51
+ digitalWrite (BOARD_LED, HIGH); // turn LED off
52
+ delay (1000 );
53
+ digitalWrite (BOARD_LED, LOW); // turn LED on
54
+
55
+ // start acquisition
56
+ Serial.println (" begin" );
57
+ dma1_ch1_Active = 1 ;
58
+
59
+ myADC.startDMA (maxSamples);
60
+ myADC.startConversion ();
61
+
62
+ while (dma1_ch1_Active == 1 ); // wait for DMA to complete.
63
+ // print out the sampled values
64
+ for (unsigned int i = 0 ; i < maxSamples; i ++) {
65
+ Serial.print (" sample[" );
66
+ Serial.print (i);
67
+ Serial.print (" ] = " );
68
+ Serial.println (dataPoints[i]);
69
+ }
70
+
71
+ delay (1000 );
66
72
}
0 commit comments