AnalogOut

Table of Contents

  1. API
  2. Hello World!
  3. Example

mbed OS 2 and mbed OS 5

This is the handbook for mbed OS 2. If you’re working with mbed OS 5, please see the new handbook and API References. For the latest AnalogOut API, please see AnalogOut.

The AnalogOut Interface is used to set the voltage of an analog output pin. The AnalogOut Interface can be used to set a voltage on pin somewhere in the range of VSS to VCC. Not all pins are capable of being AnalogOut so check the documentation. For more information on what it takes to convert an digital value to its analog representation see: http://en.wikipedia.org/wiki/Digital-to-analog_converter


API

Import library

Public Member Functions

  AnalogOut (PinName pin)
  Create an AnalogOut connected to the specified pin.
void  write (float value)
  Set the output voltage, specified as a percentage (float)
void  write_u16 (unsigned short value)
  Set the output voltage, represented as an unsigned short in the range [0x0, 0xFFFF].
float  read ()
  Return the current output voltage setting, measured as a percentage (float)
AnalogOut operator= (float percent)
  An operator shorthand for write()
AnalogOut operator= ( AnalogOut &rhs)
  An operator shorthand for write()
  operator float ()
  An operator shorthand for read()



Hello World!

Import program

00001 /* mbed Example Program
00002  * Copyright (c) 2006-2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 
00019 // Initialize a pins to perform analog and digital output fucntions
00020 AnalogOut  aout(p18);
00021 DigitalOut dout(LED1);
00022 
00023 int main(void)
00024 {
00025     while (1) {
00026         // change the voltage on the digital output pin by 0.1 * VCC
00027         //  and print what the measured voltage should be (assuming VCC = 3.3v)
00028         for (float i = 0.0f; i < 1.0f; i += 0.1f) {
00029             aout = i;
00030             printf("aout = %1.2f volts\n", aout.read() * 3.3f);
00031             // turn on the led if the voltage is greater than 0.5f * VCC
00032             dout = (aout > 0.5f) ? 1 : 0;
00033             wait(1.0f);
00034         }
00035     }
00036 }



Example

Create a sine wave

#include "mbed.h"

// The sinewave is created on this pin
AnalogOut aout(p18);

int main()
{
    const double pi = 3.141592653589793238462;
    const double amplitude = 0.5f;
    const double offset = 65535/2;
    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);
        }
    }
}