Constants aloccation in ROM by address compilator fail

General discussion on mikroC PRO for PIC.
Author
Message
renew
Posts: 28
Joined: 24 Nov 2016 13:56

Constants aloccation in ROM by address compilator fail

#1 Post by renew » 25 Nov 2022 23:22

Hi, All
The problem:

Code: Select all

const char Cells[10] = {0x01, 0x30, 0x08, 0x10, 0x15, 0x13, 0x01, 0x04, 0x14, 0x60} absolute 0x7770;
After successful compilation, I open the output HEX file and see those constant by needed address 0x7770
Next, I change In the HEX file first constant to 0x05 and flash it to PIC.

And the PIC works like the costant has the value 0x01
So I assume the compilator still use its own addressation for Cells, not designed by absolute directive.

What to do ?

paulfjujo
Posts: 1557
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: Constants aloccation in ROM by address compilator fail

#2 Post by paulfjujo » 28 Nov 2022 20:16

hello,


Check if your application program does'nt ovewrite your datas ...

No problemo on this test ..
use the tool STATISTIC , ROM Memory constant to check where your cell table is located.
* nota: i added cells1 table to see the next following adresse
Constant_in_absolute_adresse.jpg
Constant_in_absolute_adresse.jpg (142.03 KiB) Viewed 2229 times

renew
Posts: 28
Joined: 24 Nov 2016 13:56

Re: Constants aloccation in ROM by address compilator fail

#3 Post by renew » 30 Nov 2022 02:23

Hi. Thank you for the reply.
Yes, I can see those constants in the statistic chart by the needed address.
Also, I can see those in the output hex file by the needed address. But if I change the value directly in hex, the PIC continues to work with default value.
Could you do this experiment too ?
Assign some useful constant to absolute address and change it in hex using Notepad++.
You can fix a checksum here https://www.fischl.de/hex_checksum_calculator/
Check if the PIC behaviour was changed according to constant value changing.

paulfjujo
Posts: 1557
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: Constants aloccation in ROM by address compilator fail

#4 Post by paulfjujo » 30 Nov 2022 15:01

renew wrote:
30 Nov 2022 02:23
You can fix a checksum here https://www.fischl.de/hex_checksum_calculator/
Check if the PIC behaviour was changed according to constant value changing.
i test it ! it works fine !

Code: Select all


MikroC code
const char Cells[10] = {0x01, 0x30, 0x08, 0x10, 0x15, 0x13, 0x01, 0x04, 0x14, 0x60} absolute 0x7770;
const char Cells1[10] = {0x02, 0x31, 0x09, 0x11, 0x16, 0x14, 0x02, 0x05, 0x15, 0x61} absolute 0x777A;

test code
 for (i=0;i<10;i++)
    {
       ByteToStr(  Cells[i],CRam1);
      CPrint(" Cells ");
      UART1_Write(i+48);  UART1_Write('>');
      Print(CRam1);
      CRLF1();
     }
    CRLF1();
      for (i=0;i<10;i++)
    {
      ByteToStr(  Cells1[i],CRam1);
      CPrint(" Cells1 ");
      UART1_Write(i+48);  UART1_Write('>');
      Print(CRam1);
      CRLF1();
     }
      CRLF1();
	  
run programme test to print out  values in Rom
result on YAT terminal

 Cells 0>  1
 Cells 1> 48
 Cells 2>  8
 Cells 3> 16
 Cells 4> 21
 Cells 5> 19
 Cells 6>  1
 Cells 7>  4
 Cells 8> 20
 Cells 9> 96

 Cells1 0>  2
 Cells1 1> 49
 Cells1 2>  9
 Cells1 3> 17
 Cells1 4> 22
 Cells1 5> 20
 Cells1 6>  2
 Cells1 7>  5
 Cells1 8> 21
 Cells1 9> 97  
 
 
 
 modif Hex programm with  Notepad +++
 
 :0325D400204300A1
:0A7770000130081015130104146025
:0A777A000231091116140205156111
:020000040030CA
 
 original
:0A7770 00 01 30 08 10 15 13 01 04 14 60 25 <-- CRC=25
 modif
