定時器查詢方式定時器初始化:
1、設置定時器分頻數,為(x+1)分頻
2、匹配通道X中斷并復位TxTC
3、比較值(1S定時值)
4、啟動并復位TxTC
如:
T1PR = 99; // 設置定時器0分頻為100分頻,得110592Hz
T1MCR = 0x03; // 匹配通道0匹配中斷并復位T0TC
T1MR0 = 110592/2; // 比較值(1S定時值)
T1TCR = 0x03; // 啟動并復位T0TC
T1TCR = 0x01;
研究了好長一段時間,LPC210X的定時器,查詢方式定時很簡單如上面,但中斷方式要操作好多寄存器,太麻煩,一直是一頭霧水。好不容易理出了思路,現將一段例程粘貼備忘。
#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
/*************************************************************************
* 函數名稱:irq_handler
* 入口參數:無
* 返回參數:無
* 描 述:IRQ handler
*************************************************************************/
#pragma vector=IRQV
__irq __arm void irq_handler (void)
{
void (*interrupt_function)();
unsigned int vector;
vector = VICVectAddr; //獲得中斷向量
interrupt_function = (void(*)())vector;
if(interrupt_function != NULL)
{
interrupt_function(); //調用中斷指向的函數
}
else
{
VICVectAddr = 0; //清除在VIC中的中斷
}
}
/*************************************************************************
* 函數名稱: Timer0Handler
* 入口參數: 無
* 返回參數: 無
* 說 明: Timer 0 handler
*************************************************************************/
void Timer0Handler (void)
{
// clear interrupt flag
T0IR_bit.MR0INT = 1;
// Change patern
if ((IOSET & LED_MASK) == 0)
IOSET = LED_MASK; //關閉LED
else
IOCLR = LED_MASK;
//pNextPattern = pNextPattern-》pNextPattern; //調整當前的鏈表
VICVectAddr = 0;
}
/*************************************************************************
* 函數名稱: VicInit
* 入口參數: 無
* 返回參數: 無
* 說 明: 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;
}
/*************************************************************************
* 函數名稱: Init_timer0
* 入口參數: 無
* 返回參數: 無
* 說 明: 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;
}
/*************************************************************************
* 函數名稱: Init_Gpio
* 入口參數: 無
* 返回參數: 無
* 說 明: 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;
}
/*************************************************************************
* 函數名稱: Init_pll
* 入口參數: 無
* 返回參數: 無
* 說 明: Init PLL
*************************************************************************/
void Init_pll(void)
{
// Disable PLL
PLLCON = 0;
// Write Feed
PLLFEED = 0xAA;
PLLFEED = 0x55;
// Set periphery divider /4
APBDIV_bit.APBDIV = 0;
// Set MAM fully enable
MAMCR_bit.MODECTRL = 0;
MAMTIM_bit.CYCLES = 3;
MAMCR_bit.MODECTRL = 2;
}
/*************************************************************************
* 函數名稱: main
* 入口參數: 無
* 返回參數: 無
* 描 述: 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();
評論
查看更多