本帖最后由 Mcuzone_TKN 于 2020-5-14 14:29 编辑
关键词:Microchip Atmel SAM4E SAM4E-EK SAM4E16E 芯片 PCF8563 EEPROM
概述:简要解读EEPROM
EEPROM(带电可擦除编程只读存储器)是用户可更改的只读存储器(ROM),其可通过高于普通电压的作用来擦除和重新编程(重写)。
EEPROM以Byte为最小修改单位,不必将资料全部洗掉才能写入。
打开产品光盘SAM4E16E-EK/SAM4E16E-EK中文资料/softpack软件包/Atmel Studio 7,打开13a_TWI_MASTER_EXAMPLE_eeprom例子。
int main(void)
{
uint32_t i;
twi_options_t opt;
twi_packet_t packet_tx, packet_rx;
//初始化SAM系统
sysclk_init();
board_init();
//初始化控制台uart
configure_console();
//输出示例信息
puts(STRING_HEADER);
//配置systick为1ms
puts("Configure system tick to get 1ms tick period.\r");
if (SysTick_Config(sysclk_get_cpu_hz() / 1000)) {
puts("-F- Systick configuration error\r");
while (1);
}
//使能TWI外部时钟
pmc_enable_periph_clk(BOARD_ID_TWI_EEPROM);
//配置TWI驱动程序的选项
opt.master_clk = sysclk_get_cpu_hz();
opt.speed = TWI_CLK;
//配置要传送的数据包
packet_tx.chip = AT24C_ADDRESS;
packet_tx.addr[0] = EEPROM_MEM_ADDR >> 8;
packet_tx.addr[1] = EEPROM_MEM_ADDR;
packet_tx.addr_length = EEPROM_MEM_ADDR_LENGTH;
packet_tx.buffer = (uint8_t *) test_data_tx;
packet_tx.length = TEST_DATA_LENGTH;
//配置要接收的数据包
packet_rx.chip = packet_tx.chip;
packet_rx.addr[0] = packet_tx.addr[0];
packet_rx.addr[1] = packet_tx.addr[1];
packet_rx.addr_length = packet_tx.addr_length;
packet_rx.buffer = gs_uc_test_data_rx;
packet_rx.length = packet_tx.length;
if (twi_master_init(BOARD_BASE_TWI_EEPROM, &opt) != TWI_SUCCESS) {
puts("-E-\tTWI master initialization failed.\r");
while (1) ;
}
//发送测试模式到EEPROM
if (twi_master_write(BOARD_BASE_TWI_EEPROM, &packet_tx) != TWI_SUCCESS) {
puts("-E-\tTWI master write packet failed.\r");
while (1) ;
}
printf("Write:\tOK!\n\r");
//等待至少10ms
mdelay(WAIT_TIME);
//从EEPROM获得内存
if (twi_master_read(BOARD_BASE_TWI_EEPROM, &packet_rx) != TWI_SUCCESS) {
puts("-E-\tTWI master read packet failed.\r");
while (1) ;
}
puts("Read:\tOK!\r");
//比较发送和接收
for (i = 0; i < TEST_DATA_LENGTH; i++) {
if (test_data_tx != gs_uc_test_data_rx) {
puts("Data comparison:\tUnmatched!\r");
while (1) ;
}
}
puts("Data comparison:\tMatched!\r");
while (1) ;
}
|