【生物传感器评估平台MAX30001GEVKIT】使用STM32采集传感器数据
[复制链接]
原理图
使用STM32的SPI1引脚 PA4-PA7
断开如下4个跳线1和2引脚的短接,STM32的PA4-7接如下4个跳线处3引脚。
接口与寄存器
MAX30001g的SPI接口时序如下
支持模式0和3,MSB,最大12M,详见手册
寄存器
代码
SPI接口
void max30001g_spi_trans(uint8_t sendval, uint8_t* readval)
{
SPI_I2S_SendData(SPI1, (uint8_t)sendval);
*readval = (uint8_t)(SPI_I2S_ReceiveData(SPI1));
}
void max30001g_spi_cs(uint8_t cs)
{
if(cs)
{
GPIO_SetBits(GPIOA, GPIO_Pin_4);
}
else
{
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
}
}
void spi_init()
{
GPIO_InitTypeDef GPIO_InitStruct;
SPI_InitTypeDef SPI_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
/* PA4 - CS
PA5 - SCK
PA6 - MISO
PA7 - MOSI
*/
GPIO_StructInit(&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStruct.GPIO_Speed =GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Speed =GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;
GPIO_Init(GPIOA, &GPIO_InitStruct);
/* SPI1 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
SPI_StructInit(&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_High;
SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; /*72/16M*/
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(SPI1, &SPI_InitStruct);
SPI_Cmd(SPI1, ENABLE);
}
寄存器操作
测试
spi_init();
while(1)
{
max30001g_read_reg(0x21,&(rx_buffer[0]));
max30001g_read_reg(0x23,&(rx_buffer[1]));
max30001g_read_reg(0x25,&(rx_buffer[2]));
max30001g_read_reg(0x01,&(rx_buffer[3]));
}
#include <stdint.h>
extern void max30001g_spi_trans(uint8_t sendval, uint8_t* readval);
extern void max30001g_spi_cs(uint8_t);
int max30001g_read_reg(uint8_t reg, uint32_t* val)
{
uint8_t readval;
uint32_t tmp = 0;
max30001g_spi_cs(0);
max30001g_spi_trans((reg<<1)|0x01, &readval); /* A6-A0 bit0=1 R*/
max30001g_spi_trans(0xFF, &readval); /* D23-D16*/
readval |= (uint32_t)readval<<16;
max30001g_spi_trans(0xFF, &readval); /* D15-D8*/
readval |= (uint32_t)readval<<8;
max30001g_spi_trans(0xFF, &readval); /* D7-D0*/
readval |= (uint32_t)readval<<0;
max30001g_spi_cs(1);
*val = tmp;
return 0;
}
int max30001g_write_reg(uint8_t reg, uint32_t val)
{
uint8_t readval;
max30001g_spi_cs(0);
max30001g_spi_trans((reg<<1)|0x00, &readval); /* A6-A0 bit0=0 W*/
max30001g_spi_trans((val>>16) & 0xFF, &readval); /* D23-D16*/
max30001g_spi_trans((val>>8) & 0xFF, &readval); /* D15-D8*/
max30001g_spi_trans((val>>0) & 0xFF, &readval); /* D7-D0*/
max30001g_spi_cs(1);
return 0;
}
#ifndef MAX30001G_H
#define MAX30001G_H
#include <stdint.h>
int max30001g_read_reg(uint8_t reg, uint32_t* val);
int max30001g_write_reg(uint8_t reg, uint32_t val);
#endif
|