:0A7770 00 02 31 09 11 14 12 00 03 13 59 

https://www.fischl.de/hex_checksum_calculator/
:0A77700002310911141200031359

Address: 777016 = 3057610
Byte count: 0A16 = 1010
Record type: 0016 = Data
Checksum: 16
Calculated checksum: 2D16
Checksum too short!
Checksum mismatch!
Calculated checksum: E716

change in notepad++  (added checksum  E7 !)
:0A77700002310911141200031359E7
then reccord the into Hex file 
then write the file with Pickit3 into the PIC
restart the program and get
Pay attention : value in decimal here ! 
but in hexadecimal in the notepad 

 Cells 0>  2
 Cells 1> 49
 Cells 2>  9
 Cells 3> 17
 Cells 4> 20
 Cells 5> 18
 Cells 6>  0
 Cells 7>  3
 Cells 8> 19
 Cells 9> 89

 Cells1 0>  2
 Cells1 1> 49
 Cells1 2>  9
 Cells1 3> 17
 Cells1 4> 22
 Cells1 5> 20
 Cells1 6>  2
 Cells1 7>  5
 Cells1 8> 21
 Cells1 9> 97

 the changes has been applied ..OK !


Test_modif_constantes_MikroC_avec_Notepad_plus_OK_2022-1130.jpg
Test_modif_constantes_MikroC_avec_Notepad_plus_OK_2022-1130.jpg (195.17 KiB) Viewed 2198 times

renew
Posts: 28
Joined: 24 Nov 2016 13:56

Re: Constants aloccation in ROM by address compilator fail

#5 Post by renew » 01 Dec 2022 03:01

Thank you !
Will think what is wrong with me .

renew
Posts: 28
Joined: 24 Nov 2016 13:56

Re: Constants aloccation in ROM by address compilator fail

#6 Post by renew » 03 Dec 2022 04:20

Unfortunatelly it does not work for me.

Code: Select all

// Cells
const char Cells[10] = {0x05, 0x30, 0x08, 0x10, 0x15, 0x13, 0x01, 0x04, 0x14, 0x60} absolute 0x7770;

void cells_reading(void){
   Disp_time = Bcd2Dec(Cells[0]);
   Disp_time *= 60000;                      // minutes to ms
   Off_time = Bcd2Dec(Cells[1]);
   Off_time *= 60000;                       // minutes to ms
   Rel_Del =  Bcd2Dec(Cells[2]);            // Delay in ms
   min_for_start = Bcd2Dec(Cells[3]);       // Power with tens parts
   max_for_start = Bcd2Dec(Cells[4]) * 10;  // power in Watts
   Auto_delta = Bcd2Dec(Cells[5]) * 10;     // SWR with tens parts
   Auto = Bcd2Dec(Cells[6]);
   Cal_b = Bcd2Dec(Cells[7]) / 10.0;
   Cal_a = Bcd2Dec(Cells[8]) / 100.0 + 1.0;
   Peak_cnt = Bcd2Dec(Cells[9]) * 10 / 6;
   rldl = Rel_Del;
   return;
}
When I changing Cells in the hex file, PIC works with values assigned by default.

I dont know what to do .

paulfjujo
Posts: 1557
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: Constants aloccation in ROM by address compilator fail

#7 Post by paulfjujo » 03 Dec 2022 11:21

Hello,

did you do the same test as me ?

why using Bcd2dec ?
// Cells
// direct use of decimal
const char Cells[10] = {5,30,8, 10,15, 13, 1, 4,14,60} absolute 0x7770;

if you try to modify constant , compiler will not be happy ..

what is the size of Disp_time and Off_time ?
long integer ?
what is the size of
Cal_a Cal_b
float ?

are your results OK, without modifing constant in hex file ?

is the CRC value added at the end of 16 Hex value OK
after some Hex value are modified ?
else the Upload into the PIC could be corrupted or nothing is write

renew
Posts: 28
Joined: 24 Nov 2016 13:56

Re: Constants aloccation in ROM by address compilator fail

#8 Post by renew » 03 Dec 2022 19:05

Ok, I found what is wrong.

It works properly when I just read Cells and use them values for some purpose.

Code: Select all

