LUFA and C and me and decrementing For loops (bug?)

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, all,

I have finally moved on from writing everything in assembler and tried out C, mostly to grasp and fiddle with USB on AVR, with LUFA! Yay!

First, kudoes: It just worked. Unlike the Atmel supplied code, a LUFA demo just worked.

I'm using the DualVirtualSerial, and am wondering if I've done something silly or found a serious problem...

Echoing a character? Worked fine. For grins, I threw in an incrementer (Receivebyte++) line, so if you give it a 'c' you get back a 'd'. Incidentally, I suggest this for all serial port demos, as it keeps out confusion possible through local echo settings.

So I got a little more ambitious, and set up a character array, char Test[50], and initialized it with a little for loop

	for (i = 0; i < 50; i++) {
	  Test[i] = i + 0x30;
	  }

This fills the array with ASCII "0123..." through "a". To send it to the USB Serial device, another for loop:

   for (i = 10; i < 20; i++ ) {
      ReceivedByte = Test[i];
  CDC_Device_SendByte(&VirtualSerial2_CDC_Interface, (uint8_t)ReceivedByte);
          }
      ReceivedByte = 0x0D;
  CDC_Device_SendByte(&VirtualSerial2_CDC_Interface, (uint8_t)ReceivedByte);
          }

With a handy carriage return, sending a slice of the array off to the PC.

This works fine.

Now (still with me?) comes the problem.

I want to send a similar slice of the array in reverse order. Replacing only the loop with a decrementing one does NOT work:

  for (i = 20; i < 10; i-- ) { 

I get no characters at all. The carriage return (outside the for loop) still shows up just fine. But nothing else...

Have I done something silly here? (I'm using WinAVR and Studio 4, programming with avrdude) Am I trampling on a variable somewhere I shouldn't be?

Anyone?

S.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
for (i = 20; i > 10; i-- ) { 

Remember the for loop keeps on going as long the second part is true. It's a concealed while; not as in most other languages an until.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It's "less than" 10 in the code that doesn't work. i is supposed to be counting down - Yet never gets there? I think your loop would execute once.

S.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ahh. I may have gotten it... My bad?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I think your loop would execute once.
No, it is your code that does not execute any loops since the condition is false on the very first iteration.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have got it. It loops *while* the condition is true, not *until* the condition is true. Thanks to both o' ya!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

PS - It works fine now, although I've still got off-by-one errors and i < 0 is its own kettle o' fish when i is an unsigned int... :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The coding error rate diminishes proportional to the hollering of "you dummy, you!"

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

To include zero, I do the ugly trick of writing

for (uint8_t i = 20; i != 0xFF; i--)