• AVR Freaks

AnsweredHot![Solved] PIC18F47K40 - Troubles Using Table Reads

Author
PICnik
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2020/09/22 02:27:15
  • Location: 0
  • Status: offline
2020/09/27 13:22:06 (permalink)
0

[Solved] PIC18F47K40 - Troubles Using Table Reads

Hello!
I'm having troubles reading data from an array stored in the program memory. Please have patience with me as I'm a newbie.
 
My setup: 
HW: PIC18F47K40 on Curiosity HPC board and Mikroe OLED W Click board populating the MikroBUS slot 1. Power supply set to 3.3V.
SW: MPLAB X IDE v5.40, XC8 v2.30
MCC - latest version, I think, to configure pins and peripherals. 
The IDE has all updates applied almost as soon as I was prompted to. 
 
My problem:
I have a 480 bytes long array defined as const so it is stored in the program memory - this works, I've checked the memory view.
I want to read this, byte by byte, and write these bytes to the display (the OLED click);
 
So far, from what I can tell, the problem seems to be that, due to some reason that escapes me, the TBLRD* instruction is not executed. 
This is my main.c:
#include "mcc_generated_files/mcc.h"
#include "oled.h"

extern const uint8_t font[];
extern const uint8_t xpress[];
uint8_t xpress_buff[96];
uint8_t i;
uint8_t j;
uint8_t k;
uint8_t lcd_byte1;
uint8_t lcd_byte2;

/*
                         Main application
 */
void main(void)
{
    // Initialize the device
    SYSTEM_Initialize();
 
    i=0;
    j=0;
    k=0;
    lcd_byte1=0x55;
    lcd_byte2=0xAA;
    OLED_Initialize();
    LED_D5_SetLow();
    OLED_Clear();
    OLED_SetRow(0);
    OLED_SetColumn(0);
    OLED_Data(0xAA);
    OLED_Data(0x55);
    OLED_Data(0xAA);
    OLED_Data(0x55);
    __delay_ms(1000);
    OLED_Clear();
    
    //test copying a variable from a program data stored array
    
    xpress_buff[0]=xpress[0]; //somehow this fails
    xpress_buff[1]=xpress[1]; // as does this
    xpress_buff[2]=xpress[2]; // and this
    
    //end test */
    
    //fill the screen with a checker board pattern -> this works
    for( i=0; i<5; i++) // 5*8=40 pixel rows (actually 39)
    {
        OLED_SetRow( i);
        OLED_SetColumn(0);
        for( j=0; j<96; j++) // 96 pixel columns
        {
            OLED_Data(lcd_byte1);
            lcd_byte1=~lcd_byte1;
        }
    }
    //end screen fill test */
    
    __delay_ms(1000);
    
    while (1)
    {
        LED_D5_Toggle();
        __delay_ms(500);
    }
}

 
I've run this code step by step in disassembly view, and thus I came to the conclusion that the TBLRD* does not seem to work. The Table pointer registers are loaded with the correct values, but when TBLRD* is executed, the TABLAT still reads 0x00, though it should certainly read something else.
 
Please help!!
post edited by PICnik - 2020/09/29 08:10:50
#1
NorthGuy
Super Member
  • Total Posts : 6350
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: PIC18F47K40 - Troubles Using Table Reads 2020/09/28 08:52:48 (permalink) ☼ Best Answerby PICnik 2020/09/29 08:07:12
+4 (4)
Read the errata. There's an erratum for TBLRD instruction:
 
"The affected silicon revisions of the PIC18FXXK40 devices improperly require the NVMREG<1:0> bits in the NVMCON register to be set for  TBLRD access of the various memory regions. The issue is most apparent in compiled C programs when the user defines a  CONST  type and the compiler uses TBLRD  instructions to retrieve the data from pro-gram Flash memory (PFM). The issue is also apparent when the user defines an array in RAM for which the complier creates start-up code, exe-cuted before main(), that uses  TBLRD  instructions to initialize RAM from PFM."
#2
dan1138
Super Member
  • Total Posts : 3921
  • Reward points : 0
  • Joined: 2007/02/21 23:04:16
  • Location: 0
  • Status: offline
Re: PIC18F47K40 - Troubles Using Table Reads 2020/09/28 09:34:40 (permalink)
0
carp.andrei
... So far, from what I can tell, the problem seems to be that, due to some reason that escapes me, the TBLRD* instruction is not executed. 
 
...
 
Please help!!

I cannot reproduce your problem using MPLABX v5.40 simulator and XC8 v2.20:

post edited by dan1138 - 2020/09/28 09:51:42

Attached Image(s)

#3
ric
Super Member
  • Total Posts : 28691
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC18F47K40 - Troubles Using Table Reads 2020/09/28 13:13:12 (permalink)
0
Dan, which rev PIC were you using?
As mentioned by NorthGuy, it's an errata in the older revs.

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!
#4
1and0
Access is Denied
  • Total Posts : 11343
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: PIC18F47K40 - Troubles Using Table Reads 2020/09/28 13:36:43 (permalink)
0
ric
which rev PIC were you using?
As mentioned by NorthGuy, it's an errata in the older revs.

Can we pick the Rev for the SIMulator?
#5
ric
Super Member
  • Total Posts : 28691
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC18F47K40 - Troubles Using Table Reads 2020/09/28 13:41:07 (permalink)
+2 (2)
Oops, I missed the word "simulator" in there.
In that case, I doubt the simulator accurately emulates the errata behaviour!
 

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!
#6
PICnik
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2020/09/22 02:27:15
  • Location: 0
  • Status: offline
Re: PIC18F47K40 - Troubles Using Table Reads 2020/09/29 08:09:25 (permalink)
0
@NorthGuy: Thank you so much!
 
I added the workaround to my code and now it works!!
I totally forgot about the errata!
#7
Jump to:
© 2020 APG vNext Commercial Version 4.5