FSM thermostat
port_temp_sensor.c
Go to the documentation of this file.
1 
10 /* Standard C includes */
11 #include <stdint.h>
12 #include <stdio.h>
13 
14 /* HW dependent includes */
15 #include "port_temp_sensor.h"
16 #include "stm32f4xx.h"
17 #include "port_system.h"
18 
19 /* Global variables -----------------------------------------------------------*/
21 
22 /* Private functions */
23 
31 uint32_t _adc_to_mvolts(uint32_t adcValue, uint8_t adc_res_bits)
32 {
33  uint32_t mvolts = (ADC_VREF_MV * adcValue) / ((1 << adc_res_bits) - 1);
34  return mvolts;
35 }
36 /* Function definitions ------------------------------------------------------*/
38 {
39  return p_temp->temperature_celsius;
40 }
41 
42 void port_temp_sensor_save_adc_value(port_temp_hw_t *p_temp, double adc_value)
43 {
44  // Convert the ADC value to temperature in Celsius.
45  // LM35 sensor has a linear response of 10mV/°C
46  p_temp->temperature_celsius = _adc_to_mvolts(adc_value, 12) / 10.0;
47 
48  // There are few problems to print double values using printf with SWO. The value is multiplied by 10 and printed as an integer the decimal point is added manually.
49  printf("Temperature: %ld.%d oC\n", (uint32_t)(p_temp->temperature_celsius), (uint8_t)((10*p_temp->temperature_celsius))%10);
50 }
51 
53 {
54  // Initialize the GPIO
56 
57  // Initialize the ADC with 12-bit resolution and EOC interrupt enable
59 
60  // Enable the ADC global interrupt
62 
63  // Enable the ADC
65 }
ADC_VREF_MV
#define ADC_VREF_MV
Definition: port_system.h:59
port_temp_sensor_get_temperature
double port_temp_sensor_get_temperature(port_temp_hw_t *p_temp)
Gets the temperature in Celsius of the temperature sensor.
Definition: port_temp_sensor.c:37
TEMP_SENSOR_THERMOSTAT_PIN
#define TEMP_SENSOR_THERMOSTAT_PIN
Definition: port_temp_sensor.h:23
port_temp_hw_t::adc_channel
uint32_t adc_channel
Definition: port_temp_sensor.h:36
port_temp_hw_t::temperature_celsius
double temperature_celsius
Definition: port_temp_sensor.h:37
GPIO_PUPDR_NOPULL
#define GPIO_PUPDR_NOPULL
Definition: port_system.h:47
port_system_adc_enable
void port_system_adc_enable(ADC_TypeDef *p_adc)
Enable the ADC peripheral to work.
Definition: port_system.c:357
GPIO_MODE_ANALOG
#define GPIO_MODE_ANALOG
Definition: port_system.h:45
temp_sensor_thermostat
port_temp_hw_t temp_sensor_thermostat
Definition: port_temp_sensor.c:20
TEMP_SENSOR_THERMOSTAT_ADC
#define TEMP_SENSOR_THERMOSTAT_ADC
Definition: port_temp_sensor.h:24
port_temp_hw_t::p_port
GPIO_TypeDef * p_port
Definition: port_temp_sensor.h:33
port_system_adc_interrupt_enable
void port_system_adc_interrupt_enable(uint8_t priority, uint8_t subpriority)
Enable the ADC global interrupts in NVIC. ADC1, ADC2, and ADC3 share the same interrupt.
Definition: port_system.c:351
port_temp_sensor.h
Header file for the temperature sensor port layer.
ADC_RESOLUTION_12B
#define ADC_RESOLUTION_12B
Definition: port_system.h:61
port_system_adc_single_ch_init
void port_system_adc_single_ch_init(ADC_TypeDef *p_adc, uint8_t channel, uint32_t cr_mode)
Configure the ADC peripheral for a single channel.
Definition: port_system.c:261
port_temp_sensor_save_adc_value
void port_temp_sensor_save_adc_value(port_temp_hw_t *p_temp, double adc_value)
Saves the ADC value of the temperature sensor and converts it to Celsius.
Definition: port_temp_sensor.c:42
_adc_to_mvolts
uint32_t _adc_to_mvolts(uint32_t adcValue, uint8_t adc_res_bits)
Converts an ADC value to millivolts.
Definition: port_temp_sensor.c:31
port_system.h
Header for port_system.c file.
port_temp_hw_t::p_adc
ADC_TypeDef * p_adc
Definition: port_temp_sensor.h:35
port_temp_hw_t
Structure to define the HW dependencies of a temperature sensor.
Definition: port_temp_sensor.h:31
port_system_gpio_config
void port_system_gpio_config(GPIO_TypeDef *p_port, uint8_t pin, uint8_t mode, uint8_t pupd)
Configure the mode and pull of a GPIO.
Definition: port_system.c:158
TEMP_SENSOR_THERMOSTAT_GPIO
#define TEMP_SENSOR_THERMOSTAT_GPIO
Definition: port_temp_sensor.h:22
port_temp_sensor_init
void port_temp_sensor_init(port_temp_hw_t *p_temp)
Initializes the temperature sensor.
Definition: port_temp_sensor.c:52
ADC_EOC_INTERRUPT_ENABLE
#define ADC_EOC_INTERRUPT_ENABLE
Definition: port_system.h:66
port_temp_hw_t::pin
uint8_t pin
Definition: port_temp_sensor.h:34
TEMP_SENSOR_THERMOSTAT_ADC_CHANNEL
#define TEMP_SENSOR_THERMOSTAT_ADC_CHANNEL
Definition: port_temp_sensor.h:25