User manual
LPCXpresso Experiment Kit - User’s Guide
Page 40
Copyright 2013 © Embedded Artists AB
- RTS, a control output signal for peripheral block UART
- CT32B0_CAP0, an input signal to 32-bit timer #0
By default, after reset, the register is initialized to PIO1_5, have a pull-up resistor enabled and disabled
input hysteresis. As we know from the previous experiment, there is another register that controls the
direction of the general purpose digital input/output and this register initialize PIO1_5 to be an input
after reset.
Hence, after a reset, PIO1_5 is an input with pull-up resistor enabled. The pin is pulled high weakly
which is exactly what we need. When pressing the push-button the pin will be pulled low. The input will
be read high when no push-button is pressed and low when it is pressed.
In Experiment 1b, a function called GPIOSetDir was created. Even thought the direction of
PIO1_5 is correct from reset it is good programming practice to initialize the pin according to need. It is
simpler for other programmers to read and understand an application if there are no hidden
assumptions.
Register LPC_GPIO1->DATA holds the current state of the pins in port #1. Bit 5 in this register reflects
the state of pin PIO1_5. Since the register reflects all pins in the port the bit of interest must be masked
out. Use the same principle as presented in Lab 1a, i.e., AND with (1 << bitNumber).
Create a program that reads the state of the pin (and hence the push-button) and copy the result to a
LED. Turn on the LED when the push-button is pressed. Below is the skeleton of the program that you
shall create.
// Create defines for simpler access of LED1
#define LED1_PORT PORT0
#define LED1_PIN 2
#define LED_ON 0 //Low output turn LED on
#define LED_OFF 1 //High output turn LED off
// Create define for simpler access of push-button
#define SW2_PIN 5
// Initialize pins to be inputs and outputs,
// set outputs to defined states
...
uint8_t ledState;
//enter forever loop
while (1)
{
//Check if push-button is pressed (input is low)
if ( (LPC_GPIO1->DATA & (1 << SW2_PIN)) == 0)
ledState = LED_ON;
else
ledState = LED_OFF;
//Control LED
GPIOSetValue( LED1_PORT, LED1_PIN, ledState);
}
There are many things that can be done to create macro/defines to get a better abstraction structure of
the program above. First, the push-button states (pressed, not pressed) can have constants defined.
The LPC_GPIO1->DATA register can be defined as #define SW2_DATAPORT LPC_GPIO1->DATA. It
is also possible to create a general SW2_VALUE macro where the pin state is returned.
Update the code above according to these principles (more general and better structured code).
It is also possible to create a general function GPIOGetValue(), just like GPIOSetValue(). This will be
an exercise in the next experiment.