tags:

views:

84

answers:

2

I have a PIC18F4455 microcontroller which I am trying to use to send 200 values over USB. Basically I am using a for loop and a printf statement to print the values to the usb output stream. However, when the code executes I see in my serial port monitor that it is only sending the first 25 or so values, then stopping. My PIC C code is below. It will send out the 25th or so value (and the comma), but not send anything after and not send a newline character. I'm trying to get it to send all the values, then a newline character at the end. I am sending them all as characters because I can convert them on the PC end of it.

//print #3
   for (i = 0; i <= 199; i++){if (data[i]=='\0' || data[i]=='\n'){data[i]++;}}
   for (i = 0; i < 199; i++){printf(usb_cdc_putc, "%c,", data[i]);}
   printf(usb_cdc_putc, "%c\n", data[199]);
A: 

I figured it out. I ended up needing to put a delay in after printing to the stream. A delay_us(20) within the for loop after the print statement cleared things up.

Adam
+2  A: 

You might be over-filling the buffer (FIFO in hardware) if it's too short. This is why the delay has solved the problem - because you gave the hardware time to actually send out some bytes before you filled in new ones into the FIFO.

Read the datasheet of the controller to see how large that FIFO is. There's probably a way to check how full it is, and thus wait less than a constant 20 usec.

Also, I would use putchar for printing out single chars, not printf.

Eli Bendersky