View previous topic :: View next topic |
Author |
Message |
Dagaca
Joined: 05 Jul 2017 Posts: 4
|
NVM Write EEprom New PICs |
Posted: Thu Aug 10, 2017 5:19 pm |
|
|
Hello.
The new PICs are a real headache.
I'm using pic 16f18325 and compiler version 5.070.
There is no way I can write in eeprom. I'm using the following function, (getenv doesn't work, this is other question)
Code: |
#byte NVMCON1 = 0x895
#byte NVMCON2 = 0x896
#byte NVMADRH = 0x892
#byte NVMADRL = 0x891
#byte NVMDATH = 0x894
#byte NVMDATL = 0x893
#bit NVRD=NVMCON1.0
#bit NVWR=NVMCON1.1
#bit WREN=NVMCON1.2
#bit FREE=NVMCON1.4
#bit LWLO=NVMCON1.5
#bit NVMREGS=NVMCON1.6
void write_NVM (int8 addr, int8 data)
{
NVMREGS = 1;
NVMDATL = data; //dato
NVMDATH = 0;
NVMADRL = addr; //direccion
NVMADRH = 0x70;
//unlock sequence
disable_interrupts(GLOBAL);
WREN = 1; //enable writes eeprom
NVMCON2 = 0x55;
delay_cycles(1);
NVMCON2 = 0xAA;
delay_cycles(1);
NVWR = 1;
//end unlock operation
while (NVWR){} //espera a que termine de escribir
WREN = 0;
NVMREGS = 0;
enable_interrupts(GLOBAL);
}
|
If someone can provide some solution I will be very grateful!!!!!! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Aug 10, 2017 6:13 pm |
|
|
Here's the code produced by vs. 5.074. It looks like they fixed whatever
problem you had. You didn't post the .LST file from your version.
Code: | .................... write_eeprom(addr, data);
0013: MOVF INTCON,W
0014: MOVWF @77
0015: BCF INTCON.GIE
0016: MOVLB 00
0017: MOVF addr,W
0018: MOVLB 11
0019: MOVWF NVMADRL
001A: MOVLW 70
001B: MOVWF NVMADRH
001C: MOVLB 00
001D: MOVF data,W
001E: MOVLB 11
001F: MOVWF NVMDATL
0020: BSF NVMCON1.NVMREGS
0021: BSF NVMCON1.WREN
0022: MOVLW 55
0023: MOVWF NVMCON2
0024: MOVLW AA
0025: MOVWF NVMCON2
0026: BSF NVMCON1.WR
0027: BTFSC NVMCON1.WR
0028: GOTO 027
0029: BCF NVMCON1.WREN
002A: BCF NVMCON1.NVMREGS
002B: MOVF @77,W
002C: IORWF INTCON,F
....................
|
Test program:
Code: |
#include <16F18325.h>
#fuses NOWDT
#use delay(internal=4M)
//==============================
void main()
{
int8 addr;
int8 data;
write_eeprom(addr, data);
while(TRUE); |
|
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Thu Aug 10, 2017 7:27 pm |
|
|
I need to learn how to do this as well.
I've needed it desperately for ages...
How do i know what addresses are free after i load a gigantic program?
Does the posted code work?
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Aug 10, 2017 9:38 pm |
|
|
Gabriel wrote: | I need to learn how to do this as well.
I've needed it desperately for ages...
|
I'm not sure what you want to do.
Do you want to:
1. Write to NVM in the newer PICs ?
-or-
2. Write your own routines to replace non-working CCS routines ?
-or-
3. Something else ?
Gabriel wrote: | How do i know what addresses are free after i load a gigantic program? |
This seems like a separate topic than your first question.
If you're talking about finding unused Flash Memory, here's one way:
First, compile your program with no errors.
Then if you're using MPLAB vs. 8.92, go to the View / Program Memory window.
Then you can scroll through the entire Flash memory. For example with
a 16F PIC, every flash address that is equal to 3FFF is free. |
|
|
Dagaca
Joined: 05 Jul 2017 Posts: 4
|
|
Posted: Fri Aug 11, 2017 2:13 am |
|
|
Thanks PCM Programmer.
I just downloaded the demo version 5.074, now it works correctly.
Very good service, thank you very much!!! |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Fri Aug 11, 2017 6:53 am |
|
|
I'd like to store values on the NVM as with EEPROM on older PICs.
This knowledge has always eluded me.
So from the little info on this thread:
CCS functions don't work in some versions.
I would need to compile first.
Check which addresses are free.
Code the available addresses to my code.
Recompile.
Success, this could have been avoided with eeprom? _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Aug 11, 2017 8:16 am |
|
|
Quote: |
CCS functions don't work in some versions.
I would need to compile first.
Check which addresses are free.
Code the available addresses to my code.
Recompile. |
That's not the way to replace a defective CCS function. You don't go in
at the machine code level and patch addresses.
You replace the whole function with a new, working one. Then give the
function a unique name, which is different from the normal CCS function,
so they don't conflict. If you give your function the same name as the
CCS function, the compiler will give you an error.
Example:
In the first post in this thread, Dagaca has written his own function
to replace write_eeprom(), and called it write_NVM().
There is an alternate way to replace a CCS function, using a #define
statement (called a 'macro'), but it's more complex. Using the method
shown in post #1 is OK. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Aug 11, 2017 5:32 pm |
|
|
Gabriel,
I think I finally figured out what you really want. You want to use flash
memory as a substitute for eeprom, in older PICS that don't have eeprom.
CCS added a driver file to support this, probably around vs. 5.060.
Here is their news release page on it:
http://www.ccsinfo.com/newsdesk_info.php?newsdesk_id=192
The filename of the driver is: virtual_eeprom.c
Here is the file description:
Code: |
Virtual EEPROM Driver
This driver uses two flash erase pages in program memory as virtual
eeprom memory. This driver uses write-balancing to avoid excessive
erasing of program memory. This driver is optimized to use lower
eeprom addresses first; however, it is not a requirement to use the
available addresses in any particular order. The functions
which drive operation of the driver are detailed below.
|
The file was added after your compiler versions were released.
You could email CCS support and ask them to send it to you. |
|
|
|