ShortToStr(Bcd2Dec(Cells[0]), txt);
oled_write(1, 0, txt, 5, 'w');
But it Does not work, when I assign Var = Cells and use Var in further.

Code: Select all

cell_0 = Bcd2Dec(Cells[0]);
 ShortToStr(cell_0, txt);
 oled_write(1, 0, txt, 5, 'w');
It does not work even if I use
volatile char Var;

Suggestions ?

paulfjujo
Posts: 1557
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: Constants aloccation in ROM by address compilator fail

#9 Post by paulfjujo » 05 Dec 2022 11:13

Hello,
renew wrote:
03 Dec 2022 19:05
...
But it Does not work, when I assign Var = Cells and use Var in further.
.....
Suggestions ?



:D it's work fine for me !!

result on my Terminal

05/12/2022

Test Forum MikroC :

viewtopic.php?f=88&t=79656
Cells 0> 1
Cells 1> 48 <- all value are in decimal instead of Hexadecimal - original data =0x30)
Cells 2> 8
Cells 3> 16
Cells 4> 21
Cells 5> 19
Cells 6> 1
Cells 7> 4
Cells 8> 20
Cells 9> 96

1
1
30 <-- 0x30 = 48
8



Code: Select all

CPrint( "\r\n Test Forum MikroC :\r\n");
   CPrint("\r\n https://forum.mikroe.com/viewtopic.php?f=88&t=79656 \r\n");
    for (i=0;i<10;i++)
    {
       ByteToStr(  Cells[i],CRam1);
      CPrint(" Cells ");
      UART1_Write(i+48);  UART1_Write('>');
      Print(CRam1);
      CRLF1();
     }
       CRLF1();
       ShortToStr(Bcd2Dec(Cells[0]), txt);
       Print(txt); CRLF1();
       // oled_write(1, 0, txt, 5, 'w');
       //But it Does not work, when I assign Var = Cells and use Var in further.
       Cell_0 = Bcd2Dec(Cells[0]);
       ShortToStr(Cell_0, txt);
       Print(txt); CRLF1();
       // oled_write(1, 0, txt, 5, 'w');
       Cell_0 = Bcd2Dec(Cells[1]);
       ShortToStr(Cell_0, txt);
       Print(txt); CRLF1();
       Cell_0 = Bcd2Dec(Cells[2]);
       ShortToStr(Cell_0, txt);
       Print(txt); CRLF1();
    while(1);
	
as you can see, i don't have a OLED connected to my PIC, so
i use UART print out on PC terminal ..
it is the main difference with your code ..
so maybe problem is with OLED !

for this kind of problem
DIVISER POUR MIEUX REGNER

check only a few part of your code
process step by step ...

renew
Posts: 28
Joined: 24 Nov 2016 13:56

Re: Constants aloccation in ROM by address compilator fail

#10 Post by renew » 06 Dec 2022 02:01

Is there some way to disable compilator optimisation ?
I just want my code work exactly as written.

paulfjujo
Posts: 1557
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: Constants aloccation in ROM by address compilator fail

#11 Post by paulfjujo » 06 Dec 2022 11:04

By the way, PIC reference ?

My tests : on PIC18F27K42 with 128K Flash ROM !

can you do the test with a PC terminal on UART instead of a OLED LCD?
Is there some way to disable compilator optimisation ?
I just want my code work exactly as written.

Code: Select all

 Tools 
    Options
	    Ouput settings
		  optimisation level 
		      Four 
my test was with level four
you can select Zero !

but i think the problem is elsewhere!
Test if you can get a good result only with the minimum code size...
if it works, the problem is external ..

renew
Posts: 28
Joined: 24 Nov 2016 13:56

Re: Constants aloccation in ROM by address compilator fail

#12 Post by renew » 10 Dec 2022 19:27

Hi.
The main project is using PIC16F18877 processor 4k RAM 32k ROM
But for fast test I use a special developer board with colored OLED and PIC16F1939

I have set Optimisation parameter to Zero - it does not make effect, I changed first cell to 01 but device is working as it is 05.

paulfjujo
Posts: 1557
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: Constants aloccation in ROM by address compilator fail

