-
Notifications
You must be signed in to change notification settings - Fork 0
/
HardwareAveraging.ino
111 lines (94 loc) · 3.08 KB
/
HardwareAveraging.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Arduino-Apollo3ADC is an Arduino Library for the ADC module on the
* Ambiq Apollo3 Blue MCU.
* Copyright (C) 2021 eResearch, James Cook University
* Author: NigelB
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* Repository: https://github.com/jcu-eresearch/Arduino-Apollo3ADC
*
*/
#include "Arduino.h"
#include "Arduino-Apollo3ADC.h"
Apollo3ADC *adc;
Apollo3ADC_Slot *slot;
void setup()
{
Serial.begin(115200);
Serial.println("Starting... ");
adc = new Apollo3ADC();
/*
Valid values for setReference:
AM_HAL_ADC_REFSEL_INT_2P0
AM_HAL_ADC_REFSEL_INT_1P5
AM_HAL_ADC_REFSEL_EXT_2P0
AM_HAL_ADC_REFSEL_EXT_1P5
*/
adc->setReference(AM_HAL_ADC_REFSEL_INT_2P0);
// Valid values are Apollo3ADC_Slot_0 through Apollo3ADC_Slot_7
slot = adc->getADCSlot(Apollo3ADC_Slot_1);
/*
Valid values for setAveragingFreq:
AM_HAL_CTIMER_CLK_PIN
AM_HAL_CTIMER_HFRC_12MHZ
AM_HAL_CTIMER_HFRC_3MHZ
AM_HAL_CTIMER_HFRC_187_5KHZ
AM_HAL_CTIMER_HFRC_47KHZ
AM_HAL_CTIMER_HFRC_12KHZ
AM_HAL_CTIMER_XT_32_768KHZ
AM_HAL_CTIMER_XT_16_384KHZ
AM_HAL_CTIMER_XT_2_048KHZ
AM_HAL_CTIMER_XT_256HZ
AM_HAL_CTIMER_LFRC_512HZ
AM_HAL_CTIMER_LFRC_32HZ
AM_HAL_CTIMER_LFRC_1HZ
AM_HAL_CTIMER_LFRC_1_16HZ
AM_HAL_CTIMER_RTC_100HZ
AM_HAL_CTIMER_HCLK_DIV4
AM_HAL_CTIMER_XT_DIV4
AM_HAL_CTIMER_XT_DIV8
AM_HAL_CTIMER_XT_DIV32
*/
adc->setAveragingFreq(AM_HAL_CTIMER_LFRC_32HZ);
adc->setAveragingPeriod(10);
adc->setAveragingOnTime(5);
/*
Valid values for setAveraging:
AM_HAL_ADC_SLOT_AVG_1
AM_HAL_ADC_SLOT_AVG_2
AM_HAL_ADC_SLOT_AVG_4
AM_HAL_ADC_SLOT_AVG_8
AM_HAL_ADC_SLOT_AVG_16
AM_HAL_ADC_SLOT_AVG_32
AM_HAL_ADC_SLOT_AVG_64
AM_HAL_ADC_SLOT_AVG_128
*/
slot->setAveraging(AM_HAL_ADC_SLOT_AVG_2);
// Valid values are AM_HAL_ADC_SLOT_8IT, AM_HAL_ADC_SLOT_10BIT, AM_HAL_ADC_SLOT_12BIT, AM_HAL_ADC_SLOT_14BIT
slot->setPrecision(AM_HAL_ADC_SLOT_14BIT);
/*
Valid values for setClock:
AM_HAL_ADC_CLKSEL_HFRC
AM_HAL_ADC_CLKSEL_HFRC_DIV2
*/
adc->setClock(AM_HAL_ADC_CLKSEL_HFRC);
}
void loop()
{
long start = micros();
int32_t val = slot->readAnalog(A29);
long res = micros() - start;
Serial.printf("[TS: %lu ms] [Sample Time: %lu us] Reading: %i\r\n", millis(), res, val);
}