【生物传感器评估平台MAX30001GEVKIT】使用STM32采集传感器数据
<h1><b>原理图</b></h1><p >使用STM32的SPI1引脚 PA4-PA7</p>
<p > </p>
<p > </p>
<p >断开如下4个跳线1和2引脚的短接,STM32的PA4-7接如下4个跳线处3引脚。</p>
<p > </p>
<p > </p>
<h1 ><b>接口与寄存器</b></h1>
<p >MAX30001g的SPI接口时序如下</p>
<p >支持模式0和3,MSB,最大12M,详见手册</p>
<p > </p>
<p > </p>
<p > </p>
<p >寄存器</p>
<p > </p>
<p > </p>
<p > </p>
<h1 ><b>代码</b></h1>
<p > </p>
<h2 ><b>SPI接口</b></h2>
<p >void max30001g_spi_trans(uint8_t sendval, uint8_t* readval)</p>
<p >{</p>
<p > </p>
<p >SPI_I2S_SendData(SPI1, (uint8_t)sendval);</p>
<p >*readval = (uint8_t)(SPI_I2S_ReceiveData(SPI1));</p>
<p >}</p>
<p > </p>
<p >void max30001g_spi_cs(uint8_t cs)</p>
<p >{</p>
<p >if(cs)</p>
<p > {</p>
<p > GPIO_SetBits(GPIOA, GPIO_Pin_4);</p>
<p >}</p>
<p >else</p>
<p > {</p>
<p > GPIO_ResetBits(GPIOA, GPIO_Pin_4);</p>
<p >}</p>
<p >}</p>
<p > </p>
<p >void spi_init()</p>
<p >{</p>
<p >GPIO_InitTypeDef GPIO_InitStruct;</p>
<p >SPI_InitTypeDef SPI_InitStruct;</p>
<p >RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);</p>
<p >/* PA4 - CS</p>
<p > PA5 - SCK</p>
<p > PA6 - MISO</p>
<p > PA7 - MOSI</p>
<p >*/</p>
<p >GPIO_StructInit(&GPIO_InitStruct);</p>
<p >GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;</p>
<p >GPIO_InitStruct.GPIO_Speed =GPIO_Speed_50MHz;</p>
<p >GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;</p>
<p >GPIO_Init(GPIOA, &GPIO_InitStruct);</p>
<p > </p>
<p >GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;</p>
<p >GPIO_Init(GPIOA, &GPIO_InitStruct);</p>
<p > </p>
<p >GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;</p>
<p >GPIO_Init(GPIOA, &GPIO_InitStruct);</p>
<p > </p>
<p >GPIO_InitStruct.GPIO_Speed =GPIO_Speed_50MHz;</p>
<p >GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;</p>
<p >GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;</p>
<p >GPIO_Init(GPIOA, &GPIO_InitStruct);</p>
<p > </p>
<p >/* SPI1 */</p>
<p >RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);</p>
<p > SPI_StructInit(&SPI_InitStruct);</p>
<p >SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;</p>
<p >SPI_InitStruct.SPI_Mode = SPI_Mode_Master;</p>
<p >SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;</p>
<p >SPI_InitStruct.SPI_CPOL = SPI_CPOL_High;</p>
<p >SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;</p>
<p >SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;</p>
<p >SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; /*72/16M*/</p>
<p >SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;</p>
<p >SPI_Init(SPI1, &SPI_InitStruct);</p>
<p >SPI_Cmd(SPI1, ENABLE);</p>
<p >}</p>
<p > </p>
<h2 ><b>寄存器操作</b></h2>
<p > </p>
<h2 ><b>测试</b></h2>
<p >spi_init();</p>
<p >while(1)</p>
<p > {</p>
<p > max30001g_read_reg(0x21,&(rx_buffer));</p>
<p > max30001g_read_reg(0x23,&(rx_buffer));</p>
<p > max30001g_read_reg(0x25,&(rx_buffer));</p>
<p > max30001g_read_reg(0x01,&(rx_buffer));</p>
<p >}</p>
<p > </p>
<pre>
<code class="language-cpp">
#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;
}
</code></pre>
<p> </p>
<pre>
<code class="language-cpp">#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
</code></pre>
<p> </p>
<p>各位原理代码都非常详细,感谢分享!</p>
<p>寄存器操作还是感觉有点稍微麻烦一些</p>
页:
[1]