初次用PIC18F66K80这款单片机,写入一个简单的数进入EEPROM,然后再读出来,点亮LED灯。发现EEPROM里面的值没有变化,读出来还是0XFF的值,请高手指点,谢谢!用的晶振是16M,采用4倍频。程序如下:
void WriteEEprom(unsigned int EEaddr,unsigned char WEEdata)
{
char GIE_BIT_VAL = 0;
do{}
while(EECON1bits.WR);
EEADRH = ((unsigned char)(EEaddr>>8))&0X03;
EEADR = (unsigned char)(EEaddr&0X00FF);
EEDATA = WEEdata;
EECON1bits.EEPGD = 0;
EECON1bits.CFGS = 0;
EECON1bits.WREN = 1;
GIE_BIT_VAL = INTCONbits.GIE;
INTCONbits.GIE = 0;
EECON2 = 0X55;
Nop();
Nop();
Nop();
EECON2 = 0XAA;
EECON1bits.WR = 1;
do{}
while(EECON1bits.WR);
Nop();
Nop();
Nop();
INTCONbits.GIE = GIE_BIT_VAL;
EECON1bits.WREN = 0;
}
unsigned char ReadEEprom(unsigned int EEaddr)
{
do{}
while(EECON1bits.RD);
EEADRH = ((unsigned char)(EEaddr>>8))&0X03;
EEADR = (unsigned char)(EEaddr&0X00FF);
EECON1bits.EEPGD = 0;
EECON1bits.CFGS = 0;
EECON1bits.RD = 1;
do{}
while(EECON1bits.RD);
Nop();
Nop();
Nop();
return (EEDATA);
}
void main(void)
{
#pragma config CONFIG1L = 0X10; //仅使用主时钟
#pragma config CONFIG1H = 0X12; //4倍频,振荡器为HS
#pragma config CONFIG2L = 0X00; //禁止欠压复位
#pragma config CONFIG2H = 0X3A; //看门狗定时65.5s
#pragma config CONFIG3H = 0X80; //使能/MCLR
#pragma config CONFIG4L = 0X00; //RB6和RB7只能用于调试
#pragma config CONFIG5L = 0X00; //BLOCK受代码保护
#pragma config CONFIG5H = 0X00; //EEPROM受代码保护
#pragma config CONFIG6L = 0X00; //BLOCK受写保护
#pragma config CONFIG6H = 0X00; //EEPROM受写保护
#pragma config CONFIG7L = 0X00; //禁止BLOCK读操作
#pragma config CONFIG7H = 0X00; //禁止引导区执行表读操作
while(1)
{
unsigned int i = 0;
CLRWDT();
WriteEEprom(0X0000,0X01);
i = ReadEEprom(0X0000);
if(i == 0X01)
bLED_LOOP = 0; //LED灯亮
}
} |