Skip to content

Commit

Permalink
feat(temperature):implement header file
Browse files Browse the repository at this point in the history
-add function TEMPERATURE_Init()
-add function TEMPERATURE_Read()
  • Loading branch information
abdullahbagyapan committed Apr 19, 2024
1 parent a52ff79 commit 4b7cfd7
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions temperature/temperature.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
@module: TEMPERATURE
@author: Abdullah Bagyapan
@date: 19/04/2024
*/

/*================================== Libraries ==================================*/


#include "temperature.h"

#include "pico/stdlib.h"
#include "hardware/adc.h"


/*================================== Functions ==================================*/


/*
@brief: Calculate ADC value according to the formula
@author Abdullah Bagyapan
@date 19/04/2024
@param uint16_t ui16ADC_voltage, The analog data from temperature sensor
@return The temperature, in celsius format
*/
static inline uint8_t _TEMPERATURE_Calculate_Celsius(uint16_t ui16ADC_voltage) {

// T = 27 - (ADC_voltage - 0.706)/0.001721
// The given above formula according to the RP2040 datasheet

return 27 - (ui16ADC_voltage - 0.706) / 0.001721;

}


void TEMPERATURE_Init(void) {

// Initialize onboard temperature sensor
adc_gpio_init(TEMPERATURE_GPIO_PIN);

// Set onboard temperature sensor as ADC
adc_select_input(TEMPERATURE_ADC_PIN);

}

uint8_t TEMPERATURE_Read(void) {

uint8_t ui8TemperatureCelsius;

uint16_t ui16ADC_voltage = adc_read();

ui8TemperatureCelsius = _TEMPERATURE_Calculate_Celsius(ui16ADC_voltage);

return ui8TemperatureCelsius;
}

0 comments on commit 4b7cfd7

Please sign in to comment.