User manual

LPCXpresso Experiment Kit - User’s Guide
Page 35
Copyright 2013 © Embedded Artists AB
Include a file called type.h in all program files. The main content of the file is presented below:
#if defined ( __GNUC__ )
#include <stdint.h>
#else
/* exact-width signed integer types */
typedef signed char int8_t;
typedef signed short int int16_t;
typedef signed int int32_t;
typedef signed __int64 int64_t;
/* exact-width unsigned integer types */
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
typedef unsigned int uint32_t;
typedef unsigned __int64 uint64_t;
#endif // __GNUC__
#ifndef NULL
#define NULL ((void *)0)
#endif
#ifndef FALSE
#define FALSE (0)
#endif
#ifndef TRUE
#define TRUE (1)
#endif
As seen, there are four signed and four unsigned variable types of length 1, 2, 4 or 8 bytes (8, 16, 32,
or 64 bits). The file also declares the commonly used constants: NULL, FALSE and TRUE.
Code becomes much more portable (between different compilers) if a common include file like this is
used. It also becomes more readable.
7.2.2 Lab 1b: GPIO and Bit Masking
There is hardware support in the GPIO peripheral block for accessing selected bits, as opposed to
accessing all of them. This is described in the LPC111x user’s manual, chapter 12.4.1 – Write/read
data operations. In short, the GPIOxDATA register can be accessed on many different addresses. The
address used to access the register determines which bit(s) that is/are accessed.
Below is a copy of a function from NXP’s driver library for the LPC111x. As seen, it is a general
function for manipulating any GPIO output (any port, any pin). The array named
MASKED_ACCESS[…] is used to get the correct access address to the GPIOxDATA register, given
which bit(s) to access. Note that the function below only allows one bit at a time to be accessed.
(1<<bitPosi) is used to index into array MASKED_ACCESS[…]. It is possible to create more general
access functions where several pins can be controlled simultaneous, for example
MASKED_ACCESS[(1<<bitPosi1) | (1<<bitPosi2) | (1<<bitPosi3)].
/*****************************************************************************
** Function name: GPIOSetValue
**
** Descriptions: Set/clear a bitvalue in a specific bit position
** in GPIO portX(X is the port number.)
**
** parameters: port num, bit position, bit value
** Returned value: None
**
*****************************************************************************/
void
GPIOSetValue( uint32_t portNum, uint32_t bitPosi, uint32_t bitVal )
{
// Check that bitVal is a binary value - 0 or 1
if (bitVal <2 )
{
/* The MASKED_ACCESS registers give the ability to write to a specific bit