stm32f103c8t6利用AD8232的心电信号通过ST778显示心率的程序
时间: 2023-08-26 17:04:54 浏览: 200
以下是一个基于STM32F103C8T6和AD8232的心电信号测量和ST7789显示心率的程序示例。
首先,需要连接AD8232到STM32F103C8T6板上,并连接ST7789显示屏。AD8232连接到ADC1通道0,ST7789连接到SPI1。
```c
#include "stm32f10x.h"
#include "stm32f10x_adc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_spi.h"
#define SPI1_CS_PIN GPIO_Pin_4
#define SPI1_CS_PORT GPIOA
#define SPI1_DC_PIN GPIO_Pin_5
#define SPI1_DC_PORT GPIOA
void SPI1_Init(void);
void SPI1_SendData(uint8_t data);
void SPI1_SendCommand(uint8_t cmd);
void SPI1_SendData16(uint16_t data);
void SPI1_SendCommand16(uint16_t cmd);
void ST7789_Init(void);
void ST7789_SetRotation(uint8_t m);
void ST7789_DrawPixel(uint16_t x, uint16_t y, uint16_t color);
void ST7789_FillRect(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color);
void ADC1_Init(void);
uint16_t ADC1_ReadChannel(uint8_t channel);
int main(void)
{
uint16_t heart_rate = 0;
char buffer[5];
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = SPI1_CS_PIN;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI1_CS_PORT, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = SPI1_DC_PIN;
GPIO_Init(SPI1_DC_PORT, &GPIO_InitStruct);
SPI1_Init();
ST7789_Init();
ADC1_Init();
while (1)
{
heart_rate = ADC1_ReadChannel(0);
sprintf(buffer, "%d", heart_rate);
ST7789_FillRect(0, 0, 240, 240, 0xFFFF);
ST7789_SetRotation(1);
ST7789_DrawString(0, 0, buffer, 0x0000, 0xFFFF, 2);
Delay(1000);
}
}
void SPI1_Init(void)
{
SPI_InitTypeDef SPI_InitStruct;
SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(SPI1, &SPI_InitStruct);
SPI_Cmd(SPI1, ENABLE);
}
void SPI1_SendData(uint8_t data)
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
;
SPI_I2S_SendData(SPI1, data);
}
void SPI1_SendCommand(uint8_t cmd)
{
GPIO_ResetBits(SPI1_DC_PORT, SPI1_DC_PIN);
GPIO_ResetBits(SPI1_CS_PORT, SPI1_CS_PIN);
SPI1_SendData(cmd);
GPIO_SetBits(SPI1_CS_PORT, SPI1_CS_PIN);
}
void SPI1_SendData16(uint16_t data)
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
;
SPI_I2S_SendData(SPI1, (data >> 8) & 0xFF);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
;
SPI_I2S_SendData(SPI1, data & 0xFF);
}
void SPI1_SendCommand16(uint16_t cmd)
{
GPIO_ResetBits(SPI1_DC_PORT, SPI1_DC_PIN);
GPIO_ResetBits(SPI1_CS_PORT, SPI1_CS_PIN);
SPI1_SendData16(cmd);
GPIO_SetBits(SPI1_CS_PORT, SPI1_CS_PIN);
}
void ST7789_Init(void)
{
SPI1_SendCommand(ST7789_SWRESET);
Delay(150);
SPI1_SendCommand(ST7789_SLPOUT);
Delay(500);
SPI1_SendCommand(ST7789_COLMOD);
SPI1_SendData(0x55);
SPI1_SendCommand(ST7789_MADCTL);
SPI1_SendData(0x00);
SPI1_SendCommand(ST7789_CASET);
SPI1_SendData16(0x0000);
SPI1_SendData16(0x00EF);
SPI1_SendCommand(ST7789_RASET);
SPI1_SendData16(0x0000);
SPI1_SendData16(0x013F);
SPI1_SendCommand(ST7789_INVON);
Delay(10);
SPI1_SendCommand(ST7789_NORON);
Delay(10);
SPI1_SendCommand(ST7789_DISPON);
Delay(500);
}
void ST7789_SetRotation(uint8_t m)
{
SPI1_SendCommand(ST7789_MADCTL);
uint8_t data = 0;
switch (m)
{
case 0:
data = ST7789_MADCTL_MX | ST7789_MADCTL_MY | ST7789_MADCTL_RGB;
break;
case 1:
data = ST7789_MADCTL_MY | ST7789_MADCTL_MV | ST7789_MADCTL_RGB;
break;
case 2:
data = ST7789_MADCTL_RGB;
break;
case 3:
data = ST7789_MADCTL_MX | ST7789_MADCTL_MV | ST7789_MADCTL_RGB;
break;
}
SPI1_SendData(data);
}
void ST7789_DrawPixel(uint16_t x, uint16_t y, uint16_t color)
{
SPI1_SendCommand(ST7789_CASET);
SPI1_SendData16(x);
SPI1_SendData16(x);
SPI1_SendCommand(ST7789_RASET);
SPI1_SendData16(y);
SPI1_SendData16(y);
SPI1_SendCommand(ST7789_RAMWR);
SPI1_SendData16(color);
}
void ST7789_FillRect(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color)
{
uint16_t width = x2 - x1 + 1;
uint16_t height = y2 - y1 + 1;
uint16_t size = width * height;
SPI1_SendCommand(ST7789_CASET);
SPI1_SendData16(x1);
SPI1_SendData16(x2);
SPI1_SendCommand(ST7789_RASET);
SPI1_SendData16(y1);
SPI1_SendData16(y2);
SPI1_SendCommand(ST7789_RAMWR);
GPIO_SetBits(SPI1_DC_PORT, SPI1_DC_PIN);
GPIO_ResetBits(SPI1_CS_PORT, SPI1_CS_PIN);
for (uint16_t i = 0; i < size; i++)
{
SPI1_SendData16(color);
}
GPIO_SetBits(SPI1_CS_PORT, SPI1_CS_PIN);
}
void ADC1_Init(void)
{
ADC_InitTypeDef ADC_InitStruct;
ADC_StructInit(&ADC_InitStruct);
ADC_InitStruct.ADC_Mode = ADC_Mode_Independent;
ADC_InitStruct.ADC_ScanConvMode = DISABLE;
ADC_InitStruct.ADC_ContinuousConvMode = ENABLE;
ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStruct.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStruct);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while (ADC_GetResetCalibrationStatus(ADC1))
;
ADC_StartCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1))
;
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5);
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
uint16_t ADC1_ReadChannel(uint8_t channel)
{
ADC_RegularChannelConfig(ADC1, channel, 1, ADC_SampleTime_55Cycles5);
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC))
;
return ADC_GetConversionValue(ADC1);
}
void Delay(uint32_t nCount)
{
for (uint32_t i = 0; i < nCount; i++)
;
}
```
这个程序会读取ADC1通道0的值,并将其转换为心率。然后,使用ST7789显示屏显示心率。可以根据需要调整显示屏的位置和大小。
阅读全文