Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

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?

UserRevisionLine numberNew 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 #include "cmsis.h"
sahilmgandhi 18:6a4db94011d3 19 #include "pinmap.h"
sahilmgandhi 18:6a4db94011d3 20
sahilmgandhi 18:6a4db94011d3 21 void analogout_init(dac_t *obj, PinName pin) {
sahilmgandhi 18:6a4db94011d3 22 MBED_ASSERT(pin == P0_12);
sahilmgandhi 18:6a4db94011d3 23
sahilmgandhi 18:6a4db94011d3 24 LPC_SYSCON->SYSAHBCLKCTRL0 |= (1 << 29);
sahilmgandhi 18:6a4db94011d3 25 LPC_SYSCON->PDRUNCFG &= ~(1 << 12);
sahilmgandhi 18:6a4db94011d3 26 LPC_IOCON->PIO0_12 = 0;
sahilmgandhi 18:6a4db94011d3 27 LPC_SWM->PINENABLE0 &= ~(1 << 24);
sahilmgandhi 18:6a4db94011d3 28 LPC_DAC->CTRL = 0;
sahilmgandhi 18:6a4db94011d3 29
sahilmgandhi 18:6a4db94011d3 30 analogout_write_u16(obj, 0);
sahilmgandhi 18:6a4db94011d3 31 }
sahilmgandhi 18:6a4db94011d3 32
sahilmgandhi 18:6a4db94011d3 33 void analogout_free(dac_t *obj)
sahilmgandhi 18:6a4db94011d3 34 {
sahilmgandhi 18:6a4db94011d3 35 LPC_SYSCON->SYSAHBCLKCTRL0 &= ~(1 << 29);
sahilmgandhi 18:6a4db94011d3 36 LPC_SWM->PINENABLE0 |= (1 << 24);
sahilmgandhi 18:6a4db94011d3 37 }
sahilmgandhi 18:6a4db94011d3 38
sahilmgandhi 18:6a4db94011d3 39 static inline void dac_write(int value) {
sahilmgandhi 18:6a4db94011d3 40 value &= 0xFFF; // 12-bit
sahilmgandhi 18:6a4db94011d3 41
sahilmgandhi 18:6a4db94011d3 42 // Set the DAC output
sahilmgandhi 18:6a4db94011d3 43 LPC_DAC->VAL = (value << 4);
sahilmgandhi 18:6a4db94011d3 44 }
sahilmgandhi 18:6a4db94011d3 45
sahilmgandhi 18:6a4db94011d3 46 static inline int dac_read() {
sahilmgandhi 18:6a4db94011d3 47 return ((LPC_DAC->VAL >> 4) & 0xFFF);
sahilmgandhi 18:6a4db94011d3 48 }
sahilmgandhi 18:6a4db94011d3 49
sahilmgandhi 18:6a4db94011d3 50 void analogout_write(dac_t *obj, float value) {
sahilmgandhi 18:6a4db94011d3 51 if (value < 0.0f) {
sahilmgandhi 18:6a4db94011d3 52 dac_write(0);
sahilmgandhi 18:6a4db94011d3 53 } else if (value > 1.0f) {
sahilmgandhi 18:6a4db94011d3 54 dac_write(0xFFF);
sahilmgandhi 18:6a4db94011d3 55 } else {
sahilmgandhi 18:6a4db94011d3 56 dac_write((uint32_t)(value * (float)0xFFF));
sahilmgandhi 18:6a4db94011d3 57 }
sahilmgandhi 18:6a4db94011d3 58 }
sahilmgandhi 18:6a4db94011d3 59
sahilmgandhi 18:6a4db94011d3 60 void analogout_write_u16(dac_t *obj, uint16_t value) {
sahilmgandhi 18:6a4db94011d3 61 dac_write(value);
sahilmgandhi 18:6a4db94011d3 62 }
sahilmgandhi 18:6a4db94011d3 63
sahilmgandhi 18:6a4db94011d3 64 float analogout_read(dac_t *obj) {
sahilmgandhi 18:6a4db94011d3 65 uint32_t value = dac_read();
sahilmgandhi 18:6a4db94011d3 66 return (float)value * (1.0f / (float)0xFFF);
sahilmgandhi 18:6a4db94011d3 67 }
sahilmgandhi 18:6a4db94011d3 68
sahilmgandhi 18:6a4db94011d3 69 uint16_t analogout_read_u16(dac_t *obj) {
sahilmgandhi 18:6a4db94011d3 70 return (uint16_t)dac_read();
sahilmgandhi 18:6a4db94011d3 71 }