Tutorial: How to use the Serial port on mbed

On this tutorial, we will show how to send messages using the serial communication. The mbed has 3 serial ports that can be configured. This can be used to communicate with other ICs or for debug purposes. Also, the board has a serial port that can be emulated via USB:

Connect the mbed to your PC. Open the compiler as in http://garagelab.com/profiles/blogs/tutorial-mbed-first-steps. Open that link and import that library to the program.

Inside the main.cpp file, paste the code:

#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;
}

}
}

Open a serial terminal program, like PuTTY:

When you press u the onboard LED will get brighter, if you press d, it will fade out.

That's it! Enjoy using your board with serial communication.

References:

http://mbed.org/handbook/mbed-NXP-LPC1768

http://mbed.org/handbook/Serial

http://mbed.org/handbook/SerialPC

Views: 1923

Tags: mbed, serial, tutorial

Comment

You need to be a member of GarageLab (arduino, electronics, robotics, hacking) to add comments!

Join GarageLab (arduino, electronics, robotics, hacking)

Latest Activity

GarageLab posted a blog post

Smartwatch Projects Buttons on Your Arm

This Smartwatch was developed after Gierad Laput, a PhD student, asked “how do you make a watch…See More
9 hours ago
Profile IconGeorge Ray Learmond, Steve Barnes and Diego Leite Mendes joined GarageLab (arduino, electronics, robotics, hacking)
14 hours ago
GarageLab posted a blog post
Friday
Stu porley is now a member of GarageLab (arduino, electronics, robotics, hacking)
Thursday
GarageLab posted blog posts
Thursday
Atari Punk Console posted a status
"I backed Garag Lab Kick Starter Project the Garagino, I can't wait till I get my Garagino! https://www.kickstarter.com/projects/931487098/g"
Thursday
Profile IconDan bgarage and Joe Cannella joined GarageLab (arduino, electronics, robotics, hacking)
Wednesday
GarageLab posted blog posts
Oct 14
Profile IconSim Wilkins G. Entendez, Charles Hamilton and Colin Goldberg joined GarageLab (arduino, electronics, robotics, hacking)
Oct 13
GarageLab posted a blog post

Flexible 'Tentacle Robots' Could Aid Planetary Exploration

 Researchers are developing new types of robotic systems inspired by elephant trunks, octopus arms…See More
Oct 10
David Rios is now a member of GarageLab (arduino, electronics, robotics, hacking)
Oct 10
GarageLab posted blog posts
Oct 9
Marcelo Rodrigues is now friends with Tharaka Madhusanka and Guruprasad Balaji
Oct 7
GarageLab posted a blog post
Oct 7
GarageLab posted a blog post
Oct 6
Profile IconScorpio Imran, Joaquim Moroni da Silva, Delan and 1 more joined GarageLab (arduino, electronics, robotics, hacking)
Oct 6
GarageLab posted blog posts
Oct 3
kaisat posted a discussion

Smart developer device with wireless connectivity

We've been developing this new, small IoT device that can be used for tracking and monitoring…See More
Oct 3
Profile IconWONG CHANG LUNG and John price joined GarageLab (arduino, electronics, robotics, hacking)
Oct 3
GarageLab posted a blog post
Oct 1

© 2014   Created by Marcelo Rodrigues.

Badges  |  Report an Issue  |  Terms of Service