Skip to content

Commit 063c5a4

Browse files
committed
Add example
1 parent fbfdb25 commit 063c5a4

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Using the differential ADC
3+
By: Nathan Seidle
4+
SparkFun Electronics
5+
Date: July 17th, 2020
6+
License: This code is public domain. Based on deepsleep.c from Ambiq SDK v2.2.0.
7+
8+
There are two differential ADC ports on the Artemis. These are located
9+
on pads 12/13 (DIFF0N/DIFF0P) and 14/15 (DIFF1P/DIFF10).
10+
11+
A differential ADC port measures -1.0V to 1.0V with
12+
0 = -1.0V, 8192 = 0V, and 16383 = 1.0V when the ADC is in 14-bit mode.
13+
14+
SparkFun labored with love to create this code. Feel like supporting open source hardware?
15+
Buy a board from SparkFun! https://www.sparkfun.com/products/15376
16+
*/
17+
18+
#define ANALOG_RESOLUTION 14 //Artemis has 14 bit ADC but can range from 8 to 14 bit
19+
20+
int maxADCValue = pow(2, ANALOG_RESOLUTION);
21+
22+
void setup()
23+
{
24+
Serial.begin(115200);
25+
Serial.println("Differential analog conversion example");
26+
27+
pinMode(LED_BUILTIN, OUTPUT);
28+
29+
analogReadResolution(ANALOG_RESOLUTION); //Default is 10 bit. Increase to 14 bit.
30+
}
31+
32+
void loop()
33+
{
34+
int myAnalog0 = analogRead(ADIFF0); //Pads 12/13. Pins 9/10 on RedBoard Artemis.
35+
//int myAnalog0 = analogRead(ADIFF1); //Pads 14/15
36+
float myVoltage0 = mapfloat(myAnalog0, 0, maxADCValue, -1.0, 1.0);
37+
Serial.print("Diff0 voltage: ");
38+
Serial.print(myVoltage0, 3);
39+
Serial.println();
40+
41+
digitalWrite(LED_BUILTIN, HIGH);
42+
delay(25);
43+
digitalWrite(LED_BUILTIN, LOW);
44+
delay(25);
45+
}
46+
47+
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
48+
{
49+
if (x > in_max)
50+
x = in_max;
51+
52+
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
53+
}

0 commit comments

Comments
 (0)