Flash Program Memory Write Problem

Discussion in 'Embedded Systems and Microcontrollers' started by jpanhalt, Nov 13, 2014.

  1. jpanhalt

    Thread Starter Expert

    Jan 18, 2008
    This continues from my other thread. Got program memory read and erase working, but can't get it to write in simulation. Using MPLAB SIM. Processor = PIC16F1519 Language=MPASM

    PMCON1 is mostly a read/write register that controls read, write, and erase functions on program memory. Most notably, PMCON1,WR is the write bit (bit<1>), which can only be set and not cleared by user. Clearing is by the MCU after writing.

    Code (ASM):
    2. Save
    3.      BCF       INTCON,GIE ; Disable ints so required sequences will execute properly
    4.      BANKSEL   PMADRH ; Bank 3
    5.      movlw     0x00          ;0x3F
    6.      MOVWF     PMADRH ;
    7.      movlw     0xE0               ;0x80
    8.      MOVWF     PMADRL ;
    10.      BCF       PMCON1,CFGS ; Not configuration space PMCON1=80 (after step)
    11.      BSF       PMCON1,WREN ; Enable writes PMCON1=84 (after step)
    12.      BSF       PMCON1,LWLO ; Only Load Write Latches PMCON1=A4 (after step)
    15.      movf      TzL,w
    16.      MOVWF     PMDATL ;
    17.      movf      TzH,w
    18.      MOVWF     PMDATH ;
    19.      MOVLW     55h ; Start of required write sequence:
    20.      MOVWF     PMCON2 ; Write 55h
    21.      MOVLW     0AAh ;
    22.      MOVWF     PMCON2 ; Write AAh
    23. *  BSF       PMCON1,WR ; Set WR bit to begin write PMCON1=A4(after step, no change)
    24.      NOP       ; NOP instructions are forced as processor
    25.                ; loads program memory write latches
    26.      NOP       ;
    28. PM_WRITE
    29.      BCF       PMCON1,LWLO ; No more loading latches - start Flash program
    30.                            ; memory write PCON1=84 (after step)
    31.      MOVLW     55h ; Start of required write sequence:
    32.      MOVWF     PMCON2 ; Write 55h
    33.      MOVLW     0AAh ;
    34.      MOVWF     PMCON2 ; Write AAh
    35. *  BSF       PMCON1,WR ; Set WR bit to begin write PMCON1=84 (after step, no change)
    36.      NOP       ; NOP instructions are forced as processor writes
    37.                ; all the program memory write latches simultaneously
    38.      NOP       ; to program memory.
    39.                ; After NOPs, the processor
    40.                ; stalls until the self-write process in complete
    41.                ; after write processor continues with 3rd instruction
    42.      BCF       PMCON1,WREN ; Disable writes
    43. ;     BSF       INTCON,GIE ; Enable interrupts
    44.      Return
    In a watch window, I traced PMCON1 and added comments to the code. That register changes as expected with one exception: Setting WR does not change the value (added asterisk for accent). With WR, PMCON1 should be 0x86 or 0xA6 as appropriate.

    1) Does anyone know whether MPLAB SIM properly simulate this code for writing to program memory?
    2) Is there a problem with the code?

    BTW, This code really does nothing. It is just a sandbox to test writing and reading program memory. The code was directly copied from the Microchip datasheet example, except indirect addressing was removed since only one byte is being tested. Config bits are properly set, I think:


    I think I got it to work with MPLAB ICD3. However, the PMCON1 still doesn't change in the watch window when bit<1> is set. That concerns me. Had to add a couple of lines to Example 11-3. Now to test with MPLAB SIM.

    Unfortunately, my joy was short lived. I was looking at a register being populated with the data byte. When I do an actual read on the chip and look at program memory, I can see where it was erased and read, but no data is loaded there. So, the problem seems to still be present.


    The variable is in program memory 00E0h , and its value 0064h. The biggest secret was probably getting a short nap.
    Last edited: Nov 13, 2014
  2. jpanhalt

    Thread Starter Expert

    Jan 18, 2008
    I feel like just an old man talking to himself. My only excuse is I am old and live in the middle of the woods.

    I finally got all parts of the program playing nice with each other. In brief, I have a Memsic MXD2020E accelerometer (±1 g) that is run remotely and sends its data as raw counts via serial link to a 16F1519. In reality, almost everything was working more than a year ago, except I take a break during good weather to deal with my hobby farm. A couple of glitches had crept in that took forever to find. In the end, they all turned out to be "hiding in plain sight," e.g., a misplaced clrf. Then I decided to use flash program memory to save a zero calibration that corrects for temperature changes and small manufacturing variations.

    In my cold work room, the "zero error" is about 0.6°. Here is a screen shot of the program memory after re-zeroing. This is from actual running, not simulation.


    3FE0 is the beginning of the last line of flash memory and is called HEF by Microchip. 0x12 = d.16, and ±16 counts about zero (nominally 5000 counts) is about 0.6°. (Sensitivity decreases greatly at tilts of more than 15° or so.) Memory was retained after cycling the power supply. I was concerned about the limited number of R/W cycles that HEF memory allows, so it is only read on power up. It is only written when the operator chooses to re-set it.

    killivolt likes this.
  3. jpanhalt

    Thread Starter Expert

    Jan 18, 2008
    UPDATE #2

    The project is effectively done. All I need to do is package the accelerometer sensor and display in separate boxes with batteries. The tilt sensor is a Memsic 2020E/L (±1 g FS), display is a New Haven Displays 64x128, MCU's are Pic12F683 (sensor) and 16F1519 (display). Wireless communication between sensor and display is by a pair of ZigBee modules. The 12F683 used software USART; the 16F1519 is hardware EUSART. Zero setting is stored in flash ram and does not have to be reset after power off. Language = MPASM.

    The sine table and dimension blocks are reasonably accurate, but not lab standards. Two tilts are shown, 0° and 2°. I flip the sensor to show the symmetrical response. There is a slight offset from level, which I did not completely remove. One error I couldn't eliminate is the floor/table tilt that was sensed when I moved. The thing can detect tilt from the thickness of a sheet of paper in a few inches.

    This is also my first attempt ever at posting a video. I reduced it to 854X720. Hope it is viewable. There is no intended audio. Had to upload as a zipped file. Original is MP4.

    Edit: The "Tips" column represents the up or down deflection of a set of 42" pallet forks on the front-end loader of a tractor. The whole purpose of this project is to facilitate leveling the pallet fork tips, which you cannot be seen from the operator's station.

    Last edited: Dec 14, 2014
  4. killivolt

    Distinguished Member

    Jan 10, 2010
  5. jpanhalt

    Thread Starter Expert

    Jan 18, 2008
    Home automation is my next project. Since my current home is a log home and ALL of the interior walls are T&G cedar, it is extremely hard to pull new wires for conventional wiring. That is, you can't just knock a hole in the drywall and repair it. There is no drywall. I want to install intelligent fixtures in a few places that will only need power and will be turned on or off by a serial signal from the wall switch transmitted over the power line. I know there is a commercial unit for that purpose, but I am not sure it is still on the market. Plus, it is kind of expensive.

    Thanks for looking at the video. This project has evolved over several years, although my efforts have been limited to only a few months per year, and a good part of that time has been re-learning.

  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    Sorry I can't view the video. Congratulations on tour project.

    The older protocol was called X 10. The newer variants are all going to modern protocols such as wifi so your smart phone can be the controller.
  7. jpanhalt

    Thread Starter Expert

    Jan 18, 2008
    I don't have a smart phone. My pre-paid cell stays in the car and is used only for emergencies. I don't even know its number. I have never used it while driving. So, even at my age, I may live longer than those who do. :D

  8. killivolt

    Distinguished Member

    Jan 10, 2010
    Thats what I do for the Uni. We build smart rooms but; it's with Crestron Control Software using RS-232 / 422 for device control. They are moving to network control slowly. They can work with wifi products and yes they can be expensive. We aren't stuck using their devices which are usually priced above some manufactures; but those manufactures do supply the API protocols for their equipment. Enhancing an installation for the General Public.
  9. killivolt

    Distinguished Member

    Jan 10, 2010
    You don't need a smart phone you just need a tablet; and a home network to set it up. I don't know whats available outside of Crestron but; I'm sure it's available. Cheaper than a touch panel.
  10. killivolt

    Distinguished Member

    Jan 10, 2010
    This is what I plan to use; I have to surplus some out of my Stock at the Uni. I think I should be able to get them around $30; I can also daisy chain them if I need more ports.


    • 2-Series engine | Dual-bus architecture, Integrated AV Switcher/Processor
    • Cresnet port - master/slave selectable, 10/100 Ethernet | SSL encryption
    • e-Control 2 and RoomView enabled | SNMP support, 2 Com ports (RS-232/422/485)
    • 4 IR/serial ports, 4 Versiport I/O ports, 4 Low-voltage relay ports
    • IR wireless compatible using CNXRMIRD, Complete setup in minutes using MP2 Wizard Software
    Roomview is a network software that enables network monitoring. Updates, Management, and Alerts e-mail txt etc.

    Edit: I just need to build an AP for my iPad. Then I'm good to go; it's just connected to my home network.
    jpanhalt likes this.
  11. jbjoe30

    New Member

    Jul 7, 2015
    jpanhalt, in your updated posts, you mentioned that you have the code working. The original assembly has some of the lines commented out. I have a project using a PIC16F1614 and I can read, and erase, but not write. Can you share your functional s/w to assist please?