|
MAX30102+stm32測心率血氧
只留下MAX30102、main、I2C、算法、四個文件就行,在I2C中改一下IO
單片機(jī)源程序如下:
- /** \file max30102.cpp ******************************************************
- *
- * Project: MAXREFDES117#
- * Filename: max30102.cpp
- * Description: This module is an embedded controller driver for the MAX30102
- *
- * Revision History:
- *\n 1-18-2016 Rev 01.00 GL Initial release.
- *\n
- */
- #include "max30102.h"
- #include "myiic.h"
- #define max30102_WR_address 0xAE
- bool maxim_max30102_write_reg(uint8_t uch_addr, uint8_t uch_data)
- /**
- * \brief Write a value to a MAX30102 register
- * \par Details
- * This function writes a value to a MAX30102 register
- *
- * \param[in] uch_addr - register address
- * \param[in] uch_data - register data
- *
- * \retval true on success
- */
- {
- /* 第1步:發(fā)起I2C總線啟動信號 */
- i2c_Start();
- /* 第2步:發(fā)起控制字節(jié),高7bit是地址,bit0是讀寫控制位,0表示寫,1表示讀 */
- i2c_SendByte(max30102_WR_address | I2C_WR); /* 此處是寫指令 */
- /* 第3步:發(fā)送ACK */
- if (i2c_WaitAck() != 0)
- {
- goto cmd_fail; /* EEPROM器件無應(yīng)答 */
- }
- /* 第4步:發(fā)送字節(jié)地址 */
- i2c_SendByte(uch_addr);
- if (i2c_WaitAck() != 0)
- {
- goto cmd_fail; /* EEPROM器件無應(yīng)答 */
- }
- /* 第5步:開始寫入數(shù)據(jù) */
- i2c_SendByte(uch_data);
- /* 第6步:發(fā)送ACK */
- if (i2c_WaitAck() != 0)
- {
- goto cmd_fail; /* EEPROM器件無應(yīng)答 */
- }
- /* 發(fā)送I2C總線停止信號 */
- i2c_Stop();
- return true; /* 執(zhí)行成功 */
- cmd_fail: /* 命令執(zhí)行失敗后,切記發(fā)送停止信號,避免影響I2C總線上其他設(shè)備 */
- /* 發(fā)送I2C總線停止信號 */
- i2c_Stop();
- return false;
- }
- bool maxim_max30102_read_reg(uint8_t uch_addr, uint8_t *puch_data)
- /**
- * \brief Read a MAX30102 register
- * \par Details
- * This function reads a MAX30102 register
- *
- * \param[in] uch_addr - register address
- * \param[out] puch_data - pointer that stores the register data
- *
- * \retval true on success
- */
- {
- /* 第1步:發(fā)起I2C總線啟動信號 */
- i2c_Start();
- /* 第2步:發(fā)起控制字節(jié),高7bit是地址,bit0是讀寫控制位,0表示寫,1表示讀 */
- i2c_SendByte(max30102_WR_address | I2C_WR); /* 此處是寫指令 */
- /* 第3步:發(fā)送ACK */
- if (i2c_WaitAck() != 0)
- {
- goto cmd_fail; /* EEPROM器件無應(yīng)答 */
- }
- /* 第4步:發(fā)送字節(jié)地址, */
- i2c_SendByte((uint8_t)uch_addr);
- if (i2c_WaitAck() != 0)
- {
- goto cmd_fail; /* EEPROM器件無應(yīng)答 */
- }
- /* 第6步:重新啟動I2C總線。下面開始讀取數(shù)據(jù) */
- i2c_Start();
- /* 第7步:發(fā)起控制字節(jié),高7bit是地址,bit0是讀寫控制位,0表示寫,1表示讀 */
- i2c_SendByte(max30102_WR_address | I2C_RD); /* 此處是讀指令 */
- /* 第8步:發(fā)送ACK */
- if (i2c_WaitAck() != 0)
- {
- goto cmd_fail; /* EEPROM器件無應(yīng)答 */
- }
- /* 第9步:讀取數(shù)據(jù) */
- {
- *puch_data = i2c_ReadByte(); /* 讀1個字節(jié) */
- i2c_NAck(); /* 最后1個字節(jié)讀完后,CPU產(chǎn)生NACK信號(驅(qū)動SDA = 1) */
- }
- /* 發(fā)送I2C總線停止信號 */
- i2c_Stop();
- return true; /* 執(zhí)行成功 返回data值 */
- cmd_fail: /* 命令執(zhí)行失敗后,切記發(fā)送停止信號,避免影響I2C總線上其他設(shè)備 */
- /* 發(fā)送I2C總線停止信號 */
- i2c_Stop();
- return false;
- }
- bool maxim_max30102_init(void)
- /**
- * \brief Initialize the MAX30102
- * \par Details
- * This function initializes the MAX30102
- *
- * \param None
- *
- * \retval true on success
- */
- {
- if(!maxim_max30102_write_reg(REG_INTR_ENABLE_1, 0xc0)) // INTR setting
- return false;
- if(!maxim_max30102_write_reg(REG_INTR_ENABLE_2, 0x00))
- return false;
- if(!maxim_max30102_write_reg(REG_FIFO_WR_PTR, 0x00)) //FIFO_WR_PTR[4:0]
- return false;
- if(!maxim_max30102_write_reg(REG_OVF_COUNTER, 0x00)) //OVF_COUNTER[4:0]
- return false;
- if(!maxim_max30102_write_reg(REG_FIFO_RD_PTR, 0x00)) //FIFO_RD_PTR[4:0]
- return false;
- if(!maxim_max30102_write_reg(REG_FIFO_CONFIG, 0x6f)) //sample avg = 8, fifo rollover=false, fifo almost full = 17
- return false;
- if(!maxim_max30102_write_reg(REG_MODE_CONFIG, 0x03)) //0x02 for Red only, 0x03 for SpO2 mode 0x07 multimode LED
- return false;
- if(!maxim_max30102_write_reg(REG_SPO2_CONFIG, 0x2F)) // SPO2_ADC range = 4096nA, SPO2 sample rate (400 Hz), LED pulseWidth (411uS)
- return false;
- if(!maxim_max30102_write_reg(REG_LED1_PA, 0x17)) //Choose value for ~ 4.5mA for LED1
- return false;
- if(!maxim_max30102_write_reg(REG_LED2_PA, 0x17)) // Choose value for ~ 4.5mA for LED2
- return false;
- if(!maxim_max30102_write_reg(REG_PILOT_PA, 0x7f)) // Choose value for ~ 25mA for Pilot LED
- return false;
- return true;
- }
- bool maxim_max30102_read_fifo(uint32_t *pun_red_led, uint32_t *pun_ir_led)
- /**
- * \brief Read a set of samples from the MAX30102 FIFO register
- * \par Details
- * This function reads a set of samples from the MAX30102 FIFO register
- *
- * \param[out] *pun_red_led - pointer that stores the red LED reading data
- * \param[out] *pun_ir_led - pointer that stores the IR LED reading data
- *
- * \retval true on success
- */
- {
- uint32_t un_temp;
- uint8_t uch_temp;
- *pun_ir_led = 0;
- *pun_red_led = 0;
- maxim_max30102_read_reg(REG_INTR_STATUS_1, &uch_temp);
- maxim_max30102_read_reg(REG_INTR_STATUS_2, &uch_temp);
- /* 第1步:發(fā)起I2C總線啟動信號 */
- i2c_Start();
- /* 第2步:發(fā)起控制字節(jié),高7bit是地址,bit0是讀寫控制位,0表示寫,1表示讀 */
- i2c_SendByte(max30102_WR_address | I2C_WR); /* 此處是寫指令 */
- /* 第3步:發(fā)送ACK */
- if (i2c_WaitAck() != 0)
- {
- printf("read fifo failed");
- goto cmd_fail; /* EEPROM器件無應(yīng)答 */
- }
- /* 第4步:發(fā)送字節(jié)地址, */
- i2c_SendByte((uint8_t)REG_FIFO_DATA);
- if (i2c_WaitAck() != 0)
- {
- goto cmd_fail; /* EEPROM器件無應(yīng)答 */
- }
- /* 第6步:重新啟動I2C總線。下面開始讀取數(shù)據(jù) */
- i2c_Start();
- /* 第7步:發(fā)起控制字節(jié),高7bit是地址,bit0是讀寫控制位,0表示寫,1表示讀 */
- i2c_SendByte(max30102_WR_address | I2C_RD); /* 此處是讀指令 */
- /* 第8步:發(fā)送ACK */
- if (i2c_WaitAck() != 0)
- {
- goto cmd_fail; /* EEPROM器件無應(yīng)答 */
- }
- un_temp = i2c_ReadByte();
- i2c_Ack();
- un_temp <<= 16;
- *pun_red_led += un_temp;
- un_temp = i2c_ReadByte();
- i2c_Ack();
- un_temp <<= 8;
- *pun_red_led += un_temp;
- un_temp = i2c_ReadByte();
- i2c_Ack();
- *pun_red_led += un_temp;
- un_temp = i2c_ReadByte();
- i2c_Ack();
- un_temp <<= 16;
- *pun_ir_led += un_temp;
- un_temp = i2c_ReadByte();
- i2c_Ack();
- un_temp <<= 8;
- *pun_ir_led += un_temp;
- un_temp = i2c_ReadByte();
- i2c_Ack();
- *pun_ir_led += un_temp;
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
stm32 max30102上傳整數(shù)型數(shù)據(jù).zip
(366.15 KB, 下載次數(shù): 820)
2018-4-16 12:54 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評分
-
查看全部評分
|