STM32F4模拟I2C读写HTU21D程序

2019-07-20 08:19发布

///////////////////////htu21d.c///////////////////////////////////////////////////
#include "htu21d.h"
#include "myiic.h"
#include "delay.h"
#include "usart.h"
#include "lcd.h"
#include "text.h"


void HTU21D_SoftReset(void)
{
        I2C1_Start();
        I2C1_Send_Byte(0x80);
        I2C1_Wait_Ack();
        I2C1_Send_Byte(0xFE);
        I2C1_Wait_Ack();
        I2C1_Stop();
        delay_ms(30);
}

void HTU21D_Init(void)
{
        I2C1_Init();
        delay_us(5);
        HTU21D_SoftReset();
}

float HTU21D_Measure_Temperature_NHM(void)
{
        u8 tmph, tmpl;
        u16 tmp;
        float temperature;

        I2C1_Start();
        I2C1_Send_Byte(0x80);
        I2C1_Wait_Ack();
        I2C1_Send_Byte(0xF3); //0xF3
        I2C1_Wait_Ack();

        do
        {
                delay_us(1000);
                I2C1_Start();
                I2C1_Send_Byte(0x81);
        } while(I2C1_Poll_Ack());
        //μè′y2a´áêø

        tmph = I2C1_Recv_Byte(1); //Data(MSB),response with ACK
        tmpl = I2C1_Recv_Byte(1); //Data(LSB),response with ACK
        I2C1_Recv_Byte(0); //Checksum,response without ACK

        I2C1_Stop();

        tmp = (tmph<<8) + tmpl;
        tmp &= ~0x0003;
        temperature = ((float)(tmp) * 0.00268127) - 46.85; //temperature conversion

        return temperature;
}

float HTU21D_Measure_Humidity_NHM(void)
{
        u8 humh, huml;
        u16 hum;
        float humidity;
        u8 ack;

        I2C1_Start();
        I2C1_Send_Byte(0x80);
        I2C1_Wait_Ack();
        I2C1_Send_Byte(0xF5); //0xF5
        I2C1_Wait_Ack();

        do
        {
                delay_us(500);
                I2C1_Start();
                I2C1_Send_Byte(0x81);
                ack = I2C1_Poll_Ack();
        } while(ack);
        //μè′y2aá&#191;&#189;áê&#248;

        humh = I2C1_Recv_Byte(1); //Data(MSB),response with ACK
        huml = I2C1_Recv_Byte(1); //Data(LSB),response with ACK
        I2C1_Recv_Byte(0); //Checksum,response without ACK

        I2C1_Stop();

        hum = (humh<<8) + huml;
        hum &= ~0x0003;
        humidity = ((float)(hum) * 0.00190735) - 6; //humidity conversion

        return humidity;
}

