LPCOpen LPC13xx Systick Timer and GPIO

In an article set I want to describe and show some code snippets for the lpc13xx in our case the lpc1313. I work with lpcxpresso and the lpcopen code base. A getting started with lpcopen can be found here.

The articles will contain code snippets for the most commonly used peripheral of the controller starting with the Systick timer and GPIO manipulation.

The Systick timer contains to the core of the cortex m3 processor. It is a 24 bit Timer and can be used as time reference. A block diagramm of the Systick timer can be found in the user manual of the lpc13xx in chapter 17.

This programm will toogle an LED on PIO0.9 and will use the systick timer as time reference.

// Tobias Markus
#include "chip.h"
#include <cr_section_macros.h>
#include <gpio_13xx_2.h>
 
volatile uint32_t msTicks=0;
 
// Systick Handler which is called each ms
void SysTick_Handler(void)
{
    msTicks++;
}
 
// blocks for dlyTicks ms...
__INLINE static void Delay (uint32_t dlyTicks) 
{
    uint32_t curTicks;
 
    curTicks = msTicks;
    while ((msTicks - curTicks) < dlyTicks);
}
 
int main(void) 
{
    Chip_GPIO_Init(LPC_GPIO_PORT);
    SysTick_Config(72000000 / 1000);
    Chip_GPIO_WriteDirBit(LPC_GPIO_PORT, 0, 9, true);
    Chip_GPIO_WritePortBit(LPC_GPIO_PORT, 0, 9, false);
    while(1)
    {
        Chip_GPIO_WritePortBit(LPC_GPIO_PORT, 0, 9, true);
        Delay(1000);
    	Chip_GPIO_WritePortBit(LPC_GPIO_PORT, 0, 9, false);
    	Delay(1000);
    }
    return 0 ;
}

In the example above the Systick timer is set to call the Systick handler each millisecond. Then there is a delay function which blocks for the given ms.

In the main function we initialise the GPIOs with the Chip_GPIO_Init(LPC_GPIO_T *pGPIO); function. It needs the base address of the GPIO peripheral which is given with LPC_GPIO_PORT to the function. The function then enable the GPIO clock.

To configure the Systick timer we use the SysTick_Config(uint32_t ticks); funtion. With ticks we can set the Systick timer interval. In our case we have a 72 MHz clock and we want it to fire each ms. So the tick has to be set to 72MHz/1000.

With the Chip_GPIO_WriteDirBit(LPC_GPIO_T *pGPIO, uint32_t port, uint8_t bit, bool setting); function the direction of a GPIO Pin is set in our case we set PIO0.9 to output wihch corresponds to set the setting to true if you want to set the pin to input the setting has to be false. The function also needs the base address of the GPIO peripheral like in the init.

To set the GPIO pins to high or low we need the  Chip_GPIO_WritePortBit(LPC_GPIO_T *pGPIO, uint32_t port, uint8_t bit, bool setting); function. Like the WriteDirBit function it needs the base address of the GPIO peripheral, the port number and the bit. This time true is used to set the bit high and false to set the bit low.

After this functions the main program enters a while loop in which we set PIO0.9 to high, wait 1s, set PIO0.9 to low, wait 1s again and than loop again.

Dieser Beitrag wurde unter Allgemein, ARM lpc1xxx, Mikrocontroller abgelegt und mit , , , , , , , , , , , verschlagwortet. Setze ein Lesezeichen auf den Permalink.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.