HTU21D sıcaklık ve nem ölçüm sensörü kullanan var mı ?

Başlatan baran123, 07 Temmuz 2015, 23:13:12

baran123

Bu I2C ömrümü yedi, bitirdi. :) 1 haftadır aklımı oynatacağım.Çalışmıyor.Bayağı bir şeyler yaptım fakat olmadı.Bulduğum bir örneği çevireyim dedim gene olmadı.Uygulamayı bırakacağım ama inat var biraz. :D Bu son zaten bitsin I2C TÖVBE !

Kullanmış, kullanmamış arkadaşlar bir bakarsanız çok sevinirim.

Datasheet : http://txyz.info/b10n1c/datasheets/HTU21D.pdf

main.h
/*****************************************************************************

* File Name         : main.h

* Description       : Header file for main.c

* Author            : Baran EKREM

*******************************************************************************/
#ifndef HTU21D_H
#define HTU21D_H

/*******************************************************************************
* HTU Defines
*******************************************************************************/
#define HTU21D_I2C	            I2C1    /* HTU I2C Modul define */
#define HTU21D_ADDRESS          0x40	/* Read user register   */
#define HTU21D_WRITE_ADDRESS    0xE6	/* Write user register  */
#define HTU21D_READ_ADDRESS     0xE7    /* Read  user register  */
#define HTU21D_SOFT_RESET  	    0xFE    /* Reset Command        */
#define HTU21D_TRIG_TEMP_HOLD   0xE3
#define HTU21D_TRIG_HUMD_HOLD   0xE5
#define HTU21D_TRIG_TEMP_NHOLD  0xF3
#define HTU21D_TRIG_HUMD_NHOLD  0xF5

/*******************************************************************************
* PinPack Defines
*******************************************************************************/
#define USART2_PIN_PACK     GPIO_Pin_2 | GPIO_Pin_3

#endif


main.c
/*****************************************************************************

* File Name         : main.c

* Description       :

* Author            : Baran EKREM

*******************************************************************************/

/*******************************************************************************
* Header File Includes
*******************************************************************************/
#include "stm32f4xx_conf.h"
#include "tm_stm32f4_i2c.h"
#include "main.h"
#include <stdio.h>

/*******************************************************************************
* Config Functions
*******************************************************************************/
static void Init_RCC(void);
static void Init_GPIO(void);
static void Init_UART(void);
static void Init_TIM3(void);
static void Init_NVIC(void);

/*******************************************************************************
* String Functions
*******************************************************************************/
char* FloatToString(float f);
void USART_SendString(USART_TypeDef*, char*);

/*******************************************************************************
* HTU21D Functions
*******************************************************************************/
void HTU21D_Init(void);
void HTU21D_Reset(void);
char HTU21D_ReadData(char);
float HTU21D_ReadHumidity(char);
float HTU21D_ReadTemperature(char);

/*******************************************************************************
* Global variables
*******************************************************************************/
static uint8_t TxReady = 0;
static float Temperature, Humidity;

/*******************************************************************************
* Function Name  : TIM3_IRQHandler
* Description    :
* Input          : void
* Return         : void
*******************************************************************************/
void TIM3_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
        TxReady = 1;
    TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
}

/*******************************************************************************
* Function Name  : main
* Description    :
* Input          : void
* Return         : 0
*******************************************************************************/
int main(void)
{
    Init_RCC();
    Init_GPIO();
    Init_UART();
    Init_TIM3();
    Init_NVIC();
    HTU21D_Init();

    while(1)
    {
        if(TxReady)
        {
            Humidity = HTU21D_ReadHumidity(HTU21D_ReadData(HTU21D_TRIG_HUMD_HOLD));
            Temperature = HTU21D_ReadTemperature(HTU21D_ReadData(HTU21D_TRIG_TEMP_HOLD));
            USART_SendString(USART2, FloatToString(Humidity));
            USART_SendString(USART2, FloatToString(Temperature));
            TxReady = 0;
        }
    }
    return (0);
}

/*******************************************************************************
* Function Name  : Init_RCC
* Description    :
* Input          : void
* Return         : void
*******************************************************************************/
static void Init_RCC(void)
{
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 | RCC_APB1Periph_USART2, ENABLE);
}

/*******************************************************************************
* Function Name  : Init_GPIO
* Description    :
* Input          : void
* Return         : void
*******************************************************************************/
static void Init_GPIO(void)
{
    GPIO_InitTypeDef   GPIO_InitStructure;

    GPIO_InitStructure.GPIO_Mode    = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType   = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Pin     = USART2_PIN_PACK;
    GPIO_InitStructure.GPIO_PuPd    = GPIO_PuPd_UP;
    GPIO_InitStructure.GPIO_Speed   = GPIO_Speed_100MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
}

