Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
March 28, 2024, 11:47:18 23:47


Login with username, password and session length


Pages: [1]
Print
Author Topic: PIC 16F1827 XC8 flash write and erase  (Read 9505 times)
0 Members and 1 Guest are viewing this topic.
xmas
Inactive

Offline Offline

Posts: 5

Thank You
-Given: 2
-Receive: 7


« on: February 10, 2015, 03:53:10 15:53 »

I have programmed some functions for erasing and programming the program flash memory for PIC16F1827.
The documentation from microchip is very poor and wrong, also they do not describe the way for writing only one word to the flash.
Tested with
MPLAB-X v2.30
XC8 v1.33
pic16F1827

« Last Edit: February 10, 2015, 04:15:10 16:15 by xmas » Logged
arslanweb
Newbie
*
Offline Offline

Posts: 13

Thank You
-Given: 1
-Receive: 4


« Reply #1 on: February 10, 2015, 09:41:57 21:41 »

Hi,

Use in MPLABX the code configrator. It creats for you, this codes.
Logged

Gallymimu
Hero Member
*****
Offline Offline

Posts: 704

Thank You
-Given: 151
-Receive: 214


« Reply #2 on: February 12, 2015, 04:42:36 04:42 »

I have programmed some functions for erasing and programming the program flash memory for PIC16F1827.
The documentation from microchip is very poor and wrong, also they do not describe the way for writing only one word to the flash.
Tested with
MPLAB-X v2.30
XC8 v1.33
pic16F1827



could you elaborate one what's "wrong" with the microchip documentation so the rest of us can avoid the pitfalls?
Logged
jamesbond
Newbie
*
Offline Offline

Posts: 23

Thank You
-Given: 50
-Receive: 9


« Reply #3 on: February 12, 2015, 07:13:11 07:13 »

You cannot erase one or to bytes in flash.When you modify you have to erase the entire block.
First read a flash block into a array of 32 bytes.
Make your changes in that array.
Erase the flash block you last read and then rewrite the flash with the modified array.

I use these functions to write and read from flash.I found these functions on internet.
Logged
xmas
Inactive

Offline Offline

Posts: 5

Thank You
-Given: 2
-Receive: 7


« Reply #4 on: February 12, 2015, 08:04:39 08:04 »

I do not say that I can erase one word !
Your FlashWriteRow do not work on pic16F1827 because the write blocksize is 8 not 32. This is one of the bugs in the pic16F1827 documentation !
I only give simple example function for write, read and erase.
I thought everybody who wants to use these functions knows, that before you write you have to erase !
Because of performance, normaly if you want to write a program to flash, you erase the whole flash or a part of this, then you do the writes. Like the MPLAP do !
I was searching for a long time, specially for pic16F1827, for these functions in the web and did not find them.
Also you will NOT find the function for write only ONE word (for shure you have to erase before).

arslanweb is right, that with the MPLAB code configurater you can generate a similar code, I did not know.


Posted on: February 12, 2015, 08:46:16 08:46 - Automerged

could you elaborate one what's "wrong" with the microchip documentation so the rest of us can avoid the pitfalls?

The datasheet says that the write blocksize is 32, the correct size is 8 !
The examples in the datasheet do not say explicit that you have to store in EEDATL and EEDATH the FIRST word again before write, see:
   EEPGD=1;
   WREN=1;
   CFGS=0;
   LWLO=1;
   EEADR = adr;
   for(i=0;i<cnt;i++)
   {
      EEDATL = data[i*2+1];
      EEDATH = data[i*2];
      EECON2=0x55;
      EECON2=0xAA;
      WR=1;
      NOP();
      NOP();
      EEADR++;
   }
   EEDATL = data[1];      // absolutely necessary !! if not the data of the first address will be overwritten
   EEDATH = data[0];      // absolutely necessary !! if not the data of the first address will be overwritten
   LWLO=0;
   EEADR=adr;
   EECON2=0x55;
   EECON2=0xAA;
   WR=1;
   NOP();
   NOP();
   WREN=0;
« Last Edit: February 12, 2015, 08:09:18 08:09 by xmas » Logged
xmas
Inactive

Offline Offline

Posts: 5

Thank You
-Given: 2
-Receive: 7


« Reply #5 on: February 12, 2015, 03:47:23 15:47 »

Hi Manuel, we speak about programming the PROGRAMM flash memory not the data memory
Logged
jamesbond
Newbie
*
Offline Offline

Posts: 23

Thank You
-Given: 50
-Receive: 9


« Reply #6 on: February 12, 2015, 10:08:36 22:08 »

