Skip to content

Commit d12707c

Browse files
committed
MCU8MASS-1127 Add example with how to utilise the GPIO pins with DxCore
1 parent bbbe9c7 commit d12707c

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed

examples/gpio/gpio.ino

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* @brief This example demonstrates how to use the GPIO pins on the board, where
3+
* for the AVR-IoT Cellular board the pin naming is a bit different than in a
4+
* regular arduino environment.
5+
*/
6+
#include <Arduino.h>
7+
8+
/**
9+
* @brief Note that it is important that variables modified in an interrupt are
10+
* volatile, so that the compiler don't optimize them away.
11+
*/
12+
static volatile bool button_pressed = false;
13+
14+
/**
15+
* @brief Gets called when the SW0 button is pressed.
16+
*/
17+
static void buttonInterrupt(void) { button_pressed = true; }
18+
19+
void setup() {
20+
// The pins on the board are not named in a typical way as with standard
21+
// Arduino (with numbers), but with port and pin. The pinout can be seen in
22+
// the hardware user guide located at (figure 2-1, page 7):
23+
// https://ww1.microchip.com/downloads/en/DeviceDoc/AVR-IoT-Cellular-Mini-HW-UserGuide-DS50003320.pdf
24+
//
25+
// In order to modify GPIO pins, we need to use the light gray values showed
26+
// in the hardware user guide (e.g. PB2, which stands for port B and pin 2)
27+
28+
// To configure a pin as output (in this case PB2, which is the user led),
29+
// do the following:
30+
pinConfigure(PIN_PB2, PIN_DIR_OUTPUT);
31+
32+
// Then we can turn on the led by a regular digitalWrite (note that the LED
33+
// is active low)
34+
digitalWrite(PIN_PB2, LOW);
35+
36+
// In order to configure a pin as an input, for example the button SW0 at
37+
// PD2, we can do the following:
38+
pinConfigure(PIN_PD2, PIN_DIR_INPUT);
39+
40+
// Then we can e.g. attach an interrupt to the button when it is pressed (on
41+
// the falling edge). If we want to have an interrupt when the button is
42+
// released, we'd need to use the rising edge instead.
43+
//
44+
// We could also use digitalRead(PIN_PD2) in a loop in order to check the
45+
// value continuously
46+
attachInterrupt(PIN_PD2, buttonInterrupt, FALLING);
47+
48+
// We start the Serial3, which is used to print messages. As one can also
49+
// see on the same page in the hardware user guide, the USART3 is used for
50+
// sending messages to the debugger, which again is connected to the
51+
// computer via USB. We thus have to be careful to use Serial3 and not
52+
// Serial for printing
53+
Serial3.begin(115200);
54+
55+
// Analog functionality is quite similar, we can for example set up analog
56+
// reading of the voltage measurement pin on the board. As one can see in
57+
// the hardware user guide, this pin is PE0 (under power supply
58+
// connections).
59+
60+
// First we need to set the voltage measure pin (PB3) high to tell the
61+
// hardware that we want to read the supply voltage:
62+
pinConfigure(PIN_PB3, PIN_DIR_OUTPUT);
63+
digitalWrite(PIN_PB3, HIGH);
64+
65+
// Delay some to let the changes take effect
66+
delay(100);
67+
68+
// Now we can do a regular analog read. We do one analogRead first as the
69+
// result initially might be unstable and not settled.
70+
analogRead(PIN_PE0);
71+
72+
float analog_value = (float)analogRead(PIN_PE0);
73+
74+
// The analog value will be a value from 0 to 1023 (10 bit resolution), so
75+
// we divide by 1023 to get a value between 0 and 1:
76+
analog_value /= 1023.0f;
77+
78+
// The input pin runs on a logic level of 3.3 V, so we have to upscale by
79+
// that
80+
analog_value *= 3.3f;
81+
82+
// The voltage read is actually 1/4 of the true value since it is within a
83+
// voltage divider, so we have to multiply by 4 (look at page 32 in the
84+
// hardware user guide under VMUX Voltage Measure):
85+
analog_value *= 4.0f;
86+
87+
// You should see a voltage of approximately 4.8 V for when the board is
88+
// connected through USB (USB is delivering 5 V, but there are some
89+
// voltage drop over the components on the way to the voltage measurement
90+
// circuit)
91+
Serial3.print("The voltage supplied is: ");
92+
Serial3.println(analog_value);
93+
}
94+
95+
void loop() {
96+
97+
if (button_pressed) {
98+
Serial3.println("Button pressed");
99+
button_pressed = false;
100+
}
101+
}

src/low_power.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,12 +662,12 @@ float LowPowerClass::getSupplyVoltage(void) {
662662
}
663663

664664
// The default resolution is 10 bits, so divide by that to get the fraction
665-
// of VDD, which is 3.3 V
665+
// of VDD, which is 3.3 V (which logic level at the input pin is at)
666666
//
667667
// The voltage is in a voltage divider, and is divided by 4, so have to
668668
// multiply it up
669669
float value = 4.0f * 3.3f * ((float)analogRead(VOLTAGE_MEASURE_PIN)) /
670-
1024.0f;
670+
1023.0f;
671671

672672
return value;
673673
}

0 commit comments

Comments
 (0)