int float2str(float val, int precision, char *buf)
{
        char *cur = NULL;
        char *end = NULL;

        if(!buf)
                return -1;

        if((precision >= 6) || (precision <0))
                return -1;

        switch(precision)
        {
                case 0:
                        sprintf(buf, "%.0f", val);
                        break;
                case 1:
                        sprintf(buf, "%.1f", val);
                        break;
                case 2:
                        sprintf(buf, "%.2f", val);
                        break;
                case 3:
                        sprintf(buf, "%.3f", val);
                        break;
                case 4:
                        sprintf(buf, "%.4f", val);
                        break;
                case 5:
                        sprintf(buf, "%.5f", val);
                        break;
        }

        return 0;
}
/*
int float2str(float val, int precision, char *buf)
{
        char *cur = NULL;
        char *end = NULL;

        if(!buf)
                return -1;

        if((precision >= 6) || (precision <0))
                return -1;

        switch(precision)
        {
                case 0:
                        sprintf(buf, "%.0f", val);
                        break;
                case 1:
                        sprintf(buf, "%.1f", val);
                        break;
                case 2:
                        sprintf(buf, "%.2f", val);
                        break;
                case 3:
                        sprintf(buf, "%.3f", val);
                        break;
                case 4:
                        sprintf(buf, "%.4f", val);
                        break;
                case 5:
                        sprintf(buf, "%.5f", val);
                        break;
        }

        return 0;
}

void HTU21D_SoftReset(void)
{
        I2C1_Start();
        //I2C1_Send_Byte((HTU21D_DEVICE_ADDR<<1) | 0x00);
        I2C1_Send_Byte(0x80);
        I2C1_Wait_Ack();
        I2C1_Send_Byte(COMMAND_SOFT_RESET); //0xFE
        I2C1_Wait_Ack();
        I2C1_Stop();
        delay_ms(50);
}

void HTU21D_Init(void)
{
        I2C1_Init();
        delay_us(1);
        HTU21D_SoftReset();
}

float HTU21D_Measure_Temperature_HM(void)
{
        u8 tmph, tmpl;
        u16 tmp;
        float temperature;

        I2C1_Start();
        I2C1_Send_Byte((HTU21D_DEVICE_ADDR<<1) | 0x00);
        I2C1_Wait_Ack();
        I2C1_Send_Byte(COMMAND_TRIG_TEMP_MEASUREMENT_HM); //0xE3
        I2C1_Wait_Ack();

        I2C1_Start();
        I2C1_Send_Byte((HTU21D_DEVICE_ADDR<<1) | 0x01);
        I2C1_Poll_Ack();
        //μè′y2aá&#191;&#189;áê&#248;
        //I2C1_SCL = 1;
        GPIO_Set(GPIOB, PIN10, GPIO_MODE_IN, GPIO_OTYPE_PP, GPIO_SPEED_2M, GPIO_PUPD_NONE); //PB10éè&#214;&#195;
        //SCL1_IN(); //SCLéè&#214;&#195;&#206;aê&#228;è&#235;
        //delay_ms(80);
        while(READ_SCL1 == 0)
        {
                delay_ms(2);
        }
        //delay_us(5);
        GPIO_Set(GPIOB, PIN10, GPIO_MODE_OUT, GPIO_OTYPE_OD, GPIO_SPEED_50M, GPIO_PUPD_PU); //PB10éè&#214;&#195;
        //SCL1_OUT(); //SCLéè&#214;&#195;&#206;aê&#228;3&#246;
        tmph = I2C1_Recv_Byte(1); //Data(MSB),response with ACK
        tmpl = I2C1_Recv_Byte(1); //Data(LSB),response with ACK
        I2C1_Recv_Byte(0); //Checksum,response without ACK

        I2C1_Stop();

        tmp = (tmph<<8) + tmpl;
        tmp &= ~0x0003;
        temperature = ((float)(tmp) * 0.00268127) - 46.85; //temperature conversion

        return temperature;
}

float HTU21D_Measure_Humidity_HM(void)
{
        u8 humh, huml;
        u16 hum;
        float humidity;

        I2C1_Start();
        I2C1_Send_Byte((HTU21D_DEVICE_ADDR<<1) | 0x00);
        I2C1_Wait_Ack();
        I2C1_Send_Byte(COMMAND_TRIG_HUMI_MEASUREMENT_HM); //0xE5
        I2C1_Wait_Ack();

        I2C1_Start();
        I2C1_Send_Byte((HTU21D_DEVICE_ADDR<<1) | 0x01);
        I2C1_Poll_Ack();
        //μè′y2aá&#191;&#189;áê&#248;
        //I2C1_SCL = 1;
        GPIO_Set(GPIOB, PIN10, GPIO_MODE_IN, GPIO_OTYPE_PP, GPIO_SPEED_2M, GPIO_PUPD_NONE); //PB10éè&#214;&#195;
        //SCL1_IN(); //SCLéè&#214;&#195;&#206;aê&#228;è&#235;
        //delay_ms(50);
        while(READ_SCL1 == 0)
        {
                delay_ms(2);
        }
        //delay_us(5);
        GPIO_Set(GPIOB, PIN10, GPIO_MODE_OUT, GPIO_OTYPE_OD, GPIO_SPEED_50M, GPIO_PUPD_PU); //PB10éè&#214;&#195;
        //SCL1_OUT(); //SCLéè&#214;&#195;&#206;aê&#228;3&#246;
        humh = I2C1_Recv_Byte(1); //Data(MSB),response with ACK
        //huml = I2C1_Recv_Byte(1); //Data(LSB),response with ACK
        huml = I2C1_Recv_Byte(0); //Data(LSB),response with ACK
        //I2C1_Recv_Byte(0); //Checksum,response without ACK

        I2C1_Stop();

        hum = (humh<<8) + huml;
        hum &= ~0x0003;
        humidity = ((float)(hum) * 0.00190735) - 6; //humidity conversion

        return humidity;
}

float HTU21D_Measure_Temperature_NHM(void)
{
        u8 tmph, tmpl;
        u16 tmp;
        float temperature;

        I2C1_Start();
        I2C1_Send_Byte((HTU21D_DEVICE_ADDR<<1) | 0x00);
        I2C1_Wait_Ack();
        I2C1_Send_Byte(COMMAND_TRIG_TEMP_MEASUREMENT_NHM); //0xF3
        I2C1_Wait_Ack();

        do
        {
                delay_us(1000);
                I2C1_Start();
                I2C1_Send_Byte((HTU21D_DEVICE_ADDR<<1) | 0x01);
        } while(I2C1_Poll_Ack());
        //μè′y2aá&#191;&#189;áê&#248;

        tmph = I2C1_Recv_Byte(1); //Data(MSB),response with ACK
        tmpl = I2C1_Recv_Byte(1); //Data(LSB),response with ACK
        I2C1_Recv_Byte(0); //Checksum,response without ACK

        I2C1_Stop();

        tmp = (tmph<<8) + tmpl;
        tmp &= ~0x0003;
        temperature = ((float)(tmp) * 0.00268127) - 46.85; //temperature conversion

        return temperature;
}

float HTU21D_Measure_Humidity_NHM(void)
{
        u8 humh, huml;
        u16 hum;
        float humidity;
        u8 ack;
       

        I2C1_Start();
        I2C1_Send_Byte((HTU21D_DEVICE_ADDR<<1) | 0x00);
        I2C1_Wait_Ack();
        I2C1_Send_Byte(COMMAND_TRIG_HUMI_MEASUREMENT_NHM); //0xF5
        I2C1_Wait_Ack();

        do
        {
                delay_us(5000);
                I2C1_Start();
                I2C1_Send_Byte((HTU21D_DEVICE_ADDR<<1) | 0x01);
                ack = I2C1_Poll_Ack();
        } while(ack);
        //μè′y2aá&#191;&#189;áê&#248;

        humh = I2C1_Recv_Byte(1); //Data(MSB),response with ACK
        huml = I2C1_Recv_Byte(1); //Data(LSB),response with ACK
        I2C1_Recv_Byte(0); //Checksum,response without ACK

        I2C1_Stop();

        hum = (humh<<8) + huml;
        hum &= ~0x0003;
        humidity = ((float)(hum) * 0.00190735) - 6; //humidity conversion

        return humidity;
}
*/
u8 HTU21D_Read_User_Register(void)
{
        u8 reg_val;

        I2C1_Start();
        I2C1_Send_Byte((HTU21D_DEVICE_ADDR<<1) | 0x00);
        I2C1_Wait_Ack();
        I2C1_Send_Byte(COMMAND_READ_USER_REG); //0xE7
        I2C1_Wait_Ack();

        I2C1_Start();
        I2C1_Send_Byte((HTU21D_DEVICE_ADDR<<1) | 0x01);
        I2C1_Wait_Ack();
        reg_val = I2C1_Recv_Byte(0);

        I2C1_Stop();

        return reg_val;
}

