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

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

Profile IconUmer Shehzad, FRANK KWABENA, hamed bigdeli and 1 more joined GarageLab (arduino, electronics, robotics, hacking)
5 hours ago
GarageLab posted blog posts
14 hours ago
Profile IconAllan Paculan and Nilesh Parmar joined GarageLab (arduino, electronics, robotics, hacking)
yesterday
keith vasilakes replied to Andy S's discussion Arduino MEGA boolean?
"sounds like an uninitialized variable when used in your task, what ever that is"
Wednesday
GarageLab posted blog posts
Wednesday
Fajer commented on GarageLab's blog post Tutorial: Make your line following robot
"Hi there can u stop the motors if there are no more lines??"
Tuesday
Profile IconDivergent Programmer, avengineer01, Fajer and 3 more joined GarageLab (arduino, electronics, robotics, hacking)
Tuesday
GarageLab posted blog posts
Tuesday
David Hay posted a status
"Just finished the first phaseof a homebrewing project. Holds temps & is consistent. Check it out: www.callaid-taigh-gruide.com"
Monday
GarageLab posted blog posts
Oct 24
GarageLab posted a blog post

Scientists Create a Real Hoverboard

Ever since the fictional hoverboards in 1989’s Back to the Future Part II children (and adults)…See More
Oct 22
Colin Goldberg posted a discussion

Can I use my Samsung Tab 4 tablet as a usb host to a beaglebone board black?

Hi,As a newbie to this space, I would like to know what to think about before I try to connect my…See More
Oct 21
Antonio Turner posted a status
Oct 21
GarageLab posted blog posts
Oct 21
Profile IconGeorge Ray Learmond, Steve Barnes and Diego Leite Mendes joined GarageLab (arduino, electronics, robotics, hacking)
Oct 20
GarageLab posted a blog post
Oct 17
Stu porley is now a member of GarageLab (arduino, electronics, robotics, hacking)
Oct 16
GarageLab posted blog posts
Oct 16
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"
Oct 15
Profile IconDan bgarage and Joe Cannella joined GarageLab (arduino, electronics, robotics, hacking)
Oct 15

© 2014   Created by Marcelo Rodrigues.

Badges  |  Report an Issue  |  Terms of Service