Skip to content

Commit 5d1ad43

Browse files
author
Qinghao Shi
authored
Merge pull request #91 from maciejbocianski/move_analog_in_out_examples
add Analog In/Out examples
2 parents cfe874c + 50f0a20 commit 5d1ad43

File tree

10 files changed

+147
-0
lines changed

10 files changed

+147
-0
lines changed

APIs_Drivers/AnalogIn_ex_1/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# AnalogIn example
2+
3+
This example shows how to control an R/C servo with analog input.

APIs_Drivers/AnalogIn_ex_1/main.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (c) 2017-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
AnalogIn position(A0);
9+
PwmOut servo(D3);
10+
11+
int main()
12+
{
13+
// servo requires a 20ms period
14+
servo.period(0.020f);
15+
while (1) {
16+
// servo position determined by a pulse width between 1-2ms
17+
servo.pulsewidth(0.001f + 0.001f * position);
18+
}
19+
}

APIs_Drivers/AnalogIn_ex_2/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# AnalogIn example
2+
3+
This example shows how to sample analog signal, reading 16-bit normalized samples with AnalogIn.

APIs_Drivers/AnalogIn_ex_2/main.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2017-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
AnalogIn input(A0);
9+
10+
#define NUM_SAMPLES 1024
11+
12+
int main()
13+
{
14+
uint16_t samples[NUM_SAMPLES];
15+
16+
for (int i = 0; i < NUM_SAMPLES; i++) {
17+
samples[i] = input.read_u16();
18+
ThisThread::sleep_for(1);
19+
}
20+
21+
printf("Results:\n");
22+
for (int i = 0; i < NUM_SAMPLES; i++) {
23+
printf("%d, 0x%04X\n", i, samples[i]);
24+
}
25+
}

APIs_Drivers/AnalogIn_ex_3/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# AnalogIn example
2+
3+
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.

APIs_Drivers/AnalogIn_ex_3/main.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2017-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
// Initialize a pins to perform analog input and digital output functions
9+
AnalogIn ain(A0);
10+
DigitalOut dout(LED1);
11+
12+
int main(void)
13+
{
14+
while (1) {
15+
// test the voltage on the initialized analog pin
16+
// and if greater than 0.3 * VCC set the digital pin
17+
// to a logic 1 otherwise a logic 0
18+
if (ain > 0.3f) {
19+
dout = 1;
20+
} else {
21+
dout = 0;
22+
}
23+
24+
// print the percentage and 16 bit normalized values
25+
printf("percentage: %3.3f%%\n", ain.read() * 100.0f);
26+
printf("normalized: 0x%04X \n", ain.read_u16());
27+
ThisThread::sleep_for(200);
28+
}
29+
}

APIs_Drivers/AnalogOut_ex_1/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# AnalogOut example
2+
3+
This example shows how to generat a sine wave on analog output.
4+
5+
**Note:** Adjust the analog output pin name to your board specification.

APIs_Drivers/AnalogOut_ex_1/main.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2017-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
const double pi = 3.141592653589793238462;
9+
const double amplitude = 0.5f;
10+
const double offset = 65535 / 2;
11+
12+
// The sinewave is created on this pin
13+
// Adjust analog output pin name to your board spec.
14+
AnalogOut aout(A5);
15+
16+
int main()
17+
{
18+
double rads = 0.0;
19+
uint16_t sample = 0;
20+
21+
while (1) {
22+
// sinewave output
23+
for (int i = 0; i < 360; i++) {
24+
rads = (pi * i) / 180.0f;
25+
sample = (uint16_t)(amplitude * (offset * (cos(rads + pi))) + offset);
26+
aout.write_u16(sample);
27+
}
28+
}
29+
}

APIs_Drivers/AnalogOut_ex_2/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# AnalogOut example
2+
3+
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.
4+
5+
**Note:** Adjust the analog output pin name to your board specification.

APIs_Drivers/AnalogOut_ex_2/main.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2017-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
// Initialize a pins to perform analog and digital output functions
9+
// Adjust analog output pin name to your board spec.
10+
AnalogOut aout(A5);
11+
DigitalOut dout(LED1);
12+
13+
int main(void)
14+
{
15+
while (1) {
16+
// change the voltage on the digital output pin by 0.1 * VCC
17+
// and print what the measured voltage should be (assuming VCC = 3.3v)
18+
for (float i = 0.0f; i < 1.0f; i += 0.1f) {
19+
aout = i;
20+
printf("aout = %1.2f volts\n", aout.read() * 3.3f);
21+
// turn on the led if the voltage is greater than 0.5f * VCC
22+
dout = (aout > 0.5f) ? 1 : 0;
23+
ThisThread::sleep_for(1000);
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)