/* * stm32_temperature.c * * Created on: 07.11.2023 * Author: balbekova */ #include "common_config.h" #include "FreeRTOS.h" #include "task.h" #include #include #include #include "megatec.h" #ifdef PRINTF_STDLIB #include #endif #ifdef PRINTF_CUSTOM #include "tinystdio.h" #endif #include #include #define DBG if (0) #define TEMPERATURE_SHIFT 15 void adc_init() { ADC_InitTypeDef ADC_InitStruct; ADC_CommonInitTypeDef ADC_CommonInitStruct; /* сбрасываем настройки АЦП */ ADC_DeInit(); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); ADC_CommonInitStruct.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStruct.ADC_Prescaler = ADC_Prescaler_Div8; ADC_CommonInitStruct.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStruct.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; ADC_CommonInit(&ADC_CommonInitStruct); ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b; ADC_InitStruct.ADC_ScanConvMode = DISABLE; ADC_InitStruct.ADC_ContinuousConvMode = ENABLE; ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStruct.ADC_NbrOfConversion = 1; ADC_Init(ADC1, &ADC_InitStruct); ADC_RegularChannelConfig(ADC1, ADC_Channel_TempSensor, 1, ADC_SampleTime_144Cycles); ADC_TempSensorVrefintCmd(ENABLE); ADC_Cmd(ADC1, ENABLE); } u16 readADC1(u8 channel){ // начинаем работу ADC_SoftwareStartConv(ADC1); // ждём пока преобразуется напряжение в код while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); // возвращаем результат return ADC_GetConversionValue(ADC1); } //void stm32_temperature_meas(void) void stm32_temperature_meas(void *params) { float adc_temperature; for (;;) { adc_temperature = readADC1(16); adc_temperature *= 3300; adc_temperature /= 0xfff; //Reading in mV adc_temperature /= 1000.0; //Reading in Volts adc_temperature -= 0.760; // Subtract the reference voltage at 25°C adc_temperature /= .0025; // Divide by slope 2.5mV adc_temperature += 25.0; // Add the 25°C UPS.temp_stm32 = adc_temperature - TEMPERATURE_SHIFT; printf("stm32_temperature %0.1f\r\n", UPS.temp_stm32); vTaskDelay(5000); } } void stm32_temperature_init(void) { adc_init(); xTaskCreate(stm32_temperature_meas, ( char * ) "stm32_temperature_meas", configMINIMAL_STACK_SIZE * 2, NULL, tskIDLE_PRIORITY, NULL); }