• AVR Freaks

Hot!PIC16F18326 write & read to EEPROM not working, using MCC generated code

Page: < 123 > Showing page 2 of 3
Author
ric
Super Member
  • Total Posts : 24605
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: PIC16F18326 write & read to EEPROM not working, using MCC generated code 2019/08/08 19:20:49 (permalink)
0
I assume you're using MPLABX. Which version of XC8 are you using?
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#21
ric
Super Member
  • Total Posts : 24605
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: PIC16F18326 write & read to EEPROM not working, using MCC generated code 2019/08/08 19:32:17 (permalink)
0
Search in your project folders for a file with a ".lst" extension.
It's probably in .\dist\default\production\

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#22
teranz
Starting Member
  • Total Posts : 80
  • Reward points : 0
  • Joined: 2019/03/06 21:45:25
  • Location: 0
  • Status: offline
Re: PIC16F18326 write & read to EEPROM not working, using MCC generated code 2019/08/08 19:52:41 (permalink)
0
I am using XC8 2.05... I have found the .lst file, and I have found the code for the DATAEE_WriteByte() function.  I have not looked at the ASM code in a long time, I will need to study it.
#23
teranz
Starting Member
  • Total Posts : 80
  • Reward points : 0
  • Joined: 2019/03/06 21:45:25
  • Location: 0
  • Status: offline
Re: PIC16F18326 write & read to EEPROM not working, using MCC generated code 2019/08/08 20:05:46 (permalink)
0
I see now that the functions eeprom_write() and eeprom_read() are part of the XC8 peripheral library for devices with EEPROM.  These appear to work well.  I will try as time allows to figure why the MMC code does not work.
#24
ric
Super Member
  • Total Posts : 24605
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: PIC16F18326 write & read to EEPROM not working, using MCC generated code 2019/08/08 20:15:25 (permalink)
+1 (1)
Most likely it's a problem with the magic number sequence.
You never answered the "what optimisation level are you using" question.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#25
ric
Super Member
  • Total Posts : 24605
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: PIC16F18326 write & read to EEPROM not working, using MCC generated code 2019/08/08 20:18:41 (permalink)
0
Have you found the "__eeprom" qualifier for creating variables yet?
That is an even easier way to save information in PIC16F chips when using XC8.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#26
1and0
Access is Denied
  • Total Posts : 9994
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: PIC16F18326 write & read to EEPROM not working, using MCC generated code 2019/08/08 20:25:43 (permalink)
0
teranz
I am using XC8 2.05... I have found the .lst file, and I have found the code for the DATAEE_WriteByte() function.  I have not looked at the ASM code in a long time, I will need to study it.

Why don't you post the disassembly here?
 
#27
1and0
Access is Denied
  • Total Posts : 9994
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: PIC16F18326 write & read to EEPROM not working, using MCC generated code 2019/08/08 20:26:55 (permalink)
0
teranz
These write seem to work....  anyone know why?  Note the address is just the LSB.
 
eeprom_write(0x00, 0x12);
eeprom_write(0x01, 0x34);
eeprom_write(0x02, 0x56);
eeprom_write(0x03, 0x78);

Because the eeprom_write() function sets the upper address byte to 0x70.
#28
teranz
Starting Member
  • Total Posts : 80
  • Reward points : 0
  • Joined: 2019/03/06 21:45:25
  • Location: 0
  • Status: offline
Re: PIC16F18326 write & read to EEPROM not working, using MCC generated code 2019/08/08 20:37:56 (permalink)
0
ric
Have you found the "__eeprom" qualifier for creating variables yet?
That is an even easier way to save information in PIC16F chips when using XC8.

I have not needed to look at using eeprom functionality in the past so all this is a bit new to me.  The xc8 optimization level is set at 0.  I will definitely look at the __eeprom qualifier, another new item for me.
 
Below is the assembly code from the .lst file for the DATAEE_WriteByte()

