[资料分享与下载] FRDM-K64F+MBED串口功能-1

[复制链接]
1384|7
 楼主| ccw1986 发表于 2015-10-9 07:48 | 显示全部楼层 |阅读模式
在 MBED 编程环境下的串口例程如下:
  1. #include "mbed.h"

  2. DigitalOut myled(LED_GREEN);
  3. Serial pc(USBTX, USBRX);

  4. int main()
  5. {
  6.     int i = 0;
  7.     pc.printf("Hello World!\n");

  8.     while (true) {
  9.         wait(0.5f); // wait a small period of time
  10.         pc.printf("%d \n", i); // print the value of variable i
  11.         i++; // increment the variable
  12.         myled = !myled; // toggle a led
  13.     }
  14. }


 楼主| ccw1986 发表于 2015-10-9 07:49 | 显示全部楼层
程序运行后会通过虚拟串口打印讯息,每一次打印时会将板底的绿色 LED 状态反向。

P01.JPG (22.04 KB, 下载次数: 0)
下载附件
2015-7-18 20:16 上传




FRDM-K64F 板是使用了编号为 MK64FN1M0VLL12 100pin LQFP 包装的 MCU,这个包装有 5 组串口,这 5 组串口在 FRDM-K64F 板上使用的分配如下:
  1. UART0_RX        PTB16   OpenSDAv2 (虚拟串口)
  2. UART0_TX        PTB17   OpenSDAv2 (虚拟串口)

  3. UART1_RX        PTC3    (D7)
  4. UART1_TX        PTC4    (D9)

  5. UART2_RX        PTD2    (D11)
  6. UART2_TX        PTD3    (D12)

  7. UART3_RX        PTC16   (D0) RX
  8. UART3_TX        PTC17   (D1) TX

  9. UART4_RX        PTC14   (BT_TX)
  10. UART4_TX        PTC15   (BT_RX)


 楼主| ccw1986 发表于 2015-10-9 07:49 | 显示全部楼层
其中的 UART4 是连接到 J199 接头,这个接头是保留给蓝芽模块使用。


  1. MBED 对 FRDM-K64F 的 UART0 管脚定义为
  2. USBTX = PTB17
  3. USBRX = PTB16
因此程序中
Serial pc(USBTX, USBRX);

Serial pc(PTB17, PTB16);
的功能是一样的。

相同的道理,假如我们要使用其他的串口功能也可以用相同的方式定义,比如说要使用 UART3 那么我们修改程序如下:
 楼主| ccw1986 发表于 2015-10-9 07:50 | 显示全部楼层
  1. #include "mbed.h"

  2. DigitalOut myled(LED_GREEN);
  3. //Serial pc(USBTX, USBRX);
  4. Serial pc(PTB17, PTB16);
  5. Serial uart3(PTC17, PTC16);

  6. int main()
  7. {
  8.     int i = 0;
  9.     pc.printf("Hello World!\n");
  10.     uart3.printf("Hello World! UART3\n");

  11.     while (true) {
  12.         wait(0.5f); // wait a small period of time
  13.         pc.printf("%d \n", i); // print the value of variable i
  14.         uart3.printf("%d \n", i);
  15.         i++; // increment the variable
  16.         myled = !myled; // toggle a led
  17.     }
  18. }
 楼主| ccw1986 发表于 2015-10-9 07:50 | 显示全部楼层
运行结果



使用 UART1
Serial uart1(PTC4, PTC3);



使用 UART2
Serial uart2(PTD3, PTD2);



在程序中使用 4 个串口:
 楼主| ccw1986 发表于 2015-10-9 07:51 | 显示全部楼层
  1. #include "mbed.h"

  2. DigitalOut myled(LED_GREEN);
  3. //Serial pc(USBTX, USBRX);
  4. Serial pc(PTB17, PTB16);
  5. Serial uart1(PTC4, PTC3);
  6. Serial uart2(PTD3, PTD2);
  7. Serial uart3(PTC17, PTC16);

  8. int main()
  9. {
  10.     int i = 0;
  11.     pc.printf("Hello World!\n");
  12.     uart1.printf("Hello World! UART1\n");
  13.     uart2.printf("Hello World! UART2\n");
  14.     uart3.printf("Hello World! UART3\n");

  15.     while (true) {
  16.         wait(0.5f); // wait a small period of time
  17.         pc.printf("%d \n", i); // print the value of variable i
  18.         uart1.printf("%d \n", i);
  19.         uart2.printf("%d \n", i);
  20.         uart3.printf("%d \n", i);
  21.         i++; // increment the variable
  22.         myled = !myled; // toggle a led
  23.     }
  24. }
除了虚拟串口之外,我连接了 2 条串口线同时显示 3 个串口输出。





FRDM-K64F 的管脚配置



奥德赛 发表于 2015-10-9 08:14 | 显示全部楼层
这个MBED的串口真的挺实用
zhao16881159 发表于 2016-4-1 09:35 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

84

主题

925

帖子

6

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