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: 1750

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

Antonio Turner posted a status
3 hours ago
GarageLab posted blog posts
23 hours ago
Profile Iconleon crist, Tim Owen and Mishca de Costa joined GarageLab (arduino, electronics, robotics, hacking)
Monday
GarageLab posted a blog post

Arduino-Powered Turntables That Turn DJing Into Child’s Play

Per Holmquist has always had a finely tuned ear for music. “When I hear a song for the first time I…See More
Monday
GarageLab posted a blog post
Friday
Jean-Guillaume LALANNE posted a discussion

Smallest Arduino with BLE stack and push button integrated

Hi,I am a newbie in the maker world. I have bought a RFDUINO some month ago to play with.I did find…See More
Friday
Profile IconDavid bolles and Abhisht Yadav joined GarageLab (arduino, electronics, robotics, hacking)
Sep 11
GarageLab posted blog posts
Sep 11
chuby okafor updated their profile
Sep 10
maurice patrick commented on GarageLab's blog post 3D printer uses Arduino controller and makes food in unusual ways
"Sweet!!!!"
Sep 10
Stephen Wizowski left a comment for Stephen Wizowski
"NO... do not like this approach... AT ALL!"
Sep 10
Profile IconBernie Whalen, FRANK KWABENA, mikedaniels and 11 more joined GarageLab (arduino, electronics, robotics, hacking)
Sep 10
GarageLab posted a blog post

This Robot Crawls Through Acid, Ice and Fire

This robot, developed at Professor George M. Whitesides’ lab at Harvard University has recently…See More
Sep 10
GarageLab posted a blog post
Sep 8
Antonio Turner posted a status
Sep 8
GarageLab posted blog posts
Sep 5
Profile IconWilliam Ritchie, Chuck Untulis, David Perez and 3 more joined GarageLab (arduino, electronics, robotics, hacking)
Sep 5
GarageLab posted blog posts
Sep 3
GarageLab posted blog posts
Sep 2
Joonas Karjalainen is now a member of GarageLab (arduino, electronics, robotics, hacking)
Sep 2

© 2014   Created by Marcelo Rodrigues.

Badges  |  Report an Issue  |  Terms of Service