Recently used temperature and humidity sensor, AM2305 drive, single bus, after power on at least a delay of 2S to read temperature and humidity, temperature accuracy is high, with the same as DS18B20, data with calibration, will not appear ds18b20, and timing requirements are not high
/*************************************************************************************************************
* File name: am2305.c * Function: STM32 AM2305 high precision temperature and humidity sensor driver * Author: cp1300@139.com * created on: 2014-09-20 * Last modified: 2014-09-20 * Detailed : Requires delay function support * Note that the delay accuracy is as high as possible * do not be interrupted long time when reading, because 1wire is strict in time requirements ************************************************** /#include "system.h" #include "delay.h" #include " AM2305.h "//ds18b20 uses GPIOE0 #define AM2305_CLK_INIT () deviceclockenable (Am2305_dev, ENABLE); Clock enable #define AM2305_IO_OUT () gpiox_init (am2305_gpiox,am2305_bitx,out_pp,speed_10m); Set AM2305 IO as output, #define AM2305_IO_IN () gpiox_init (am2305_gpiox,am2305_bitx,in_ipu,0); Set AM2305 io as the pull-up input, #define AM2305_DQ_IN () ((am2305_in)? 1:0)//Read AM2305 IO #define AM2305_DQ_OUT (x) (am2305_o UT = x)//write AM2305 IO #define Am2305_iodown () gpiox_init (am2305_gpiox,am2305_bitx,in_ipt,0); Power-down, drop-down input//exact US delay #define Am2305_delay_us (x) delay_us (x)//am2305 power-down void Am2305_powerdown (void) {Am2305_iodown ();} /************************************************************************************************************** * Function: void Am2305_reset (void) * Function: AM2305 reset * Parameter: None * return: No * dependency: underlying macro definition * AS By: Cp1300@139.com * Time: 2014-09-20 * Last modified: 2014-09-20 * Description: No ************************************
/void Am2305_reset (void) { Am2305_io_out (); SET OUTPUT am2305_dq_out (0); Pull Low DQ am2305_delay_us (1000); Pull down at least 800uS am2305_dq_out (1); Dq=1 Am2305_delay_us (30); Host pull High 20~40us}/*********************************************************************************************** * Function: bool Am2305_check (void) * function: Wait for AM2305 response * parameter: None * return : FALSE: Detection failure;
TRUE: Detection succeeded * Dependency: underlying macro definition * Author: cp1300@139.com * Time: 2014-09-20 * Last modified: 2014-09-20 * Description: Detect the presence of AM2305 ***************************************************************************************************************
/bool Am2305_check (void) {u32 retry=0;
Am2305_io_in ();//set INPUT while (am2305_dq_in () &&retry<100)//am2305 pulls down 75~85us {retry++;
Am2305_delay_us (1);
} if (retry>=100) {uart_printf ("am2305:retry1:%d\r\n", retry);
return FALSE;
} retry=0; while (!
Am2305_dq_in () &&retry<100)//am2305 pull down will again pull high 75~85us {retry++;
Am2305_delay_us (1);
} if (retry>=100) {uart_printf ("am2305:retry2:%d\r\n", retry);
return FALSE;
} return TRUE; }
/************************************************************************************************************* * Function: U8 am2305_readbit (void) * function: Read AM2305 1bit data * parameter: NO * return: Data * dependent : the underlying macro definition
* Author: cp1300@139.com * Time: 2014-09-20 * Last modified: 2014-09-20 * Description: **********************************
/U8 am2305_readbit (void)
{U8 retry=0;
while (am2305_dq_in () &&retry<80)//wait for the low level {retry++;
Am2305_delay_us (1);
}//if (RETRY>=80)//{//uart_printf ("am2305:retry3:%d\r\n", retry);//} retry=0; while (!
Am2305_dq_in () &&retry<60)//wait for high level {retry++;
Am2305_delay_us (1);
}//if (RETRY>=60)//{//uart_printf ("am2305:retry4:%d\r\n", retry);//} Am2305_delay_us (38);
if (am2305_dq_in ()) return 1;
else return 0; }
/************************************************************************************************************* * Function: U8 am2305_readbyte (void) * function: Reads a byte from the AM2305 * parameter: no * return: Read data * dependent : underlying macro definition * Author: cp1300@139.com * Time: 2014-09-20 * Last modified: 2014-09-20 * Description: No ****************************************************************************************************
/U8 Am2305_readbyte (void) {U8 i,dat;
dat=0;
for (i=0;i<8;i++) {dat<<=1;
Dat|=am2305_readbit ();
} return dat; }
/************************************************************************************************************* * Function: BOOL Am2305_readdata (S16 *ptemp,u16 *phumi) * Function: Read data from AM2305 * parameter: ptemp: Temperature pointer, Phumi: Humidity Data pointer * Return: FALSE: Read failed; TRUE: Read Success * dependency: underlying macro definition * Author: cp1300@139.com * Time: 2014-09-20 * Last modified: 2014-09-20 * Description: 2014-09-29: Fix negative temperature bug *******************************************************************************************************
/bool Am2305_readdata (S16 *ptemp,u16 *phumi) {U8 buf[5];
U8 I;
U16 Utemp;
Am2305_reset (); if (Am2305_check () ==true) {for (i=0;i<5;i++)//Read 40-bit data {buf[i]=am2305_readbyte ();
}////Uart_printf ("\nam2305:");
for (i=0;i<5;i++)//Read 40-bit data//{//uart_printf ("0x%02x\t", Buf[i]);//}//uart_printf ("\ n");
if ((U8) (buf[0]+buf[1]+buf[2]+buf[3]) ==buf[4]) {*phumi=buf[0];
*phumi <<= 8;
*PHUMI|=BUF[1];
UTEMP=BUF[2];
utemp<<=8;
UTEMP|=BUF[3];
if (Utemp > 0x8000) {utemp-= 0x8000;
*ptemp = 0-utemp;
} else {*ptemp = utemp;
} return TRUE;
} else {uart_printf ("AM2305 data check failed!\r\n");
return FALSE;
}} else {uart_printf ("AM2305 detection failed!\r\n");
return FALSE; }
}
/************************************************************************************************************ * Function: bool Am2305_init (void) * Function: Initialize AM2305, and detect existence * Parameters: None * return: FALSE: failed; TRUE: Success * Dependency: underlying macro definition * Author: cp1300@139.com * Time: 2014-09-20 * Last modified: 2014-09-20 * Description: No *********** ****/bool
Am2305_init (void) {am2305_clk_init ();
Am2305_io_out ();
Am2305_reset ();
return Am2305_check ();
}
Header file
/*************************************************************************************************************
* File name: am2305.c
* Function: STM32 AM2305 High precision temperature and humidity sensor driver
* Author: cp1300@139.com
* created on: 2014-09-20
* Last modified: 2014-09-20
* Verbose: requires delay function support * Note that the delay accuracy is as high as possible
* do not interrupt long periods of time when reading Because 1wire is very strict in time requirements
****************************************************************************************** /
#ifndef am2305_h_
#define Am2305_h_
#include "system.h"
#if (Board_ Support) //requires board level
#include "board.h"
#else //default support
//PC3
#define Am2305_out Pcout (3)
#define AM2305_IN pcin (3)
//io
#define AM2305_DEV dev_gpioc
#define AM2305 _gpiox gpioc
#define AM2305_BITX BIT3
#endif
void Am2305_powerdown (void); AM2305 Power
-down bool Am2305_init (void); AM2305 initialization of
bool Am2305_readdata (s16 *ptemp,u16 *phumi); Reading temperature and humidity data
#endif/*am2305_h_*/
Reading temperature and humidity
Am2305_readdata (&temp, &hum);