I2C à setup pour température

This commit is contained in:
Maxime
2025-05-07 17:45:50 +02:00
parent 57c69e222d
commit 09cb9c4ebd
3 changed files with 7 additions and 46 deletions

View File

@@ -1,9 +0,0 @@
#ifndef INC_TEMPSENSOR_H_
#define INC_TEMPSENSOR_H_
#include "stm32g4xx_hal.h"
#include "stm32g4xx_hal_i2c.h" // <-- Ajoute cette ligne pour que I2C_HandleTypeDef soit reconnu
float MCP9808_ReadTemp(I2C_HandleTypeDef *hi2c);
#endif /* INC_TEMPSENSOR_H_ */

View File

@@ -21,8 +21,8 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <string.h>
#include "tempSensor.h"
//#include <string.h>
//#include "tempSensor.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@@ -97,7 +97,7 @@ int main(void)
MX_GPIO_Init();
MX_TIM3_Init();
MX_USART2_UART_Init();
MX_I2C1_Init();
//MX_I2C1_Init(); TODO c'est ça qu'il faut modifier
/* USER CODE BEGIN 2 */
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_SET){
BAU_State = 0;
@@ -112,9 +112,9 @@ int main(void)
while (1)
{
float temp = MCP9808_ReadTemp(&hi2c1);
char msg[16];
//HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY);
float temp = MCP9808_ReadTemp(&hi2c1);
char msg[16];
HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY); //message à remplir (fonction de transformation de float vers string qui foire
/*
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
HAL_Delay(500);
@@ -355,7 +355,7 @@ static void MX_GPIO_Init(void)
/* USER CODE BEGIN 4 */
uint32_t last_debounce_time = 0;
const uint32_t debounce_delay = 1000; // 50ms d'antirebond
const uint32_t debounce_delay = 1000; // 1s d'antirebond
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{

View File

@@ -1,30 +0,0 @@
/*
* tempSensor.c
*
* Created on: May 5, 2025
* Author: maxch
*/
#include "tempSensor.h"
#include "main.h"
float MCP9808_ReadTemp(I2C_HandleTypeDef *hi2c) {
uint8_t reg = 0x05; // registre de température
uint8_t data[2];
if (HAL_I2C_Master_Transmit(hi2c, 0x30, &reg, 1, HAL_MAX_DELAY) != HAL_OK) {
return -1000.0f; // erreur
}
if (HAL_I2C_Master_Receive(hi2c, 0x30, data, 2, HAL_MAX_DELAY) != HAL_OK) {
return -1000.0f; // erreur
}
uint16_t raw = (data[0] << 8) | data[1];
raw &= 0x1FFF;
float temp = (raw & 0x1000) ? (raw - 8192) * 0.0625f : raw * 0.0625f;
return temp;
}