怎样把整型通过UART打印在电脑终端

[复制链接]
1178|2
 楼主| zhaoruzhe 发表于 2016-12-26 15:13 | 显示全部楼层 |阅读模式
前两天卤煮遇到一个外国小伙伴问怎样把他从传感器获取的整型数据通过PSoC4的UART打印到电脑的终端上。卤煮脑子慢,首先觉得很简单,直接UartPutInt呗,然后愣了愣,确认了下PSoC其实根本没有这个函数...

整型的0.....用PutChar直接打在电脑上打不出来啊,必须转它的ASC II码,那么,直接加0x30!
OK, Done!
马上问题又来了,如果是0-9的数字,加0x30固然方便。可是如果是大于9的数字怎么办? 算来算去有点烦的。
不过觉得肯定有前辈研究过这个问题,然后果然...google到早有小伙伴想到了,用stdio.h里的sprintf就可以解决了!试了一下果然好用!
现在心里就想吐槽问这个问题的外国小伙伴: 你家也是可以用google的...


当然,吐槽不是重点啦,重点是想抛砖引玉,想知道有没有其他好方法~

  1. #include <project.h>
  2. #include <stdio.h>

  3. int main()
  4. {
  5.     uint8 input;
  6.     char Temp[2];
  7.     CyDelay(300);// Avoid messy data at reset.
  8.     /* Start SCB (UART mode) operation */
  9.     UART_Start();

  10.     UART_UartPutString("\r\n***********************************************************************************\r\n");
  11.     UART_UartPutString("\r\n");

  12.     for (;;)
  13.     {

  14.         for(input=0u; input<=100u; ++input)
  15.         {
  16.             
  17.             UART_UartPutChar(input+0x30);
  18.             UART_UartPutString("\r\n");

  19.             sprintf(Temp, "%d", input);

  20.             UART_UartPutString(Temp);
  21.             UART_UartPutString("\r\n\r\n");
  22.             CyDelay(100);
  23.         }

  24.     }
  25. }


给力芯片 发表于 2016-12-26 21:55 | 显示全部楼层
看了你的程序,认为不用加stdio 都可以的
 楼主| zhaoruzhe 发表于 2016-12-27 15:40 | 显示全部楼层
给力芯片 发表于 2016-12-26 21:55
看了你的程序,认为不用加stdio 都可以的

谢谢回复!
测试了一下:
1. 如果不加“#include <stdio.h>”的话,正如你所认为,结果也可以正确打印。但是会有两个warning,如下所示。
main.c:26:13: warning: implicit declaration of function 'sprintf' [-Wimplicit-function-declaration]
main.c:26:13: warning: incompatible implicit declaration of built-in function 'sprintf'

2. 如果不加“#include <stdio.h>”和“sprintf(Temp, "%d", input);”, 直接打印UART_UartPutString(input),则显示乱码。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:小搬运工

26

主题

289

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部