The mbed Microcontroller can communicate with a host PC through a "USB Virtual Serial Port" over the same USB cable that is used for programming.
This enables you to:
#include "mbed.h"
Serial pc(USBTX, USBRX); // tx, rx
int main() {
pc.printf("Hello World!\n");
}
Your mbed Microcontroller can appear on your computer as a serial port. On Mac and Linux, this will happen by default. For Windows, you need to install a driver:
It is common to use a terminal application on the host PC to communicate with the mbed Microcontroller. This allows the mbed Microcontroller to print to your PC screen, and for you to send characters back.
Some terminal programs (e.g. TeraTerm) list the available serial ports by name. However, if you do need to know the identity of the serial port so that you can attach a terminal or an application to it:
Communication over the USB Serial port simply uses the standard Serial Interface, specifying the internal (USBTX, USBRX) pins to connect to the Serial Port routed over USB.
The Serial Interface defaults to a 9600 baud standard serial connection (8 bits, 1 stop bit, no parity), so your host program should be set to the same settings. If you want to communicate at a different standard baud rate, ensure you modify the settings of both the Serial Interface and the Host PC application!
#include "mbed.h"
Serial pc(USBTX, USBRX);
int main() {
pc.printf("Echoes back to the screen anything you type\n");
while(1) {
pc.putc(pc.getc());
}
}
#include "mbed.h"
Serial pc(USBTX, USBRX); // tx, rx
PwmOut led(LED1);
float brightness = 0.0;
int main() {
pc.printf("Press 'u' to turn LED1 brightness up, 'd' to turn it down\n");
while(1) {
char c = pc.getc();
if((c == 'u') && (brightness < 0.5)) {
brightness += 0.01;
led = brightness;
}
if((c == 'd') && (brightness > 0.0)) {
brightness -= 0.01;
led = brightness;
}
}
}
#include "mbed.h"
Serial pc(USBTX, USBRX);
Serial uart(p28, p27);
DigitalOut pc_activity(LED1);
DigitalOut uart_activity(LED2);
int main() {
while(1) {
if(pc.readable()) {
uart.putc(pc.getc());
pc_activity = !pc_activity;
}
if(uart.readable()) {
pc.putc(uart.getc());
uart_activity = !uart_activity;
}
}
}
#include "mbed.h"
int main() {
printf("Hello World!\n");
}
If you have having difficulties with USB serial communication:
If you have any problems, or think this tutorial could be improved, please tell us in the Forum!
Please login to post comments.