void WR_EEP(uchar addr,uchar dat)
{
EEADR = addr;
EEDATA = dat;
EEPGD = 0;
CFGS = 0;
WREN = 1;
GIE = 0;
EECON2 = 0x55;
EECON2 = 0xAA;
WR = 1;
while(WR);
GIE = 1;
WREN = 0;
}
uchar RD_EEP(uchar addr)
{
EEADR = addr;
EEPGD = 0;
CFGS = 0;
RD = 1;
return EEDATA;
}
WR__EEP(0,10);
val = RD_EEP(0);
可以读出来10;
//WR__EEP(0,10);
val = RD_EEP(0);
断电后,读出来是0.不解啊。
仿真结果竟然EECON2写不进去。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
直接先用库函数试试再说啊,
C编译自带的库函数应该有这方面的操作吧,自己找找帮助手册
2.jpg (247.01 KB, 下载次数: 0)
下载附件
2016-9-22 15:45 上传
eecon2 只能写不能读,仿真看不到结果的
给你一个pic18f87k22的程序你参考一下。
- #include "flash_eeprom.h"
- #define EEPROM_START 0
- #define ALIVE 0x55
- system_param_t system_param;
- void read_eeprom(uint8_t *array,uint8_t length)
- {
- uint8_t i;
- for(i=0;i<length;i++) {
- EEADRH = (EEPROM_START+i)>>8;
- EEADR = (EEPROM_START+i)&0xff;
- EECON1bits.EEPGD = 0;
- EECON1bits.CFGS = 0;
- EECON1bits.RD = 1;
- NOP();
- NOP();
- array[i] = EEDATA;
- }
- }
- void write_eeprom(uint8_t *array,uint8_t length)
- {
- uint8_t i;
- for(i=0;i<length;i++) {
- EEADRH = (EEPROM_START+i)>>8;
- EEADR = (EEPROM_START+i)&0xff;
- EEDATA = array[i];
- EECON1bits.EEPGD = 0;
- EECON1bits.CFGS = 0;
- EECON1bits.WREN = 1;
- INTCONbits.GIE = 0;
- EECON2 = 0x55;
- EECON2 = 0xaa;
- EECON1bits.WR = 1;
- while(EECON1bits.WR);
- INTCONbits.GIE = 1;
-
- EECON1bits.WREN = 0;
- }
- }
- void flash_eeprom_init()
- {
- read_eeprom((uint8_t *)&system_param,(uint8_t)sizeof(system_param));
- if(system_param.isAlive != ALIVE) {
- system_param.isAlive = ALIVE;
- system_param.isAuto = 1;
- system_param.windowTime = 25;
- write_eeprom((uint8_t *)&system_param,(uint8_t)sizeof(system_param));
- }else {
-
- }
- }
复制代码一周热门 更多>