qinyunti 发表于 2023-9-10 00:47

【生物传感器评估平台MAX30001GEVKIT】使用STM32采集传感器数据

<h1><b>原理图</b></h1>

<p >使用STM32的SPI1引脚 PA4-PA7</p>

<p > &nbsp;</p>

<p >&nbsp;</p>

<p >断开如下4个跳线1和2引脚的短接,STM32的PA4-7接如下4个跳线处3引脚。</p>

<p > &nbsp;</p>

<p >&nbsp;</p>

<h1 ><b>接口与寄存器</b></h1>

<p >MAX30001g的SPI接口时序如下</p>

<p >支持模式0和3,MSB,最大12M,详见手册</p>

<p >&nbsp;</p>

<p > &nbsp;</p>

<p >&nbsp;</p>

<p >寄存器</p>

<p > &nbsp;</p>

<p > &nbsp;</p>

<p >&nbsp;</p>

<h1 ><b>代码</b></h1>

<p >&nbsp;</p>

<h2 ><b>SPI接口</b></h2>

<p >void max30001g_spi_trans(uint8_t sendval, uint8_t* readval)</p>

<p >{</p>

<p >&nbsp;</p>

<p >SPI_I2S_SendData(SPI1, (uint8_t)sendval);</p>

<p >*readval = (uint8_t)(SPI_I2S_ReceiveData(SPI1));</p>

<p >}</p>

<p >&nbsp;</p>

<p >void max30001g_spi_cs(uint8_t cs)</p>

<p >{</p>

<p >if(cs)</p>

<p >&nbsp;&nbsp;{</p>

<p >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GPIO_SetBits(GPIOA, GPIO_Pin_4);</p>

<p >}</p>

<p >else</p>

<p >&nbsp;&nbsp;{</p>

<p >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GPIO_ResetBits(GPIOA, GPIO_Pin_4);</p>

<p >}</p>

<p >}</p>

<p >&nbsp;</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 >&nbsp;&nbsp;&nbsp;PA5 - SCK</p>

<p >&nbsp;PA6 - MISO</p>

<p >&nbsp;PA7 - MOSI</p>

<p >*/</p>

<p >GPIO_StructInit(&amp;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, &amp;GPIO_InitStruct);</p>

<p >&nbsp;</p>

<p >GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;</p>

<p >GPIO_Init(GPIOA, &amp;GPIO_InitStruct);</p>

<p >&nbsp;</p>

<p >GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;</p>

<p >GPIO_Init(GPIOA, &amp;GPIO_InitStruct);</p>

<p >&nbsp;</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, &amp;GPIO_InitStruct);</p>

<p >&nbsp;</p>

<p >/* SPI1 */</p>

<p >RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);</p>

<p >&nbsp;&nbsp;SPI_StructInit(&amp;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, &amp;SPI_InitStruct);</p>

<p >SPI_Cmd(SPI1, ENABLE);</p>

<p >}</p>

<p >&nbsp;</p>

<h2 ><b>寄存器操作</b></h2>

<p >&nbsp;</p>

<h2 ><b>测试</b></h2>

<p >spi_init();</p>

<p >while(1)</p>

<p >&nbsp;&nbsp;{</p>

<p >&nbsp;&nbsp;max30001g_read_reg(0x21,&amp;(rx_buffer));</p>

<p >&nbsp;&nbsp;max30001g_read_reg(0x23,&amp;(rx_buffer));</p>

<p >&nbsp;&nbsp;max30001g_read_reg(0x25,&amp;(rx_buffer));</p>

<p >&nbsp;&nbsp;max30001g_read_reg(0x01,&amp;(rx_buffer));</p>

<p >}</p>

<p >&nbsp;</p>

<pre>
<code class="language-cpp">
#include &lt;stdint.h&gt;


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&lt;&lt;1)|0x01, &amp;readval);/* A6-A0 bit0=1 R*/
        max30001g_spi_trans(0xFF, &amp;readval);   /* D23-D16*/
        readval |= (uint32_t)readval&lt;&lt;16;
        max30001g_spi_trans(0xFF, &amp;readval);   /* D15-D8*/
        readval |= (uint32_t)readval&lt;&lt;8;
        max30001g_spi_trans(0xFF, &amp;readval);   /* D7-D0*/
        readval |= (uint32_t)readval&lt;&lt;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&lt;&lt;1)|0x00, &amp;readval);/* A6-A0 bit0=0 W*/
        max30001g_spi_trans((val&gt;&gt;16) &amp; 0xFF, &amp;readval);/* D23-D16*/
        max30001g_spi_trans((val&gt;&gt;8) &amp; 0xFF, &amp;readval);/* D15-D8*/
        max30001g_spi_trans((val&gt;&gt;0) &amp; 0xFF, &amp;readval);/* D7-D0*/
        max30001g_spi_cs(1);
        return 0;
}

</code></pre>

<p>&nbsp;</p>

<pre>
<code class="language-cpp">#ifndef MAX30001G_H
#define MAX30001G_H

#include &lt;stdint.h&gt;
int max30001g_read_reg(uint8_t reg, uint32_t* val);
int max30001g_write_reg(uint8_t reg, uint32_t val);

#endif
</code></pre>

<p>&nbsp;</p>

lugl4313820 发表于 2023-9-10 10:54

<p>各位原理代码都非常详细,感谢分享!</p>

Jacktang 发表于 2023-9-10 21:22

<p>寄存器操作还是感觉有点稍微麻烦一些</p>
页: [1]
查看完整版本: 【生物传感器评估平台MAX30001GEVKIT】使用STM32采集传感器数据