1
完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
设备:16F1824编译器:XC8V1.45 ID:XV4.01问题:I2C通信有时工作I2C配置代码:这是我的I2C代码:这是主代码。C:有时命令命令失败标志被设置,有时命令Debug失败标志被清除,我需要家伙检查我的I2C流来检查这是不是如果上述代码是正确的:从属芯片的ADC每秒扫描数据(电压、电流等),将这些数据存储到EEPROM寄存器中,然后将操作寄存器映射到EEPROM寄存器中,只有I2C可以通信的是操作寄存器。在芯片向操作寄存器中更新数据时,从芯片是否可能对主响应?非常感谢!谢谢您。
以上来自于百度翻译 以下为原文 device: 16F1824 compiler: XC8 v1.45 IDE: X v4.01 PROBLEM: I2C communication SOMETIMES works The I2C configuration code: SSPADD = 0x0a; // clock frequency of SLK = Fosc/(SSPADD+1)*4 = 100KHz have error, tuning it to 80Khz SSP1STATbits.CKE = 1; // SMBus compatible SSP1CON = 0b00101000; // bit 7: WCOL = 0 // bit 6: SSPOV = 0 Receive Overflow Indicator bit // bit 5: SSPEN = 1 // bit 4: CKP no use in I2C master mode // bit 3-0: 1000 = I2C Master mode, clock = FOSC/(4 * (SSP1ADD+1))(4) SSP1CON3 = 0b01110000; // bit 7: ACKTIM 0 = Not an Acknowledge sequence, cleared on 9TH rising edge of SCL clock // bit 6: PCIE 1 = Enable interrupt on detection of Stop condition // bit 5: SCIE 1 = Enable interrupt on detection of Start or Restart conditions // bit 4: BOEN This bit is ignored in I2C master mode // bit 3: SDAHT (I don't know what this do) // bit 2: SBCDE (I2C Slave mode only) // bit 1: AHEN (I2C Slave mode only) // bit 0: DHEN (I2C Slave mode only) This is my I2C code: bit I2C_Start(void) { while ( ( SSPCON2 & 0x1F ) || ( R_nW ) ); // Idle? SSP1CON2bits.SEN = 1; // Initial start condition while (SSP1CON2bits.SEN != 0); if (PIR2bits.BCL1IF == 1){ PIR2bits.BCL1IF = 0; return FAILURE; } else{ PIR1bits.SSP1IF = 0; return SUCCESS; } } bit I2C_RepeatedStart(void) { while ( ( SSPCON2 & 0x1F ) || ( R_nW ) ); // Idle? SSPCON2bits.RSEN = 1; while (SSP1CON2bits.RSEN != 0); if (PIR2bits.BCL1IF == 1){ PIR2bits.BCL1IF = 0; return FAILURE; } else{ PIR1bits.SSP1IF = 0; return SUCCESS; } } bit I2C_SendAddress(const unsigned char address) { while ( ( SSPCON2 & 0x1F ) || ( R_nW ) ); // Idle? SSP1BUF=address; if(SSP1CON2bits.ACKSTAT == 1){ //slave沒回應 return FAILURE; } else{ if(PIR2bits.BCL1IF == 1){ PIR2bits.BCL1IF == 0; return FAILURE; } else return SUCCESS; } } unsigned char I2C_Receive(void) { while ( ( SSPCON2 & 0x1F ) || ( R_nW ) ); // Idle? SSP1CON2bits.RCEN = 1; // 1 = Enables Receive mode for I2C while(SSP1CON2bits.RCEN != 0); PIR1bits.SSP1IF = 0; return SSP1BUF; } bit I2C_AckSequence(unsigned char AckOrNack) { while ( ( SSPCON2 & 0x1F ) || ( R_nW ) ); // Idle? SSPCON2bits.ACKDT = AckOrNack; //Ack = 0, Nack = 1 SSPCON2bits.ACKEN = 1; // tell slave while(SSPCON2bits.ACKEN != 0); if(PIR2bits.BCL1IF == 1){ return FAILURE; } else{ PIR1bits.SSP1IF = 0; return SUCCESS; } } bit I2C_Stop(void) { while ( ( SSPCON2 & 0x1F ) || ( R_nW ) ); // Idle? SSP1CON2bits.PEN = 1; while(SSP1CON2bits.PEN != 0); if (PIR2bits.BCL1IF == 1){ return FAILURE; } else{ PIR1bits.SSP1IF = 0; return SUCCESS; } } This is code in main.c: if( I2C_Start() == FAILURE ) I2C_FLAGbits.START_FAILURE = 1; else I2C_FLAGbits.START_FAILURE = 0; if( I2C_SendAddress(OZ8920_W) == FAILURE ) I2C_FLAGbits.SLAVE_W_FAILURE = 1; else I2C_FLAGbits.SLAVE_W_FAILURE = 0; if( I2C_SendAddress(VOLTAGE) == FAILURE ) I2C_FLAGbits.COMMAND_FAILURE = 1; else I2C_FLAGbits.COMMAND_FAILURE = 0; if( I2C_RepeatedStart() == FAILURE ) I2C_FLAGbits.REPEATED_FAILURE = 1; else I2C_FLAGbits.REPEATED_FAILURE = 0; if( I2C_SendAddress(OZ8920_R) == FAILURE ) I2C_FLAGbits.SLAVE_R_FAILURE = 1; else I2C_FLAGbits.SLAVE_R_FAILURE = 0; Sometimes COMMAND_FAILURE flag is set, sometimes COMMAND_FAILURE flag is cleared, I need guys to examine my I2C flow to check whether this is wrong or not. IF the code above all are right: The slave chip's ADC scan data(voltage, current etc) every second and store these data into EEPROM register, and then Operation register is mapped into EEPROM register. Only thing I2C can communicate is operation register. Is it possible that the slave chip doesn't response to master during the chip is updating data into Operation Register? Very appreciate it! Thank you. |
|
相关推荐
1个回答
|
|
解决了从芯片复位I2C通信中断太多的问题
以上来自于百度翻译 以下为原文 Problem solved. Too many interrupt result in slave's chip resetting I2C communication |
|
|
|
只有小组成员才能发言,加入小组>>
5238 浏览 9 评论
2028 浏览 8 评论
1950 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3204 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2253 浏览 5 评论
778浏览 1评论
666浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
595浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
677浏览 0评论
576浏览 0评论
小黑屋| 手机版| Archiver| 德赢Vwin官网 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-23 06:39 , Processed in 1.372166 second(s), Total 75, Slave 59 queries .
Powered by 德赢Vwin官网 网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
德赢Vwin官网 观察
版权所有 © 湖南华秋数字科技有限公司
德赢Vwin官网 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号