I have tried a couple of things and this seems to work for 8 bits of data.
The thing i do not understand is when i do a erase row it erase 32 bytes of flash?
Maybe the datasheet is not clear about this.
I  only make the changes in the FlashWriteRow function.
I hope this helps you.
Logged
xmas
Inactive

Offline Offline

Posts: 5

Thank You
-Given: 2
-Receive: 7


« Reply #7 on: February 13, 2015, 06:13:58 06:13 »

Yes you erase 32 bytes of flash
for PIC 16F1827:
erase block size is 32
write block size is 8
Logged
flo0319
Junior Member
**
Offline Offline

Posts: 81

Thank You
-Given: 7
-Receive: 17


« Reply #8 on: February 14, 2015, 10:15:54 22:15 »

 Hi xmas, you are right with what is written in pic16f1826/1827 datasheet for PROGRAMMING ROW SIZE AND LATCHES(copy-paste bug typically for microchip ) the "correct" can be found here: http://ww1.microchip.com/downloads/en/DeviceDoc/41390D.pdf page 20

 But in datasheet, above the wrong table with row size (page 105), is written this : 
Quote
a device with a row size of 32 words and eight write
latches will need to load the write latches with data and
initiate a programming operation four times.
Logged
Manuel
Senior Member
****
Offline Offline

Posts: 314

Thank You
-Given: 644
-Receive: 177


« Reply #9 on: February 16, 2015, 01:04:59 13:04 »

In the past I had some problems using INTOSC as main CLOCK and FLASH write suddenly at STARTUP.

I personally suggest to introduce a checking on OSCSTAT register depending on the actived oscillator to be sure that used OSCILLATOR is ready and stable:

bit 4 HFIOFR: High Frequency Internal Oscillator Ready bit
         1 = HFINTOSC is ready
         0 = HFINTOSC is not ready
bit 3 HFIOFL: High Frequency Internal Oscillator Locked bit
         1 = HFINTOSC is at least 2% accurate
         0 = HFINTOSC is not 2% accurate
bit 2 MFIOFR: Medium Frequency Internal Oscillator Ready bit
        1 = MFINTOSC is ready
        0 = MFINTOSC is not ready
bit 1 LFIOFR: Low Frequency Internal Oscillator Ready bit
        1 = LFINTOSC is ready
        0 = LFINTOSC is not ready
bit 0 HFIOFS: High Frequency Internal Oscillator Stable bit
       1 = HFINTOSC is at least 0.5% accurate
       0 = HFINTOSC is not 0.5% accurate


adding:

void main(void)
{
  ANSELA = 0;
  ANSELB = 0;

  APFCON0 = 0b10000100;
 
  OSCTUNE = 0;
  OSCCON = 0b01101000;  // PLL/1101/SCS = 4Mhz
  OPTION_REG = 0b11010000;

  for(;!(OSCSTAT&0x10); ); // for HFINTOSC
...

hope can help someone.
manuel.
« Last Edit: February 16, 2015, 01:22:16 13:22 by Manuel » Logged

-> An Apple a Day does not Let U become a Macintosh!
ChumKiuFoshan
Inactive

Offline Offline

Posts: 2

Thank You
-Given: 4
-Receive: 1


« Reply #10 on: July 28, 2019, 12:05:45 00:05 »

You cannot erase one or to bytes in flash.When you modify you have to erase the entire block.
First read a flash block into a array of 32 bytes.
Make your changes in that array.
Erase the flash block you last read and then rewrite the flash with the modified array.

I use these functions to write and read from flash.I found these functions on internet.

I know that this thread is rather old, but I think that for this question an observation should be made.
Using of one or more flash memory sector is widely used in microcontroller without EEPROM on board to store information that can be changed and should be available between power on. In order to emualate an EEPROM usually we can define an array of proper size, then the first step is to copy inside the array all the needed flash data then perform the data changing inside the array, perform the flash erase and then write back the data array to the flash.
This is the common usage but it deserve a real danger that it is good to know. Imagine what could happen if before writing the flash, after it has been erased, for some reason the micro-controller should reset itself, for example due to a power failure. All the data previously stored inside the flash sector will be definitely lost. This is the real pitfall of this method. So before approaching this way pay more attention to this fact. This means that the way can be used but some countermeasures should be taken, as simple example one may plan to reserve a location of the flash that shoud have always a know value different from 0, if for some reason this location are equal to 0 at the microcontroller startup you can run a disaster recovery procedure that in its simplest form may be to copy the default values from another flash sector to the flash sector used to emulate the EEPROM behavior. This is a very simple approach to gain some security against the drawback above described.
Just my 2 Cents.
Bye
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC