PIC 1508 EEPROM

2019-04-15 14:08发布

EEPROM读、擦、写。 unsigned int eeprom_read(unsigned int address) { // GIE =0; unsigned int data = 0; PMCON1bits.CFGS = 0;//访问闪存程序存储器 PMADRH = address / 256; PMADRL = address % 256;//写入地址 PMCON1bits.RD = 1;//启动读操作 _nop(); _nop(); data = PMDATH; data = data << 8; data+ = PMDATL; // GIE = 1; return data; }     void eeprom_erase(unsigned int address) { // GIE=0; PMCON1bits.CFGS = 0;//访问闪存程序存储器 PMADRH = address / 256; PMADRL = address % 256;//擦除地址 PMCON1bits.FREE = 1;//选择擦除操作 PMCON1bits.WREN = 1;//写使能 PMCON2 = 0x55; PMCON2 = 0xAA;//解码 PMCON1bits.WR = 1;//开始擦除 _nop(); _nop(); while(PMCON1bits.WR);//等待擦除完成,硬件自动清零 PMCON1bits.WREN = 0; // GIE = 1; } void eeprom_write(unsigned int address,unsigned int data) { // GIE = 0; PMCON1bits.CFGS = 0;//访问闪存程序存储器 PMADRH = address / 256; PMADRL = address % 256;//写入地址 PMCON1bits.FREE = 0;//选择写操作 PMCON1bits.LWLO = 1;//仅装入写锁存器 PMCON1bits.WREN = 1;//使能写操作 PMDATH = data / 256; PMDATL = data % 256;//装入数据0X308 PMCON1bits.LWLO = 0;//将锁存器内容写入闪存 PMCON2 = 0x55; PMCON2 = 0xAA;//解码 PMCON1bits.WR = 1; _nop(); _nop(); while(PMCON1bits.WR);//等待擦除完成,硬件自动清零 PMCON1bits.WREN = 0; // GIE = 1; }  

热门文章