Example of AnalogOut.

Repeatedly raise voltage from 0V to VCC on output pin in 0.1*VCC V increments. The voltage values are printed to console.

Committer:
mab5449
Date:
Thu Jan 19 10:26:04 2017 -0600
Revision:
5:a32148e02ecf
Parent:
0:df98cf0f64e4
Ported mbed OS 2 to mbed OS 5

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sam_grove 0:df98cf0f64e4 1 /* mbed Example Program
sam_grove 0:df98cf0f64e4 2 * Copyright (c) 2006-2015 ARM Limited
sam_grove 0:df98cf0f64e4 3 *
sam_grove 0:df98cf0f64e4 4 * Licensed under the Apache License, Version 2.0 (the "License");
sam_grove 0:df98cf0f64e4 5 * you may not use this file except in compliance with the License.
sam_grove 0:df98cf0f64e4 6 * You may obtain a copy of the License at
sam_grove 0:df98cf0f64e4 7 *
sam_grove 0:df98cf0f64e4 8 * http://www.apache.org/licenses/LICENSE-2.0
sam_grove 0:df98cf0f64e4 9 *
sam_grove 0:df98cf0f64e4 10 * Unless required by applicable law or agreed to in writing, software
sam_grove 0:df98cf0f64e4 11 * distributed under the License is distributed on an "AS IS" BASIS,
sam_grove 0:df98cf0f64e4 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sam_grove 0:df98cf0f64e4 13 * See the License for the specific language governing permissions and
sam_grove 0:df98cf0f64e4 14 * limitations under the License.
sam_grove 0:df98cf0f64e4 15 */
sam_grove 0:df98cf0f64e4 16
sam_grove 0:df98cf0f64e4 17 #include "mbed.h"
sam_grove 0:df98cf0f64e4 18
sam_grove 0:df98cf0f64e4 19 // Initialize a pins to perform analog and digital output fucntions
mab5449 5:a32148e02ecf 20 AnalogOut aout(A5);
sam_grove 0:df98cf0f64e4 21 DigitalOut dout(LED1);
sam_grove 0:df98cf0f64e4 22
sam_grove 0:df98cf0f64e4 23 int main(void)
sam_grove 0:df98cf0f64e4 24 {
sam_grove 0:df98cf0f64e4 25 while (1) {
sam_grove 0:df98cf0f64e4 26 // change the voltage on the digital output pin by 0.1 * VCC
sam_grove 0:df98cf0f64e4 27 // and print what the measured voltage should be (assuming VCC = 3.3v)
sam_grove 0:df98cf0f64e4 28 for (float i = 0.0f; i < 1.0f; i += 0.1f) {
sam_grove 0:df98cf0f64e4 29 aout = i;
sam_grove 0:df98cf0f64e4 30 printf("aout = %1.2f volts\n", aout.read() * 3.3f);
sam_grove 0:df98cf0f64e4 31 // turn on the led if the voltage is greater than 0.5f * VCC
sam_grove 0:df98cf0f64e4 32 dout = (aout > 0.5f) ? 1 : 0;
sam_grove 0:df98cf0f64e4 33 wait(1.0f);
sam_grove 0:df98cf0f64e4 34 }
sam_grove 0:df98cf0f64e4 35 }
sam_grove 0:df98cf0f64e4 36 }