適用于C51單片機
- /*****************************************************************************
- 編寫人:Yayi
- *****************************************************************************/
- // 包含頭文件
- #include <reg52.h>
- #include <INTRINS.H>
- #include <stdio.h>
- #include <Delay.h>
- #include <IIC.h>
- #include <hdc1080.h>
- #include <UART.h>
- /*****************************************************************************
- //主程序
- *****************************************************************************/
- void main()
- {
- float temp,hum;
- unsigned char buf[5];
- int i;
- Delay_Ms(500);
- Uart_Init();
- //初始化HDC1080
- if(HDC1080_Init()==-1)
- Send_String("HDC1080 initialize error.\r\n");
- else
- Send_String("HDC1080 initialize register finished.\r\n");
- while(1)
- {
- Send_String("Temperature:");
-
- temp=HDC1080_Temperature();
-
- buf[0]=(int)temp/10+0x30;
- buf[1]=(int)temp%10+0x30;
- buf[2]='.';
- buf[3]=(int)(temp*10)%(int)temp+0x30;
- buf[4]=(int)(temp*100)%(int)(temp*10)+0x30;
-
- for(i=0;i<5;i++)
- Send_Char(buf[i]);
-
- Send_String("C\r\n");
- Send_String("Humidity:");
- hum=HDC1080_Humidity();
-
- buf[0]=((int)hum)/10+0x30;
- buf[1]=((int)hum)%10+0x30;
- buf[2]='.';
- buf[3]=(int)(hum*10)%(int)hum+0x30;
- buf[4]=(int)(hum*100)%(int)(hum*10)+0x30;
-
- for(i=0;i<5;i++)
- Send_Char(buf[i]);
-
- Send_String("%%\r\n");
- Delay_Ms(1000);
- }
- }
復制代碼
- /*****************************************************************************
- 定義類型及變量
- *****************************************************************************/
- #define HDC1080_I2CADDR 0x40
- #define HDC1080_TEMPERATURE 0x00
- #define HDC1080_HUMIDITY 0x01
- #define HDC1080_CONFIG 0x02
- #define HDC1080_CONFIG_RST (1 << 15)
- #define HDC1080_CONFIG_HEAT (1 << 13)
- #define HDC1080_CONFIG_MODE (1 << 12)
- #define HDC1080_CONFIG_BATT (1 << 11)
- #define HDC1080_CONFIG_TRES_14 0
- #define HDC1080_CONFIG_TRES_11 (1 << 10)
- #define HDC1080_CONFIG_HRES_14 0
- #define HDC1080_CONFIG_HRES_11 (1 << 8)
- #define HDC1080_CONFIG_HRES_8 (1 << 9)
- #define HDC1080_SERIAL_ID_FIRST 0xFB
- #define HDC1080_SERIAL_ID_MID 0xFC
- #define HDC1080_SERIAL_ID_LAST 0xFD
- #define HDC1080_MANUFID 0xFE
- #define HDC1080_DEVICEID 0xFF
- /*****************************************************************************
- HDC1080初始化
- /****************************************************************************/
- unsigned char HDC1080_Init()
- {
- unsigned short int config = HDC1080_CONFIG_RST | HDC1080_CONFIG_MODE | HDC1080_CONFIG_TRES_14 | HDC1080_CONFIG_HRES_14;
- unsigned char buf[2];
- unsigned short int tmp;
- SlaveAddress=HDC1080_I2CADDR;
- Single_WriteI2C(HDC1080_CONFIG, (config>>8)&0x00FF);
-
- Delay_Ms(15);
-
- Single_ReadI2C(HDC1080_MANUFID,buf,2);
- tmp = (buf[0]<<8)+buf[1];
- if(tmp!=0x5449)
- return -1;
-
- Single_ReadI2C(HDC1080_DEVICEID,buf,2);
- tmp = (buf[0]<<8)+buf[1];
- if(tmp!=0x1050)
- return -1;
-
- return 0;
- }
- /*****************************************************************************
- HDC1080讀取溫度
- /****************************************************************************/
- float HDC1080_Temperature(void)
- {
- float tmp;
- unsigned char buf[2];
- Single_ReadI2C(HDC1080_TEMPERATURE,buf,2);
- tmp=(buf[0]<<8)+buf[1];
-
- tmp /= 65536;
- tmp *= 165;
- tmp -= 40;
- return tmp;
- }
- /*****************************************************************************
- HDC1080讀取濕度
- /****************************************************************************/
- float HDC1080_Humidity(void)
- {
- float tmp;
- unsigned char buf[4];
-
- Single_ReadI2C(HDC1080_HUMIDITY,buf,2);
- tmp=((buf[0]<<8)+buf[1])& 0xFFFF;
-
- tmp /= 65536;
- tmp *= 100;
- return tmp;
- }
復制代碼
Keil代碼下載:
c樣本51hei.7z
(26.77 KB, 下載次數: 34)
2021-12-14 00:42 上傳
點擊文件名下載附件
|