作者:新纪元 | 更新时间:2016-04-02 | 浏览量:4331
应网友要求,奉上自己编写的一份基于AM2320温湿度传感器的源代码!希望能起到举一反三的效果。希望各位大神们轻笑、轻拍!
AM2320,我采用的是单总线通信方式,具体电路图如下:

采用ESP8266的GPIO0管脚 ,打开GPIO0的外部中断功能,部分代码如下:
void ICACHE_FLASH_ATTR
AM2320_interrupt(void){/*GPIO0管脚 外部中断处理函数*/
unsigned int tmp = 0xFF;
static unsigned int index = 0;
static unsigned int timer[1] = {0};
static unsigned int AM2320_index = 0xFF;
static unsigned char data[5] = {0,0,0,0,0};
if(!index){
timer[0] = system_get_time();
index ++;
}else{
index = system_get_time();/*暂存*/
timer[0] = index - timer[0];
if(__timer_cmp(150,170,timer[0])){
/*响应时间*/
tmp = 3;
}else if(__timer_cmp(65,100,timer[0])){
/*0*/
tmp = 0;
}else if(__timer_cmp(110,145,timer[0])){
/*1*/
tmp = 1;
}
timer[0] = index;
index = 1;
}
switch(tmp){
case 3: /*响应*/
AM2320_index = 0;
break;
case 0: /*0*/
case 1: /*1*/
if(AM2320_index < 0xFF){
__Ecode(data,AM2320_index,tmp);
AM2320_index++;
if(AM2320_index == 40){
AM2320_index = 0;
for(tmp = 0;tmp < 4;tmp++){
AM2320_index += data[tmp];
}
if((AM2320_index & 0xFF) == data[4]){
for(tmp = 0;tmp < 5;tmp++)
{
Sensor_Data[tmp] = data[tmp];
}
}
AM2320_index = 0xFF;
}
}
break;
}
}
/*获取数据 刷新函数 通过调用此函数 来触发一次am2320温湿度传感器的通信*/
void ICACHE_FLASH_ATTR
AM2320_upData(void){/*获取温湿度数据 每隔3毫秒调用一次*/
static unsigned int timer_counter = 0;
static unsigned int index = 0;
// Read_Sensor();
timer_counter++;
if(((timer_counter * AM2320_UPDATA_TIMER) >= AM2320_GETDATA_TIMER)
|| (index)){
if(!index){
timer_counter = 0;
index++;
PIN_PULLUP_EN(PERIPHS_IO_MUX_GPIO0_U);/*使能上拉*/
GPIO_OUTPUT_SET(GPIO_ID_PIN(0), 0);
}else{
index = 0;
GPIO_OUTPUT_SET(GPIO_ID_PIN(0), 1);
GPIO_DIS_OUTPUT(GPIO_ID_PIN(0));/*配置为输入*/
}
}
} 本人语言水平有限,就直接奉上源码了