/*******************************************************************************
* Function Name  : Init_UART
* Description    :
* Input          : void
* Return         : void
*******************************************************************************/
static void Init_UART(void)
{
    USART_InitTypeDef   USART_InitStructure;

    USART_InitStructure.USART_BaudRate              = 9600;
    USART_InitStructure.USART_HardwareFlowControl   = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode                  = USART_Mode_Tx;
    USART_InitStructure.USART_Parity                = USART_Parity_No;
    USART_InitStructure.USART_StopBits              = USART_StopBits_1;
    USART_InitStructure.USART_WordLength            = USART_WordLength_8b;

    USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
    USART_Init(USART2, &USART_InitStructure);
    USART_Cmd(USART2, ENABLE);
}

/*******************************************************************************
* Function Name  : Init_TIM3
* Description    :
* Input          : void
* Return         : void
*******************************************************************************/
static void Init_TIM3(void)
{
    TIM_TimeBaseInitTypeDef   TIM_InitStructure;

    TIM_InitStructure.TIM_Prescaler         = 1000;
    TIM_InitStructure.TIM_CounterMode       = TIM_CounterMode_Up;
    TIM_InitStructure.TIM_Period            = 16800;
    TIM_InitStructure.TIM_ClockDivision     = TIM_CKD_DIV1;

    TIM_TimeBaseInit(TIM3, &TIM_InitStructure);
    TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
    TIM_Cmd(TIM3, ENABLE);
}

/*******************************************************************************
* Function Name  : Init_NVIC
* Description    :
* Input          : void
* Return         : void
*******************************************************************************/
static void Init_NVIC(void)
{
    NVIC_InitTypeDef   NVIC_InitStructure;

    NVIC_InitStructure.NVIC_IRQChannel                      = TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelCmd                   = ENABLE;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority           = 0;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority    = 0;

    NVIC_Init(&NVIC_InitStructure);
}

/*******************************************************************************
* Function Name  : FloatToString
* Description    : Verilen float parametreyi string'e dцnьюtьrьr
* Input          : float f
* Return         : char*
*******************************************************************************/
char* FloatToString(float f)
{
    static char MyText[32];

    sprintf(MyText, "%4.3f\r\n", f);

    return (MyText);
}

/*******************************************************************************
* Function Name  : USART_SendString
* Description    : Usarttan string yollar
* Input          : USART_TypeDef* USARTx, char* s
* Return         : void
*******************************************************************************/
void USART_SendString(USART_TypeDef* USARTx, char* s)
{
    while(*s)
    {
        while( !(USARTx->SR & 0x00000040) );
        USARTx->DR = (*s & (uint16_t)0x01FF);
        s++;
    }
}

/*******************************************************************************
* Function Name  : HTU21D_Init
* Description    :
* Input          : void
* Return         : void
*******************************************************************************/
void HTU21D_Init(void)
{
    TM_I2C_Init(HTU21D_I2C, TM_I2C_PinsPack_1, 100000); // I2C Init
    HTU21D_Reset();                                     // HTU software Reset
}

/*******************************************************************************
* Function Name  : HTU21D_Reset
* Description    :
* Input          : void
* Return         : void
*******************************************************************************/
void HTU21D_Reset(void)
{
    TM_I2C_Write(HTU21D_I2C, HTU21D_ADDRESS, HTU21D_WRITE_ADDRESS, HTU21D_SOFT_RESET);
}

/*******************************************************************************
* Function Name  : HTU21D_ReadData
* Description    :
* Input          : char
* Return         : char
*******************************************************************************/
char HTU21D_ReadData(char regSelect)
{
	unsigned char value = 0;
	char reg_data[3];
    char reg = regSelect;

	TM_I2C_WriteNoRegister(HTU21D_I2C, HTU21D_ADDRESS, reg);

	reg_data[0] = TM_I2C_Read(HTU21D_I2C, HTU21D_ADDRESS, HTU21D_READ_ADDRESS);
    reg_data[1] = TM_I2C_Read(HTU21D_I2C, HTU21D_ADDRESS, HTU21D_READ_ADDRESS);
    reg_data[2] = TM_I2C_Read(HTU21D_I2C, HTU21D_ADDRESS, HTU21D_READ_ADDRESS);

	value = ((unsigned)reg_data[0] << 8) | reg_data[1];
	return value & 0xFFFC;
}

