Recently used in the temperature and humidity sensor, AM2305 drive, single bus, on the power of at least 2S to read temperature and humidity, temperature accuracy is high, like DS18B20, data with the checksum, will not appear ds18b20, and the timing is not high
/*************************************************************************************************************
* FileName: am2305.c * function: STM32 AM2305 high precision temperature and humidity sensor driver * Author: cp1300@139.com * Date: 2014-09-20 * Last modified: 2014-09-20 * Detail : Need delay function Support * Note the delay accuracy as high as possible * Read time not to be interrupted for a long time, because 1wire is very strict time requirements ************************************************** /#include "system.h" #include "delay.h" #include " AM2305.h "//ds18b20 uses the GPIOE0 #define AM2305_CLK_INIT () deviceclockenable (Am2305_dev, ENABLE); The clock enables #define AM2305_IO_OUT () gpiox_init (am2305_gpiox,am2305_bitx,out_pp,speed_10m); Set AM2305 IO for output, #define AM2305_IO_IN () gpiox_init (am2305_gpiox,am2305_bitx,in_ipu,0); Set AM2305 io for pull 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 off, drop down input//precision US delay #define Am2305_delay_us (x) delay_us (x)//am2305 power off void Am2305_powerdown (void) {Am2305_iodown ();} /************************************************************************************************************** * Function: void Am2305_reset (void) * Function: AM2305 reset * Parameter: no * return: no * dependencies: underlying macro definition * Person: 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); Lower 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: no * return : FALSE: Detection failure;
TRUE: Detect Success * Dependencies: underlying macro definition * Author: cp1300@139.com * Time: 2014-09-20 * Last modified: 2014-09-20 * Description: Detect the existence of AM2305 ***************************************************************************************************************
/bool Am2305_check (void) {u32 retry=0;
Am2305_io_in ();//set INPUT while (am2305_dq_in () &&retry<100)//am2305 will pull 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 low and then 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 * dependency : 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 to become 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 to change 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: read one byte * parameter from AM2305: no * return: Read to 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 once from AM2305 * parameter: ptemp: Temperature pointer, Phumi: Humidity Data pointer * Returns: 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 checksum failure!\r\n");
return FALSE;
} else {uart_printf ("AM2305 Detect failed!\r\n");
return FALSE; }
}
/************************************************************************************************************ * Function: bool Am2305_init (void) * Function: Initialize AM2305, and detect presence * Parameter: no * 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
* Date Created: 2014-09-20
* Last modified: 2014-09-20
* Details: need delay function Support
* Note the delay precision as high as possible
* read time not to be interrupted for a long time, Because the 1wire is very strict with time requirements
****************************************************************************************** /
#ifndef am2305_h_
#define Am2305_h_
#include "system.h"
#if (Board_ SUPPORT //requires board level support
#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 off
am2305_init (void); AM2305 initialization of
bool Am2305_readdata (s16 *ptemp,u16 *phumi); Read temperature and humidity data
#endif/*am2305_h_*/
Read temperature and humidity
Am2305_readdata (&temp, &hum);