FRDM-KL25z simple tutorial example of controlling the RGB LED to flash different colors, and sending status messages to the host via the pc serial connection. Includes extensive explanatory comments.

Dependencies:   mbed

Committer:
cweems
Date:
Thu Jul 18 14:47:52 2013 +0000
Revision:
0:209b9385be7b
Child:
1:c07d099d8e31
FRDM-KL25z simple example of controlling the RGB LED and using the pc serial port, with extensive documenting comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cweems 0:209b9385be7b 1 /* Test of FRDM-KL25z RGB LED and Serial out to PC.
cweems 0:209b9385be7b 2 * This program blinks the LED through the series of colors
cweems 0:209b9385be7b 3 * red, greeen, and blue, displaying each for 1 second, with
cweems 0:209b9385be7b 4 * a 1 second off period between each color. As it makes each
cweems 0:209b9385be7b 5 * change in the status of the LED, it sends a message to the
cweems 0:209b9385be7b 6 * pc serial port over the USB programming connection. The
cweems 0:209b9385be7b 7 * pattern repeats 5 times, then two short white flashes are
cweems 0:209b9385be7b 8 * displayed by turning all threee LEDs on and off at once, and
cweems 0:209b9385be7b 9 * a final message is sent to the serial port.
cweems 0:209b9385be7b 10 *
cweems 0:209b9385be7b 11 * Tested from OSX 10.8.4. To find the serial port device,
cweems 0:209b9385be7b 12 * launch Terminal, then type:
cweems 0:209b9385be7b 13 *
cweems 0:209b9385be7b 14 * ls /dev/tty.usbmodem*
cweems 0:209b9385be7b 15 *
cweems 0:209b9385be7b 16 * It will return something like /dev/tty.usbmodem1d22
cweems 0:209b9385be7b 17 * To change Terminal to connect to the serial port, use the screen
cweems 0:209b9385be7b 18 * command, specifying the path returned by ls and the baud rate:
cweems 0:209b9385be7b 19 *
cweems 0:209b9385be7b 20 * screen /dev/tty.usbmodem1d22 9600
cweems 0:209b9385be7b 21 *
cweems 0:209b9385be7b 22 * As a shortcut, if you have just one serial device connected, you
cweems 0:209b9385be7b 23 * can skip the first command and type the second command as far as
cweems 0:209b9385be7b 24 * tty.usb and hit the tab key and it will autocomplete.
cweems 0:209b9385be7b 25 * Once screen is running, type Ctrl-a then ? for a list of available
cweems 0:209b9385be7b 26 * commands.
cweems 0:209b9385be7b 27 */
cweems 0:209b9385be7b 28
cweems 0:209b9385be7b 29 #include "mbed.h"
cweems 0:209b9385be7b 30
cweems 0:209b9385be7b 31 DigitalOut redLED(LED_RED); // Define digital outputs for each LED color
cweems 0:209b9385be7b 32 DigitalOut greenLED(LED_GREEN);
cweems 0:209b9385be7b 33 DigitalOut blueLED(LED_BLUE);
cweems 0:209b9385be7b 34 Serial pc(USBTX, USBRX); // Declare the serial port
cweems 0:209b9385be7b 35
cweems 0:209b9385be7b 36 int main() {
cweems 0:209b9385be7b 37 pc.baud (9600); // Set the baud rate for the serial connection
cweems 0:209b9385be7b 38 redLED = 1; // This a common cathode RGB LED, with the cathodes connected
cweems 0:209b9385be7b 39 greenLED = 1; // to 1. Thus, to turn a color off you counterintuitively set
cweems 0:209b9385be7b 40 blueLED = 1; // it to 1, so there is no voltage difference across the LED
cweems 0:209b9385be7b 41 for (int i = 1; i <= 5; i++) { // Do 5 times:
cweems 0:209b9385be7b 42 redLED = 0; // Turn the red LED on
cweems 0:209b9385be7b 43 pc.printf("Red LED ON \n\r"); // Send a status message
cweems 0:209b9385be7b 44 wait(1.0); // Wait 1.0 seconds
cweems 0:209b9385be7b 45 redLED = 1; // Turn the red LED off
cweems 0:209b9385be7b 46 pc.printf("Red LED OFF \n\r"); // Send a status message
cweems 0:209b9385be7b 47 wait(1.0); // Wait 1.0 seconds
cweems 0:209b9385be7b 48 greenLED = 0; // Repeat the above for the green LED
cweems 0:209b9385be7b 49 pc.printf("Green LED ON \n\r");
cweems 0:209b9385be7b 50 wait(1.0);
cweems 0:209b9385be7b 51 greenLED = 1;
cweems 0:209b9385be7b 52 pc.printf("Green LED OFF \n\r");
cweems 0:209b9385be7b 53 wait(1.0);
cweems 0:209b9385be7b 54 blueLED = 0; // Repeat for the blue LED
cweems 0:209b9385be7b 55 pc.printf("Blue LED ON \n\r");
cweems 0:209b9385be7b 56 wait(1.0);
cweems 0:209b9385be7b 57 blueLED = 1;
cweems 0:209b9385be7b 58 pc.printf("Blue LED OFF \n\r");
cweems 0:209b9385be7b 59 wait(1.0);
cweems 0:209b9385be7b 60 }
cweems 0:209b9385be7b 61 redLED = greenLED = blueLED = 0; // Give a short double-flash of white
cweems 0:209b9385be7b 62 wait(0.25);
cweems 0:209b9385be7b 63 redLED = greenLED = blueLED = 1;
cweems 0:209b9385be7b 64 wait(0.25);
cweems 0:209b9385be7b 65 redLED = greenLED = blueLED = 0;
cweems 0:209b9385be7b 66 wait(0.25);
cweems 0:209b9385be7b 67 redLED = greenLED = blueLED = 1; // Leave the LED off at the end
cweems 0:209b9385be7b 68 pc.printf("Goodbye \n\r"); // Send a farewell message
cweems 0:209b9385be7b 69 }