/*******************************************************************************
* Function Name  : HTU21D_ReadTemperature
* Description    :
* Input          : char
* Return         : float
*******************************************************************************/
float HTU21D_ReadTemperature(char ValueTemp)
{
	return (float)(-46.85 + 175.72 * (ValueTemp / 65536.0));
}

/*******************************************************************************
* Function Name  : HTU21D_ReadHumidity
* Description    :
* Input          : char
* Return         : float
*******************************************************************************/
float HTU21D_ReadHumidity(char ValueTemp)
{
	return (float)(-6.0 + 125.0 * ValueTemp / 65536.0);
}


MikroC örneği var onu çevireyim biraz dedim.Olmadı.
/*
#define HTU21D_ADDR                 (0x40)
#define HTU21D_CLEAR_BIT            (0x40)    // Clears any pending interrupt (write 1 to clear)
#define HTU21D_WORD_BIT             (0x20)    // 1 = read/write word (rather than byte)
#define HTU21D_BLOCK_BIT            (0x10)    // 1 = using block read/write
#define TRIGGER_TEMP_MEASURE_HOLD    0xE3
#define TRIGGER_HUMD_MEASURE_HOLD    0xE5
#define TRIGGER_TEMP_MEASURE_NOHOLD  0xF3
#define TRIGGER_HUMD_MEASURE_NOHOLD  0xF5
#define WRITE_USER_REG               0xE6
#define READ_USER_REG                0xE7
#define SOFT_RESET                   0xFE

char HTU21D_Soft_Reset()
{
	char reg_command[1];
	reg_command[0] = SOFT_RESET;
	I2C1_Start();                                                // issue I2C start signal
	I2C1_Write(HTU21D_ADDR,reg_command, 1, END_MODE_STOP);
	Delay_ms(15);
}

void HTU21D_REG_read()
{
	char reg_data[1], txt[12];
	int reg_value = 0;
	reg_data[0] = READ_USER_REG;
	I2C1_Start();
	I2C1_Write(HTU21D_ADDR, reg_data, 1, END_MODE_RESTART);
	I2C1_Read (HTU21D_ADDR, reg_data, 1, END_MODE_STOP);
	reg_value=reg_data[0];

	IntToStr(reg_value, txt);
	UART1_Write_Text(txt);
}

void HTU21D_REG_write()
{
	char reg_data[1], txt[12];
	int reg_value=0;
	reg_data[0]=WRITE_USER_REG;
	reg_data[1]=0xC1;
	I2C1_Start();
	I2C1_Write(HTU21D_ADDR, reg_data, 2, END_MODE_STOP);
	reg_value = reg_data[0];
}

unsigned HTU21D_ReadValue(char regSelect)
{
	unsigned value = 0;
	char reg_data[3];

	reg_data[0] = regSelect;
	I2C1_Start();
	I2C1_Write(HTU21D_ADDR, reg_data, 1, END_MODE_RESTART);
	I2C1_Read (HTU21D_ADDR, reg_data, 3, END_MODE_STOP);
	value = ((unsigned)reg_data[0] << 8) | reg_data[1] ;
	return value & 0xFFFC;            // Clear status bits
}

void procTemperatureValue(unsigned ValueTemp)
{
	char txt[12];
	float calc;
	calc = -46.85 + 175.72 * ValueTemp / 65536.0;
	sprintf(txt, "%4.3f", calc);
	UART_Write_Text(txt);
}

void procHumidityValue(unsigned ValueTemp)
{
	char txt[12];
	float calc;
	calc = -6.0 + 125.0 * ValueTemp / 65536.0;
	sprintf(txt, "%4.3f", calc);
	UART_Write_Text(txt);
}

void main()
{
	InitMCU();
	HTU21D_Soft_Reset();
	
	Delay_ms(50);
	UART_Write_Text("HTU21D Example\r\n\r\n");
	
	while(1)
	{
		UART_Write_Text("Temperature=");
		procTemperatureValue(HTU21D_ReadValue(TRIGGER_TEMP_MEASURE_HOLD));
		UART_Write_Text("°C\t");

		UART_Write_Text("Humidity=");
		procHumidityValue(HTU21D_ReadValue(TRIGGER_HUMD_MEASURE_HOLD));
		UART_Write_Text("%RH\r\n");
		Delay_ms(1000);
	}
}
*/

ogy

Hocam ben o sensörü MikroC de kullandım.Problem olmamıştı.MikroC kodlarını aşağıda verdim.Belki işinize yarar.Uygulama sıcaklık ve nemi hesaplayıp 1 saniye aralıkla seri porttan gönderiyor.

