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

Pic 24EP and compiler 5

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



Joined: 28 Feb 2006
Posts: 151

View user's profile Send private message

Pic 24EP and compiler 5
PostPosted: Tue May 12, 2015 12:52 pm     Reply with quote

Hi, I'm trying a pic24EP512GU806 on a board, and I'm writing a small code, but I'm having some strange troubles.

Compiler version 5.042

I'm just printing the task_queue buffer. But, i want to ignore when the characters are 0xFF or 255 in decimal (character: 'ÿ')

This code works fine, the 255 characters are ignored

Code:

#define TASK_QUEUE_SIZE 300
char Task_queue[TASK_QUEUE_SIZE];

void Show_task_queue()
{
   int16 task_index_debug = 0;
   
   fprintf(U2,"Sys Task queue");
   for(task_index_debug = 0; task_index_debug <= TASK_QUEUE_SIZE; task_index_debug++)
   {
      if(task_queue[task_index_debug] != 'ÿ')
         fprintf(U2,"%c",task_queue[task_index_debug]);
   }
   
return;
}



But this one not, it prints all the characters, including the 255.

Code:

#define TASK_QUEUE_SIZE 300
char Task_queue[TASK_QUEUE_SIZE];

void Show_task_queue()
{
   int16 task_index_debug = 0;
   
   fprintf(U2,"Sys Task queue");
   for(task_index_debug = 0; task_index_debug <= TASK_QUEUE_SIZE;   task_index_debug++)
   {
      if(task_queue[task_index_debug] != 0xFF)
         fprintf(U2,"%c",task_queue[task_index_debug]);
   }

return;
}



both codes are exactly the same... only changes:

if(task_queue[task_index_debug] != 'ÿ')

for

if(task_queue[task_index_debug] != 255)
jeremiah



Joined: 20 Jul 2010
Posts: 1354

View user's profile Send private message

PostPosted: Tue May 12, 2015 2:02 pm     Reply with quote

Have you tried (char)(0xFF)?

I know some versions of the PCD compiler have had issues doing comparisons versus defined constants because it assumes a different type promotion for the constant than it does the variable. You end up getting a sign extended version compared to a non sign extended versions

Since the default type of constants on PCD is 16 bit and a char is 8bit, I could see this being a possible cause.

EDIT: Also, as a matter of practice, one should always (always) use {} with their if statements. Not doing so is going to bite you one day.

EDIT2: Just tossed your code into a sample PCD project. This was the LST output:
Code:

....................       if(task_queue[task_index_debug] != 0xFF){
00210:  MOV     #1000,W4
00212:  MOV     112C,W3
00214:  ADD     W3,W4,W0
00216:  MOV.B   [W0],W5L
00218:  SE      W5,W5
0021A:  MOV     #FF,W4
0021C:  CP      W4,W5
0021E:  BRA     Z,220


Notice how the variable gets copied as a byte (MOV.B) and then sign extended (SE) and the is compared to a non sign extended 0xFF (Which happens when 0xFF is considered a 16 bit value).

Casting 0xFF as a char changes it to:

Code:

if(task_queue[task_index_debug] != (char)0xFF){
0020E:  MOV     #1000,W4
00210:  MOV     112C,W3
00212:  ADD     W3,W4,W0
00214:  MOV.B   [W0],W4L
00216:  XOR.B   #FF,W4L
00218:  BRA     Z,21A

which will probably work (I don't have anything to test on).
Ttelmah



Joined: 11 Mar 2010
Posts: 19537

View user's profile Send private message

PostPosted: Wed May 13, 2015 2:05 am     Reply with quote

You are also printing one to many entries. An array with 'TASK_QUEUE_SIZE' entries has entries from 0 to TASK_QUEUE_SIZE-1.
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