[Kinetis] 【FRDM-K64F+MBED】串口功能-1

[复制链接]
 楼主| 发表于 2015-12-21 21:22 | 显示全部楼层 |阅读模式
在 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. }


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



FRDM-K64F 板是使用了编号为 MK64FN1M0VLL12 100pin LQFP 包装的 MCU,这个包装有 5 组串口,这 5 组串口在 FRDM-K64F 板上使用的分配如下:
 楼主| 发表于 2015-12-21 21:23 | 显示全部楼层
  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)

  11. 其中的 UART4 是连接到 J199 接头,这个接头是保留给蓝芽模块使用。


 楼主| 发表于 2015-12-21 21:24 | 显示全部楼层




MBED 对 FRDM-K64F 的 UART0 管脚定义为
  1. USBTX = PTB17
  2. USBRX = PTB16

  3. 因此程序中
  4. Serial pc(USBTX, USBRX);

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


 楼主| 发表于 2015-12-21 21:25 | 显示全部楼层
  1. 相同的道理,假如我们要使用其他的串口功能也可以用相同的方式定义,比如说要使用 UART3 那么我们修改程序如下:
  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. }
 楼主| 发表于 2015-12-21 21:26 | 显示全部楼层
运行结果



使用 UART1
Serial uart1(PTC4, PTC3);



使用 UART2
 楼主| 发表于 2015-12-21 21:27 | 显示全部楼层
Serial uart2(PTD3, PTD2);



在程序中使用 4 个串口:
 楼主| 发表于 2015-12-21 21:27 | 显示全部楼层
  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.     }
 楼主| 发表于 2015-12-21 21:28 | 显示全部楼层
除了虚拟串口之外,我连接了 2 条串口线同时显示 3 个串口输出。





FRDM-K64F 的管脚配置
 楼主| 发表于 2015-12-21 21:29 | 显示全部楼层





FRDM-K64F 原理图
您需要登录后才可以回帖 登录 | 注册

本版积分规则

36

主题

363

帖子

1

粉丝
快速回复 返回顶部 返回列表