#define HTU21D_ADDR                 (0x40)

#define HTU21D_CLEAR_BIT            (0x40)    // Clears any pending interrupt (write 1 to clear)
#define HTU21D_WORD_BIT             (0x20)    // 1 = read/write word (rather than byte)
#define HTU21D_BLOCK_BIT            (0x10)    // 1 = using block read/write

#define TRIGGER_TEMP_MEASURE_HOLD    0xE3
#define TRIGGER_HUMD_MEASURE_HOLD    0xE5
#define TRIGGER_TEMP_MEASURE_NOHOLD  0xF3
#define TRIGGER_HUMD_MEASURE_NOHOLD  0xF5
#define WRITE_USER_REG               0xE6
#define READ_USER_REG                0xE7
#define SOFT_RESET                   0xFE

void InitMCU(){                //  Init MCU function
  UART1_Init(19200);           // Initialize UART module at 19200 bps
  I2C1_Init_Advanced(100000, &_GPIO_MODULE_I2C1_PB67);   // Configure I2C module
}

char HTU21D_Soft_Reset(){
char reg_command[1];
  reg_command[0] = SOFT_RESET;
  I2C1_Start();                                                // issue I2C start signal
  I2C1_Write(HTU21D_ADDR,reg_command, 1, END_MODE_STOP);
  Delay_ms(15);
}

void HTU21D_REG_read(){
  char reg_data[1], txt[12];
  int reg_value = 0;
  reg_data[0] = READ_USER_REG;
  I2C1_Start();
  I2C1_Write(HTU21D_ADDR, reg_data, 1, END_MODE_RESTART);
  I2C1_Read (HTU21D_ADDR, reg_data, 1, END_MODE_STOP);
  reg_value=reg_data[0];

  IntToStr(reg_value, txt);
  UART1_Write_Text(txt);
}

void HTU21D_REG_write(){
  char reg_data[1], txt[12];
  int reg_value=0;
  reg_data[0]=WRITE_USER_REG;
  reg_data[1]=0xC1;
  I2C1_Start();
  I2C1_Write(HTU21D_ADDR, reg_data, 2, END_MODE_STOP);
  reg_value = reg_data[0];
}

unsigned HTU21D_ReadValue(char regSelect){
unsigned value = 0;
char reg_data[3];

  reg_data[0] = regSelect;
  I2C1_Start();
  I2C1_Write(HTU21D_ADDR, reg_data, 1, END_MODE_RESTART);
  I2C1_Read (HTU21D_ADDR, reg_data, 3, END_MODE_STOP);
  value = ((unsigned)reg_data[0] << 8) | reg_data[1] ;
  return value & 0xFFFC;            // Clear status bits
}

void procTemperatureValue(unsigned ValueTemp){
  char txt[12];
  float calc;
  calc = -46.85 + 175.72 * ValueTemp / 65536.0;
  sprintf(txt, "%4.3f", calc);
  UART_Write_Text(txt);
}

void procHumidityValue(unsigned ValueTemp){
  char txt[12];
  float calc;
  calc = -6.0 + 125.0 * ValueTemp / 65536.0;
  sprintf(txt, "%4.3f", calc);
  UART_Write_Text(txt);
}

void main(){
  InitMCU();
  HTU21D_Soft_Reset();
  Delay_ms(50);
  UART_Write_Text("HTU21D Example\r\n\r\n");
  while(1){
    UART_Write_Text("Temperature=");
    procTemperatureValue(HTU21D_ReadValue(TRIGGER_TEMP_MEASURE_HOLD));
    UART_Write_Text("°C\t");
    
    UART_Write_Text("Humidity=");
    procHumidityValue(HTU21D_ReadValue(TRIGGER_HUMD_MEASURE_HOLD));
    UART_Write_Text("%RH\r\n");
    Delay_ms(1000);
  }
}

mehmet

@Baran Ekrem Ardino kütüphanelerini incele.

Düzgün bir I2C motoru bul ve ilgili sensörün
erişim kodlarını kendin yaz.
Olan olmuştur,
olacak olan da olmuştur.
Olacak bir şey yoktur.
---------------------------------------------
http://www.mehmetbilgi.net.tr
https://creativecommons.org/licenses/by/4.0/deed.tr "CC BY"

baran123

@ogy o kodları biliyorum MikroC nin örnek kodları.Fakat ben gcc ile yaptığımdan her şeyi kedim yazdım.Hazır bir tane bulup denedim olmuyor.
@mehmet hocam ben yazdımda daha ı2c start işleminde kilitleniyor.