资源简介
利用STM32F407开发板对MAX30102体温模块的开发,可以接受整形数据,需要对IIC串口初始化,还有数据协议包的发送
代码片段和文件信息
#include
#include “stm32f4xx.h“
#include “SysTick.h“
void GECM4_AT24CXX_INIT(void)
{
// 1. 配置i2c1引脚
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB ENABLE);
GPIO_InitTypeDef p;
p.GPIO_Mode = GPIO_Mode_AF;
p.GPIO_OType = GPIO_OType_OD;
p.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
p.GPIO_PuPd = GPIO_PuPd_NOPULL;
p.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB &p);
GPIO_PinAFConfig(GPIOB GPIO_PinSource8 GPIO_AF_I2C1);
GPIO_PinAFConfig(GPIOB GPIO_PinSource9 GPIO_AF_I2C1);
// 2. 初始化I2C控制器
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1 ENABLE);
I2C_InitTypeDef i;
i.I2C_Ack = I2C_Ack_Enable;
i.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
i.I2C_ClockSpeed = 100000;
i.I2C_DutyCycle = I2C_DutyCycle_16_9;
i.I2C_Mode = I2C_Mode_I2C;
i.I2C_OwnAddress1 = 0x55; //没用到
I2C_Init(I2C1 &i);
// 3. I2C Enable
I2C_Cmd(I2C1 ENABLE);
}
void Write_A_Byte_To_Max30102(uint8_t word_addr uint8_t data)
{
//(1) Send Start Signal
//printf(“%d\r\n“ __LINE__);
//当IIC总线BUSY时,我们一直等
while ( I2C_GetFlagStatus(I2C1 I2C_FLAG_BUSY) == SET );
//printf(“%d\r\n“ __LINE__);
//产生一个i2c起始信号
I2C_GenerateSTART(I2C1 ENABLE);
//printf(“%d\r\n“ __LINE__);
//wait EV5
while ( I2C_CheckEvent(I2C1 I2C_EVENT_MASTER_MODE_SELECT) == ERROR);
//printf(“%d\r\n“ __LINE__);
//------
// I2C Slave ADDR(表示你要与哪个设备通信)
I2C_Send7bitAddress(I2C1 0xAE I2C_Direction_Transmitter); //主发
//wait EV6 ; Master Send mode
while ( I2C_CheckEvent(I2C1
I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) == ERROR);
// Send Word Addr
I2C_SendData(I2C1 word_addr);
//wait EV8
while ( I2C_CheckEvent(I2C1
I2C_EVENT_MASTER_BYTE_TRANSMITTING) == ERROR);
//Send data
I2C_SendData(I2C1 data);
//wait EV8
while ( I2C_CheckEvent(I2C1
I2C_EVENT_MASTER_BYTE_TRANSMITTED) == ERROR);
I2C_GenerateSTOP(I2C1 ENABLE);
//printf(“finished\r\n“);
}
uint8_t Read_A_Byte_From_Max30102(uint8_t word_addr)
{
uint8_t recv;
//(1) Send Start Signal
//当IIC总线BUSY时,我们一直等
while ( I2C_GetFlagStatus(I2C1 I2C_FLAG_BUSY) == SET );
//printf(“%s L_%d\n“ __FUNCTION__ __LINE__);
//产生一个i2c起始信号
I2C_GenerateSTART(I2C1 ENABLE);
// wait ev5
while ( I2C_CheckEvent(I2C1 I2C_EVENT_MASTER_MODE_SELECT) == ERROR);
//send I2C Slave ADDR
I2C_Send7bitAddress(I2C1 0xAE I2C_Direction_Transmitter);
// wait ev6
while ( I2C_CheckEvent(I2C1
I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) == ERROR);
// word addr
I2C_SendData(I2C1word_addr);
while ( I2C_CheckEvent(I2C1 I2C_EVENT_MASTER_BYTE_TRANSMITTED) == ERROR);
I2C_GenerateSTART(I2C1 ENABLE);
// wait ev5
while ( I2C_CheckEvent(I2C1 I2C_EVENT_MASTER_MODE_SELECT) == ERROR);
//send I2C Slave ADDR
I2C_Send7bitAddress(I2C1 0xAF I2C_
评论
共有 条评论