在线时间1465 小时
UID2037623
注册时间2013-8-30
NXP金币0
TA的每日心情 | 开心 2021-12-10 16:14 |
---|
签到天数: 1442 天 连续签到: 1 天 [LV.10]以坛为家III
金牌会员
 
- 积分
- 8291
- 最后登录
- 2021-12-10
|
本帖最后由 slotg 于 2015-7-18 20:26 编辑
在 MBED 编程环境下的串口例程如下:
- #include "mbed.h"
- DigitalOut myled(LED_GREEN);
- Serial pc(USBTX, USBRX);
- int main()
- {
- int i = 0;
- pc.printf("Hello World!\n");
- while (true) {
- wait(0.5f); // wait a small period of time
- pc.printf("%d \n", i); // print the value of variable i
- i++; // increment the variable
- myled = !myled; // toggle a led
- }
- }
复制代码
程序运行后会通过虚拟串口打印讯息,每一次打印时会将板底的绿色 LED 状态反向。
FRDM-K64F 板是使用了编号为 MK64FN1M0VLL12 100pin LQFP 包装的 MCU,这个包装有 5 组串口,这 5 组串口在 FRDM-K64F 板上使用的分配如下:
UART0_RX PTB16 OpenSDAv2 (虚拟串口)
UART0_TX PTB17 OpenSDAv2 (虚拟串口)
UART1_RX PTC3 (D7)
UART1_TX PTC4 (D9)
UART2_RX PTD2 (D11)
UART2_TX PTD3 (D12)
UART3_RX PTC16 (D0) RX
UART3_TX PTC17 (D1) TX
UART4_RX PTC14 (BT_TX)
UART4_TX PTC15 (BT_RX)
其中的 UART4 是连接到 J199 接头,这个接头是保留给蓝芽模块使用。
MBED 对 FRDM-K64F 的 UART0 管脚定义为
USBTX = PTB17
USBRX = PTB16
因此程序中
Serial pc(USBTX, USBRX);
与
Serial pc(PTB17, PTB16);
的功能是一样的。
相同的道理,假如我们要使用其他的串口功能也可以用相同的方式定义,比如说要使用 UART3 那么我们修改程序如下:
- #include "mbed.h"
- DigitalOut myled(LED_GREEN);
- //Serial pc(USBTX, USBRX);
- Serial pc(PTB17, PTB16);
- Serial uart3(PTC17, PTC16);
- int main()
- {
- int i = 0;
- pc.printf("Hello World!\n");
- uart3.printf("Hello World! UART3\n");
- while (true) {
- wait(0.5f); // wait a small period of time
- pc.printf("%d \n", i); // print the value of variable i
- uart3.printf("%d \n", i);
- i++; // increment the variable
- myled = !myled; // toggle a led
- }
- }
复制代码
运行结果
使用 UART1
Serial uart1(PTC4, PTC3);
使用 UART2
Serial uart2(PTD3, PTD2);
在程序中使用 4 个串口:
- #include "mbed.h"
- DigitalOut myled(LED_GREEN);
- //Serial pc(USBTX, USBRX);
- Serial pc(PTB17, PTB16);
- Serial uart1(PTC4, PTC3);
- Serial uart2(PTD3, PTD2);
- Serial uart3(PTC17, PTC16);
- int main()
- {
- int i = 0;
- pc.printf("Hello World!\n");
- uart1.printf("Hello World! UART1\n");
- uart2.printf("Hello World! UART2\n");
- uart3.printf("Hello World! UART3\n");
- while (true) {
- wait(0.5f); // wait a small period of time
- pc.printf("%d \n", i); // print the value of variable i
- uart1.printf("%d \n", i);
- uart2.printf("%d \n", i);
- uart3.printf("%d \n", i);
- i++; // increment the variable
- myled = !myled; // toggle a led
- }
- }
复制代码
除了虚拟串口之外,我连接了 2 条串口线同时显示 3 个串口输出。
FRDM-K64F 的管脚配置
FRDM-K64F 原理图
FRDM-K64F_SCH.pdf
(208.38 KB, 下载次数: 100)
|
评分
-
查看全部评分
|