CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Access to ROM const

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Pekub



Joined: 20 Dec 2005
Posts: 16

View user's profile Send private message Send e-mail

Access to ROM const
PostPosted: Wed Nov 23, 2011 10:50 am     Reply with quote

Hello,

I tried use ROM constant in my program.
Please, can someone explain me what is the right way to access a ROM constant ?

#include <18F26K22.h>
#device CONST=ROM

..
const int8 yy = 10;
int8 xx;

xx = 100;

if(xx < yy) // doesn`t work
;

if(xx < (int8)yy) // it work`s
;


Thank`s

Peter
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Nov 23, 2011 1:25 pm     Reply with quote

You didn't give a full test program or your compiler version, so I made
a test program and tested it with vs. 4.127. It works. I got this output
in the MPLAB simulator:
Quote:

Print this if 100 is NOT less than 10.

Test program:
Code:

#include <18F26K22.h>
#device CONST=ROM
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//======================================
void main(void)
{
const int8 yy = 10;
int8 xx;

xx = 100;

if(xx < yy)   // Is (100 < 10) ?
   printf("Print this if 100 is less than 10. \r");   
else   
   printf("Print this if 100 is NOT less than 10. \r"); 


while(1);
}
Pekub



Joined: 20 Dec 2005
Posts: 16

View user's profile Send private message Send e-mail

PostPosted: Thu Nov 24, 2011 3:56 am     Reply with quote

O.K. It`s work.

But try this one:
Code:

int8 xx;
const int8 Table[2] = {10, 50};
   
xx = 100;
   
if(xx < Table[0])
   Led_PrgEE_On();
else
    Led_PrgEE_Off();
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 24, 2011 2:13 pm     Reply with quote

Again, you didn't post your compiler version.

But it works for me. The program below gives this output in MPLAB
simulator. It's correct:
Quote:

LED off

Code:

#include <18F26K22.h>
#device CONST=ROM
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//====================================
void main()
{
int8 xx;
const int8 Table[2] = {10, 50};
   
xx = 100;
   
if(xx < Table[0])
   printf("LED on \r");
else
   printf("LED off \r");
     
while(1);
}
Pekub



Joined: 20 Dec 2005
Posts: 16

View user's profile Send private message Send e-mail

PostPosted: Sat Nov 26, 2011 4:02 am     Reply with quote

I tried it maybe 20 times with V 4.127, V 4.122,
but it doesn`t work.

Code:

#include <18F26K22.h>
#device CONST=ROM
#fuses INTRC,,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=16000000)

void main()
{
   int8 xx;
   const int8 Table[2] = {10, 50};
   
   xx = 100;
   
   if(xx < Table[0])
      output_high(PIN_B2); // LED On
     
   else
      output_low(PIN_B2);  // LED Off
     
   while (1) 
      ;
}




I dont`t understand assembler, but there is a difference between these methods:

.................... if(xx < (int8)Table[0])
00052: CLRF FF8
00054: SETF FF7
00056: MOVLW FE
00058: MOVWF FF6
0005A: CLRF FEA
0005C: MOVLW 06
0005E: MOVWF FE9
00060: CLRF 0B
00062: MOVLW 02
00064: MOVWF 0A
00066: MOVLB 0
00068: BRA 0004
0006A: MOVF 06,W
0006C: SUBWF 05,W
0006E: BC 0076
.................... output_high(PIN_B2);
00070: BCF F93.2
00072: BSF F8A.2


.................... if(xx < Table[0])
00052: CLRF FF8
00054: SETF FF7
00056: MOVLW FE
00058: MOVWF FF6
0005A: CLRF FEA
0005C: MOVLW 06
0005E: MOVWF FE9
00060: CLRF 0B
00062: MOVLW 02
00064: MOVWF 0A
00066: MOVLB 0
00068: BRA 0004
0006A: MOVF 07,F
0006C: BNZ 0074
0006E: MOVF 06,W

00070: SUBWF 05,W
00072: BC 007A
.................... output_high(PIN_B2);
00074: BCF F93.2
00076: BSF F8A.2
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Sat Nov 26, 2011 5:26 am     Reply with quote

OK.

What is happening is quite interesting. It appears that if the CONST=ROM device statement is present, it is making the compiler treat the returned value from the table fetch, as a 16bit value, even if it is defined as an 8bit value...
So the extra test, is testing if the the top byte returned is non zero, and if it is, then the value must be larger than an int8 value, so skips the rest of the test....
It doesn't happen if you explicitly type cast, _or_ if you swap the variables. So:
Code:

   if(Table[0] > xx)
      output_high(PIN_B2); // LED On
     
   else
      output_low(PIN_B2);  // LED Off

Works fine.

It is a bug, and should be reported, but is very simple to program round.

Best Wishes
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat Nov 26, 2011 5:35 am     Reply with quote

Quote:
It is a bug, and should be reported, but is very simple to program round.

It's present at least for CCS C V4.120 until V4.127. V4.112 is still O.K.

You can cause even more confusion by extending the table to 4 bytes, which makes CCS C treating Table[] as a long quantity.

Once more exceeding all (bad) expectations...

Code:
const int8 Table[4] = {10, 50, 51, 52};
...
15:                if(xx < Table[1])
  0090    6AF8     CLRF 0xff8, ACCESS
  0092    68F7     SETF 0xff7, ACCESS
  0094    0EFD     MOVLW 0xfd
  0096    6EF6     MOVWF 0xff6, ACCESS
  0098    6AEA     CLRF 0xfea, ACCESS
  009A    0E06     MOVLW 0x6
  009C    6EE9     MOVWF 0xfe9, ACCESS
  009E    6A0F     CLRF 0xf, ACCESS
  00A0    0E04     MOVLW 0x4
  00A2    6E0E     MOVWF 0xe, ACCESS
  00A4    0100     MOVLB 0
  00A6    D7B8     BRA 0x18
  00A8    5209     MOVF 0x9, F, ACCESS
  00AA    E107     BNZ 0xba
  00AC    5208     MOVF 0x8, F, ACCESS
  00AE    E105     BNZ 0xba
  00B0    5207     MOVF 0x7, F, ACCESS
  00B2    E103     BNZ 0xba
  00B4    5006     MOVF 0x6, W, ACCESS
  00B6    5C05     SUBWF 0x5, W, ACCESS
  00B8    E206     BC 0xc6
16:                   printf("LED on \r");
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group