MAX30102 IIC驱动

/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file           : main.c
 * @brief          : Main program body
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2025 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "string.h"
#include "stdint.h"
#include "stdlib.h"
#include "stdio.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
char UART_MSG[60] = {0};
int msgLength = 0;

#define MAX30102_ADDRESS (0x57 << 1)
#define SAMPLE_RATE 100 // 采样率 100Hz
#define NUM_SAMPLES 100 // 采样 100 次计算 HR

uint32_t red_buffer[NUM_SAMPLES]; // 存储红光数据
uint32_t ir_buffer[NUM_SAMPLES];  // 存储红外数据
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int calculate_hr() {
  int peak_count = 0;
  int last_peak_index = -1;
  int interval_sum = 0;

  for (int i = 1; i < NUM_SAMPLES - 1; i++) {
      if (red_buffer[i] > red_buffer[i - 1] && red_buffer[i] > red_buffer[i + 1]) {
          if (last_peak_index != -1) {
              interval_sum += (i - last_peak_index);
              peak_count++;
          }
          last_peak_index = i;
      }
  }

  if (peak_count < 1) return -1;  // 没找到足够的峰值

  int avg_interval = interval_sum / peak_count;
  int hr = (60 * SAMPLE_RATE) / avg_interval;

  return hr;
}

/* USER CODE END 0 */

/**
 * @brief  The application entry point.
 * @retval int
 */
int main(void)
{

  /* USER CODE BEGIN 1 */
  
  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */

  HAL_UART_Transmit(&huart1, "start drive", strlen("start drive"), HAL_MAX_DELAY);
  HAL_Delay(50);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  HAL_Delay(10);

  uint8_t WHO_AM_I;

  HAL_I2C_Mem_Read(&hi2c1, MAX30102_ADDRESS, 0xff, 1, &WHO_AM_I, 1, 0xffff);

  msgLength = sprintf(UART_MSG, "WHO_AM_I:%2x\r\n", WHO_AM_I);
  HAL_UART_Transmit(&huart1, UART_MSG, msgLength, HAL_MAX_DELAY);
  // 初始化 MAX30102
  uint8_t MODE_CONFIG_ADDRESS = 0x09; // MODE_CONFIG
  uint8_t MODE_CONFIG = 0x03;         // 心率+血氧模式
  HAL_I2C_Mem_Write(&hi2c1, MAX30102_ADDRESS, MODE_CONFIG_ADDRESS, 1, &MODE_CONFIG, 1, HAL_MAX_DELAY);

  uint8_t SPO2_CONFIG_ADDRESS = 0x0A; // SPO2_CONFIG
  uint8_t SPO2_CONFIG = 0x27;         // 100Hz 采样, 16-bit 分辨率
  HAL_I2C_Mem_Write(&hi2c1, MAX30102_ADDRESS, SPO2_CONFIG_ADDRESS, 1, &SPO2_CONFIG, 1, HAL_MAX_DELAY);

  uint8_t FIFO_CONFIG_ADDRESS = 0x08; // FIFO配置
  uint8_t FIFO_CONFIG = 0x4F;         // 溢出时覆盖旧数据
  HAL_I2C_Mem_Write(&hi2c1, MAX30102_ADDRESS, FIFO_CONFIG_ADDRESS, I2C_MEMADD_SIZE_8BIT, &FIFO_CONFIG, 1, HAL_MAX_DELAY);

  uint8_t LED1_PA_ADDRESS = 0x0C; // LED1_PA (红光)
  uint8_t LED1_PA = 0x24;         // 7.6mA
  HAL_I2C_Mem_Write(&hi2c1, MAX30102_ADDRESS, LED1_PA_ADDRESS, 1, &LED1_PA, 1, HAL_MAX_DELAY);

  uint8_t LED2_PA_ADDRESS = 0x0D; // LED2_PA (红外光)
  uint8_t LED2_PA = 0x24;         // 7.6mA
  HAL_I2C_Mem_Write(&hi2c1, MAX30102_ADDRESS, LED2_PA_ADDRESS, 1, &LED2_PA, 1, HAL_MAX_DELAY);

  uint8_t DATA_REGISTER_ADDRESS = 0x07;
  uint8_t DATA_REGISTER[6] = {0};
  uint32_t red = 0, ir = 0;
  int count = 0;
  while (1)
  {

    if (count < 100)
    {
      HAL_I2C_Mem_Read(&hi2c1, MAX30102_ADDRESS, DATA_REGISTER_ADDRESS, 1, DATA_REGISTER, 6, HAL_MAX_DELAY);
      red = (uint32_t)DATA_REGISTER[0] << 16 | (uint32_t)DATA_REGISTER[1] << 8 | (uint32_t)DATA_REGISTER[2];
      ir = (uint32_t)DATA_REGISTER[3] << 16 | (uint32_t)DATA_REGISTER[4] << 8 | (uint32_t)DATA_REGISTER[5];
      red_buffer[count]=red;
      count++;
      HAL_Delay(10);
    }else{
      int heart_rate = calculate_hr();

      char MAX_DATA[60] = {0};
      int dataLength = sprintf(MAX_DATA, "heart rate:%d  red:%ld ir:%ld \r\n", heart_rate,red, ir);
      HAL_UART_Transmit(&huart1, MAX_DATA, dataLength, HAL_MAX_DELAY);
      count=0;
    }

   
    
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
 * @brief System Clock Configuration
 * @retval None
 */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
   */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
   * in the RCC_OscInitTypeDef structure.
   */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 100;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
   */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
 * @brief  This function is executed in case of error occurrence.
 * @retval None
 */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef USE_FULL_ASSERT
/**
 * @brief  Reports the name of the source file and the source line number
 *         where the assert_param error has occurred.
 * @param  file: pointer to the source file name
 * @param  line: assert_param error line source number
 * @retval None
 */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值