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(PinName tx, PinName rx, const char *name=NULL); | | | | void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1); | 设置Uart传输的格式,包括一个字长的位数、奇偶检验的方法、停止位的位数,默认为一个字长为8位,无奇偶检验,1位停止位 | | | | | void attach(void (*fptr)(void), IrqType type=RxIrq); | | void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC); | 设置Uart的流控方法,其目标是提高数据发送的可靠性,流控方法有无流控,RTS流控,CTS流控,RTSCTS流控,后面两个常数为流控的管脚设置 | | | | | int printf(const char* format, ...) | 格式化Uart的输出,参数等同标准C的printf | int scanf(const char* format, ...); | |
以下是我的程序:
#include "mbed.h"
Serial pc(USBTX, USBRX);
int main() {
while(1)
{
pc.printf("Hello World\n");
wait(0.2);
}
}
以下是编译通过的截图:
以下是输出结果。总体感觉mbed上手极速地快。
|