STM32C8T6利用AD8232和st7789即做到液晶屏显示心电波形又做到显示心率的详细程序
时间: 2024-05-14 15:19:04 浏览: 160
基于STM32的心率检测及波形显示
以下是液晶屏显示心电波形和心率的STM32程序,使用AD8232心电模块和ST7789液晶屏。请注意,此程序仅供参考,需要根据具体硬件和需求进行修改。
```c
#include "main.h"
#include "spi.h"
#include "gpio.h"
#include "tim.h"
#include "AD8232.h"
#include "ST7789.h"
#define ADC_BUF_LEN 1000
#define HEART_RATE_BUF_LEN 10
volatile uint16_t adc_buf[ADC_BUF_LEN];
volatile uint16_t adc_buf_pos = 0;
volatile uint8_t adc_buf_full = 0;
volatile uint16_t heart_rate_buf[HEART_RATE_BUF_LEN];
volatile uint8_t heart_rate_buf_pos = 0;
void SystemClock_Config(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_SPI1_Init();
MX_TIM3_Init();
AD8232_Init();
ST7789_Init();
HAL_TIM_Base_Start_IT(&htim3);
while (1)
{
if (adc_buf_full)
{
// Draw ECG waveform
ST7789_DrawECGWaveform(adc_buf, ADC_BUF_LEN, 0, 0);
// Calculate heart rate
uint16_t heart_rate = AD8232_CalculateHeartRate(adc_buf, ADC_BUF_LEN);
heart_rate_buf[heart_rate_buf_pos] = heart_rate;
heart_rate_buf_pos = (heart_rate_buf_pos + 1) % HEART_RATE_BUF_LEN;
// Draw heart rate
ST7789_DrawString("Heart Rate:", 0, 160);
uint16_t avg_heart_rate = 0;
for (int i = 0; i < HEART_RATE_BUF_LEN; i++)
{
avg_heart_rate += heart_rate_buf[i];
}
avg_heart_rate /= HEART_RATE_BUF_LEN;
char heart_rate_str[5];
sprintf(heart_rate_str, "%d", avg_heart_rate);
ST7789_DrawString(heart_rate_str, 120, 160);
adc_buf_full = 0;
}
}
}
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
adc_buf[adc_buf_pos] = HAL_ADC_GetValue(hadc);
adc_buf_pos = (adc_buf_pos + 1) % ADC_BUF_LEN;
if (adc_buf_pos == 0)
{
adc_buf_full = 1;
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim->Instance == TIM3)
{
AD8232_Sample();
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/**Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/**Configure LSE Drive Capability
*/
HAL_PWR_EnableBkUpAccess();
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_HIGH);
/**Configure the RTC clock source
*/
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
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_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
/**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);
}
```
在ST7789.h头文件中,定义了以下ST7789液晶屏相关的函数:
```c
void ST7789_Init(void);
void ST7789_DrawPixel(uint16_t x, uint16_t y, uint16_t color);
void ST7789_DrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color);
void ST7789_DrawRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color);
void ST7789_DrawFilledRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color);
void ST7789_DrawCircle(uint16_t x, uint16_t y, uint16_t r, uint16_t color);
void ST7789_DrawFilledCircle(uint16_t x, uint16_t y, uint16_t r, uint16_t color);
void ST7789_DrawString(char *str, uint16_t x, uint16_t y);
void ST7789_DrawECGWaveform(uint16_t *buf, uint16_t buf_len, uint16_t x, uint16_t y);
```
在AD8232.h头文件中,定义了以下AD8232心电模块相关的函数:
```c
void AD8232_Init(void);
void AD8232_Sample(void);
uint16_t AD8232_CalculateHeartRate(uint16_t *buf, uint16_t buf_len);
```
阅读全文