打印
[Atmel]

mbed环境下SAMR21开发板3:串口通讯

[复制链接]
3237|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
mbed串口上手也很方便哈。只要包括mbed.h函数即可,一些初始化在mbed.h中已经定义好了。

以下是mbed.h:
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MBED_H
#define MBED_H

#define MBED_LIBRARY_VERSION 113

#include "platform.h"

// Useful C libraries
#include <math.h>
#include <time.h>

// mbed Debug libraries
#include "mbed_error.h"
#include "mbed_interface.h"

// mbed Peripheral components
#include "DigitalIn.h"
#include "DigitalOut.h"
#include "DigitalInOut.h"
#include "BusIn.h"
#include "BusOut.h"
#include "BusInOut.h"
#include "PortIn.h"
#include "PortInOut.h"
#include "PortOut.h"
#include "AnalogIn.h"
#include "Ana**ut.h"
#include "PwmOut.h"
#include "Serial.h"
#include "SPI.h"
#include "SPISlave.h"
#include "I2C.h"
#include "I2CSlave.h"
#include "Ethernet.h"
#include "CAN.h"
#include "RawSerial.h"

// mbed Internal components
#include "Timer.h"
#include "Ticker.h"
#include "Timeout.h"
#include "LowPowerTimeout.h"
#include "LowPowerTicker.h"
#include "LowPowerTimer.h"
#include "LocalFileSystem.h"
#include "InterruptIn.h"
#include "wait_api.h"
#include "sleep_api.h"
#include "rtc_time.h"

using namespace mbed;
using namespace std;

#endif
可见只要你把API打对了,就可以工作了

而Serial.h中又把接收继承父类。

/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MBED_SERIAL_H
#define MBED_SERIAL_H

#include "platform.h"

#if DEVICE_SERIAL

#include "Stream.h"
#include "SerialBase.h"
#include "serial_api.h"

namespace mbed {

/** A serial port (UART) for communication with other serial devices
*
* Can be used for Full Duplex communication, or Simplex by specifying
* one pin as NC (Not Connected)
*
* Example:
* @code
* // Print "Hello World" to the PC
*
* #include "mbed.h"
*
* Serial pc(USBTX, USBRX);
*
* int main() {
*     pc.printf("Hello World\n");
* }
* @endcode
*/
class Serial : public SerialBase, public Stream {

public:
#if DEVICE_SERIAL_ASYNCH
    using SerialBase::read;
    using SerialBase::write;
#endif

    /** Create a Serial port, connected to the specified transmit and receive pins
     *
     *  @param tx Transmit pin
     *  @param rx Receive pin
     *
     *  @note
     *    Either tx or rx may be specified as NC if unused
     */
    Serial(PinName tx, PinName rx, const char *name=NULL);

protected:
    virtual int _getc();
    virtual int _putc(int c);
};

} // namespace mbed

#endif

#endif
而实际操作只要掌握基本函数即可:


类名
方法
用途
Serial
Serial(PinName tx, PinName rx, const char *name=NULL);
构造函数,把tx,rx设成Uart的输出输入管脚
void baud(int baudrate);
设置Uart的波特率,默认为9600
void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1);
设置Uart传输的格式,包括一个字长的位数、奇偶检验的方法、停止位的位数,默认为一个字长为8位,无奇偶检验,1位停止位
int readable();
返回Uart是否有数据到达
int writeable();
返回Uart是否还有空间进行数据发送
void attach(void (*fptr)(void), IrqType type=RxIrq);
设置Uart中断是需要执行的用户自定义函数
void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC);
设置Uart的流控方法,其目标是提高数据发送的可靠性,流控方法有无流控,RTS流控,CTS流控,RTSCTS流控,后面两个常数为流控的管脚设置
int getc()
从Uart读取一个字符
int putc(int c);
向Uart发送一个字符
int printf(const char* format, ...)
格式化Uart的输出,参数等同标准C的printf
int scanf(const char* format, ...);
格式化Uart的输入,参数等同标准C的scanf




以下是我的程序:
#include "mbed.h"



Serial pc(USBTX, USBRX);

int main() {
     while(1)
     {
     pc.printf("Hello World\n");
     wait(0.2);
     }
}
以下是编译通过的截图:



以下是输出结果。总体感觉mbed上手极速地快。


相关帖子

沙发
shaoziyang| | 2016-2-10 21:02 | 只看该作者
R21的ADC采样波动比较大,可能是什么原因?

使用特权

评论回复
板凳
ddllxxrr|  楼主 | 2016-2-11 11:24 | 只看该作者
shaoziyang 发表于 2016-2-10 21:02
R21的ADC采样波动比较大,可能是什么原因?

可能是外围电路不稳,或ADC采得太快,再有分辨率设低了

使用特权

评论回复
地板
枫吹落| | 2018-8-8 18:06 | 只看该作者
您好,请问宏定义中DEVICE_SERIAL和DEVICE_SERIAL_ASYNCH有什么功能上的区别吗?串口本来就是异步的呀?

使用特权

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

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2398

主题

6956

帖子

67

粉丝