- int main(void)
- {
- i2c_status_type i2c_status;
-
- /* 初始化系统时钟 */
- system_clock_config();
-
- /* 初始化中断优先级分组 */
- nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
- /* 初始化延时函数 */
- delay_init();
-
- /* 初始化LCD */
- lcd_init(LCD_DISPLAY_VERTICAL);
- /* 初始化EEPROM */
- eeprom_init(EE_I2C_CLKCTRL_400K);
-
- /* 显示信息 */
- lcd_string_show(10, 20, 200, 24, 24, (uint8_t *)"EEPROM Test");
- /* 轮询方式写数据到EEPROM */
- if((i2c_status = eeprom_data_write(&hi2c2, EE_MODE_POLL, EEPROM_I2C_ADDRESS, 0, tx_buf1, BUF_SIZE, I2C_TIMEOUT)) != I2C_OK)
- {
- error_handler(i2c_status);
- }
-
- delay_ms(1);
-
- /* 轮询方式从EEPROM读数据 */
- if((i2c_status = eeprom_data_read(&hi2c2, EE_MODE_POLL, EEPROM_I2C_ADDRESS, 0, rx_buf1, BUF_SIZE, I2C_TIMEOUT)) != I2C_OK)
- {
- error_handler(i2c_status);
- }
-
- delay_ms(1);
-
- /*中断方式写数据到EEPROM */
- if((i2c_status = eeprom_data_write(&hi2c2, EE_MODE_INT, EEPROM_I2C_ADDRESS, 0, tx_buf2, BUF_SIZE, I2C_TIMEOUT)) != I2C_OK)
- {
- error_handler(i2c_status);
- }
-
- delay_ms(1);
-
- /* 中断方式从EEPROM读数据 */
- if((i2c_status = eeprom_data_read(&hi2c2, EE_MODE_INT, EEPROM_I2C_ADDRESS, 0, rx_buf2, BUF_SIZE, I2C_TIMEOUT)) != I2C_OK)
- {
- error_handler(i2c_status);
- }
-
- delay_ms(1);
-
- /* DMA方式写数据到EEPROM */
- if((i2c_status = eeprom_data_write(&hi2c2, EE_MODE_DMA, EEPROM_I2C_ADDRESS, 0, tx_buf3, BUF_SIZE, I2C_TIMEOUT)) != I2C_OK)
- {
- error_handler(i2c_status);
- }
-
- delay_ms(1);
-
- /* DMA方式从EEPROM读数据 */
- if((i2c_status = eeprom_data_read(&hi2c2, EE_MODE_DMA, EEPROM_I2C_ADDRESS, 0, rx_buf3, BUF_SIZE, I2C_TIMEOUT)) != I2C_OK)
- {
- error_handler(i2c_status);
- }
-
- /* 比较写入和读取的数据是否正确 */
- if((buffer_compare(tx_buf1, rx_buf1, BUF_SIZE) == 0) &&
- (buffer_compare(tx_buf2, rx_buf2, BUF_SIZE) == 0) &&
- (buffer_compare(tx_buf3, rx_buf3, BUF_SIZE) == 0))
- {
- lcd_string_show(10, 60, 310, 24, 24, (uint8_t *)"eeprom write/read ok");
- }
- else
- {
- error_handler(i2c_status);
- }
-
- while(1)
- {
- }
- }
void eeprom_init(void)函数代码描述
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] eeprom_init.
- * @param none.
- * @retval none.
- */
- void eeprom_init(void)
eeprom_data_read()函数代码描述
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] read data from eeprom.
- * @param hi2c: the handle points to the operation information.
- * @param mode: i2c transfer mode.
- * - EE_MODE_POLL: transmits data through polling mode.
- * - EE_MODE_INT: transmits data through interrupt mode.
- * - EE_MODE_DMA: transmits data through dma mode.
- * @param address: eeprom address.
- * @param mem_address: eeprom memory address.
- * @param pdata: data buffer.
- * @param number: the number of data to be transferred.
- * @param timeout: maximum waiting time.
- * @retval i2c status.
- */
- i2c_status_type eeprom_data_read(i2c_handle_type* hi2c, eeprom_i2c_mode_type mode, uint16_t address, uint16_t mem_address, uint8_t* pdata, uint16_t number, uint32_t timeout)
void eeprom_data_write()函数代码描述
- /**
- * @brief write data to eeprom.
- * @param hi2c: the handle points to the operation information.
- * @param mode: i2c transfer mode.
- * - EE_MODE_POLL: transmits data through polling mode.
- * - EE_MODE_INT: transmits data through interrupt mode.
- * - EE_MODE_DMA: transmits data through dma mode.
- * @param address: eeprom address.
- * @param mem_address: eeprom memory address.
- * @param pdata: data buffer.
- * @param number: the number of data to be transferred.
- * @param timeout: maximum waiting time.
- * @retval i2c status.
- */
- i2c_status_type eeprom_data_write(i2c_handle_type* hi2c, eeprom_i2c_mode_type mode, uint16_t address, uint16_t mem_address, uint8_t* pdata, uint16_t number, uint32_t timeout)
下载验证
如果通讯正常,写入和读取的数据相同LCD屏显示eeprom write/read ok
如果通讯错误,LCD屏显示eeprom write/read error