完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
您好,我想使用DS1631温度传感器与微控制器PIC18F45 K22(MPLABX V 3.26 +XC8)接口,使用I2C,但它不是WROK,我不知道为什么。我在proteus上模拟了传感器,我检查了I2C的波形,它的OK…但是当我从传感器读取温度时,我只读取发送给它的最后一个命令(0x91)。谢谢代码:(在这些代码中,我把编译器从XC8改为MCC18,只是为了测试什么错误)。
以上来自于百度翻译 以下为原文 Hello, Im traying to use DS1631 temperature Sensor interfacing with microcontroller pic18f45k22 (MPLABX V 3.26 + xc8) Using I2C, but its not wrok , an I dont inderstand why. I simulate the sensor on Proteus, I have checked Waveform of i2C its ok ...but when I read temperature from sensor , I read just the last command sent to it (0x91). thanks my Code : (in these code i have changed the compiler from xc8 to mcc18 just for testing what wrong) #include #include #pragma config FOSC=INTIO67 //#pragma config WDT=OFF #pragma config LVP=OFF #include #include unsigned char Init_DS1631(unsigned char dev_address) // Returns 0xff on error , 0 on success { unsigned char Idata=0b10001101; // Config=12bit / 1shot OpenI2C2(MASTER, SLEW_OFF); // Initialize I2C module SSP2ADD=0x13; // 100khz = 8Mhz / ( ( 0x13+1) * 4 ) IdleI2C2(); StartI2C2(); // Generate I2C2 start condition IdleI2C2(); if(WriteI2C2(dev_address)!=0) // { StopI2C2(); CloseI2C2(); return 0xff; // Error :no reaction from sensor } else { IdleI2C2(); WriteI2C2(0xAC); // Send config command IdleI2C2(); WriteI2C2(Idata); // Send our configuration IdleI2C2(); StopI2C2(); return 0; // All went well } } unsigned char i2c_Read(unsigned char ack) { // Read data from slave // ack should be 1 if there is going to be more data read // ack should be 0 if this is the last byte of data read unsigned char i2cReadData; IdleI2C2(); SSP2CON2bits.RSEN=1; IdleI2C2(); i2cReadData = SSP2BUF; IdleI2C2(); //i2c_Wait(); if ( ack ) { SSP2CON2bits.ACKDT=0; // Ack } else { SSP2CON2bits.ACKDT=1 ; // NAck SSP2CON2bits.ACKEN=1; // send acknowledge sequence } return( i2cReadData ); } void Tempo_ms( int ms){ int i; for (i=0; i<=ms; i++) { // __delay_ms(1); // fonction pour temporisation en ms } } void temp_config(char data) { StartI2C2(); WriteI2C2(0x90); IdleI2C2(); WriteI2C2(0xAA); IdleI2C2(); Delay10TCYx(10); IdleI2C2(); WriteI2C2(data); StopI2C2(); } void init_temp() { StartI2C2(); WriteI2C2(0x90); IdleI2C2(); WriteI2C2(0x51); IdleI2C2(); //Delay1KTCYx( 200000); StopI2C2(); temp_config(0x00);// set to 9 bit resolution } signed int read_int_temp() { //read the high byte signed int datah; unsigned long datal; float temp_c; StartI2C2(); WriteI2C2(0x90); IdleI2C2(); RestartI2C2(); WriteI2C2(0x90); IdleI2C2(); WriteI2C2(0xAA); //Delay1KTCYx( 20000); IdleI2C2(); RestartI2C2(); WriteI2C2(0x91); IdleI2C2(); // Delay1KTCYx( 2000000); datah=i2c_Read(1); IdleI2C2(); //Delay1KTCYx( 20000); datal=i2c_Read(0); IdleI2C2(); StopI2C2(); // Calculate Temp if(datah>=0x80) //if sign bit is set, then temp is negative temp_c = (float)((datah<<8 + datal) - 65536) * 0.0625; else temp_c = (float)(datah<<8 + datal) * 0.0625; //temp_f =( temp_c * 9/5) + 32; return(temp_c); } void setupI2C(void) { OpenI2C2(MASTER,SLEW_OFF); SSP2ADD=19; PIE1bits.SSPIE=1; // enable interrupt IPR1bits.SSPIP=1; // high priority interrupt TRISDbits.TRISD0=1; TRISDbits.TRISD1=1; // set up ports SSP2CON1bits.CKP=1; SSP2CON1bits.SSPEN=1; SSP2CON1bits.SSPOV=1; // SSP1CON2bits.GCEN=1; // general call enable } int i=1; void main(void) { float temp; ANSELD=0; ANSELC=0; ANSELA=0; ADCON1=0; TRISB = 0x0; TRISD = 0b11111111; OSCCON=0b01101100; OSCCON2=0x00;; setupI2C(); init_temp(); while(1) { PORTBbits.RB3=0; LATBbits.LATB3=1; //Init_DS1631(0x90); temp=read_int_temp(); } } |
|
相关推荐
2个回答
|
|
您的I2Cead函数是错误的。您只需要为NACK断言AKEN,当您需要两者时,代码应该是:
以上来自于百度翻译 以下为原文 Your i2cread function is faulty. if ( ack ) { SSP2CON2bits.ACKDT=0; // Ack } else { SSP2CON2bits.ACKDT=1 ; // NAck SSP2CON2bits.ACKEN=1; // send acknowledge sequence } You only assert ACKEN for NACK, when you need it for both, so the code should be: if ( ack ) { SSP2CON2bits.ACKDT=0; // Ack } else { SSP2CON2bits.ACKDT=1 ; // NAck } SSP2CON2bits.ACKEN=1; // send acknowledge sequence |
|
|
|
N.B.如果你查看编译后的汇编代码,最有效的代码实际上是这样做的:
以上来自于百度翻译 以下为原文 n.b. If you have a look at the compiled assembly code, the most efficient code actually comes from doing it this way: SSP2CON2bits.ACKDT=1 ; // Assume NAck if ( ack ) SSP2CON2bits.ACKDT=0; // change to Ack SSP2CON2bits.ACKEN=1; // send acknowledge sequence |
|
|
|
只有小组成员才能发言,加入小组>>
5248 浏览 9 评论
2036 浏览 8 评论
1956 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3218 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2264 浏览 5 评论
786浏览 1评论
677浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
603浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
684浏览 0评论
581浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-27 09:42 , Processed in 1.320980 second(s), Total 77, Slave 61 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (威廉希尔官方网站 图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号