完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
#define Temperature 0x00 #define Configuration 0x02 #define Device_ID 0xFF #define Device_ID_value 0x1050 #define Configuration_2 0x3000 #define I2C_SDA_PORT (P3IN&BIT1==BIT1) #define I2C_SDA_AS_IN( ) P3DIR &=~(BIT1) typedef unsigned char uint8_t; #include int main1(void); #include "hhh.h" #include "msp430x14x.h" #define CPU_F ((double)8000000) #define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0)) #define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0)) /*---------------------------------------------------------------------------------------------------------*/ /* Global variables */ /*---------------------------------------------------------------------------------------------------------*/ unsigned long temp,humi; 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 ; delay_us(1); P3OUT|= BIT3 ; delay_us(1); P3OUT &=~(BIT1); } void MyApp_I2cReStart( void ) { P3OUT &=~(BIT3); delay_us(1); I2C_SDA_AS_OUT( ); P3OUT|= BIT1; delay_us(1); P3OUT|= BIT3; delay_us(1); P3OUT &=~(BIT1); } void MyApp_I2cStop( void ) { P3OUT &=~(BIT3); delay_us(1); I2C_SDA_AS_OUT( ); P3OUT &=~(BIT1); delay_us(1); P3OUT|= BIT3 ; delay_us(5); P3OUT|= BIT1; } void MyApp_I2cSendAck( void ) { P3OUT &=~(BIT3); delay_us(1); I2C_SDA_AS_OUT( ); P3OUT &=~(BIT1); delay_us(1); P3OUT|= BIT3 ; delay_us(5); } void MyApp_I2cSendNAck( void ) { P3OUT &=~(BIT3); delay_us(1); I2C_SDA_AS_OUT( ); P3OUT|= BIT1; delay_us(1); P3OUT|= BIT3; delay_us(5); } void MyApp_I2cSendByte( unsigned char bData ) { uint8_t i; uint8_t BitMask = 0x80; // 20Us/Bit for (i=0; i<8; i++) { delay_us(1); P3OUT &=~(BIT3); delay_us(1); I2C_SDA_AS_OUT( ); if (bData & BitMask) { P3OUT|= BIT1 ; } else { P3OUT &=~(BIT1) ; } BitMask >>= 1; delay_us(1); P3OUT|= BIT3 ; delay_us(1); } } 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); delay_us(5); P3OUT|= BIT3; delay_us(1); if (I2C_SDA_PORT) { ByteData |= BitMask; } delay_us(1); BitMask >>= 1; } return ByteData; } unsigned char MyApp_I2cCheckAck( void ) { P3OUT &=~(BIT3); I2C_SDA_AS_IN( ); delay_us(5); P3OUT|= BIT3 ; delay_us(1); 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); 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 Convert_HDC1080_TempHumidity(unsigned char *datax) { //100 times of the actural value temp=(unsigned long)(datax[0]<<8); temp+=datax[1]; temp=(temp*165000)>>16; temp-=40000; humi=(unsigned long)(datax[2]<<8); humi+=datax[3]; humi=(humi*1000)>>16; } 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]; I2C_Read_HDC1080(HDC1080_I2C_ADDR,Device_ID,datax); data2=(unsigned int)(datax[0]<<8); data2+=datax[1]; data1=Configuration_1; I2C_Write_HDC1080(HDC1080_I2C_ADDR, Configuration, data1); delay_ms(40); I2C_Read_HDC1080(HDC1080_I2C_ADDR,Configuration,datax); data1=(unsigned int)(datax[0]<<8); data1+=datax[1]; return 1; } /* Main Function*/ int main1(void) { unsigned char datax[4]; MyApp_InitI2c(); HDC1080_Init(); I2C_Read_HDC1080(HDC1080_I2C_ADDR,Temperature,datax); I2C_Read_HDC1080(HDC1080_I2C_ADDR,Humidity,datax); Convert_HDC1080_TempHumidity(datax); return 0; } |
|
相关推荐
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
嵌入式学习-飞凌嵌入式ElfBoard ELF 1板卡-shell脚本编写之传参
205 浏览 0 评论
【xG24 Matter开发套件试用体验】点灯之后的UART测试
304 浏览 0 评论
【xG24 Matter开发套件试用体验】第一个LED测试工程
276 浏览 0 评论
【xG24 Matter开发套件试用体验】蓝牙mesh基本功能开发调试
244 浏览 0 评论
嵌入式学习-飞凌嵌入式ElfBoard ELF 1板卡-shell脚本编写之变量
566 浏览 0 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
10984 浏览 31 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-9-4 16:23 , Processed in 0.717556 second(s), Total 70, Slave 53 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191