Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
analogout_api.c
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 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 #include "analogout_api.h" 00017 00018 #if DEVICE_ANALOGOUT 00019 00020 #include "cmsis.h" 00021 #include "pinmap.h" 00022 #include "error.h" 00023 00024 static const PinMap PinMap_DAC[] = { 00025 {P0_26, DAC_0, 2}, 00026 {NC , NC , 0} 00027 }; 00028 00029 void analogout_init(dac_t *obj, PinName pin) { 00030 obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); 00031 if (obj->dac == (uint32_t)NC) { 00032 error("DAC pin mapping failed"); 00033 } 00034 00035 // power is on by default, set DAC clk divider is /4 00036 LPC_SC->PCLKSEL0 &= ~(0x3 << 22); 00037 00038 // map out (must be done before accessing registers) 00039 pinmap_pinout(pin, PinMap_DAC); 00040 00041 analogout_write_u16(obj, 0); 00042 } 00043 00044 void analogout_free(dac_t *obj) {} 00045 00046 static inline void dac_write(int value) { 00047 value &= 0x3FF; // 10-bit 00048 00049 // Set the DAC output 00050 LPC_DAC->DACR = (0 << 16) // bias = 0 00051 | (value << 6); 00052 } 00053 00054 static inline int dac_read() { 00055 return (LPC_DAC->DACR >> 6) & 0x3FF; 00056 } 00057 00058 void analogout_write(dac_t *obj, float value) { 00059 if (value < 0.0) { 00060 dac_write(0); 00061 } else if (value > 1.0) { 00062 dac_write(0x3FF); 00063 } else { 00064 dac_write(value * (float)0x3FF); 00065 } 00066 } 00067 00068 void analogout_write_u16(dac_t *obj, uint16_t value) { 00069 dac_write(value >> 6); // 10-bit 00070 } 00071 00072 float analogout_read(dac_t *obj) { 00073 uint32_t value = dac_read(); 00074 return (float)value * (1.0f / (float)0x3FF); 00075 } 00076 00077 uint16_t analogout_read_u16(dac_t *obj) { 00078 uint32_t value = dac_read(); // 10-bit 00079 return (value << 6) | ((value >> 4) & 0x003F); 00080 } 00081 00082 #endif
Generated on Thu Jul 14 2022 07:43:03 by
1.7.2