Mouse code for the MacroRat
mbed-dev/targets/TARGET_NXP/TARGET_LPC176X/analogout_api.c@46:b156ef445742, 2017-06-03 (annotated)
- Committer:
- sahilmgandhi
- Date:
- Sat Jun 03 00:22:44 2017 +0000
- Revision:
- 46:b156ef445742
- Parent:
- 18:6a4db94011d3
Final code for internal battlebot competition.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahilmgandhi | 18:6a4db94011d3 | 1 | /* mbed Microcontroller Library |
sahilmgandhi | 18:6a4db94011d3 | 2 | * Copyright (c) 2006-2013 ARM Limited |
sahilmgandhi | 18:6a4db94011d3 | 3 | * |
sahilmgandhi | 18:6a4db94011d3 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sahilmgandhi | 18:6a4db94011d3 | 5 | * you may not use this file except in compliance with the License. |
sahilmgandhi | 18:6a4db94011d3 | 6 | * You may obtain a copy of the License at |
sahilmgandhi | 18:6a4db94011d3 | 7 | * |
sahilmgandhi | 18:6a4db94011d3 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sahilmgandhi | 18:6a4db94011d3 | 9 | * |
sahilmgandhi | 18:6a4db94011d3 | 10 | * Unless required by applicable law or agreed to in writing, software |
sahilmgandhi | 18:6a4db94011d3 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sahilmgandhi | 18:6a4db94011d3 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sahilmgandhi | 18:6a4db94011d3 | 13 | * See the License for the specific language governing permissions and |
sahilmgandhi | 18:6a4db94011d3 | 14 | * limitations under the License. |
sahilmgandhi | 18:6a4db94011d3 | 15 | */ |
sahilmgandhi | 18:6a4db94011d3 | 16 | #include "mbed_assert.h" |
sahilmgandhi | 18:6a4db94011d3 | 17 | #include "analogout_api.h" |
sahilmgandhi | 18:6a4db94011d3 | 18 | |
sahilmgandhi | 18:6a4db94011d3 | 19 | #include "cmsis.h" |
sahilmgandhi | 18:6a4db94011d3 | 20 | #include "pinmap.h" |
sahilmgandhi | 18:6a4db94011d3 | 21 | |
sahilmgandhi | 18:6a4db94011d3 | 22 | static const PinMap PinMap_DAC[] = { |
sahilmgandhi | 18:6a4db94011d3 | 23 | {P0_26, DAC_0, 2}, |
sahilmgandhi | 18:6a4db94011d3 | 24 | {NC , NC , 0} |
sahilmgandhi | 18:6a4db94011d3 | 25 | }; |
sahilmgandhi | 18:6a4db94011d3 | 26 | |
sahilmgandhi | 18:6a4db94011d3 | 27 | void analogout_init(dac_t *obj, PinName pin) { |
sahilmgandhi | 18:6a4db94011d3 | 28 | obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); |
sahilmgandhi | 18:6a4db94011d3 | 29 | MBED_ASSERT(obj->dac != (DACName)NC); |
sahilmgandhi | 18:6a4db94011d3 | 30 | |
sahilmgandhi | 18:6a4db94011d3 | 31 | // power is on by default, set DAC clk divider is /4 |
sahilmgandhi | 18:6a4db94011d3 | 32 | LPC_SC->PCLKSEL0 &= ~(0x3 << 22); |
sahilmgandhi | 18:6a4db94011d3 | 33 | |
sahilmgandhi | 18:6a4db94011d3 | 34 | // map out (must be done before accessing registers) |
sahilmgandhi | 18:6a4db94011d3 | 35 | pinmap_pinout(pin, PinMap_DAC); |
sahilmgandhi | 18:6a4db94011d3 | 36 | |
sahilmgandhi | 18:6a4db94011d3 | 37 | analogout_write_u16(obj, 0); |
sahilmgandhi | 18:6a4db94011d3 | 38 | } |
sahilmgandhi | 18:6a4db94011d3 | 39 | |
sahilmgandhi | 18:6a4db94011d3 | 40 | void analogout_free(dac_t *obj) {} |
sahilmgandhi | 18:6a4db94011d3 | 41 | |
sahilmgandhi | 18:6a4db94011d3 | 42 | static inline void dac_write(int value) { |
sahilmgandhi | 18:6a4db94011d3 | 43 | value &= 0x3FF; // 10-bit |
sahilmgandhi | 18:6a4db94011d3 | 44 | |
sahilmgandhi | 18:6a4db94011d3 | 45 | // Set the DAC output |
sahilmgandhi | 18:6a4db94011d3 | 46 | LPC_DAC->DACR = (0 << 16) // bias = 0 |
sahilmgandhi | 18:6a4db94011d3 | 47 | | (value << 6); |
sahilmgandhi | 18:6a4db94011d3 | 48 | } |
sahilmgandhi | 18:6a4db94011d3 | 49 | |
sahilmgandhi | 18:6a4db94011d3 | 50 | static inline int dac_read() { |
sahilmgandhi | 18:6a4db94011d3 | 51 | return (LPC_DAC->DACR >> 6) & 0x3FF; |
sahilmgandhi | 18:6a4db94011d3 | 52 | } |
sahilmgandhi | 18:6a4db94011d3 | 53 | |
sahilmgandhi | 18:6a4db94011d3 | 54 | void analogout_write(dac_t *obj, float value) { |
sahilmgandhi | 18:6a4db94011d3 | 55 | if (value < 0.0f) { |
sahilmgandhi | 18:6a4db94011d3 | 56 | dac_write(0); |
sahilmgandhi | 18:6a4db94011d3 | 57 | } else if (value > 1.0f) { |
sahilmgandhi | 18:6a4db94011d3 | 58 | dac_write(0x3FF); |
sahilmgandhi | 18:6a4db94011d3 | 59 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 60 | dac_write(value * (float)0x3FF); |
sahilmgandhi | 18:6a4db94011d3 | 61 | } |
sahilmgandhi | 18:6a4db94011d3 | 62 | } |
sahilmgandhi | 18:6a4db94011d3 | 63 | |
sahilmgandhi | 18:6a4db94011d3 | 64 | void analogout_write_u16(dac_t *obj, uint16_t value) { |
sahilmgandhi | 18:6a4db94011d3 | 65 | dac_write(value >> 6); // 10-bit |
sahilmgandhi | 18:6a4db94011d3 | 66 | } |
sahilmgandhi | 18:6a4db94011d3 | 67 | |
sahilmgandhi | 18:6a4db94011d3 | 68 | float analogout_read(dac_t *obj) { |
sahilmgandhi | 18:6a4db94011d3 | 69 | uint32_t value = dac_read(); |
sahilmgandhi | 18:6a4db94011d3 | 70 | return (float)value * (1.0f / (float)0x3FF); |
sahilmgandhi | 18:6a4db94011d3 | 71 | } |
sahilmgandhi | 18:6a4db94011d3 | 72 | |
sahilmgandhi | 18:6a4db94011d3 | 73 | uint16_t analogout_read_u16(dac_t *obj) { |
sahilmgandhi | 18:6a4db94011d3 | 74 | uint32_t value = dac_read(); // 10-bit |
sahilmgandhi | 18:6a4db94011d3 | 75 | return (value << 6) | ((value >> 4) & 0x003F); |
sahilmgandhi | 18:6a4db94011d3 | 76 | } |