#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG1
#pragma config FOSC = 0B11 // Oscillator Selection (ECH, External Clock, High Power Mode (4-32 MHz): device clock supplied to CLKIN pin)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT enabled)
#pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is enabled)
#pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
// CONFIG2
#pragma config WRT = ALL // Flash Memory Self-Write Protection (Write protection off)
#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 LPBOR = OFF // Low-Power Brown-out Reset (Low-Power BOR is disabled)
#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
#define _XTAL_FREQ 20000000
int flash_temp;
int RO_hrs_hi_adrs=0x1f;
int RO_hrs_lo_adrs=0xff; // i.e my storage address is 1fff;
//////------ sub routines functions-----//////
void interrput_ini(void)
{
{
INTCONbits.GIE=1; // enable global intrpt
INTCONbits.T0IE=1; // enable timer0 intrpt
OPTION_REG&=0XC5; // timer0 speed
}
}
unsigned int read_flash(int adrs_hi,int adrs_lo) // read the flash memory
{
PMADRL=adrs_lo ;//LSB bits of Flash memory address
PMADRH=adrs_hi;//MSB bits of Flash memory address
PMCON1bits.CFGS=0; // don't select configure bits.
PMCON1bits.RD = 1; // Flash Read
int adrs_hi_value=PMDATL; // MSB data of the address
time=adrs_hi_value;
return time; //return value
}
//--------------------unlock flash to write data------------------------
void unlock_flash(void)
{
PMCON2=0x55;
PMCON2=0xaa;
PMCON1bits.WR=1;
NOP();
NOP();
}
//----------------------------write falsh memory---------------
void write_flash(int adr_hi,int adr_lo,int data_lo)
{
unsigned int INTCON_Save;
INTCON_Save=INTCON; // save the intrrput function
INTCON=0; // desable all itterupts
PMADRH=adr_hi; // address high
PMADRL=adr_lo; // put address low
PMCON1bits.CFGS=0;
PMCON1bits.WREN=1; // writing bit enable
PMCON1bits.LWLO=1;
PMDATL=data_lo; // data add of low - bit
PMCON1bits.LWLO=0;
unlock_flash(); // writing function enable
__delay_ms(2);
PMCON1bits.WREN=0;
INTCON=INTCON_Save; // again set intturrput velues.
}
/////------------------------- interrupt service routine------------------////
void interrupt ISR()
{
if(T0IF)
{
if(RC1==1) //reset switch
{
flash_temp=0x00;
write_flash(flash_adrs_hi,flash_adrs_lo,flash_temp); //write Flash
}
read_flash(flash_adrs_hi,flash_adrs_lo); //read previous data
flash_temp=flash_temp+1; // add previous data+1
write_flash(flash_adrs_hi,flash_adrs_lo,Flash_temp);// then write new data
}
T0IF=0;
}
////-------main program----///
void main()
{
TRISCbits.TRISC1=1; // make input
TRISBbits.TRISB3=0; // output
RB3=0; //default value
RC1=0;
interrput_ini();
read_flash(flash_adrs_hi,flash_adrs_lo);
flash_temp=time;
while(1)
{
if(flash>100)
{
RB3=1;
}
else
RB3=0;
}
}
this is my program . when i press reset switch that time flash write and read everything are ok. but when i put switch of my device that time my address value goes to "111 1111 1111 1111"; my procedure correct or wrong please tell me. thanks.