1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 #include "mbed_assert.h"
17 #include "analogout_api.h"
21 static const PinMap PinMap_DAC[] = {
26 void analogout_init(dac_t *obj, PinName pin) {
27 obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
28 MBED_ASSERT(obj->dac != (DACName)NC);
30 // power is on by default, set DAC clk divider is /4
31 LPC_SC->PCLKSEL0 &= ~(0x3 << 22);
33 // map out (must be done before accessing registers)
34 pinmap_pinout(pin, PinMap_DAC);
36 analogout_write_u16(obj, 0);
39 void analogout_free(dac_t *obj) {}
41 static inline void dac_write(int value) {
42 value &= 0x3FF; // 10-bit
45 LPC_DAC->DACR = (0 << 16) // bias = 0
49 static inline int dac_read() {
50 return (LPC_DAC->DACR >> 6) & 0x3FF;
53 void analogout_write(dac_t *obj, float value) {
56 } else if (value > 1.0f) {
59 dac_write(value * (float)0x3FF);
63 void analogout_write_u16(dac_t *obj, uint16_t value) {
64 dac_write(value >> 6); // 10-bit
67 float analogout_read(dac_t *obj) {
68 uint32_t value = dac_read();
69 return (float)value * (1.0f / (float)0x3FF);
72 uint16_t analogout_read_u16(dac_t *obj) {
73 uint32_t value = dac_read(); // 10-bit
74 return (value << 6) | ((value >> 4) & 0x003F);