PIC C - USB_CDC_GETC() 和检索字符串

发布于 2024-08-27 08:27:04 字数 813 浏览 7 评论 0原文

我正在使用 PIC C 对 PIC18F4455 微控制器进行编程。我正在使用 USB_CDC.h 头文件。我的计算机上有一个程序发送一个字符串,例如“W250025”。但是,当我使用 usb_cdc_getc() 获取第一个字符时,它会冻结。有时程序只发送“T”,所以我真的只想获取第一个字符。

为什么我的代码永远不会执行过去的 receive=usb_cdc_getc();当我发送“W250025”时?

if (usb_cdc_kbhit())
      {
         //printf(lcd_putc, "Check 3"); delay_ms(3000); printf(lcd_putc, "\f");
         received = usb_cdc_getc();
         printf(lcd_putc, "Received "); lcd_putc(received); delay_ms(3000); printf(lcd_putc, "\f");
         if (received == 'W'){   //waveform
            disable_interrupts(INT_TIMER1);
            set_adc_channel(0);
            load_and_print_array(read_into_int(), read_into_int());}
         else if (received == 'T'){ //temperature
            set_adc_channel(1);
            enable_interrupts(INT_TIMER1);}
      }

I'm programming a PIC18F4455 Microcontroller using PIC C. I'm using the USB_CDC.h header file. I have a program on the computer sending a string such as "W250025". However, when I use usb_cdc_getc() to get the first char, it freezes. Sometimes the program sends only 'T', so I really want to just get the first character.

Why does my code never execute past received=usb_cdc_getc(); when I send "W250025"?

if (usb_cdc_kbhit())
      {
         //printf(lcd_putc, "Check 3"); delay_ms(3000); printf(lcd_putc, "\f");
         received = usb_cdc_getc();
         printf(lcd_putc, "Received "); lcd_putc(received); delay_ms(3000); printf(lcd_putc, "\f");
         if (received == 'W'){   //waveform
            disable_interrupts(INT_TIMER1);
            set_adc_channel(0);
            load_and_print_array(read_into_int(), read_into_int());}
         else if (received == 'T'){ //temperature
            set_adc_channel(1);
            enable_interrupts(INT_TIMER1);}
      }

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

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

发布评论

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

评论(2

驱逐舰岛风号 2024-09-03 08:27:04

我不知道 PIC 微控制器的具体情况,但是,假设 usb_cdc_getc 的行为类似于正常的 getc,最可能的原因是您的字符没有到达函数,通常会阻塞。您要发送换行符吗?也可能是硬件问题,字符首先无法到达您的 uC。

如果是前者,而不是所需的行为,则可能存在等效的非阻塞 getch

I don't know the specifics of the PIC microcontroller, but, assuming that usb_cdc_getc behaves like the normal getc, the most likely cause is that your characters aren't reaching the function, which normally blocks. Are you sending a newline? It could also be a hardware problem where the characters aren't reaching your uC in the first place.

If it is the former, and not the desired behavior there likely is a nonblocking getch equivalent.

倾听心声的旋律 2024-09-03 08:27:04

也许这有帮助:

usb_cdc_getc() - 从接收缓冲区获取一个字符。如果
接收缓冲区中没有数据,它将等待
接收缓冲区中有数据。如果你不想
要在无限循环中等待,请先使用 usb_cdc_kbhit()
在调用usb_cdc_getc()之前检查是否有数据。"

来自 https://github.com/tkrworks/PICnome-Firmware/blob/master/usb_cdc.h

Maybe this helps:

usb_cdc_getc() - Gets a character from the receive buffer. If
there is no data in the receive buffer it will wait until
there is data in the receive buffer. If you do not want
to wait in an infinit loop, use usb_cdc_kbhit() first to
check if there is data before calling usb_cdc_getc()."

from https://github.com/tkrworks/PICnome-Firmware/blob/master/usb_cdc.h

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