Writing & Reading 2 Bytes to/from Flash


Closed Thread
Results 1 to 12 of 12
  1. #1
    Join Date
    Jul 2013
    Posts
    41

    Question Writing & Reading 2 Bytes to/from Flash

    Hello,

    I want to store and recall 2 Bytes from Flash on a PIC18F27J13 for pairing up a remote control to a mobile unit.
    These are RadioID(RF Address) and RadioCH (Channel#) which are both just 8 bit VARs each.
    I think I have the basic idea as below but I cannot figure out how to configure Flash programming.
    The (2) memory locations ($1800 and $1900) I picked as it looks like my main code is way below.

    Although I think this wastes a whole 64Byte block per each, I do not care at the moment.
    I did try setting the above VARs to Words instead of Bytes but that did not help.
    Reading through various posts I think I need to define an area of memory for these (2) bytes and un-protect them from being written to. However, I have be unable to find any simple examples and am stumped.

    Thanks,


    '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''
    '' Flash Experiments


    Flash:


    If Push = 0 Then ' Should Increment ID & CH by 1
    Pause 10
    If Push = 1 Then ' Noise
    Return
    EndIf
    Do
    Pause 10
    Loop Until Push = 1 ' Wait for User Release
    GLED = 1
    EraseCode $1800 ' Whole Block of 4 Words $1800 to $1864?
    EraseCode $1900
    RadioID = RadioID + 1
    RadioCH = RadioCH + 1
    WriteCode $1803, RadioID ' Writes to 4th Word in Block
    WriteCode $1903, RadioCH
    ReadCode $1803, RadioID ' Reads from 4th Word in Block
    ReadCode $1903, RadioCH
    hserout2["RadioID: ", dec RadioID, 13,10]
    hserout2["RadioCH: ", dec RadioCH, 13,10]
    GLED = 0
    EndIf


    GoTo Flash


    '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: Writing & Reading 2 Bytes to/from Flash

    Melabs manual says

    Flash program writes must be enabled in the configuration for the PICmicro MCU at device programming time for ERASECODE to be able to erase.

    Have you done this when programming the pic?

  3. #3
    Join Date
    Jul 2013
    Posts
    41


    Did you find this post helpful? Yes | No

    Default Re: Writing & Reading 2 Bytes to/from Flash

    I have looked high and low but cannot see where this is set.
    What exactly is this called and where can I find it?

    (I have the U2 Programmer with version 4.51.)
    Last edited by Zebryk; - 17th May 2015 at 00:17. Reason: Additional Info

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    2,388


    Did you find this post helpful? Yes | No

    Default Re: Writing & Reading 2 Bytes to/from Flash

    its in the hardware configuration registers (fuse settings)

    WPFP = PAGE_0
    WPCFG = OFF
    WPDIS = OFF
    WPEND = PAGE_WPFP
    shown here with default pbp values (no code protection )
    it does help if you post you whole code (in code tags) not just snippets, and include what version of pbp used

  5. #5
    Join Date
    Jul 2013
    Posts
    41


    Did you find this post helpful? Yes | No

    Default Re: Writing & Reading 2 Bytes to/from Flash

    Richard,

    I tried your suggestion but it did not work.
    As my whole code is >73,000 characters it will not post.
    I am using PBPX 3.0.7.4
    The PIC is a PIC18F27J13.

    Is my syntax / technique correct?

    Thanks,

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    2,388


    Did you find this post helpful? Yes | No

    Default Re: Writing & Reading 2 Bytes to/from Flash

    what syntax ? at no stage have you posted the relevant config fuse settings you have employed.

    why not make a small pgm to test the idea

  7. #7
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Writing & Reading 2 Bytes to/from Flash

    Richard is asking about config settings where watchdog timer, crystal mode, write protect, etc are all set, and what you are doing there.
    The program looks right to me tho I have not used it. Except the output doesn’t absolutely verify what was written, only what read back.

    Code:
    verifya var type ' byte or word
    verifyb var type '
    verified var bit '
    
    EraseCode $1800 ' Whole Block of 4 Words $1800 to $1864?
    EraseCode $1900
    RadioID = RadioID + 1
    RadioCH = RadioCH + 1
    WriteCode $1803, RadioID ' Writes to 4th Word in Block
    WriteCode $1903, RadioCH
    
    ReadCode $1803, verifya ' Reads from 4th Word in Block
    ReadCode $1903, verifyb
    
    verify = 1
    if verifya != RadioID then verified = 0
    if verifyb != RadioCh then verified = 0
    
    if verify = 1 then
    hserout2["RadioID: ", dec RadioID, 13,10]
    hserout2["RadioCH: ", dec RadioCH, 13,10]
    else
    hserout2[“It’s broken :(",13,10]
    endif

  8. #8
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,803


    Did you find this post helpful? Yes | No

    Default Re: Writing & Reading 2 Bytes to/from Flash

    Each PIC controller has different page size that you can WRITE to.

    For example if your chip has 64 words page, then you have to read all 64 words, change what you want, then write back the whole 64 words page.

    It is a bit clumsy but that is how it works. You cannot change only the words you really want. And if you write less tat the page size, then data will not be written!

    So, my suggestion is to make an array, get the page form Flash, make the changes, then store back to Flash the array.

    Ioannis

  9. #9
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: Writing & Reading 2 Bytes to/from Flash

    Or, if reading/writing to a whole page of flash program memory does not appeal to you.
    You can always switch to a PIC model that has internally EEPROM or add an external Serial EEPROM.

    Regards,
    TABSoft

  10. #10
    Join Date
    Jul 2013
    Posts
    41


    Did you find this post helpful? Yes | No

    Default Re: Writing & Reading 2 Bytes to/from Flash

    Ioannis,

    This is getting more complicated than I figured.
    Might you be available to implement this for a fee?
    I can send you money in advance via PayPal.

    Thanks,

    Jay Zebryk
    Zebryk Engineering
    Southbridge, Massachusetts

  11. #11
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: Writing & Reading 2 Bytes to/from Flash

    I would also suggest adding an external I2C serial eeprom of small size and using it to store your data. Something as small as an 24LC01 1K device.
    Dave Purola,
    N8NTA
    EN82fn

  12. #12
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,803


    Did you find this post helpful? Yes | No

    Default Re: Writing & Reading 2 Bytes to/from Flash

    Zebryk,

    1. how many times will your write new values to flash? Flash has less durabilit than EEPROM.

    2. how many words will you store to flash? Think ahead in the future.

    3. Are you sure that the address you have chosen will be free?

    Ioannis

    P.S. No fee necessary, we are here to help each other! Look at your P.M. also.

Similar Threads

  1. Reading, writing, erasing flash (16F88)
    By RussMartin in forum mel PIC BASIC Pro
    Replies: 26
    Last Post: - 8th March 2010, 08:13
  2. writing 256 bytes to eeprom
    By astouffer in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 22nd December 2008, 14:44
  3. Writing & Reading to iButton EEPROM
    By crhomberg in forum Code Examples
    Replies: 2
    Last Post: - 6th October 2008, 19:40
  4. Help reading/writing DS1996
    By Peder in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 12th October 2007, 12:12
  5. Writing and reading to a 24LC1025
    By Angus Anderson in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 20th April 2007, 11:49

Members who have read this thread : 1

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts