Atmel Evaluation Board using the SAM7SE Microcontroller AT91SAM7SE-EK AT91SAM7SE-EK Data Sheet

Product codes
AT91SAM7SE-EK
Page of 24
14
6295A–ATARM–27-Mar-07
Application Note
3.2.5.4
Wait Function
Using the global counter, a wait function taking a number of milliseconds as its parameter is very
easy to implement.
When called, the function first saves the current value of the global counter in a local variable. It
adds the requested number of milliseconds which has been given as an argument. Then, it sim-
ply loops until the global counter becomes equal to or greater than the computed value.
For proper implementation, the global counter must be declared with the volatile keyword in C.
Otherwise, the compiler might decide that being in a empty loop prevents the modification of the
counter; obviously, this is not the case since it can be altered by the interrupt handler.
3.2.6
Using the Parallel Input/Output controller
3.2.6.1
Purpose
Most pins on AT91SAM microcontrollers can either be used by a peripheral function (e.g.
USART, SPI, etc.) or used as generic input/outputs. All those pins are managed by one or more
Parallel Input/Output (PIO) controllers.
A PIO controller enables the programmer to configure each pin as used by the associated
peripheral or as a generic IO. In the second case, the level of the pin can be read/written using
several registers of the PIO controller. Each pin can also have an internal pull-up activated
individually.
In addition, the PIO controller can detect a status change on one or more pins, optionally trigger-
ing an interrupt whenever this event occurs. Note that the generated interrupt is considered
internal by the AIC, so it must be configured as level-sensitive (see 
).
In this example, the PIO controller manages two LEDs and two buttons. The buttons are config-
ured to trigger an interrupt when pressed (as defined in 
).
3.2.6.2
Initialization
There are two step for initializing the PIO controller. First, its peripheral clock must be enabled in
the PMC. After that, its interrupt source can be configured in the AIC.
3.2.6.3
Configuring LEDs
The two PIOs connected to the LEDs must be configured as outputs, in order to turn them on or
off. First, the PIOC control must be enabled in PIO Enable Register (PER) by writing the value
corresponding to a logical OR between the two LED IDs.
PIO direction is controlled using two registers: Output Enable Register (OER) and Output Dis-
able Register (ODR). Since in this case the two PIOs must be output, the same value as before
shall be written in OER.
Note that there are individual internal pull-ups on each PIO pin. These pull-ups are enabled by
default. Since they are useless for driving LEDs, they should be disabled, as this reduces power
consumption. This is done through the Pull Up Disable Register (PUDR) of the PIOC.
Here is the code for LED configuration:
/* Configure the pins as outputs */
AT91C_BASE_PIOA->PIO_OER = (LED_A | LED_B);
/* Enable PIOC control on the pins*/