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

Loop to read tables going infinite

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



Joined: 14 May 2004
Posts: 330

View user's profile Send private message

Loop to read tables going infinite
PostPosted: Sat May 22, 2004 10:48 pm     Reply with quote

I'm starting with C, seeing how things are...

I did this:

Code:
const char ter[256] = { 00, 01, 02, 03, 04, 05, 06, 07,10, 11, 12, 13, 14, 15, 16, 17,
                         10, 11, 12, 13, 14, 15, 16, 17,10, 11, 12, 13, 14, 15, 16, 17,
                        20, 21, 22, 23, 24, 25, 26, 27,10, 11, 12, 13, 14, 15, 16, 17,
                        30, 31, 32, 33, 34, 35, 36, 37,10, 11, 12, 13, 14, 15, 16, 17,
                        40, 41, 42, 43, 44, 45, 46, 47,10, 11, 12, 13, 14, 15, 16, 17,
                        50, 51, 52, 53, 54, 55, 56, 57,10, 11, 12, 13, 14, 15, 16, 17,
                        60, 61, 62, 63, 64, 65, 66, 67,10, 11, 12, 13, 14, 15, 16, 17,
                         10, 11, 12, 13, 14, 15, 16, 17,10, 11, 12, 13, 14, 15, 16, 17,
                        20, 21, 22, 23, 24, 25, 26, 27,10, 11, 12, 13, 14, 15, 16, 17,
                        30, 31, 32, 33, 34, 35, 36, 37,10, 11, 12, 13, 14, 15, 16, 17,
                        40, 41, 42, 43, 44, 45, 46, 47,10, 11, 12, 13, 14, 15, 16, 17,
                        50, 51, 52, 53, 54, 55, 56, 57,10, 11, 12, 13, 14, 15, 16, 17,
                        60, 61, 62, 63, 64, 65, 66, 67,10, 11, 12, 13, 14, 15, 16, 17,
                         10, 11, 12, 13, 14, 15, 16, 17,10, 11, 12, 13, 14, 15, 16, 17,
                        20, 21, 22, 23, 24, 25, 26, 27,10, 11, 12, 13, 14, 15, 16, 17,
                        30, 31, 32, 33, 34, 35, 36, 37,10, 11, 12, 13, 14, 15, 16, 17 };
   for(index=0; index<=255; index++) {
      delay_ms(1);
      putc(ter[index]);
   };


It goes to a infinite loop sending chars via serial port.

Compiler is 3.190.

Does anyone know what I am doing wrong?
Haplo



Joined: 06 Sep 2003
Posts: 659
Location: Sydney, Australia

View user's profile Send private message

PostPosted: Sat May 22, 2004 11:53 pm     Reply with quote

'index' is a byte variable (remember in CCS, int is one byte), so it will roll over to zero after 255. This means the condition index<=255 never becomes false, since index never becomes equal to 256 and always stays less or equal to 255.
future



Joined: 14 May 2004
Posts: 330

View user's profile Send private message

PostPosted: Sun May 23, 2004 11:16 am     Reply with quote

I dont think I'll have to use a 16bit variable to do the indexing of a 256bytes array.

There has to be a way ;) even basic do that.
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Sun May 23, 2004 11:27 am     Reply with quote

The program is doing what you coded:
Code:

 for(index = 0; index <= 255; index++)


Probably you want to do:
Code:

 for(index = 0; index < 255; index++)


HTH

Humberto
future



Joined: 14 May 2004
Posts: 330

View user's profile Send private message

PostPosted: Sun May 23, 2004 12:00 pm     Reply with quote

But this way the last char of the table will not be sent.

That's why I used <= Rolling Eyes
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Sun May 23, 2004 12:24 pm     Reply with quote

Your consult was regarding this matter:
Quote:

It goes to a infinite loop sending chars via serial port.


A byte variable size is 255 bits, NOT 256

Humberto
future



Joined: 14 May 2004
Posts: 330

View user's profile Send private message

PostPosted: Sun May 23, 2004 12:54 pm     Reply with quote

How do you do something like this?

Would you declare an int16 variable to do the indexing?
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

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

PostPosted: Sun May 23, 2004 1:14 pm     Reply with quote

I'd do this and let the index roll over

Code:

index=0;
while(1)
{
   delay_ms(1);
   putc(ter[index++]);
}


Or, if you wanted to leave after the 255th

Code:

index=0;
do
{
   delay_ms(1);
   putc(ter[index++];
} while(index); // or index!=0 if you prefer

_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
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