打印
[资料分享与下载]

FRDM-K64F+MBED串口功能-1

[复制链接]
1031|7
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
ccw1986|  楼主 | 2015-10-9 07:48 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
在 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
    }
}


相关帖子

沙发
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 板上使用的分配如下:
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)


使用特权

评论回复
板凳
ccw1986|  楼主 | 2015-10-9 07:49 | 只看该作者
其中的 UART4 是连接到 J199 接头,这个接头是保留给蓝芽模块使用。


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

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

相同的道理,假如我们要使用其他的串口功能也可以用相同的方式定义,比如说要使用 UART3 那么我们修改程序如下:

使用特权

评论回复
地板
ccw1986|  楼主 | 2015-10-9 07:50 | 只看该作者
#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
    }
}

使用特权

评论回复
5
ccw1986|  楼主 | 2015-10-9 07:50 | 只看该作者
运行结果



使用 UART1
Serial uart1(PTC4, PTC3);



使用 UART2
Serial uart2(PTD3, PTD2);



在程序中使用 4 个串口:

使用特权

评论回复
6
ccw1986|  楼主 | 2015-10-9 07:51 | 只看该作者
#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 的管脚配置



使用特权

评论回复
7
奥德赛| | 2015-10-9 08:14 | 只看该作者
这个MBED的串口真的挺实用

使用特权

评论回复
8
zhao16881159| | 2016-4-1 09:35 | 只看该作者
不错

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

84

主题

925

帖子

6

粉丝