User manual
LPCXpresso Experiment Kit - User’s Guide
Page 44
Copyright 2013 © Embedded Artists AB
Modify the existing code in this experiment so that the LED and the buzzer are controlled the same
way (LED on = buzzer on).
As a fun extra experiment, repeat the Morse code experiment in Lab 1d with the buzzer as Morse code
output instead of the LED.
Note that the buzzer will turn on during program download (on LPCXpresso boards). This is because
pin PIO0_7 is also connected to a LED on the LPCXpresso board. This will drive the signal low and
hence turn on the buzzer. The solution is to connect a 330 ohm (pull-up) resistor between signal
PIO0_7 and +3.3V.
7.3.4 Lab 2d: Toggling LED
In this experiment we will introduce a state. Pressing the push-button shall turn the LED on. Pressing
again will turn the LED off. Another way of expressing it is that the LED is toggled every time the push-
button is pressed.
The structure of the program is outlined below. When the push-button first is pressed, the LED is
toggled. Check the current state of the LED and inverse it. The recommended structure for this is to
store the LED state in a separate variable. After having toggled the LED, the program must wait until
the push-button has been released. If this last step is omitted, the LED would constantly toggle at a
high rate as long as the push-button is pressed. That would not be a desirable solution since the LED
can be in any state when the push-button is finally released.
// declare variables
uint8_t stateLED;
// Initialize pins to be inputs and outputs,
// set outputs to defined states
...
//enter forever loop
while (1)
{
//check if push-button is pressed
if (...)
{
//toggle LED
...
//wait until push-button is released
while(...);
}
}
You will probably notice that the LED will toggle a little more than expected. For example when
releasing the push-button, sometimes the LED will not change state. This is because of contact
bounce inside the push-button. The microcontroller is so fast so it will detect multiple presses/releases.
In the next experiment you will find one way of dealing with this problem.
7.3.5 Lab 2e: Sampling of Inputs
In this experiment we will introduce the concept of sampling. In the previous experiments the outputs
have been controlled as quickly as possible and the inputs have been read as often as possible.
Although simple, it is often desirable to have more detailed control of the system behavior.
Sampling is a concept where the state of inputs is read at defined points in time, the sample period.
Outputs are also controlled/changed at these points in time. More advanced systems can have many
different rates active at the same time. Some inputs are read at high rate (for example 1000 Hz, once
each 1 ms) while others are read at lower date, say 10 Hz (i.e., once each 100 ms). The used rate is a
trade-off between workload for the microcontroller and how fast the input can change (or how fast the