Chào mọi người, mình đang code giao tiếp I2C trên STM8S003F3 nhưng gặp lỗi. Đầu tiên mình khởi tạo xung clock
Sau đó đọc I2C thì bị lỗi:
Sau khi I2C_GenerateSTART(ENABLE); thì nó báo cờ I2C_FLAG_BUSBUSY, nhờ các tiền bối gỡ giúp lỗi này.
Mạch kết nối 2 ngoại vi i2c trên cùng 1 bus, điạ chỉ lần lượt là 0x29 và 0x39.
Code:
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); CLK_PeripheralClockConfig(TCS34725_I2C_CLK, ENABLE); /* I2C configuration */ /* sEE_I2C Peripheral Enable */ I2C_Cmd( ENABLE); /* sEE_I2C configuration after enabling it */ I2C_Init(I2C_MAX_FAST_FREQ, TCS34725_ADDRESS7, I2C_DUTYCYCLE_2, I2C_ACK_CURR, I2C_ADDMODE_7BIT, 16);
Code:
TCS34725_TimeOut = DEFAULT_TIMEOUT; while(I2C_GetFlagStatus( I2C_FLAG_BUSBUSY)) { if((TCS34725_TimeOut--) == 0) return 99; } I2C_GenerateSTART(ENABLE); I2C_Send7bitAddress(TCS34725_ADDRESS7, TCS34725_DIRECTION_TX); /* Test on EV5 and clear it (cleared by reading SR1 then writing to DR) */ TCS34725_TimeOut = DEFAULT_TIMEOUT; while(!I2C_CheckEvent( I2C_EVENT_MASTER_MODE_SELECT)) { if((TCS34725_TimeOut--) == 0) return 88; } /* Test on EV6 and clear it */ TCS34725_TimeOut = DEFAULT_TIMEOUT; while(!I2C_CheckEvent( I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) { if((TCS34725_TimeOut--) == 0) return 77; } /* Test on EV8 and clear it */ TCS34725_TimeOut = DEFAULT_TIMEOUT; while(I2C_GetFlagStatus(I2C_FLAG_TRANSFERFINISHED) == RESET) { if((TCS34725_TimeOut--) == 0) return 66; } /* Send START condition a second time */ I2C_GenerateSTART( ENABLE); /* Test on EV5 and clear it (cleared by reading SR1 then writing to DR) */ TCS34725_TimeOut = DEFAULT_TIMEOUT; while(!I2C_CheckEvent( I2C_EVENT_MASTER_MODE_SELECT)) { if((TCS34725_TimeOut--) == 0) return 55; } /* Send EEPROM address for read */ //I2C_Send7bitAddress(TCS34725_ADDRESS7 , I2C_DIRECTION_RX); I2C_Send7bitAddress(addr , I2C_DIRECTION_RX); /* Wait on ADDR flag to be set (ADDR is still not cleared at this level */ TCS34725_TimeOut = DEFAULT_TIMEOUT; while(I2C_GetFlagStatus( I2C_FLAG_ADDRESSSENTMATCHED) == RESET) { if((TCS34725_TimeOut--) == 0) return 44; } /* Disable Acknowledgement */ I2C_AcknowledgeConfig(I2C_ACK_NONE); /* Call User callback for critical section start (should typically disable interrupts) */ //sEE_EnterCriticalSection_UserCallback(); /* Clear ADDR register by reading SR1 then SR3 register (SR1 has already been read) */ (void)TCS34725_I2C->SR3; /* Send STOP Condition */ I2C_GenerateSTOP( ENABLE); /* Call User callback for critical section end (should typically re-enable interrupts) */ //sEE_ExitCriticalSection_UserCallback(); /* Wait for the byte to be received */ TCS34725_TimeOut = DEFAULT_TIMEOUT; while(I2C_GetFlagStatus( I2C_FLAG_RXNOTEMPTY) == RESET) { if((TCS34725_TimeOut--) == 0) return 33; } /* Read the byte received from the EEPROM */ tmp = I2C_ReceiveData(); /* Decrement the read bytes counter */ //(uint16_t)(*NumByteToRead)--; /* Wait to make sure that STOP control bit has been cleared */ TCS34725_TimeOut = DEFAULT_TIMEOUT; while(TCS34725_I2C->CR2 & I2C_CR2_STOP) { if((TCS34725_TimeOut--) == 0) return 22; } /* Re-Enable Acknowledgement to be ready for another reception */ I2C_AcknowledgeConfig( I2C_ACK_CURR);
Mạch kết nối 2 ngoại vi i2c trên cùng 1 bus, điạ chỉ lần lượt là 0x29 và 0x39.
Comment