/** * @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) { } }
void I2C_EE_Log(uint8_t* LogData) { if (currentByte >=EEPROM_BYTES) {I2C_EE_FinishLog();} else { I2C_EE_BufferWrite(LogData, currentByte, ENTRYBYTES); currentByte+=ENTRYBYTES; } }
/***************************************************************************//** * @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); }
void I2C_EE_Erase(void) { int percentage; unsigned int LEDbackup; int i; clear(); write('E'); write('R'); write('A'); write('S'); write('I'); write('N'); write('G'); for (i=CONFIGLENGTH;i<EEPROM_BYTES;i++) { I2C_EE_BufferWrite(blank, i, 1); // I2C_EE_ByteWrite(blank, i); if((i%100)==0){ setCursor(0,1); percentage = (100*i)/(EEPROM_BYTES-CONFIGLENGTH); writenumber(percentage); write('%'); write(' '); write(' ');} LEDbyte = LEDRripple[(i/80)%10]; setLEDS(); } clear(); complete(); delay_ms(2000); clear(); standby(); LEDbyte=LEDbackup; setLEDS(); }
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 */ void Task_USART1(void *p_arg) { (void)p_arg; //'p_arg'没有用到,防止编译器警告 while(1) { unsigned char errkey, err; // u16 i; // printf("等待A8"); /* car1经过 */ OSSemPend(key_SEM,0,&errkey); //将car1数据保存至buffer并串口输出 weight = (char*)OSMboxPend(adc_MBOX,0,&err); save_in_buffer(car1,*weight); /* 读写EEPROM */ //将I2c_Buf_Write中顺序递增的数据写入EERPOM中 I2C_EE_BufferWrite((u8 *)sendData, EEP_Firstpage, 256); OSTimeDlyHMSM(0,0,0,500); /* car2经过 */ OSSemPend(key_SEM,0,&errkey); //将car1数据保存至buffer并串口输出 weight = (char*)OSMboxPend(adc_MBOX,0,&err); save_in_buffer(car2,*weight); /* 读写EEPROM */ //将I2c_Buf_Write中顺序递增的数据写入EERPOM中 I2C_EE_BufferWrite((u8 *)sendData, EEP_Firstpage, 256); OSTimeDlyHMSM(0,0,0,500); /* car3经过 */ OSSemPend(key_SEM,0,&errkey); //将car1数据保存至buffer并串口输出 weight = (char*)OSMboxPend(adc_MBOX,0,&err); save_in_buffer(car3,*weight); /* 读写EEPROM */ //将I2c_Buf_Write中顺序递增的数据写入EERPOM中 I2C_EE_BufferWrite((u8 *)sendData, EEP_Firstpage, 256); OSTimeDlyHMSM(0,0,0,500); // printf("\n\r读出eeprom的数据\n\r"); //将EEPROM读出数据顺序保持到I2c_Buf_Read中 // I2C_EE_BufferRead(I2c_Buf_Read, EEP_Firstpage, 256); //将I2c_Buf_Read中的数据通过串口打印 // for (i=0; i<=50; i++) // { // printf("%c", I2c_Buf_Read[i]); // } // OSTimeDlyHMSM(0,0,0,500); } }
/** 往eeprom中写数据 @param pInBuf 要写入数据的地址 @param Addr eeprom的地址 @param NumOfBytes 要写入多少个字节 */ void eeprom_write(u8 *pInBuf,u16 Addr,u16 NumOfBytes) { I2C_EE_BufferWrite(pInBuf, Addr, NumOfBytes); }
void I2C_EE_WriteConfig(void) { I2C_EE_BufferWrite((uint8_t *)Config, 0, CONFIGLENGTH); logging_timer_init(); }