请问一下各位前辈,这个EEPROM我这样写AN2口可以正常输出。
但是为什么第二次烧录的时候,我注释掉了写代码,就读不到第一次写进去的数据了捏。
EEPROM掉电后数据不是不会清除吗?
// PIC12F1822 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1
#pragma config FOSC = INTOSC // Oscillator Selection (HS Oscillator, High-speed crystal/resonator connected between OSC1 and OSC2 pins)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable (PWRT enabled)
#pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = OFF // PLL Enable (4x PLL disabled)
#pragma config STVREN = OFF // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will not cause a Reset)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
void EE_W(unsigned char data,unsigned char addr)//EEPROM写配置
{
EEDAT=data;
EEADR=addr;
EECON2=0x55;
EECON2=0xAA;
EECON1bits.WR=1;
EECON1=0x07;
while(EECON1bits.WR==1);
}
unsigned char EE_R(unsigned char addr)//EEPROM读配置
{
unsigned char rec;
EEADR=addr;
EECON1bits.RD=1;
while(EECON1bits.RD==1);
rec=EEDATA;
return rec;
}
void main(void)
{
OSCCON=0x6A;//内部时钟4MHZ
TRISA=0x0;
// ANSELA=0x0;
int num,rec;
for(;;)
{
for(num=10;num>0;num--)
{
EE_W(num,num+1);
}
if(EE_R(8)==7)PORTA=0x02;//判断第八字节是7否,是则AN2口输出1
else PORTA=0;
}
}
|