控制/MCU
定时器查询方式定时器初始化:
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();
全部0条评论
快来发表一下你的评论吧 !