STM32心率检测

设计介绍

这是一个简单的心率检测的设计,使用到的模块有STM32C8T6,一个LCD1602液晶显示,用来显示测得的心率值以及设置的阈值,阈值可以通过按键设置,当测得的心率值超过阈值时,蜂鸣器会报警,以及一个心率传感器。需要参考的朋友可以在文章底部点击下载源码和原理图。

main.c的代码如下:

#include "main.h"
#include "stm32f1xx_hal.h"
#include "rtc.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

/* USER CODE BEGIN Includes */
#include "LCD1602.h"
#include "MEMORY.h"
/* USER CODE END Includes */

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

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
extern  void RTC_CalendarShow(void);

void police_dispose(void);
void display(void);
void key_scan(void);
extern unsigned int xinlv;
void dis_play(void);
extern void Sensor(void);
char Read_ADC=0;
uint16_t ADC_Dat=0;
uint8_t key1_flag = 0,key2_flag = 0,key3_flag = 0;
uint8_t sec1=0,sec2=0,memory_flag=0;
static uint8_t state=0;
uint8_t s0,HH,LL;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void Error_Handler(void);

/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */
#define key1 HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)
#define key2 HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)
#define key3 HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_10)
#define beep_write_0 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET) 
#define beep_write_1 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET)
#define led1_0 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_11,GPIO_PIN_RESET) 
#define led1_1 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_11,GPIO_PIN_SET)
#define led2_0 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10,GPIO_PIN_RESET) 
#define led2_1 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10,GPIO_PIN_SET)
	
/* USER CODE END 0 */

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();

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

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_RTC_Init();
  MX_USART1_UART_Init();
  MX_TIM3_Init();
  MX_TIM2_Init();

  /* USER CODE BEGIN 2 */
	HAL_Delay(20);
	LCD1602_cls();
	
	HAL_TIM_Base_Start_IT(&htim3);   //启动定时器
	//HAL_TIM_Base_Start_IT(&htim2);
	led2_0;
	
//	Write_flsh_byte(0x08009000,153);
//	Write_flsh_byte(0x0800A000,41);
//  read_memory();//flash初始化
	HH= readFlash(0x08009000);
	LL= readFlash(0x0800A000);
	printf("before %d%d",HH,LL);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

	police_dispose();	
	display();		
	if(memory_flag)
	{
		
	memory_flag=0;
	Write_flsh_byte(0x08009000,HH);
	Write_flsh_byte(0x0800A000,LL);
	
		
	}
	
  /* USER CODE END 3 */
	}
}

/** System Clock Configuration
*/
void SystemClock_Config(void)
{

  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_PeriphCLKInitTypeDef PeriphClkInit;

    /**Initializes the CPU, AHB and APB busses clocks 
    */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE
                              |RCC_OSCILLATORTYPE_LSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

    /**Initializes the CPU, AHB and APB busses 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_2) != HAL_OK)
  {
    Error_Handler();
  }

  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }

    /**Enables the Clock Security System 
    */
  HAL_RCC_EnableCSS();

    /**Configure the Systick interrupt time 
    */
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

    /**Configure the Systick 
    */
  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

/* USER CODE BEGIN 4 */

void display(void)
{
	RTC_CalendarShow();//  第一行显示
	 LCD1602_write(0,0xC0);//第一行显示
	if(state==1&&s0)
	{
		LCD1602_writebyte((unsigned char *)"High:");
		LCD1602_writebyte((unsigned char *)"    ");
		LCD1602_writebyte((unsigned char *)"Low:");
		LCD1602_write(1,0x30+LL/100%10);
		LCD1602_write(1,0x30+LL/10%10);
		LCD1602_write(1,0x30+LL%10);
	}
	else if(state==2&&s0)
	{
		LCD1602_writebyte((unsigned char *)"High:");
		LCD1602_write(1,0x30+HH/100%10);
		LCD1602_write(1,0x30+HH/10%10);
		LCD1602_write(1,0x30+HH%10);
		LCD1602_writebyte((unsigned char *)" ");
		LCD1602_writebyte((unsigned char *)"Low:");
		LCD1602_writebyte((unsigned char *)"    ");
	}
	else 
	{
		LCD1602_writebyte((unsigned char *)"High:");
		LCD1602_write(1,0x30+HH/100%10);
		LCD1602_write(1,0x30+HH/10%10);
		LCD1602_write(1,0x30+HH%10);
		LCD1602_writebyte((unsigned char *)" ");
		LCD1602_writebyte((unsigned char *)"Low:");
		LCD1602_write(1,0x30+LL/100%10);		
		LCD1602_write(1,0x30+LL/10%10);
		LCD1602_write(1,0x30+LL%10);
	}
}
void police_dispose(void)
{
	if(xinlv>HH||((xinlv

需要源码和原理图的朋友可以点击下载链接
源代码

你可能感兴趣的:(STM32开发)