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.
Fork of elec350 by
Revision 16:721e41936a07, committed 2015-11-19
- Comitter:
- rmerrisonhort
- Date:
- Thu Nov 19 10:06:45 2015 +0000
- Parent:
- 15:c1362c12a896
- Commit message:
- Added InternalTemperature class.
Changed in this revision
InternalTemperature.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/InternalTemperature.h Thu Nov 19 10:06:45 2015 +0000 @@ -0,0 +1,102 @@ +/* Internal temperature sensor class. Adapted from AnalogIn.h, from: + * mbed Microcontroller Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef INTERNAL_TEMP_H +#define INTERNAL_TEMP_H + +#include "platform.h" + +#if DEVICE_ANALOGIN + +#include "analogin_api.h" + +void temp_init(analogin_t *obj) { + obj->adc = (ADCName)16; + + // Enable ADC1 clock source. + RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; + + // Enable the ADC + ADC1->CR2 |= ADC_CR2_ADON; + + // Enable internal temperature sensor. + ADC->CCR |= ADC_CCR_TSVREFE; + + // Set longest sample time just to be safe. + ADC1->SMPR1 |= 0x1c; +} + +static inline uint32_t temp_read(analogin_t *obj) { + // Select the appropriate channel + ADC1->SQR3 = (int) obj->adc; + + // Start conversion + ADC1->CR2 |= ADC_CR2_SWSTART; + + // Wait for conversion to finish + while (!(ADC1->SR & ADC_SR_EOC)); + + uint32_t data = ADC1->DR; + return data; // 12 bit +} + +static inline uint32_t temp_read_u32(analogin_t *obj) { + uint32_t value; + value = temp_read(obj); + return value; +} + +uint16_t temperature_read_u16(analogin_t *obj) { + uint32_t value = temp_read_u32(obj); + + return (value << 4) | ((value >> 8) & 0x000F); // 12 bit +} + +float temperature_read(analogin_t *obj) { + uint32_t value = temp_read_u32(obj); + return (float)value * (1.0f / (float)0xFFF); //Check range +} + +class InternalTemperature { + +public: + InternalTemperature() { + temp_init(&_adc); + } + + float read() { + return temperature_read(&_adc); + } + + unsigned short read_u16() { + return temperature_read_u16(&_adc); + } + +#ifdef MBED_OPERATORS + operator float() { + return read(); + } +#endif + +protected: + analogin_t _adc; +}; + + +#endif + +#endif +