Timer query timer initialization:

1, set the timer frequency divider, divided by (x +1)

2, match channel X interrupt and reset TxTC

3, the comparison value (1S timing value)

4, start and reset TxTC

ARM LPC2103 Timer Interrupt Mode Register Settings

Such as:

T1PR = 99; // Set timer 0 divider to 100 divider to get 110592Hz

T1MCR = 0x03; // Match channel 0 to match interrupt and reset T0TC

T1MR0 = 110592/2; // comparison value (1S timer value)

T1TCR = 0x03; // Start and reset T0TC

T1TCR = 0x01;

After studying the LPC210X timer for a long time, the timing of the inquiry mode is very simple, but the interrupt mode has to operate many registers, which is too troublesome. It's hard to figure out the idea. Paste a routine and paste it.

#include "intrinsics.h"

#include "stdio.h"

#include "iolpc2103.h"

// OSC [Hz]

#define FOSC 11059200UL

// Core clk [Hz]

#define FCCLK FOSC

// Per clk [Hz]

#define PCCLK (FOSC/4)

// TImer TIck per second

#define TICK_PER_SEC (4UL)

#define TIM_PER_S(Val) (PCCLK/Val)

#define MAX_TICK_PER TIM_PER_S(20)

#define MIN_TICK_PER TIM_PER_S(5)

// Timer Delta period [ms]

#define DELTA_PER (50UL)

#define TIM_DPER ((PCCLK*DELTA_PER)/1000UL)

#define LED_MASK 1 "18

/************************************************* ************************

* Function name: irq_handler

* Entrance parameters: None

* Return parameters: none

* Description: IRQ handler

************************************************** ***********************/

#pragma vector=IRQV

__irq __arm void irq_handler (void)

{

Void (*interrupt_function)();

Unsigned int vector;

Vector = VICVectAddr; // Get the interrupt vector

Interrupt_function = (void(*)())vector;

If(interrupt_function != NULL)

{

Interrupt_function(); //Call the function the interrupt points to

}

Else

{

VICVectAddr = 0; // Clear the interrupt in the VIC

}

}

/************************************************* ************************

* Function Name: Timer0Handler

* Entrance parameters: None

* Return parameters: None

* Description: Timer 0 handler

************************************************** ***********************/

Void Timer0Handler (void)

{

// clear interrupt flag

T0IR_bit.MR0INT = 1;

// Change patern

If ((IOSET & LED_MASK) == 0)

IOSET = LED_MASK; //turn off LED

Else

IOCLR = LED_MASK;

//pNextPattern = pNextPattern-"pNextPattern; //Adjust the current list

VICVectAddr = 0;

}

/************************************************* ************************

* Function Name: VicInit

* Entrance parameters: None

* Return parameters: None

* Description: Init VIC module

************************************************** ***********************/

Void VicInit (void)

{

// Assign all interrupt chanels to IRQ

VICIntSelect = 0;

// Diasable all interrupts

VICIntEnClear = 0xFFFFFFFF;

// Clear all software interrutps

VICSoftIntClear = 0xFFFFFFFF;

// VIC registers can be accessed in User or privileged mode

VICProtection = 0;

// Clear interrupt

VICVectAddr = 0;

// Clear address of the Interrupt Service routine (ISR) for non-vectored IRQs.

VICDefVectAddr = 0;

// Clear address of the Interrupt Service routine (ISR) for vectored IRQs.

VICVectAddr0 = VICVectAddr1 = VICVectAddr2 = VICVectAddr3 =\

VICVectAddr4 = VICVectAddr5 = VICVectAddr6 = VICVectAddr7 =\

VICVectAddr8 = VICVectAddr9 = VICVectAddr10 = VICVectAddr11 =\

VICVectAddr12 = VICVectAddr13 = VICVectAddr14 = VICVectAddr15 = 0;

// Disable all vectored IRQ slots

VICVectCntl0 = VICVectCntl1 = VICVectCntl2 = VICVectCntl3 =\

VICVectCntl4 = VICVectCntl5 = VICVectCntl6 = VICVectCntl7 =\

VICVectCntl8 = VICVectCntl9 = VICVectCntl10 = VICVectCntl11 =\

VICVectCntl12 = VICVectCntl13 = VICVectCntl14 = VICVectCntl15 = 0;

}

/************************************************* ************************

* Function Name: Init_timer0

* Entrance parameters: None

* Return parameters: None

* Description: Init tiner0

************************************************** ***********************/

Void Init_timer0(void)

{

/*

// Init timer

// Reset and stop timer0

T0TCR = 2;

// Set timer counters mode - clock by PCLK

T0CTCR = 0;

// Set timer prescaler

T0PR = 0;

// Set timer period

T0MR0 = PCCLK/TICK_PER_SEC;

// Set mack action - interrupt by MACH0 enable, reset counter

T0MCR = 3;

// No external action

T0EMR = 0;

*/

T0TCR = 2;

T0CTCR = 0;

T0PR = 0;

T0MR0 = PCCLK/TICK_PER_SEC;

T0MCR = 3;

T0EMR = 0;

// Assign to IRQ

VICIntSelect_bit.TIMER0 = 0;

// Set interrupt slots

VICVectAddr0 = (unsigned int) Timer0Handler;

VICVectCntl0_bit.NUMBER = VIC_TIMER0;

VICVectCntl0_bit.ENABLED = 1;

// Timer 0 interrupt enable

VICIntEnable_bit.TIMER0 = 1;

// Enable timer0

T0TCR = 1;

}

/************************************************* ************************

* Function Name: Init_Gpio

* Entrance parameters: None

* Return parameters: None

* Description: Init GPIO

************************************************** ***********************/

Void Init_Gpio(void)

{

// Init GPIO

PINSEL0 = PINSEL1 = 0;

// Disable fast IO

SCS_bit.GPIO0M = 0;

// Set pins connect to LEDs as outputs

IODIR = LED_MASK;

// All LEDs off

IOCLR = LED_MASK;

}

/************************************************* ************************

* Function Name: Init_pll

* Entrance parameters: None

* Return parameters: None

* Description: Init PLL

************************************************** ***********************/

Void Init_pll(void)

{

// Disable PLL

PLLCON = 0;

// Write Feed

PLLFEED = 0xAA;

PLLFEED = 0x55;

// Set additional divider /4

APBDIV_bit.APBDIV = 0;

// Set MAM fully enable

MAMCR_bit.MODECTRL = 0;

MAMTIM_bit.CYCLES = 3;

MAMCR_bit.MODECTRL = 2;

}

/************************************************* ************************

* Function name: main

* Entrance parameters: None

* Return parameters: None

* Description: main

************************************************** ***********************/

Void main(void)

{

Init_pll();

// Memory map init flash memory is maped on 0 address

#ifdef FLASH

MEMMAP_bit.MAP = 1;

#else

MEMMAP_bit.MAP = 2;

#endif

__disable_interrupt();

VicInit();

Init_Gpio();

Init_timer0();

__enable_interrupt();

While(1)

{};

}

Butt Connector

Butt Connector,Lugs Insulated Female Connectors,Insulated Female Connectors,Non-Insulated Spade Terminals Wire Connector

Taixing Longyi Terminals Co.,Ltd. , https://www.lycopperlugs.com