SHT31溫濕度傳感器+SH79F1615中穎的芯片
主程序:
- /*所有包含的頭文件*/
- #include <SH79F1615.H>
- #include <intrins.h>
- #include "myiic.h"
- #include "sht30.h"
- //==========================================================================//
- /**************************************************************************
- * 函數(shù)名: main
- * 函數(shù)功能:主函數(shù)
- * 輸入?yún)?shù): 無
- * 返回參數(shù): 無
- * 備注:
- ***************************************************************************/
- void main(void)
- {
- IIC_Init();
- while(1)
- {
- Read_sht30();
- Convert_sht30();
- delay_ms(5000);
- }
- }
復制代碼
sht30.c單片機源程序如下:
- #include "sht30.h"
-
- #include "myiic.h"
- unsigned char BUFt[2]={0};
- unsigned char BUFh[2]={0};
- unsigned int hum_value=0;
- unsigned int hum_sum=0;
- unsigned char count=0;
- /*unsigned char crc8(char *buf)
- {
- unsigned char i,j,crc;
-
- crc=0;
- for(j=1;j<7;j++)
- {
- crc=crc^(*buf);
- for(i = 8; i > 0; i--)
- {
- if(crc & 0x80)
- {
- crc = (crc<< 1)^0x31;
- }
- else
- {
- crc = crc<< 1;
- }
- }
- buf++;
- }
- return crc;
- }*/
- unsigned char Get_Crc8(unsigned char *ptr,unsigned int len)
- {
- unsigned char crc;
- unsigned char i;
- crc=0;
- while(len--)
- {
- crc^=*ptr++;
- for(i=0;i<8;i++)
- {
- if(crc&0x01)crc=(crc>>1)^0x8C;
- else crc >>= 1;
- }
- }
- return crc;
- }
- void Cmd_Write_sht30(unsigned char msb,unsigned char lsb)
- {
- IIC_Start(); //起始信號
- IIC_Send_Byte(0x88+0); //發(fā)送設(shè)備地址+寫信號
- while(IIC_Wait_Ack()); //從機應(yīng)答
- IIC_Send_Byte(msb); //發(fā)送命令
- while(IIC_Wait_Ack());
- IIC_Send_Byte(lsb); //發(fā)送命令
- while(IIC_Wait_Ack());
- IIC_Stop(); //發(fā)送停止信號
- delay_ms(5);
- }
- unsigned char crc1=0;
- unsigned char crc2=0;
- unsigned char tem ;
- unsigned char hum ;
- void Read_sht30(void)
- {
- unsigned char a=0;
- Cmd_Write_sht30(0x20,0x32); //數(shù)據(jù)采集頻率1 m/s
- delay_ms(5000);
- IIC_Start(); //起始信號
- IIC_Send_Byte(0x88+1); //發(fā)送設(shè)備地址+讀信號
- while(IIC_Wait_Ack());
- BUFt[0]=IIC_Read_Byte(1); //發(fā)送ACK
- BUFt[1]=IIC_Read_Byte(1); //發(fā)送ACK
- a=IIC_Read_Byte(1); //
- BUFh[0]=IIC_Read_Byte(1); //發(fā)送ACK
- BUFh[1]=IIC_Read_Byte(1); //發(fā)送ACK
- IIC_Stop(); //發(fā)送停止信號
- delay_ms(5);
- }
- //數(shù)據(jù)轉(zhuǎn)換
- void Convert_sht30(void)
- {
- tem=((unsigned long)175*(BUFt[0]*256+BUFt[1]))/65535-45; //攝氏度
- hum=((unsigned long)100*(BUFh[0]*256+BUFh[1]))/65535;
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
全部資料下載地址:
SHT30(51代碼).zip
(112.91 KB, 下載次數(shù): 280)
2018-1-9 16:56 上傳
點擊文件名下載附件
|