#13 Post by paulfjujo » 11 Dec 2022 12:14

Hello,
renew wrote:
10 Dec 2022 19:27
Hi.
The main project is using PIC16F18877 processor 4k RAM 32k ROM
But for fast test I use a special developer board with colored OLED and PIC16F1939

I have set Optimisation parameter to Zero - it does not make effect, I changed first cell to 01 but device is working as it is 05.
my test was made with a 18F !

16F1939 has 32K word ,but organised in 14 bits word size ..instead of 16 bits !

so you can not store 2ascii bytes in one word
or must limited to binary or ascii <=3F for 1rst byte.
2nd byte is OK for binary or ascii ...00 to FF
0xxx11 1111 1111 1111
3F FF


i allready did some test in HEF Area with a 16F1619 , it explained this problem !

i don't have your MCU
but i will do a similar test with a 16F1847...

renew
Posts: 28
Joined: 24 Nov 2016 13:56

Re: Constants aloccation in ROM by address compilator fail

#14 Post by renew » 11 Dec 2022 12:45

Hi.
Yes, I understand, but I locate 1 char in 1 14 bit cell, it is absolutely possible. See the hex file screenshot, 1 constant in 2 bytes.
You need to return my post number 8.
I am sure that the compiler precalculates variables wich calculation based on constants.

paulfjujo
Posts: 1557
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: Constants aloccation in ROM by address compilator fail

#15 Post by paulfjujo » 11 Dec 2022 16:11

renew wrote:
11 Dec 2022 12:45
Hi.
Yes, I understand, but I locate 1 char in 1 14 bit cell, it is absolutely possible. See the hex file screenshot, 1 constant in 2 bytes.
You need to return my post number 8.
I am sure that the compiler precalculates variables wich calculation based on constants.
i don't contest that ...

i did same test on my 16F1847

Source : _16F1847_Test_LCD_Open_Smart_Serial__2022-1211.c


------------------ original programm --------------------
1768 3FFF 3FFF 3FFF 3FFF 3FFF 3FFF 3FFF 3FFF
1770 3401 3430 3408 3410 3415 3413 3401 3404
1778 3414 3460 3FFF 3FFF 3FFF 3FFF 3FFF 3FFF

Test Forum MikroC :
viewtopic.php?f=88&t=79656
Cells 0> 1
Cells 1> 48
Cells 2> 8
Cells 3> 16
Cells 4> 21
Cells 5> 19
Cells 6> 1
Cells 7> 4
Cells 8> 20
Cells 9> 96

1 ,Cell_O =0x01
30 ,Cell_O =0x30
8 ,Cell_O =0x08

0x01,0x30,0x08

---------------------
manual change into Pickit3 program memory 3 values at 1770,1771,1772
1768 3FFF 3FFF 3FFF 3FFF 3FFF 3FFF 3FFF 3FFF
1770 3412 3444 3418 3410 3415 3413 3401 3404
1778 3414 3460 3FFF 3FFF 3FFF 3FFF 3FFF 3FFF
--------------------------
reload program into the PIC
Write Pickit3 command

get results :

Test Forum MikroC :
viewtopic.php?f=88&t=79656
Cells 0> 18
Cells 1> 68
Cells 2> 24
Cells 3> 16
Cells 4> 21
Cells 5> 19
Cells 6> 1
Cells 7> 4
Cells 8> 20
Cells 9> 96
:!: 1rst reading is OK !!!!
1 ,Cell_O =0x01
30 ,Cell_O =0x30
8 ,Cell_O =0x08

0x01,0x30,0x08

same strange behavior as yours !

the first reading take care about the change i made on the 3 first values
but is not permanent after ???
re use old setting !!
but were are stored old settings ?

remark : 0x34 as upper byte ? instead of 00 or FF

je donne ma langue au chat !
adaladale_clavier.gif
adaladale_clavier.gif (244.61 KiB) Viewed 2058 times

Code: Select all

