一、引脚
二、代码
iic.c文件
#include "iic.h"
void IIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
SCL_H;
SDA_H;
}
void IIC_Start(void)
{
SDA_OUT();
SDA_H;
SCL_H;
IIC_Delay_us(4);
SDA_L;
IIC_Delay_us(4);
SCL_L;
}
void IIC_Stop(void)
{
SDA_OUT();
SCL_L;
SDA_L;
IIC_Delay_us(2);
SCL_H;
IIC_Delay_us(1);
SDA_H;
IIC_Delay_us(4);
}
u8 IIC_Wait_Ack(void)
{
u8 ucErrTime = 0;
SDA_IN();
SDA_H;
IIC_Delay_us(1);
SCL_H;
IIC_Delay_us(1);
while (SDA_READ)
{
ucErrTime++;
if (ucErrTime > 250)
{
IIC_Stop();
return 1;
}
}
SCL_L;
IIC_Delay_us(30);
return 0;
}
void IIC_Ack(void)
{
SCL_L;
SDA_OUT();
SDA_L;
IIC_Delay_us(2);
SCL_H;
IIC_Delay_us(2);
SCL_L;
}
void IIC_NAck(void)
{
SCL_L;
SDA_OUT();
SDA_H;
IIC_Delay_us(2);
SCL_H;
IIC_Delay_us(2);
SCL_L;
}
void IIC_Send_Byte(u8 txd)
{
u8 t;
SDA_OUT();
SCL_L;
for (t = 0; t < 8; t++)
{
if (txd & 0x80)
SDA_H
else
SDA_L;
txd <<= 1;
IIC_Delay_us(2);
SCL_H;
IIC_Delay_us(2);
SCL_L;
}
}
u8 IIC_Read_Byte(unsigned char ack)
{
unsigned char i, receive = 0;
SDA_IN();
for (i = 0; i < 8; i++)
{
SCL_L;
IIC_Delay_us(2);
SCL_H;
receive <<= 1;
if (SDA_READ)
receive++;
IIC_Delay_us(1);
}
if (!ack)
IIC_NAck();
else
IIC_Ack();
return receive;
}
void IIC_Delay_us(u16 us)
{
{
u16 i = 0;
while (us--)
{
i = 6;
while (i--)
;
}
}
}
iic.h文件
#ifndef __IIC_H
#define __IIC_H
#include "stm32f4xx_conf.h"
#define SDA_IN() {GPIOB->MODER&=~(3<<(1*2));GPIOB->MODER|=0<<1*2;}
#define SDA_OUT() {GPIOB->MODER&=~(3<<(1*2));GPIOB->MODER|=1<<1*2;}
#define SCL_L {GPIOB->ODR&=0xFFFB;}
#define SCL_H {GPIOB->ODR|=0x0004;}
#define SDA_L {GPIOB->ODR&=0xFFFD;}
#define SDA_H {GPIOB->ODR|=0x0002;}
#define SDA_READ (GPIOB->IDR&0x0002)
#define ACK 1
#define NACK 0
void IIC_Init(void);
void IIC_Start(void);
void IIC_Stop(void);
void IIC_Send_Byte(u8 byte);
u8 IIC_Read_Byte(unsigned char ack);
u8 IIC_Wait_Ack(void);
void IIC_Ack(void);
void IIC_NAck(void);
void IIC_Delay_us(u16 us);
#endif
htu21d.c
#include <stdio.h>
#include "htu21d.h"
#include "iic.h"
#include "delay.h"
#include "usart.h"
SENSOR_DATA_t g_sensorData ={0,0,0,0,0};
static u32 timer = 0;
static u8 isTempOrHumi = 1;
static u8 htu21_res=0;
void HTU21D_Init(void){
IIC_Start();
IIC_Send_Byte(HTU21D_IIC_ADDR<<1);
IIC_Wait_Ack();
IIC_Send_Byte(CMD_RESET_CH21D);
IIC_Wait_Ack();
IIC_Stop();
Delay_ms(18);
HTU21D_Temp_Trig();
Delay_ms(60);
}
u8 HTU21D_ReadSensor(void){
if(HasElapsed(&timer, 500)){
if(isTempOrHumi){
htu21_res = HTU21D_Read_Temp(&g_sensorData.innerTemp);
HTU21D_Humi_Trig();
isTempOrHumi = 0;
}else{
htu21_res = HTU21D_Read_Humi(&g_sensorData.humi);
HTU21D_Temp_Trig();
isTempOrHumi = 1;
}
}
return htu21_res;
}
u8 HTU21D_Read_Temp(float* temp){
u32 data;
IIC_Start();
IIC_Send_Byte((HTU21D_IIC_ADDR<<1)+1);
if(!IIC_Wait_Ack()){
data = IIC_Read_Byte(ACK);
data <<= 8;
data += IIC_Read_Byte(ACK);
data <<= 8;
data += IIC_Read_Byte(NACK);
IIC_Stop();
data >>= 8;
data &= 0xFFFC;
*temp = (175.72*data)/65536 - 46.85;
return 0;
}
return 1;
}
u8 HTU21D_Read_Humi(float* humi){
u32 data;
IIC_Start();
IIC_Send_Byte((HTU21D_IIC_ADDR<<1)+1);
if(!IIC_Wait_Ack()){
data = IIC_Read_Byte(ACK);
data <<= 8;
data += IIC_Read_Byte(ACK);
data <<= 8;
data += IIC_Read_Byte(NACK);
IIC_Stop();
data >>= 8;
data &= 0xFFF0;
*humi = (125.0*data)/65536 - 6;
return 0;
}
return 1;
}
void HTU21D_Temp_Trig(){
IIC_Start();
IIC_Send_Byte(HTU21D_IIC_ADDR<<1);
IIC_Wait_Ack();
IIC_Send_Byte(CMD_T_TRIG);
IIC_Wait_Ack();
}
void HTU21D_Humi_Trig(){
IIC_Start();
IIC_Send_Byte(HTU21D_IIC_ADDR<<1);
IIC_Wait_Ack();
IIC_Send_Byte(CMD_RH_TRIG);
IIC_Wait_Ack();
}
htu21d.h
#ifndef __HTU21D_H
#define __HTU21D_H
#include "stm32f4xx_conf.h"
typedef struct
{
float pressure;
float depth;
float innerTemp;
float humi;
float waterTemp;
}SENSOR_DATA_t;
extern SENSOR_DATA_t g_sensorData;
#define HTU21D_IIC_ADDR 0x40
#define CMD_LOADER_DATA 0x81
#define CMD_T_TRIG_HOSTMODEL 0xE3
#define CMD_RH_TRIG_HOSTMODEL 0xE5
#define CMD_T_TRIG 0xF3
#define CMD_RH_TRIG 0xF5
#define CMD_USER_WRITE 0xE6
#define CMD_USER_READ 0xE7
#define CMD_RESET_CH21D 0xFE
void HTU21D_Init(void);
u8 HTU21D_ReadSensor(void);
u8 HTU21D_Read_Temp(float* temp);
u8 HTU21D_Read_Humi(float* humi);
void HTU21D_Temp_Trig(void);
void HTU21D_Humi_Trig(void);
void HTU21D_Process(void);
#endif
main.c
void HTU21D_Process(void)
{
HTU21D_ReadSensor() printf("temp:%.3f,humi:%.3f\r\n",g_sensorData.innerTemp,g_sensorData.humi);
}
}
int main()
{
while(1)
{
HTU21D_Process();
delay_ms(100);
}
}
|