void HTU21D_Write_User_Register(u8 reg_val)
{
        I2C1_Start();
        I2C1_Send_Byte((HTU21D_DEVICE_ADDR<<1) | 0x00);
        I2C1_Wait_Ack();
        I2C1_Send_Byte(COMMAND_WRITE_USER_REG); //0xE6
        I2C1_Wait_Ack();
        I2C1_Send_Byte(reg_val);
        I2C1_Wait_Ack();
        I2C1_Stop();
}

void HTU21D_Test(void)
{
        float tmp;
        u8 val;
        char buf[100] = {0};

#if 0
        tmp = HTU21D_Measure_Temperature_HM();
        printf("&#206;&#194;&#182;è1£o%f ", tmp);
        tmp = HTU21D_Measure_Humidity_HM();
        printf("êa&#182;è1£o%f ", tmp);
#endif
#if 1
        tmp = HTU21D_Measure_Temperature_NHM();
        printf("&#206;&#194;&#182;è2£o%f ", tmp);
        float2str((float)tmp, 1, buf);
        LCD_Fill(150, 40, 479, 70, BLACK);
        POINT_COLOR = RED; //éè&#214;&#195;×&#214;ì&#229;&#206;aoìé&#171;
        Show_Str(150, 40, 100, 30, (u8 *)buf, 24, 1);

        tmp = HTU21D_Measure_Humidity_NHM();
        printf("êa&#182;è2£o%f ", tmp);
        float2str((float)tmp, 1, buf);
        LCD_Fill(150, 70, 479, 100, BLACK);
        POINT_COLOR = GREEN; //éè&#214;&#195;×&#214;ì&#229;&#206;a&#194;ìé&#171;
        Show_Str(150, 70, 100, 30, (u8 *)buf, 24, 1);
#endif
        val = HTU21D_Read_User_Register();
        printf("user register£o0x%02x ", val);
}

/////////////////////////////htu21d.h///////////////////////////////////
#ifndef __HTU21D_H__
#define __HTU21D_H__

#include "sys.h"

#define HTU21D_DEVICE_ADDR 0x40
#define COMMAND_TRIG_TEMP_MEASUREMENT_HM 0xE3 //Trigger Temperature Measurement Command(Hold Master)
#define COMMAND_TRIG_HUMI_MEASUREMENT_HM 0xE5 //Trigger Humidity Measurement Command(Hold Master)
#define COMMAND_TRIG_TEMP_MEASUREMENT_NHM 0xF3 //Trigger Temperature Measurement Command(No Hold Master)
#define COMMAND_TRIG_HUMI_MEASUREMENT_NHM 0xF5 //Trigger Humidity Measurement Command(No Hold Master)
#define COMMAND_WRITE_USER_REG 0xE6 //Write user register
#define COMMAND_READ_USER_REG 0xE7 //Read user register
#define COMMAND_SOFT_RESET 0xFE //Soft Reset

void HTU21D_Init(void);
float HTU21D_Measure_Temperature_HM(void);
float HTU21D_Measure_Humidity_HM(void);
float HTU21D_Measure_Temperature_NHM(void);
float HTU21D_Measure_Humidity_NHM(void);
u8 HTU21D_Read_User_Register(void);
void HTU21D_Write_User_Register(u8 reg);
void HTU21D_SoftReset(void);
void HTU21D_Test(void);

#endif



友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。