51单片机eeprom写四个数据
时间: 2023-09-28 21:12:52 浏览: 93
好的,以下是51单片机写入EEPROM的示例代码:
```c
#include <reg51.h>
#define EEPROM_ADDR 0x30 // EEPROM的地址
void write_eeprom(unsigned char addr, unsigned char data)
{
IAP_CONTR = 0x81; // 设置IAP_CONTR寄存器,准备写入EEPROM
IAP_ADDRH = 0x00;
IAP_ADDRL = addr;
IAP_DATA = data;
IAP_CMD = 0x02; // 写入数据到EEPROM
IAP_TRIG = 0x5A;
IAP_TRIG = 0xA5;
_nop_();
IAP_CONTR = 0x00; // 清空IAP_CONTR寄存器
}
void main()
{
unsigned char data1 = 0x01;
unsigned char data2 = 0x02;
unsigned char data3 = 0x03;
unsigned char data4 = 0x04;
write_eeprom(0x00, data1);
write_eeprom(0x01, data2);
write_eeprom(0x02, data3);
write_eeprom(0x03, data4);
while (1);
}
```
在上面的代码中,我们定义了一个`write_eeprom`函数,该函数用于将数据写入EEPROM。在主函数中,我们定义了四个数据`data1`、`data2`、`data3`和`data4`,分别写入EEPROM的地址为0x00、0x01、0x02和0x03的位置。
阅读全文