main
cpp
int main()
{
//
led_init();
i2c_init();
si7006_init();
unsigned short hum;
short tem;
short temp_flag = 28;
LED1_CTRL(1);
LED3_CTRL(0);
BEEP_CTRL(1);
while(1)
{
//
hum=si7006_read_hum();
tem=si7006_read_tem();
//
hum=hum*125/65536-6;
tem=tem*175.72/65536-46.85;
if(temp_flag < tem) BEEP_CTRL(1);
else BEEP_CTRL(0);
printf("hum:%d\n",hum);
printf("tem:%d\n",tem);
delay_ms(1000);
}
return 0;
}
is7006
cpp
#include "si7006.h"
unsigned char ack;
void delay_ms(int ms)
{
int i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<2000;j++);
}
}
void si7006_init()
{
//1.鍙戦€佽捣濮嬩俊鍙?
i2c_start();
//2.鍙戦€?bit浠庢満鍦板潃鍜?bit鍐欐爣蹇?
i2c_write_byte(0x80);
//3.绛夊緟浠庢満搴旂瓟
ack = i2c_wait_ack();
//4.鍙戠敓瀵勫瓨鍣ㄥ湴鍧€ 0xe6
i2c_write_byte(0xe6);
//5.绛夊緟浠庢満搴旂瓟
ack = i2c_wait_ack();
//6.鍚戜粠鏈哄彂閫佹暟鎹?0x3a
i2c_write_byte(0x3a);
//7.绛夊緟浠庢満搴旂瓟
ack = i2c_wait_ack();
//8.鍙戦€佺粓姝俊鍙?
i2c_stop();
}
unsigned short si7006_read_hum()
{
unsigned char hum_l,hum_h;
unsigned short hum;
/*
1銆佷富鏈哄彂璧疯捣濮嬩俊鍙?
2銆佷富鏈哄彂閫?bit浠庢満鍦板潃鍜?bit鍐欐爣蹇?
3銆佺瓑寰呬粠鏈哄湴鍧€.
4.涓绘満鍙戦€?浣嶅瘎瀛樺櫒鍦板潃.
5銆佺瓑寰呬粠鏈哄簲绛?
6銆佷富鏈哄彂鐢熻捣濮嬩俊鍙?
7銆佷富鏈哄彂閫?浣嶇殑璧峰鍦板潃.
8銆佺瓑寰呬粠鏈哄簲绛?
9銆佸欢鏃剁瓑寰呬粠鏈烘祴閲忔暟鎹?
10銆佽鍙栨箍搴︾殑楂樺叓浣嶆暟鎹?
11銆佸彂閫佸簲绛斾俊鍙?
12銆佽鍙栨箍搴︾殑浣庡叓浣嶆暟鎹?
13銆佸彂鐢熼潪搴旂瓟淇″彿.
14銆佸彂閫佺粓姝俊鍙?
15銆佸皢璇诲彇鍒扮殑鏁版嵁鐨勪綆鍏綅鍜岄珮鍏綅鍚堟垚涓€涓畬鏁寸殑鏁版嵁
hum=hum_h<<8|hum_l
return hum
*/
i2c_start();
i2c_write_byte(0x80);
ack = i2c_wait_ack();
i2c_write_byte(0xe5);
ack = i2c_wait_ack();
i2c_start();
i2c_write_byte(0x81);
ack = i2c_wait_ack();
delay_ms(1);
hum_h = i2c_read_byte(ack);
i2c_ack();
hum_l = i2c_read_byte(ack);
i2c_nack();
i2c_stop();
hum=hum_h<<8|hum_l;
return hum;
}
unsigned short si7006_read_tem()
{
unsigned char tem_l,tem_h;
unsigned short tem;
/*
1銆佷富鏈哄彂璧疯捣濮嬩俊鍙?
2銆佷富鏈哄彂閫?bit浠庢満鍦板潃鍜?bit鍐欐爣蹇?
3銆佺瓑寰呬粠鏈哄湴鍧€
4.涓绘満鍙戦€?浣嶅瘎瀛樺櫒鍦板潃
5銆佺瓑寰呬粠鏈哄簲绛?
6銆佷富鏈哄彂鐢熻捣濮嬩俊鍙?
7銆佷富鏈哄彂閫?浣嶇殑璧峰鍦板潃
8銆佺瓑寰呬粠鏈哄簲绛?
9銆佸欢鏃剁瓑寰呬粠鏈烘祴閲忔暟鎹?
10銆佽鍙栨箍搴︾殑楂樺叓浣嶆暟鎹?
11銆佸彂閫佸簲绛斾俊鍙?
12銆佽鍙栨箍搴︾殑浣庡叓浣嶆暟鎹?
13銆佸彂鐢熼潪搴旂瓟淇″彿
14銆佸彂閫佺粓姝俊鍙?
15銆佸皢璇诲彇鍒扮殑鏁版嵁鐨勪綆鍏綅鍜岄珮鍏綅鍚堟垚涓€涓畬鏁寸殑鏁版嵁
16銆乼em=tem_h<<8|tem_l
return tem
*/
i2c_start();
i2c_write_byte(0x80);
ack = i2c_wait_ack();
i2c_write_byte(0xe3);
ack = i2c_wait_ack();
i2c_start();
i2c_write_byte(0x81);
ack = i2c_wait_ack();
delay_ms(1);
tem_h = i2c_read_byte(ack);
i2c_ack();
tem_l = i2c_read_byte(ack);
i2c_nack();
i2c_stop();
tem=tem_h<<8|tem_l;
return tem;
}
iic通信:
1、空闲状态:总线上没有通信时,SDA和SCL都是高电平。
2、起始:主机发送起始信号,SDA为低,SCL为高。
3、主机发送7bit地址和1bit读位
4、从机应答
5、主机发送8bit数据
6、从机应答
7、重复
8、从机发送停止信号,SCL为高时,SDA由低变高