完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
#define HDC1080_I2C_ADDR 0x80
#define Temperature 0x00 #define Humidity 0x01 #define Configuration 0x02 #define Manufacturer_ID 0xFE #define Device_ID 0xFF #define Manufacturer_ID_value 0x5449 #define Device_ID_value 0x1050 #define Configuration_1 0x1000 #define Configuration_2 0x3000 #define I2C_SCL_PORT BIT3 #define I2C_SDA_PORT BIT1 #define I2C_SDA_AS_IN( ) P3DIR &=~(BIT1) #define I2C_SDA_AS_OUT( ) P3DIR|= BIT1 typedef unsigned char uint8_t; extern int temp_value; extern int humi_value; #include #define PLLCON_SETTING CLK_PLLCON_50MHz_HXT #define PLL_CLOCK 50000000 int main1(void); #include "hhh.h" #include "msp430x14x.h" /*---------------------------------------------------------------------------------------------------------*/ /* Global variables */ /*---------------------------------------------------------------------------------------------------------*/ void DelayTime10us(unsigned char n) { unsigned char i; while(n--) for(i=0;i<10;i++); } void MyApp_InitI2c( void ) { /* Configure P1.2 and P4.1 to default Quasi-bidirectional mode */ P3DIR|= BIT1; P3DIR|= BIT3; P3OUT|= BIT3; P3OUT|= BIT1; } void MyApp_I2cStart( void ) { I2C_SDA_AS_OUT( ); P3OUT|= BIT1 ; DelayTime10us(2); P3OUT|= BIT3 ; DelayTime10us(2); P3OUT &=~(BIT1); } void MyApp_I2cReStart( void ) { P3OUT &=~(BIT3); DelayTime10us(2); I2C_SDA_AS_OUT( ); P3OUT|= BIT1; DelayTime10us(2); P3OUT|= BIT3; DelayTime10us(2); P3OUT &=~(BIT1); } void MyApp_I2cStop( void ) { P3OUT &=~(BIT3); DelayTime10us(2); I2C_SDA_AS_OUT( ); P3OUT &=~(BIT1); DelayTime10us(2); P3OUT|= BIT3 ; DelayTime10us(2); P3OUT|= BIT1; } void MyApp_I2cSendAck( void ) { P3OUT &=~(BIT3); DelayTime10us(2); I2C_SDA_AS_OUT( ); P3OUT &=~(BIT1); DelayTime10us(2); P3OUT|= BIT3 ; DelayTime10us(2); } void MyApp_I2cSendNAck( void ) { P3OUT &=~(BIT3); DelayTime10us(2); I2C_SDA_AS_OUT( ); P3OUT|= BIT1; DelayTime10us(2); P3OUT|= BIT3; DelayTime10us(2); DelayTime10us(2); } void MyApp_I2cSendByte( unsigned char bData ) { uint8_t i; uint8_t BitMask = 0x80; // 20Us/Bit for (i=0; i<8; i++) { DelayTime10us(2); P3OUT &=~(BIT3); DelayTime10us(2); I2C_SDA_AS_OUT( ); if (bData & BitMask) { P3OUT|= BIT1 ; } else { P3OUT &=~(BIT1) ; } BitMask >>= 1; DelayTime10us(2); P3OUT|= BIT3 ; DelayTime10us(2); } } unsigned char MyApp_I2cReceiveByte( void ) { uint8_t i; uint8_t ByteData = 0; uint8_t BitMask = 0x80; P3OUT &=~(BIT3); I2C_SDA_AS_IN( ); // 20Us/Bit for (i=0; i<8; i++) { P3OUT &=~(BIT3); DelayTime10us(2); DelayTime10us(2); P3OUT|= BIT3; DelayTime10us(250); if (I2C_SDA_PORT) { ByteData |= BitMask; } DelayTime10us(2); BitMask >>= 1; } return ByteData; } unsigned char MyApp_I2cCheckAck( void ) { P3OUT &=~(BIT3); I2C_SDA_AS_IN( ); DelayTime10us(2); DelayTime10us(2); P3OUT|= BIT3 ; DelayTime10us(2); if(I2C_SDA_PORT!=0) return 1; else return 0; } void I2C_Write_HDC1080(unsigned char dev_addr, unsigned char register_addr, unsigned int value) { unsigned char i; unsigned char datax[2]; datax[0] = (uint8_t)((value& 0xFF00) >> 8); datax[1] = (uint8_t)(value & 0x00FF); for(i=20;i>0;i--) { MyApp_I2cStart( ); MyApp_I2cSendByte( dev_addr); // HDC1080 Addr + W if(MyApp_I2cCheckAck( )) { MyApp_I2cStop(); continue; } MyApp_I2cSendByte( register_addr); if(MyApp_I2cCheckAck( )) { MyApp_I2cStop(); continue; } MyApp_I2cSendByte( datax[0] ); if(MyApp_I2cCheckAck( )) { MyApp_I2cStop(); continue; } MyApp_I2cSendByte( datax[1] ); if(MyApp_I2cCheckAck( )) { MyApp_I2cStop(); continue; } MyApp_I2cStop( ); return; } } void I2C_Read_HDC1080(unsigned char dev_addr, unsigned char register_addr, unsigned char *datax) { unsigned char i; datax[0]=0; datax[1]=0; for(i=20;i>0;i--) { MyApp_I2cStart( ); MyApp_I2cSendByte( dev_addr); // HDC1080 Addr + W if(MyApp_I2cCheckAck( )) { MyApp_I2cStop(); continue; } MyApp_I2cSendByte( register_addr); if(MyApp_I2cCheckAck( )) { MyApp_I2cStop(); continue; } MyApp_I2cReStart( ); MyApp_I2cSendByte( dev_addr|0x01); // HDC1080 Addr + R if(MyApp_I2cCheckAck( )) { MyApp_I2cStop(); continue; } datax[0] = MyApp_I2cReceiveByte( ); MyApp_I2cSendAck( ); datax[1] = MyApp_I2cReceiveByte( ); MyApp_I2cSendNAck( ); MyApp_I2cStop( ); return; } } void I2C_Read_HDC1080_TempHumidity(unsigned char dev_addr, unsigned char register_addr, unsigned char *datax) { datax[0]=0; datax[1]=0; datax[2]=0; datax[3]=0; MyApp_I2cStart( ); MyApp_I2cSendByte( dev_addr); // HDC1080 Addr + W if(MyApp_I2cCheckAck( )) { MyApp_I2cStop(); return; } MyApp_I2cSendByte( register_addr); if(MyApp_I2cCheckAck( )) { MyApp_I2cStop(); return; } DelayTime10us(2); DelayTime10us(2); DelayTime10us(2); DelayTime10us(2);// It's better to be set over 30ms. MyApp_I2cReStart( ); MyApp_I2cSendByte( dev_addr|0x01); // HDC1080 Addr + R if(MyApp_I2cCheckAck( )) { MyApp_I2cStop(); return; } datax[0] = MyApp_I2cReceiveByte( ); MyApp_I2cSendAck( ); datax[1] = MyApp_I2cReceiveByte( ); MyApp_I2cSendAck( ); datax[2] = MyApp_I2cReceiveByte( ); MyApp_I2cSendAck( ); datax[3] = MyApp_I2cReceiveByte( ); MyApp_I2cSendNAck( ); MyApp_I2cStop( ); } void Convert_HDC1080_TempHumidity(unsigned char *datax) { //100 times of the actural value unsigned long temp,humidity; temp=(unsigned long)(datax[0]<<8); temp+=datax[1]; temp=(temp*16500)>>16; temp-=4000; humidity=(unsigned long)(datax[2]<<8); humidity+=datax[3]; humidity=(humidity*100)>>16; } //HDC1080_Init subroutine //return : 0 - HDC1080 initiate unstable or you should check with your i2c subroutine. 1-HDC1080 initiate ok. unsigned char HDC1080_Init(void) { unsigned int data1,data2; unsigned char datax[2]; I2C_Read_HDC1080(HDC1080_I2C_ADDR,Manufacturer_ID,datax); data1=(unsigned int)(datax[0]<<8); data1+=datax[1]; printf("HDC1080 Manufacturer_ID = 0x%xn", data1); I2C_Read_HDC1080(HDC1080_I2C_ADDR,Device_ID,datax); data2=(unsigned int)(datax[0]<<8); data2+=datax[1]; printf("HDC1080 Device_ID = 0x%xn", data2); if(data1!=Manufacturer_ID_value||data2!=Device_ID_value) { printf("I2C_Read_HDC1080 Manufacturer_ID or Device_ID_value errorn"); return 0; } data1=Configuration_1; I2C_Write_HDC1080(HDC1080_I2C_ADDR, Configuration, data1); //wait at least 20ms to readout temp&humidity value printf("I2C_Write_HDC1080 register 0x02 = 0x%xn", data1); I2C_Read_HDC1080(HDC1080_I2C_ADDR,Configuration,datax); data1=(unsigned int)(datax[0]<<8); data1+=datax[1]; printf("I2C_Read_HDC1080 register 0x02 = 0x%xn", data1); //SysTickDelay_ms(2000); return 1; } /*---------------------------------------------------------------------------------------------------------*/ /* Main Function */ /*---------------------------------------------------------------------------------------------------------*/ int main1(void) { unsigned char datax[4]; /* Init System, peripheral clock and multi-function I/O */ printf("HDC1080 Test Okn"); printf("I2C0_SDA(P3.1), I2C0_SCL(P3.3) - I2C Mastern"); /* Init I2C0 */ MyApp_InitI2c(); HDC1080_Init(); printf("main while(1)n"); while(1) { //I2C_Write_HDC1080(HDC1080_I2C_ADDR, Configuration, 0x1000); I2C_Read_HDC1080_TempHumidity(HDC1080_I2C_ADDR,Temperature,datax); Convert_HDC1080_TempHumidity(datax); DelayTime10us(2); DelayTime10us(2); DelayTime10us(2); DelayTime10us(2);// this delay can be cancelled. } } |
|
相关推荐
5个回答
|
|
I2C_Read_HDC1080_TempHumidity(HDC1080_I2C_ADDR,Temperature,datax);这个是读取传感器的数据,
Convert_HDC1080_TempHumidity(datax);这个是传感器数据的格式转换,转换关系看传感器资料 最后传感器的结果存入humidity变量
最佳答案
|
|
1 条评论
|
|
把void Convert_HDC1080_TempHumidity(unsigned char *datax)里的unsigned long temp,humidity;移到主程序里应该就可以了,显示程序放在主程序里的Convert_HDC1080_TempHumidity(datax);之后
|
|
2 条评论
|
|
没用过这个传感器,也没用过430,电路上供电电压是否正确,IIC的线路连接引脚如果设置成开路输出的话,外部有没有接上拉电阻,
看资料,温度计算应该是 temp=(temp*165)>>16; temp-=40; 另外在你程序初始化部分有读传感器的信息,也可以跟资料对比这些信息,看与传感器通讯是否正确 |
|
4 条评论
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
嵌入式学习-飞凌嵌入式ElfBoard ELF 1板卡-TF卡烧录流程之烧写过程
1243 浏览 0 评论
2338 浏览 0 评论
嵌入式学习-飞凌嵌入式ElfBoard ELF 1板卡-mfgtools烧录流程之烧写原理
1669 浏览 0 评论
请问SPH0641LU4H这款麦克风如何在不使用I2S的情况下,单纯通过GPIO来进行驱动且正常读取数据呢
1240 浏览 1 评论
751 浏览 0 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
12115 浏览 31 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-1-6 12:35 , Processed in 0.628351 second(s), Total 59, Slave 54 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号