Skip to content

add Analog In/Out examples #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions APIs_Drivers/AnalogIn_ex_1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# AnalogIn example

This example shows how to control an R/C servo with analog input.
19 changes: 19 additions & 0 deletions APIs_Drivers/AnalogIn_ex_1/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2017-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/

#include "mbed.h"

AnalogIn position(A0);
PwmOut servo(D3);

int main()
{
// servo requires a 20ms period
servo.period(0.020f);
while (1) {
// servo position determined by a pulse width between 1-2ms
servo.pulsewidth(0.001f + 0.001f * position);
}
}
3 changes: 3 additions & 0 deletions APIs_Drivers/AnalogIn_ex_2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# AnalogIn example

This example shows how to sample analog signal, reading 16-bit normalized samples with AnalogIn.
25 changes: 25 additions & 0 deletions APIs_Drivers/AnalogIn_ex_2/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2017-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/

#include "mbed.h"

AnalogIn input(A0);

#define NUM_SAMPLES 1024

int main()
{
uint16_t samples[NUM_SAMPLES];

for (int i = 0; i < NUM_SAMPLES; i++) {
samples[i] = input.read_u16();
ThisThread::sleep_for(1);
}

printf("Results:\n");
for (int i = 0; i < NUM_SAMPLES; i++) {
printf("%d, 0x%04X\n", i, samples[i]);
}
}
3 changes: 3 additions & 0 deletions APIs_Drivers/AnalogIn_ex_3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# AnalogIn example

This example showcases AnalogIn. Applying a voltage across the analog input pin causes the LED to light up. You can test this with a small wire connected to VCC.
29 changes: 29 additions & 0 deletions APIs_Drivers/AnalogIn_ex_3/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2017-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/

#include "mbed.h"

// Initialize a pins to perform analog input and digital output functions
AnalogIn ain(A0);
DigitalOut dout(LED1);

int main(void)
{
while (1) {
// test the voltage on the initialized analog pin
// and if greater than 0.3 * VCC set the digital pin
// to a logic 1 otherwise a logic 0
if (ain > 0.3f) {
dout = 1;
} else {
dout = 0;
}

// print the percentage and 16 bit normalized values
printf("percentage: %3.3f%%\n", ain.read() * 100.0f);
printf("normalized: 0x%04X \n", ain.read_u16());
ThisThread::sleep_for(200);
}
}
5 changes: 5 additions & 0 deletions APIs_Drivers/AnalogOut_ex_1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# AnalogOut example

This example shows how to generat a sine wave on analog output.

**Note:** Adjust the analog output pin name to your board specification.
29 changes: 29 additions & 0 deletions APIs_Drivers/AnalogOut_ex_1/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2017-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/

#include "mbed.h"

const double pi = 3.141592653589793238462;
const double amplitude = 0.5f;
const double offset = 65535 / 2;

// The sinewave is created on this pin
// Adjust analog output pin name to your board spec.
AnalogOut aout(A5);

int main()
{
double rads = 0.0;
uint16_t sample = 0;

while (1) {
// sinewave output
for (int i = 0; i < 360; i++) {
rads = (pi * i) / 180.0f;
sample = (uint16_t)(amplitude * (offset * (cos(rads + pi))) + offset);
aout.write_u16(sample);
}
}
}
5 changes: 5 additions & 0 deletions APIs_Drivers/AnalogOut_ex_2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# AnalogOut example

This example shows how to set a specified voltage level on analog output. In the example, voltage is repeatedly raised from 0V to VCC on the output pin in 0.1*VCC V increments. The voltage values are printed to console.

**Note:** Adjust the analog output pin name to your board specification.
26 changes: 26 additions & 0 deletions APIs_Drivers/AnalogOut_ex_2/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2017-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/

#include "mbed.h"

// Initialize a pins to perform analog and digital output functions
// Adjust analog output pin name to your board spec.
AnalogOut aout(A5);
DigitalOut dout(LED1);

int main(void)
{
while (1) {
// change the voltage on the digital output pin by 0.1 * VCC
// and print what the measured voltage should be (assuming VCC = 3.3v)
for (float i = 0.0f; i < 1.0f; i += 0.1f) {
aout = i;
printf("aout = %1.2f volts\n", aout.read() * 3.3f);
// turn on the led if the voltage is greater than 0.5f * VCC
dout = (aout > 0.5f) ? 1 : 0;
ThisThread::sleep_for(1000);
}
}
}