# Flash Program Memory Write Problem

#### jpanhalt

Joined Jan 18, 2008
11,088
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:
Save
BCF       INTCON,GIE ; Disable ints so required sequences will execute properly
movlw     0x00          ;0x3F
movlw     0xE0               ;0x80

BCF       PMCON1,CFGS ; Not configuration space PMCON1=80 (after step)
BSF       PMCON1,WREN ; Enable writes PMCON1=84 (after step)
BSF       PMCON1,LWLO ; Only Load Write Latches PMCON1=A4 (after step)

LATCH_WRITE
movf      TzL,w
MOVWF     PMDATL ;
movf      TzH,w
MOVWF     PMDATH ;
MOVLW     55h ; Start of required write sequence:
MOVWF     PMCON2 ; Write 55h
MOVLW     0AAh ;
MOVWF     PMCON2 ; Write AAh
*  BSF       PMCON1,WR ; Set WR bit to begin write PMCON1=A4(after step, no change)
NOP       ; NOP instructions are forced as processor
; loads program memory write latches
NOP       ;

PM_WRITE
; memory write PCON1=84 (after step)
MOVLW     55h ; Start of required write sequence:
MOVWF     PMCON2 ; Write 55h
MOVLW     0AAh ;
MOVWF     PMCON2 ; Write AAh
*  BSF       PMCON1,WR ; Set WR bit to begin write PMCON1=84 (after step, no change)
NOP       ; NOP instructions are forced as processor writes
; all the program memory write latches simultaneously
NOP       ; to program memory.
; After NOPs, the processor
; stalls until the self-write process in complete
; after write processor continues with 3rd instruction
BCF       PMCON1,WREN ; Disable writes
;     BSF       INTCON,GIE ; Enable interrupts
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:

John

EDIT UPDATE #1:
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.

UPDATE#2:
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.

UPDATE#3:
GOT IT TO WORK:

The variable is in program memory 00E0h , and its value 0064h. The biggest secret was probably getting a short nap.

Last edited:

#### jpanhalt

Joined Jan 18, 2008
11,088
UPDATE
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.

John

#### jpanhalt

Joined Jan 18, 2008
11,088
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.

John

#### Attachments

• 1.2 MB Views: 34
Last edited:

#### jpanhalt

Joined Jan 18, 2008
11,088
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.

John

#### ErnieM

Joined Apr 24, 2011
8,196
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.

#### jpanhalt

Joined Jan 18, 2008
11,088
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.

John

#### killivolt

Joined Jan 10, 2010
805
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.
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.

#### killivolt

Joined Jan 10, 2010
805
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.

John
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.

#### killivolt

Joined Jan 10, 2010
805
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.

http://www.amazon.com/Crestron-Integrated-Control-System-Ethernet/dp/B00F0OCXSS

• 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.

#### jbjoe30

Joined Jul 7, 2015
1
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?

Thanks,
Joe