Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 09, 2016, 10:36:18 10:36


Login with username, password and session length


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

Offline Offline

Posts: 5

Thank You
-Given: 2
-Receive: 6


« on: February 10, 2015, 04:53:10 16: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, 05:15:10 17:15 by xmas » Logged
arslanweb
Newbie
*
Offline Offline

Posts: 12

Thank You
-Given: 1
-Receive: 3


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

Hi,

Use in MPLABX the code configrator. It creats for you, this codes.
Logged
Gallymimu
Hero Member
*****
Offline Offline

Posts: 579

Thank You
-Given: 101
-Receive: 151


« Reply #2 on: February 12, 2015, 05:42:36 05: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: 22

Thank You
-Given: 39
-Receive: 8


« Reply #3 on: February 12, 2015, 08:13:11 08: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: 6


« Reply #4 on: February 12, 2015, 09:04:39 09: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, 09:09:18 09:09 by xmas » Logged
xmas
Inactive

Offline Offline

Posts: 5

Thank You
-Given: 2
-Receive: 6


« Reply #5 on: February 12, 2015, 04:47:23 16:47 »

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

Posts: 22

Thank You
-Given: 39
-Receive: 8


« Reply #6 on: February 12, 2015, 11:08:36 23: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: 6


« Reply #7 on: February 13, 2015, 07:13:58 07: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: 82

Thank You
-Given: 7
-Receive: 16


« Reply #8 on: February 14, 2015, 11:15:54 23: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
Junior Member
**
Offline Offline

Posts: 94

Thank You
-Given: 81
-Receive: 62


« Reply #9 on: February 16, 2015, 02:04:59 14: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, 02:22:16 14:22 by Manuel » Logged

-> An Apple a Day does not Let U become a Macintosh!
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