/** * @brief I2C(AT24C02)读写测试 * @param 无 * @retval 无 */ void I2C_Test(void) { uint16_t i; printf("写入的数据\n\r"); for ( i=0; i<=255; i++ ) //填充缓冲 { I2c_Buf_Write[i] = i; printf("0x%02X ", I2c_Buf_Write[i]); if(i%16 == 15) { printf("\n\r"); } } I2C_EE_BufferWrite( I2c_Buf_Write, EEP_Firstpage, 256); //将I2c_Buf_Write中顺序递增的数据写入EERPOM中 printf("\n\r写成功\n\r"); printf("\n\r读出的数据\n\r"); I2C_EE_BufferRead(I2c_Buf_Read, EEP_Firstpage, 256); //将EEPROM读出数据顺序保持到I2c_Buf_Read中 for (i=0; i<256; i++) //将I2c_Buf_Read中的数据通过串口打印 { if(I2c_Buf_Read[i] != I2c_Buf_Write[i]) { printf("0x%02X ", I2c_Buf_Read[i]); printf("错误:I2C EEPROM写入与读出的数据不一致\n\r"); return; } printf("0x%02X ", I2c_Buf_Read[i]); if(i%16 == 15) { printf("\n\r"); } } printf("I2C(AT24C02)读写测试成功\n\r"); }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* System clocks configuration ---------------------------------------------*/ RCC_Configuration(); /* Initialize the I2C EEPROM driver ----------------------------------------*/ I2C_EE_Init(); /* First write in the memory followed by a read of the written data --------*/ /* Write on I2C EEPROM from EEPROM_WriteAddress1 */ I2C_EE_BufferWrite(Tx1_Buffer, EEPROM_WriteAddress1, BufferSize1); /* Read from I2C EEPROM from EEPROM_ReadAddress1 */ I2C_EE_BufferRead(Rx1_Buffer, EEPROM_ReadAddress1, BufferSize1); /* Check if the data written to the memory is read correctly */ TransferStatus1 = Buffercmp(Tx1_Buffer, Rx1_Buffer, BufferSize1); /* TransferStatus1 = PASSED, if the transmitted and received data to/from the EEPROM are the same */ /* TransferStatus1 = FAILED, if the transmitted and received data to/from the EEPROM are different */ /* Wait for EEPROM standby state */ I2C_EE_WaitEepromStandbyState(); /* Second write in the memory followed by a read of the written data -------*/ /* Write on I2C EEPROM from EEPROM_WriteAddress2 */ I2C_EE_BufferWrite(Tx2_Buffer, EEPROM_WriteAddress2, BufferSize2); /* Read from I2C EEPROM from EEPROM_ReadAddress2 */ I2C_EE_BufferRead(Rx2_Buffer, EEPROM_ReadAddress2, BufferSize2); /* Check if the data written to the memory is read correctly */ TransferStatus2 = Buffercmp(Tx2_Buffer, Rx2_Buffer, BufferSize2); /* TransferStatus2 = PASSED, if the transmitted and received data to/from the EEPROM are the same */ /* TransferStatus2 = FAILED, if the transmitted and received data to/from the EEPROM are different */ while (1) { } }
/** * @brief How to make a write followed by a read in the E²PROM. * @par Examples description * - Write 8 bytes on the devive (E²PROM) 0xA0. * - Read back the 8 bytes written on the devive (E²PROM) 0xA0. * - Check the coherency between the written and read Data. * @par Parameters: * None * @retval * None */ void main() { /* Initialize I/Os in Output Mode */ GPIO_Init(LEDS_PORT, (LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN), GPIO_MODE_OUT_PP_LOW_FAST); /* Optional: put here as example only */ I2C_DeInit(); /* Initialize the I2C */ I2C_EEInit(); /* All LEDs are ON per default */ GPIO_WriteLow(LEDS_PORT, (LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN)); /*** WRITE SEQUENCE ***/ I2C_EE_PageWrite(Buffer, Base_Add, Buffer_size); Delay(1000); /* To let eeprom the time to finish the write operation */ /*** READ SEQUENCE ***/ I2C_EE_BufferRead(Data, Base_Add, Buffer_size); /*** Check transmitted and received buffers ***/ for (i =Buffer_size; i > 0; i--) { if (Buffer[i-1] != Data[i-1]) { /* Toggle only LED1 if error is detected */ while (1) { GPIO_WriteReverse(LEDS_PORT, LED1_PIN); Delay((u16)60000); } } } /* Toggle all LEDs when I2C communication is terminated and buffers are ok */ while (1) { GPIO_WriteReverse(LEDS_PORT, (LED2_PIN | LED3_PIN | LED4_PIN)); Delay((u16)60000); } }
void I2C_EE_Upload(void) { int percentage; uint8_t byte; unsigned int LEDbackup; int i; LEDbackup=LEDbyte; clear(); write('S'); write('e'); write('n'); write('d'); write('i'); write('n'); write('g'); for (i=CONFIGLENGTH;i<EEPROM_BYTES;i++) { if((i%100)==0){ setCursor(0,1); percentage = (100*i)/(EEPROM_BYTES-CONFIGLENGTH); writenumber(percentage); // write('0'+(i/(EEPROM_BYTES-CONFIGLENGTH)*100)/100); // write('0'+((i/(EEPROM_BYTES-CONFIGLENGTH)*100)/10)%10); // write('0'+(i/(EEPROM_BYTES-CONFIGLENGTH)*100)%10); write('%'); write(' '); write(' ');} LEDbyte = LEDGripple[(i/80)%10]; setLEDS(); I2C_EE_BufferRead(&byte, i, 1); UART_send_byte(byte); } clear(); delay_ms(2000); clear(); standby(); LEDbyte=LEDbackup; setLEDS(); }
/***************************************************************************//** * @brief First, the content of Tx1_Buffer is written to the EEPROM_WriteAddress1 * and the written data are read. The written and the read buffers data are * then compared. Following the read operation, the program wait that the * EEPROM reverts to its Standby state. A second write operation is, then, * performed and this time, Tx2_Buffer is written to EEPROM_WriteAddress2, * which represents the address just after the last written one in the first * write. After completion of the second write operation, the written data * are read. The contents of the written and the read buffers are compared. ******************************************************************************/ void I2C_EEPROM(void) { /* System clocks configuration */ RCC_Configuration(); /* USART configuration */ USART_Configuration(); /* Initialize the I2C EEPROM driver */ I2C_EE_Init(); /* First write in the memory followed by a read of the written data */ /* Write on I2C EEPROM from EEPROM_WriteAddress1 */ I2C_EE_BufferWrite(Tx1_Buffer, EEPROM_WriteAddress1, BufferSize1); /* Read from I2C EEPROM from EEPROM_ReadAddress1 */ I2C_EE_BufferRead(Rx1_Buffer, EEPROM_ReadAddress1, BufferSize1); /* Check if the data written to the memory is read correctly */ TransferStatus1 = Buffercmp(Tx1_Buffer, Rx1_Buffer, BufferSize1); }
/* 与A8通信 */ void Task_A8CONNECT(void *p_arg) { (void)p_arg; //'p_arg'没有用到,防止编译器警告 while(1) { unsigned char errA8; // unsigned int i=0; // printf("A8_wait"); // for(i=0;i<10;i++) // { // printf("%c",RxBuffer1[i]); // } OSSemPend(A8_SEM,0,&errA8); // printf("A8_in"); // // if(RxBuffer1[0]!=0x01) // { // for(i=0;i<10;i++) // RxBuffer1[i]=0; // RxCounter1=0; // } CRC16((unsigned char *)RxBuffer1,5); // printf("16H:%c,16L:%c",crc16H,crc16L); // // for(i=0;i<10;i++) // printf("%c",RxBuffer1[i]); if(crc16L==RxBuffer1[6]&&crc16H==RxBuffer1[5]) { //将EEPROM读出数据顺序保持到I2c_Buf_Read中 I2C_EE_BufferRead(I2c_Buf_Read, EEP_Firstpage, 256); Receiver_Control=RxBuffer1[2]; Receiver_Spare=RxBuffer1[3]; // printf("OK!886"); replyA8(); } OSTimeDlyHMSM(0,0,0,500); } }
void I2C_24C02_Test(void) { u16 i; u8 I2C_Buf_Write[32]; //写缓冲区 u8 I2C_Buf_Read[32]; //读缓冲区 printf("AT24C02 Writing...\n\r"); for(i=0;i<=31;i++) //填充缓冲 { I2C_Buf_Write[i]=i; printf("0x%02X ",I2C_Buf_Write[i]); if(i%16 == 15) { printf("\n\r"); } } I2C_EE_BufferWrite(I2C_Buf_Write,EEP_Firstpage,32); //将I2c_Buf_Write中顺序递增的数据写入EERPOM中 printf("\n\rAT24C02 Reading...\n\r"); I2C_EE_BufferRead(I2C_Buf_Read,EEP_Firstpage,32); //将EEPROM读出数据顺序保持到I2c_Buf_Read中 for(i=0;i<32;i++) //将I2c_Buf_Read中的数据通过串口打印 { if(I2C_Buf_Read[i]!=I2C_Buf_Write[i]) { printf("0x%02X ", I2C_Buf_Read[i]); printf("Err:Data Read and Write are't Matching.\n\r"); return; } printf("0x%02X ", I2C_Buf_Read[i]); if(i%16 == 15) { printf("\n\r"); } } printf("I2C-AT24C02 Test Passed!\n\r"); }
/** 从eeprom中读出数据 @param pOutBuf 要读出数据保存的地址 @param Addr eeprom的地址 @param NumOfBytes 要读出多少个字节 */ void eeprom_read(u8 *pOutBuf,u16 Addr,u16 NumOfBytes) { I2C_EE_BufferRead(pOutBuf, Addr, NumOfBytes); }
void I2C_EE_LoadConfig(void) { I2C_EE_BufferRead((uint8_t *)Config, 0, CONFIGLENGTH); }