20142 ;; This function uses a non-reentrant model
20143 ;;
20144
20145
20146 ;psect for function _DATAEE_WriteByte
20147 07D6 _DATAEE_WriteByte:
20148
20149 ;mcc_generated_files/memory.c: 180: void DATAEE_WriteByte(uint16_t bAdd, uint8_t bData);
+ mcc_generated_files/memory.c: 181: {;mcc_generated_files/memory.c: 182: uint8_t GIEB
+ itValue = INTCONbits.GIE;
20150
20151 ;incstack = 0
20152 ; Regs used in _DATAEE_WriteByte: [wreg+status,2+status,0]
20153 07D6 3000 movlw 0
20154 07D7 1B8B btfsc 11,7 ;volatile
20155 07D8 3001 movlw 1
20156 07D9 0022 movlb 2 ; force select bank2
20157 07DA 00A3 movwf DATAEE_WriteByte@GIEBitValue^(0+256)
20158
20159 ;mcc_generated_files/memory.c: 184: NVMADRH = ((bAdd >> 8) & 0xFF);
20160 07DB 0022 movlb 2 ; force select bank2
20161 07DC 0821 movf (DATAEE_WriteByte@bAdd+1)^(0+256),w
20162 07DD 0031 movlb 17 ; force select bank17
20163 07DE 0092 movwf 18 ;volatile
20164
20165 ;mcc_generated_files/memory.c: 185: NVMADRL = (bAdd & 0xFF);
20166 07DF 0022 movlb 2 ; force select bank2
20167 07E0 0820 movf DATAEE_WriteByte@bAdd^(0+256),w
20168 07E1 0031 movlb 17 ; force select bank17
20169 07E2 0091 movwf 17 ;volatile
20170
20171 ;mcc_generated_files/memory.c: 186: NVMDATL = bData;
20172 07E3 0022 movlb 2 ; force select bank2
20173 07E4 0822 movf DATAEE_WriteByte@bData^(0+256),w
20174 07E5 0031 movlb 17 ; force select bank17
20175 07E6 0093 movwf 19 ;volatile
20176
20177 ;mcc_generated_files/memory.c: 187: NVMCON1bits.NVMREGS = 1;
20178 07E7 0031 movlb 17 ; force select bank17
20179 07E8 1715 bsf 21,6 ;volsfr
20180
20181 ;mcc_generated_files/memory.c: 188: NVMCON1bits.WREN = 1;
20182 07E9 0031 movlb 17 ; force select bank17
20183 07EA 1515 bsf 21,2 ;volsfr
20184
20185 ;mcc_generated_files/memory.c: 189: INTCONbits.GIE = 0;
20186 07EB 138B bcf 11,7 ;volatile
20187
20188 ;mcc_generated_files/memory.c: 190: NVMCON2 = 0x55;
20189 07EC 3055 movlw 85
20190 07ED 0031 movlb 17 ; force select bank17
20191 07EE 0096 movwf 22 ;volsfr
20192
20193 ;mcc_generated_files/memory.c: 191: NVMCON2 = 0xAA;
20194 07EF 30AA movlw 170
20195 07F0 0031 movlb 17 ; force select bank17
20196 07F1 0096 movwf 22 ;volsfr
20197
20198 ;mcc_generated_files/memory.c: 192: NVMCON1bits.WR = 1;
20199 07F2 0031 movlb 17 ; force select bank17
20200 07F3 1495 bsf 21,1 ;volsfr
20201 07F4 l588:
20202 ;mcc_generated_files/memory.c: 196: {;mcc_generated_files/memory.c: 197: }
20203
20204
20205 ;mcc_generated_files/memory.c: 195: while (NVMCON1bits.WR)
20206 07F4 0031 movlb 17 ; force select bank17
20207 07F5 1895 btfsc 21,1 ;volsfr
20208 07F6 2FF4 goto l588
20209
20210 ;mcc_generated_files/memory.c: 199: NVMCON1bits.WREN = 0;
20211 07F7 0031 movlb 17 ; force select bank17
20212 07F8 1115 bcf 21,2 ;volsfr
20213
20214 ;mcc_generated_files/memory.c: 200: INTCONbits.GIE = GIEBitValue;
20215 07F9 0022 movlb 2 ; force select bank2
20216 07FA 0C23 rrf DATAEE_WriteByte@GIEBitValue^(0+256),w
20217 07FB 1C03 skipc
20218 07FC 138B bcf 11,7 ;volatile
20219 07FD 1803 skipnc
20220 07FE 178B bsf 11,7 ;volatile
20221 07FF 0008 return
20222 0800 __end_of_DATAEE_WriteByte:
#29
ric
Super Member
  • Total Posts : 24605
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: PIC16F18326 write & read to EEPROM not working, using MCC generated code 2019/08/08 20:40:14 (permalink)
0
Yep, the code is bad. There's a MOVLB instruction right where it shouldn't be
20188 ;mcc_generated_files/memory.c: 190: NVMCON2 = 0x55;
20189 07EC 3055 movlw 85
20190 07ED 0031 movlb 17 ; force select bank17
20191 07EE 0096 movwf 22 ;volsfr
20192
20193 ;mcc_generated_files/memory.c: 191: NVMCON2 = 0xAA;
20194 07EF 30AA movlw 170
20195 07F0 0031 movlb 17 ; force select bank17
20196 07F1 0096 movwf 22 ;volsfr
20197
20198 ;mcc_generated_files/memory.c: 192: NVMCON1bits.WR = 1;
20199 07F2 0031 movlb 17 ; force select bank17
20200 07F3 1495 bsf 21,1 ;volsfr

Stepping up to the next optimisation level would probably fix it. You can do that even in the free version.
 
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#30
1and0
Access is Denied
  • Total Posts : 9994
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: PIC16F18326 write & read to EEPROM not working, using MCC generated code 2019/08/08 20:43:58 (permalink)
0
ric
Yep, the code is bad. There's a MOVLB instruction right where it shouldn't be

Actually there are 2 MOVLB instructions right where they shouldn't be.
#31
ric
Super Member
  • Total Posts : 24605
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: PIC16F18326 write & read to EEPROM not working, using MCC generated code 2019/08/08 20:46:56 (permalink)
0
Agree, the one before setting the WR bit is also bad.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#32
teranz
Starting Member
  • Total Posts : 80
  • Reward points : 0
  • Joined: 2019/03/06 21:45:25
  • Location: 0
  • Status: offline
Re: PIC16F18326 write & read to EEPROM not working, using MCC generated code 2019/08/08 21:10:07 (permalink)
0
I tried all the xc8 optimization levels, 0, 1, 2 and none produced working code for the DATAEE_WriteByte() function.
#33
ric
Super Member
  • Total Posts : 24605
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: PIC16F18326 write & read to EEPROM not working, using MCC generated code 2019/08/08 21:36:01 (permalink)
0
Are you using C90, or C99 mode?
I tried a small project with both, and playing with all the optimisation levels, and I cannot get XC8 2.05 to include those extra lines, it always does it correctly.
Here's my sample file.

// PIC16F18326 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1
#pragma config FEXTOSC = OFF    // FEXTOSC External Oscillator mode Selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINT32 // Power-up default value for COSC bits (HFINTOSC with 2x PLL (32MHz))
#pragma config CLKOUTEN = OFF   // Clock Out Enable bit (CLKOUT function is disabled; I/O or oscillator function on OSC2)
#pragma config CSWEN = ON       // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)

// CONFIG2
#pragma config MCLRE = ON       // Master Clear Enable bit (MCLR/VPP pin function is MCLR; Weak pull-up enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config WDTE = OFF       // Watchdog Timer Enable bits (WDT disabled; SWDTEN is ignored)
#pragma config LPBOREN = OFF    // Low-power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bits (Brown-out Reset enabled, SBOREN bit ignored)
#pragma config BORV = LOW       // Brown-out Reset Voltage selection bit (Brown-out voltage (Vbor) set to 2.45V)
#pragma config PPS1WAY = ON     // PPSLOCK bit One-Way Set Enable bit (The PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a Reset)
#pragma config DEBUG = OFF      // Debugger enable bit (Background debugger disabled)

// CONFIG3
#pragma config WRT = OFF        // User NVM self-write protection bits (Write protection off)
#pragma config LVP = ON         // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/VPP pin function is MCLR. MCLRE configuration bit is ignored.)

// CONFIG4
#pragma config CP = OFF         // User NVM Program Memory Code Protection bit (User NVM code protection disabled)
#pragma config CPD = OFF        // Data NVM Memory Code Protection bit (Data NVM code protection disabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#include <stdint.h>

void DATAEE_WriteByte(uint16_t bAdd, uint8_t bData)
{
uint8_t GIEBitValue = INTCONbits.GIE;
NVMADRH = ((bAdd >> 8) & 0xFF);
NVMADRL = (bAdd & 0xFF);
NVMDATL = bData;
NVMCON1bits.NVMREGS = 1;
NVMCON1bits.WREN = 1;
INTCONbits.GIE = 0; // Disable interrupts
NVMCON2 = 0x55;
NVMCON2 = 0xAA;
NVMCON1bits.WR = 1;
// Wait for write to complete
while (NVMCON1bits.WR)
{
}
NVMCON1bits.WREN = 0;
INTCONbits.GIE = GIEBitValue; // restore interrupt enable
}
 
uint8_t DATAEE_ReadByte(uint16_t bAdd)
{
NVMADRH = ((bAdd >> 8) & 0xFF);
NVMADRL = (bAdd & 0xFF);
NVMCON1bits.NVMREGS = 1;
NVMCON1bits.RD = 1;
NOP(); // NOPs may be required for latency at high frequencies
NOP();
return (NVMDATL);
}

void main(void) {
    volatile uint8_t scratch;
    
    DATAEE_WriteByte(0x7000,0x55);
    scratch = DATAEE_ReadByte(0x7000);
    while (1);
}

 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#34
teranz
Starting Member
  • Total Posts : 80
  • Reward points : 0
  • Joined: 2019/03/06 21:45:25
  • Location: 0
  • Status: offline
Re: PIC16F18326 write & read to EEPROM not working, using MCC generated code 2019/08/08 21:51:25 (permalink)
0
My xc8 is set for C99  ....  ELF/DWARF
I found only two differences, likely of no consequence...
 
#pragma config RSTOSC = HFINT1  yours:  #pragma config RSTOSC = HFINT32
#pragma config DEBUG = ON          yours:  #pragma config DEBUG = OFF
 
I just don`t know...  I was using the xc8 with the trial license, it expired a few months ago.
 
 
#35
ric
Super Member
  • Total Posts : 24605
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: PIC16F18326 write & read to EEPROM not working, using MCC generated code 2019/08/08 22:04:24 (permalink)
0
The IDE overrides the DEBUG config setting. It should be off, or left out altogether.
(But probably doesn't matter at all)
The clock setting won't matter.
 
I used XC8 2.05 in free mode, my license is for v1.x
 
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#36
1and0
Access is Denied
  • Total Posts : 9994
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: PIC16F18326 write & read to EEPROM not working, using MCC generated code 2019/08/08 22:06:50 (permalink)
+1 (1)
ric
Are you using C90, or C99 mode?
I tried a small project with both, and playing with all the optimisation levels, and I cannot get XC8 2.05 to include those extra lines, it always does it correctly.
 

I'm able to include those extra lines by checking "Local code generation" under Optimizations. ;)
 
#37
teranz
Starting Member
  • Total Posts : 80
  • Reward points : 0
  • Joined: 2019/03/06 21:45:25
  • Location: 0
  • Status: offline
Re: PIC16F18326 write & read to EEPROM not working, using MCC generated code 2019/08/08 22:12:15 (permalink)
0
I can use the use the functions in the peripheral library for my project but it bugs me as to what is causing the ASM code to be bad for memory.c.  Could errors creep into other code I wonder.  Maybe I can just delete the xc8 compiler and download it again to see if that fixes it.  Bothersome.  I`ll look at this again tomorrow.
 
Thanks so much for your help!
Tom
#38
teranz
Starting Member
  • Total Posts : 80
  • Reward points : 0
  • Joined: 2019/03/06 21:45:25
  • Location: 0
  • Status: offline
Re: PIC16F18326 write & read to EEPROM not working, using MCC generated code 2019/08/08 22:17:08 (permalink)
0

I'm able to include those extra lines by checking "Local code generation" under Optimizations. ;)

Great... that`s a start.  Exactly what is meant by "local code generation"?  What does it do?  What is the alternative?
 
Getting late here, I`ll reboot tomorrow-
 
Answered my own question... from the compiler docs...
Local code generation - This checkbox limits the extent to which some optimizations are applied to the program. See Section 3.7.6.8 “local”.
 
post edited by teranz - 2019/08/08 22:20:06
#39
ric
Super Member
  • Total Posts : 24605
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: PIC16F18326 write & read to EEPROM not working, using MCC generated code 2019/08/08 22:57:46 (permalink)
0
So don't turn it on.
I'm not sure why it would have been on, it defaults to OFF in new projects.
I thought I tested with that on, but must have missed it.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#40
Page: < 123 > Showing page 2 of 3
Jump to:
© 2019 APG vNext Commercial Version 4.5