PIC C - 通过 USB 发送 200 个值,但它只发送其中的 25 个左右

发布于 2024-08-27 09:04:32 字数 469 浏览 9 评论 0原文

我有一个 PIC18F4455 微控制器,我试图用它通过 USB 发送 200 个值。基本上我使用 for 循环和 printf 语句将值打印到 USB 输出流。然而,当代码执行时,我在串行端口监视器中看到它只发送前 25 个左右的值,然后停止。我的 PIC C 代码如下。它将发送第 25 个左右的值(和逗号),但不会发送任何内容,也不会发送换行符。我试图让它发送所有值,然后在末尾发送换行符。我将它们全部作为字符发送,因为我可以在 PC 端将它们转换。

//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]);

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]);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑咖 2024-09-03 09:04:32

如果缓冲区太短,您可能会过度填充缓冲区(硬件中的 FIFO)。这就是延迟解决了问题的原因 - 因为在将新字节填充到 FIFO 之前,您给了硬件时间来实际发送一些字节。

阅读控制器的数据表以了解 FIFO 有多大。可能有一种方法可以检查它有多满,从而等待少于 20 usec 的常数。

另外,我会使用 putchar 来打印单个字符,而不是 printf

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.

说谎友 2024-09-03 09:04:32

我想通了。我最终需要在打印到流后延迟。 print 语句清除之后,for 循环中的delay_us(20) 就完成了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文