Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to write flash memory in pic microcontroller

Status
Not open for further replies.

Arunkumar

New Member
Hi, I am using PIC16LF1906 for my project I tried to store my data in flash memory. That is working, but when i reset the main power all data changed in high. That means all data going default position. So please any one say how to write flash and read data what I stored in previous time. thanks regard . I use mp lap x version compiler XC8 v1.21
 
Flash does not get cleared by reset. You're either not writing to flash or MPLABX :)() is resetting it.

Mike.
 
Flash does not get cleared by reset. You're either not writing to flash or MPLABX :)() is resetting it.

Mike.
ok thanks. But when I ON that IC it's address data will go to default value(i.e high values). I can't read my previous value.
I would like tell about my project logic. i took one flash address in pic16lf1906 IC . I tried write IC usage time in that particular address. I had done that. but when I switch off the device all data of that particular address goes to default condition. Now my quarry is Can I store my data in Program memory If it is yes how please reply thanks advance.
 
#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.
 
In your read_flash routine you return variable "time" but you haven't declared the variable..
In your main... you have used the variable "flash" but again no declaration.....

I would be very surprised if the code compiles clean...
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top