unsigned char Cell_0=0;
const unsigned char Cells[10] = {0x01, 0x30, 0x08, 0x10, 0x15, 0x13, 0x01, 0x04, 0x14, 0x60} absolute 0x1770;
 
  // PIC16(L)F1847      8,192  word      last adress= 1FFFh
   // Up to 14 Kbytes Linear Program Memory
  //The enhanced mid-range core has a 15-bit program
  //counter capable of addressing a 32K x 14 program memory space.     (errata   32K ??  for me   8192 word -> 16K bytes)




  // ------------ test forum kikroE -------------------------------
  CPrint( "\r\n Test Forum MikroC :\r\n");
   CPrint("\r\n https://forum.mikroe.com/viewtopic.php?f=88&t=79656 \r\n");
    for (i=0;i<10;i++)
    {
       ByteToStr(  Cells[i],txt);
      CPrint(" Cells ");
      UART1_Write(i+48);  UART1_Write('>');
      Print(txt);
      CRLF1();
     }
       CRLF1();
       Cell_0 = Bcd2Dec(Cells[0]);
       ShortToStr(Cell_0, txt);
       Print(txt);
       Cell_0 =Cells[0];
       CPrint(" ,Cell_O =0x");ByteToHex(Cell_0,CRam1);Print(CRam1);
       CRLF1();
       Cell_0 = Bcd2Dec(Cells[1]);
       ShortToStr(Cell_0, CRam1);
       Print(CRam1);
       Cell_0 =Cells[1];
       CPrint(" ,Cell_O =0x");ByteToHex(Cell_0,CRam1);Print(CRam1);
       CRLF1();
       Cell_0 = Bcd2Dec(Cells[2]);
       ShortToStr(Cell_0, CRam1);
       Print(CRam1);
       Cell_0 =Cells[2];
       CPrint(" ,Cell_O =0x");ByteToHex(Cell_0,CRam1);Print(CRam1);
       CRLF1();
       CRLF1();
       CPrint("0x");ByteToHex(Cells[0],CRam1); Print(CRam1);UART1_Write(',') ;
       CPrint("0x");ByteToHex(Cells[1],CRam1); Print(CRam1);UART1_Write(',') ;
       CPrint("0x");ByteToHex(Cells[2],CRam1); Print(CRam1);CRLF1();
    while(1);
    //----------------------------------------------------

Reading (and Writing) in Flash memory needs special function ...
Pickit3 surely can write in flash memory
but reading by a classic way (as reading in Ram) ,normaly, doesen't works ..
but why first reading works without use of FLASH library ?

plus fort que le roquefort !
i used Flash reading function
0x01,0x30,0x08
adresse= 0x1770:01
adresse= 0x1771:30
adresse= 0x1772:08
adresse= 0x1773:10
adresse= 0x1774:15
adresse= 0x1775:13
adresse= 0x1776:01
adresse= 0x1777:04
adresse= 0x1778:14
adresse= 0x1779:60

modif:
1768 3FFF 3FFF 3FFF 3FFF 3FFF 3FFF 3FFF 3FFF
1770 3412 3420 3418 3430 3415 3413 3401 3404
1778 3414 3460 3FFF 3FFF 3FFF 3FFF 3FFF 3FFF

Write to PIC and restart

Cells 0> 18
Cells 1> 32
Cells 2> 24
Cells 3> 48
Cells 4> 21
Cells 5> 19
Cells 6> 1
Cells 7> 4
Cells 8> 20
Cells 9> 96

1 ,Cell_O =0x01
30 ,Cell_O =0x30
8 ,Cell_O =0x08

0x01,0x30,0x08
adresse= 0x1770:12
adresse= 0x1771:20
adresse= 0x1772:18
adresse= 0x1773:30
adresse= 0x1774:15
adresse= 0x1775:13
adresse= 0x1776:01
adresse= 0x1777:04
adresse= 0x1778:14
adresse= 0x1779:60
i added this code

Code: Select all


       j=0x1770;
       for (i=0;i<10;i++)
       {
       CPrint(" adresse= 0x177"); UART1_Write(i+48); UART1_Write(':');
       Cell_0= FLASH_Read(j);
       ByteToHex(Cell_0,txt);Print(txt); CRLF1();
       j++;
       }
to read back Flash zone before and after modifying the programme Memory (via Pickit3)
and read back is OK !!
the changes are there !

Post Reply

Return to “mikroC PRO for PIC General”