完善资料让更多小伙伴认识你,还能领取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; } |
|
相关推荐
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
7 浏览 0 评论
26 浏览 1 评论
基于瑞萨FPB-RA4E2智能床头灯项目——1编译环境搭建与点亮驱动ws2812全彩LED
13 浏览 0 评论
嵌入式学习-飞凌嵌入式ElfBoard ELF 1板卡-LCD显示图片编程示例之介绍mmap
644 浏览 0 评论
《DNESP32S3使用指南-IDF版_V1.6》第二章 常用的C语言知识点
1000 浏览 0 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
11771 浏览 31 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-24 17:29 , Processed in 0.579964 second(s), Total 67, Slave 51 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号