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

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 IconWayne Eddy and atherine joined GarageLab (arduino, electronics, robotics, hacking)
17 hours ago
Profile IconDinko, Bukasa Tshilombo, simataempat and 1 more joined GarageLab (arduino, electronics, robotics, hacking)
Friday
ANGEL RUIZ replied to ANGEL RUIZ's discussion Can this Xyron be used as a CNC router
"Here"
Thursday
ANGEL RUIZ posted a discussion

Can this Xyron be used as a CNC router

I don't have the money to afford a CNC router so I was wondering if I can convert this into…See More
Thursday
GarageLab posted a blog post

HAVOC: NASA’s proposed cloud city over Venus

The surface of Venus is a notorious hell-hole. If an atmospheric pressure roughly 90 times greater…See More
Thursday
roberto premoli's photo was featured

premobox25-per-web-001

premoboard+cubieboard+hdd2.5''features:-3LAN-5USB-2WIFI-500GB HDD…
Dec 16
Veritus's photo was featured

Battery Pack

Created a closed system of power in a wearable back pack. Back pack had 5 power outlets each at 220…
Dec 16
NELU NASTASOIU's photo was featured
Dec 16
wesley wortman's photo was featured
Dec 16
GarageLab posted blog posts
Dec 16
sunny lai left a comment for sunny lai
"thanks for hearing from you, it is my pleasure"
Dec 15
Enahoro Olori updated their profile
Dec 15
Profile IconRobert Sitton, Mr Abraham Akim and Magnus Nordin joined GarageLab (arduino, electronics, robotics, hacking)
Dec 15
GarageLab posted blog posts
Dec 12
Profile Icongiacomo, dan rules and Jay Thornton joined GarageLab (arduino, electronics, robotics, hacking)
Dec 11
GarageLab posted a blog post
Dec 10
GarageLab replied to Lou Liberatore's discussion LiquidCrystal_I2C library in the group Arduino
" Hi Lou Lberatone, I think you Hi Lou Lberatone, I think you probably are using the libraries…"
Dec 10
GarageLab joined Marcelo Rodrigues's group
Thumbnail

Arduino

Group for the Arduino communitySee More
Dec 10
GarageLab posted blog posts
Dec 9
Profile Iconbenjamin black and sunny lai joined GarageLab (arduino, electronics, robotics, hacking)
Dec 9

© 2014   Created by Marcelo Rodrigues.

Badges  |  Report an Issue  |  Terms of Service