在线时间261 小时
UID3145308
注册时间2015-7-11
NXP金币104
TA的每日心情 | 奋斗 2018-7-24 14:23 |
---|
签到天数: 98 天 连续签到: 1 天 [LV.6]常住居民II
金牌会员
 
- 积分
- 2236
- 最后登录
- 2024-9-19
|
本帖最后由 samplecode 于 2016-8-11 22:12 编辑
mbed的serial使用:使用的是k64f板子上的连着usb的那个串口,板子上留的连蓝牙的地方也是一个串口。
定义一个串口;
[color=rgba(0, 0, 0, 0.6)]Serial usb(USBTX, USBRX); //默认波特率是9600,USBRX([color=rgba(0, 0, 0, 0.6)]PTB16[color=rgba(0, 0, 0, 0.6)])[color=rgba(0, 0, 0, 0.6)],USBTX([color=rgba(0, 0, 0, 0.6)]PTB17[color=rgba(0, 0, 0, 0.6)])是连着USB的串口。
[color=rgba(0, 0, 0, 0.6)]Serial ptc(PTC16, PTC15); //(tx,rx)这是连连蓝牙的地方串口
设置波特率:
usb.baud(115200); //设置波特率为115200
下面用这两个串口来实验一下:
- /**
- *usb是连板子上opensda那个的
- *ptc练的是板子上留的那个连蓝牙的,用ch340g连电脑上
- **/
- #include "mbed.h"
- Serial ptc(PTC15, PTC14);
- Serial usb(USBTX, USBRX);
- DigitalOut myled(LED1);
- int main()
- {
- while(1) {
- myled = 1;
- ptc.printf("ptc on!!\r\n");
- usb.printf("usb on!!\r\n");
- wait(1);
- myled = 0;
- ptc.printf("ptc off!!\r\n");
- usb.printf("usb off!!\r\n");
- wait(1);
- }
- }
复制代码
从串口读取一个字符:
pc.getc();
串口输出;
pc.putc('R');
使用pc.printf("hello");也可以。
- #include "mbed.h"
- DigitalOut led1(LED_RED);
- DigitalOut led2(LED_GREEN);
- DigitalOut led3(LED_BLUE);
- Serial pc(USBTX, USBRX); // tx, rx
-
- int main() {
- pc.printf("Hello World!\n");
- while(1) {
- switch(pc.getc()){
- case 'r':
- led1=0;
- led2=led3=1;
- pc.putc('R');
- break;
- case 'g':
- led2=0;
- led1=led3=1;
- pc.putc('G');
- break;
- case 'b':
- led3=0;
- led1=led2=1;
- pc.putc('B');
- break;
- }
- }
- }
复制代码 这个用的accessport软件做串口调试的
slotg同学的串口使用心得
【FRDM-K64F+MBED】串口功能-1
https://www.nxpic.org.cn/module/ ... amp;fromuid=3145308
【FRDM-K64F+MBED】串口功能-2
https://www.nxpic.org.cn/module/ ... amp;fromuid=3145308
|
|