Zhengyu Ouyang
/
NewKu
123
Embed:
(wiki syntax)
Show/hide line numbers
InternalTemperature.cpp
00001 #include "InternalTemperature.h" 00002 00003 void temp_init(analogin_t *obj) { 00004 obj->adc = (ADCName)16; 00005 00006 // Enable ADC1 clock source. 00007 RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; 00008 00009 // Enable the ADC 00010 ADC1->CR2 |= ADC_CR2_ADON; 00011 00012 // Enable internal temperature sensor. 00013 ADC->CCR |= ADC_CCR_TSVREFE; 00014 00015 // Set longest sample time just to be safe. 00016 ADC1->SMPR1 |= 0x1c; 00017 } 00018 00019 static inline uint32_t temp_read(analogin_t *obj) { 00020 // Select the appropriate channel 00021 ADC1->SQR3 = (int) obj->adc; 00022 00023 // Start conversion 00024 ADC1->CR2 |= ADC_CR2_SWSTART; 00025 00026 // Wait for conversion to finish 00027 while (!(ADC1->SR & ADC_SR_EOC)); 00028 00029 uint32_t data = ADC1->DR; 00030 return data; // 12 bit 00031 } 00032 00033 static inline uint32_t temp_read_u32(analogin_t *obj) { 00034 uint32_t value; 00035 value = temp_read(obj); 00036 return value; 00037 } 00038 00039 uint16_t temperature_read_u16(analogin_t *obj) { 00040 uint32_t value = temp_read_u32(obj); 00041 00042 return (value << 4) | ((value >> 8) & 0x000F); // 12 bit 00043 } 00044 00045 float temperature_read(analogin_t *obj) { 00046 uint32_t value = temp_read_u32(obj); 00047 float ret = value * (1.0f / (float)0xFFF) * 3.0f; 00048 ret = (ret - 0.76f) / 2.5f + 25.0f; 00049 00050 return ret; 00051 }
Generated on Thu Jul 14 2022 05:52:20 by 1.7.2