main.c
#include "si7006.h"
#include "led.h"
#include "bee.h"
#include "fan.h"
int main()
{
// i2c初始化
i2c_init();
// si7006初始化
si7006_init();
// led初始化
all_led_init();
// bee初始化
bee_init();
// fan初始化
fan_init();
short tem;
unsigned int short hum;
while (1)
{
// 读取温度数据
tem = si7006_read_tem();
// 读取湿度数据
hum = si7006_read_hum();
// 计算温湿度数据
hum = 125 * hum / 65536 - 6;
tem = 175.72 * tem / 65536 - 46.85;
printf("hum:%d\n", hum);
printf("tem:%d\n", tem);
// 湿度过高
if (tem > 26)
{
fan_on();
bee_on();
}
else
{
fan_off();
bee_off();
}
// 湿度过高
if (hum > 60)
{
LED1_ON();
bee_on();
}
else
{
LED1_OFF();
bee_off();
}
// 串口打印
delay(1000);
}
return 0;
}
si7006.c:
#include"si7006.h"
//封装延时函数
void delay(int ms)
{
int i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<2000;j++)
{}
}
}
void si7006_init()
{
//发起起始信号
i2c_start();
//发送从机地址+写标志
i2c_write_byte(0X40<<1|0);
//等待从机应答
i2c_wait_ack();
//发送寄存器地址 0XE6
i2c_write_byte(0XE6);
//等待从机应答
i2c_wait_ack();
//传输要写入的数据0X3A、
i2c_write_byte(0X3A);
//等待从机应答
i2c_wait_ack();
//发送终止信号
i2c_stop();
}
short si7006_read_tem()
{
short tem;
char tem_h,tem_l;
//发起起始信号
i2c_start();
//发送从机地址+写标志
i2c_write_byte(0x40<<1|0);
//等待从机应答
i2c_wait_ack();
//发送寄存器地址 0XE3
i2c_write_byte(0xe3);
//等待从机应答
i2c_wait_ack();
//重复起始信号
i2c_start();
//发送从机地址+读标志
i2c_write_byte(0x40<<1|1);
//等待从机应答
i2c_wait_ack();
delay(100);//等待从机测量数据
//接收数据高八位
//发送应答信号
tem_h=i2c_read_byte(0);
//接收数据低八位
//发送非应答信号
tem_l=i2c_read_byte(1);
//将高八位和低八位合成一个数据 高八位<<8|低8位
tem=tem_h<<8|tem_l;
return tem;
}
unsigned short si7006_read_hum()
{
unsigned short hum;
unsigned char hum_h,hum_l;
//发起起始信号
i2c_start();
//发送从机地址+写标志
i2c_write_byte(0x40<<1|0);
//等待从机应答
i2c_wait_ack();
//发送寄存器地址 0XE3
i2c_write_byte(0xe5);
//等待从机应答
i2c_wait_ack();
//重复起始信号
i2c_start();
//发送从机地址+读标志
i2c_write_byte(0x40<<1|1);
//等待从机应答
i2c_wait_ack();
delay(100);//等待从机测量数据
//接收数据高八位
//发送应答信号
hum_h=i2c_read_byte(0);
//接收数据低八位
//发送非应答信号
hum_l=i2c_read_byte(1);
//将高八位和低八位合成一个数据 高八位<<8|低8位
hum=hum_h<<8|hum_l;
return hum;
}
si7006.h:
#ifndef __SI7006_H__
#define __SI7006_H__
#include"iic.h"
void si7006_init();
short si7006_read_tem();
unsigned short si7006_read_hum();
#endif
本文含有隐藏内容,请 开通VIP 后查看