User manual

LPCXpresso Experiment Kit - User’s Guide
Page 36
Copyright 2013 © Embedded Artists AB
* (or bits) within the GPIO data register. See the LPC11/13 user manual
* for more details.
*
* (1<<bitPosi) gives us the MASKED_ACCESS register specific to the bit
* that is being requested to be set or cleared.
*
* (bitVal<<bitPosi) will be either be 0 or will contain a 1 in the
* appropriate bit position that matches the MASKED_ACCESS register
* being written to.
*/
switch ( portNum )
{
case PORT0:
LPC_GPIO0->MASKED_ACCESS[(1<<bitPosi)] = (bitVal<<bitPosi);
break;
case PORT1:
LPC_GPIO1->MASKED_ACCESS[(1<<bitPosi)] = (bitVal<<bitPosi);
break;
case PORT2:
LPC_GPIO2->MASKED_ACCESS[(1<<bitPosi)] = (bitVal<<bitPosi);
break;
case PORT3:
LPC_GPIO3->MASKED_ACCESS[(1<<bitPosi)] = (bitVal<<bitPosi);
break;
default:
break;
}
}
}
Create a similar, general function for setting the direction of any GPIO pin (input or output). Call this
new function GPIOSetDir. The function’s input parameters shall be port number, bit number and
direction.
After that, recreate the program from the previous experiment using these two new functions.
It is good programming practice to place functions that are related in separate files. It will enhance tile
source code structure and make it easier to maintain and understand in general. An accompanying
include file (h-file) declares the functions that are exposed to other source code files.
Place the GPIO related functions in a separate file called gpio.c and create an include file,
gpio.h, that declares the exposed functions.
Also, in order to keep the file main.c reasonable short move all defines that are related to the board
to a separate include file board.h.
7.2.3 Lab 1c: Delay Function LED Flashing
Next, design a program that flash with the LED 50 ms (milli seconds) on, 150 ms off, 50 ms on and
finally and 750 ms off. Continuously repeat this 1000 ms cycle.
In is a common task in embedded systems to operate on exact time and control external devices
exactly. In this case a LED.
One obvious solution is to create a delay function. An example is listed below that forced the CPU to
execute NOP (no operation) instructions in a loop. Use this function and test different values in order to
establish a relationship between the number of NOPs and the actual delay in time.