【前言】
在上一篇,我实现了LED灯的配置与程序的烧录,这一篇将分享如何使用ebed os来实现串口输出。
【原理图】
1、从开发板的原理图我看出,开发板上UART0是TX、RX分别为P0-1,P0-0
2、打开工程,我们先进行串口0的初始化,并配置输入与输出。
首先找到UnBufferedSerial.h,对UnbufferedSerial的定,在他里面定义如下:
* @param tx Transmit pin * @param rx Receive pin * @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE) * * @note * Either tx or rx may be specified as NC if unused */ UnbufferedSerial( PinName tx, PinName rx, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE );
因此我们对他的实例化为
UnbufferedSerial uart0(UART0_TX, UART0_RX,115200);
其中TX、RX是在板子的工程中给予的宏定义的,分别为:
然后就是如何输出信息,在实例中的有如下函数
/** Write the contents of a buffer to a file * * Blocks until all data is written * * @param buffer The buffer to write from * @param size The number of bytes to write * @return The number of bytes written */ ssize_t write(const void *buffer, size_t size) override;
因此我在main.cpp中全部代码如下:
#include "mbed.h" #include "platform/mbed_thread.h" // Blinking rate in milliseconds #define BLINKING_RATE_MS 200 UnbufferedSerial uart0(UART0_TX, UART0_RX,115200); int main() { // uart0.baud(115200); // Initialise the digital pin LED1 as an output DigitalOut led(LED1); DigitalOut led1(LED2); const char* message = "Hello from UART0!\n"; uart0.write(message, strlen(message)); while (true) { uart0.write(message, strlen(message)); led = !led; thread_sleep_for(BLINKING_RATE_MS); led1 = !led1; thread_sleep_for(BLINKING_RATE_MS); } }
【实现效果】
烧写代码到开发板,接上串口助手,可以看到如下信息:
【总结】
max32625有mbedOS的支持,编程还是非常简单的。