Lab1 - bhimebau/sice-courses GitHub Wiki

LED/Button/Serial

Overview

In this lab, you will:

  • Select a lab partner.
  • Create an mbed.org account for each partner.
  • Create a CPS-Lab team for code submission.
  • Develop an application to control the LED from the serial port

Preparation for Lab

  • Read CH2 in the Course Text
  • Review the mbed handbook
  • Become familiar with the program example templates available in the "New" creation wizard as part of the mbed compiler interface.

Setup

Processing digital inputs and outputs

Later in this lab, you will be reporting the state of the user button on the nucleo. This is the blue button on the board.

The following fragment of code is an example of how to read from the button and use this value to set the state of the led.

#include "mbed.h"

DigitalOut led1(LED1);
DigitalIn button(PC_13);


int main() {
    while (true) {
        if (button.read()) led1 = 0;
        else led1=1;
    }
}

Generating Console Output from the Nucleo

You can create console output from a nucleo using the mbed Serial API. Here is a small program that prints a message and then goes into a loop that echos the characters received on the serial port.

#include "mbed.h"

Serial pc(USBTX, USBRX);

int main() {
	pc.printf("Nucleo is up and running\n\r");
	while (true) {
       pc.putc(pc.getc());
    }
}

Go to the next step to see how you can interact with this serial port from linux.

Linux Console Output

On linux, one thing that occurs when a nucleo is connected is that a device is created. This device is created in the /dev directory. It will be named ttyACM0. If you unplug the connection to the nucleo, you will notice that this device disappears.

Use the following bash one-liner to copy the most recent .bin file to the nucleo:

cp "$(ls $HOME/Downloads/*.bin -t | sed 1q)" /run/media/$USER/NODE_F303RE/

This device enables characters to be sent and received from linux. One way to read and write to this device is through a utility called screen. To open the ttyACM0 device using screen, call the following command.

screen /dev/ttyACM0 9600

Passing 9600 as an argument to screen. To kill screen, you can use the key sequence <C-a> k (Ctrl + a, then k).

Developing a control program

Create a new program called "Lab1" that takes input from the serial console to allow performs the following functions. The serial console should accept the following commands

  • LED <ON|OFF>: The command LED takes one argument. If ON is passed, then the LED should be on. If OFF is passed, then the LED should be turned off.
  • BUTTON: The command BUTTON does not take any arguments. When the user runs this command, it measures the state of the BUTTON and returns "PRESSED" or "RELEASED" for the appropriate state.
  • BLINK <number_of_times>: The command "BLINK" takes one argument. This argument specifies the number of led blink cycles to run. The range of this blinks should be from 1 to 10. The cycle time of the blinks is fixed at 0.5s on and 0.5s off. The command will not return until all of the blinks have been completed.

Between commands, the mbed should provide a prompt that looks like "cps%". Characters should be echoed as they are received. The return key should cause the command to be evaluated. After a command is received, the mbed should respond with "OK" if the command was recognized. For invalid input, the mbed should respond with "ERR".

Scoring

The submission deadline is before your lab on Friday. "Publish" your team's "Lab1" program to the CPS-Lab team.

  • Lab Report (report.txt) [Create one report for your team as a file in your Lab1 program]

    • Results [5]
    • Difficulties [5]
    • What did you learn [10]
    • SUB-TOTAL [20]
  • Control Program

    • Command Prompt [20]
    • LED Command [20]
    • BUTTON Command [20]
    • BLINK Command [20]
    • SUB-TOTAL [80]:
  • TOTAL [100]:

⚠️ **GitHub.com Fallback** ⚠️