mbed-os

Dependents:   cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more

Committer:
be_bryan
Date:
Mon Dec 11 17:54:04 2017 +0000
Revision:
0:b74591d5ab33
motor ++

Who changed what in which revision?

UserRevisionLine numberNew contents of line
be_bryan 0:b74591d5ab33 1 /* mbed Microcontroller Library
be_bryan 0:b74591d5ab33 2 * Copyright (c) 2017, STMicroelectronics
be_bryan 0:b74591d5ab33 3 * All rights reserved.
be_bryan 0:b74591d5ab33 4 *
be_bryan 0:b74591d5ab33 5 * Redistribution and use in source and binary forms, with or without
be_bryan 0:b74591d5ab33 6 * modification, are permitted provided that the following conditions are met:
be_bryan 0:b74591d5ab33 7 *
be_bryan 0:b74591d5ab33 8 * 1. Redistributions of source code must retain the above copyright notice,
be_bryan 0:b74591d5ab33 9 * this list of conditions and the following disclaimer.
be_bryan 0:b74591d5ab33 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
be_bryan 0:b74591d5ab33 11 * this list of conditions and the following disclaimer in the documentation
be_bryan 0:b74591d5ab33 12 * and/or other materials provided with the distribution.
be_bryan 0:b74591d5ab33 13 * 3. Neither the name of STMicroelectronics nor the names of its contributors
be_bryan 0:b74591d5ab33 14 * may be used to endorse or promote products derived from this software
be_bryan 0:b74591d5ab33 15 * without specific prior written permission.
be_bryan 0:b74591d5ab33 16 *
be_bryan 0:b74591d5ab33 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
be_bryan 0:b74591d5ab33 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
be_bryan 0:b74591d5ab33 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
be_bryan 0:b74591d5ab33 20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
be_bryan 0:b74591d5ab33 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
be_bryan 0:b74591d5ab33 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
be_bryan 0:b74591d5ab33 23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
be_bryan 0:b74591d5ab33 24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
be_bryan 0:b74591d5ab33 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
be_bryan 0:b74591d5ab33 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
be_bryan 0:b74591d5ab33 27 */
be_bryan 0:b74591d5ab33 28 #include "analogin_api.h"
be_bryan 0:b74591d5ab33 29
be_bryan 0:b74591d5ab33 30 #if DEVICE_ANALOGIN
be_bryan 0:b74591d5ab33 31
be_bryan 0:b74591d5ab33 32 uint16_t adc_read(analogin_t *obj);
be_bryan 0:b74591d5ab33 33
be_bryan 0:b74591d5ab33 34 uint16_t analogin_read_u16(analogin_t *obj)
be_bryan 0:b74591d5ab33 35 {
be_bryan 0:b74591d5ab33 36 uint16_t value = adc_read(obj);
be_bryan 0:b74591d5ab33 37 // 12-bit to 16-bit conversion
be_bryan 0:b74591d5ab33 38 value = ((value << 4) & (uint16_t)0xFFF0) | ((value >> 8) & (uint16_t)0x000F);
be_bryan 0:b74591d5ab33 39 return value;
be_bryan 0:b74591d5ab33 40 }
be_bryan 0:b74591d5ab33 41
be_bryan 0:b74591d5ab33 42 float analogin_read(analogin_t *obj)
be_bryan 0:b74591d5ab33 43 {
be_bryan 0:b74591d5ab33 44 uint16_t value = adc_read(obj);
be_bryan 0:b74591d5ab33 45 return (float)value * (1.0f / (float)0xFFF); // 12 bits range
be_bryan 0:b74591d5ab33 46 }
be_bryan 0:b74591d5ab33 47
be_bryan 0:b74591d5ab33 48 #endif