📄 timer.c
字号:
#include "board.h"
//* Global variable
unsigned char count_timer1_interrupt;
unsigned char count_timer2_interrupt;
extern void AT91F_DBGU_Printk( char *buffer);
//*------------------------- Internal Function --------------------------------
//*----------------------------------------------------------------------------
//* Function Name : AT91F_TC_Open
//* Object : Initialize Timer Counter Channel and enable is clock
//* Input Parameters : <tc_pt> = TC Channel Descriptor Pointer
//* <mode> = Timer Counter Mode
//* : <TimerId> = Timer peripheral ID definitions
//* Output Parameters : None
//*----------------------------------------------------------------------------
void AT91F_TC_Open ( AT91PS_TC TC_pt, unsigned int Mode, unsigned int TimerId)
//* Begin
{
unsigned int dummy;
//* First, enable the clock of the TIMER
AT91F_PMC_EnablePeriphClock ( AT91C_BASE_PMC, 1<< TimerId ) ;
//* Disable the clock and the interrupts
TC_pt->TC_CCR = AT91C_TC_CLKDIS ;
TC_pt->TC_IDR = 0xFFFFFFFF ;
//* Clear status bit
dummy = TC_pt->TC_SR;
//* Suppress warning variable "dummy" was set but never used
dummy = dummy;
//* Set the Mode of the Timer Counter
TC_pt->TC_CMR = Mode ;
//* Enable the clock
TC_pt->TC_CCR = AT91C_TC_CLKEN ;
//* End
}
//*------------------------- Interrupt Function -------------------------------
//*----------------------------------------------------------------------------
//* Function Name : timer0_c_irq_handler
//* Object : C handler interrupt function calAT91B_LED by the interrupts
//* assembling routine
//* Output Parameters : increment count_timer0_interrupt
//*----------------------------------------------------------------------------
void timer2_c_irq_handler(void)
{
AT91PS_TC TC_pt = AT91C_BASE_TC2;
unsigned int dummy;
AT91C_BASE_AIC->AIC_IVR = 0; /* Debug variant of vector read (protect mode is used) */
//AIC_IVR 读,当前中断对应的AIC_SVR。
AT91C_BASE_AIC->AIC_ICCR = AT91C_ID_TC2; /* Clear timer2 interrupt */
AT91C_BASE_AIC->AIC_EOICR = 0; /* Signal end of interrupt */
//* AcknowAT91B_LEDge interrupt status
dummy = TC_pt->TC_SR;
//* Suppress warning variable "dummy" was set but never used
dummy = dummy;
count_timer2_interrupt++;
//* Read the output state
/*
if ( count_timer2_interrupt%2 )
{
AT91F_PIO_ClearOutput( AT91C_BASE_PIOB, LED4 );
}
else
{
AT91F_PIO_SetOutput( AT91C_BASE_PIOB, LED4 );
}
*/
AT91F_DBGU_Printk("timer2_interrupt:%i\n\n\r");
//TRACE_INFO("counter_timer2_interrupt:%i\n\n\r",count_timer2_interrupt);
}
//*----------------------------------------------------------------------------
//* Function Name : timer1_c_irq_handler
//* Object : C handler interrupt function calAT91B_LED by the interrupts
//* assembling routine
//* Output Parameters : increment count_timer1_interrupt
//*----------------------------------------------------------------------------
void timer1_c_irq_handler(void)
{
AT91PS_TC TC_pt = AT91C_BASE_TC1;
unsigned int dummy;
AT91C_BASE_AIC->AIC_IVR = 0; /* Debug variant of vector read (protect mode is used) */
//AIC_IVR 读,当前中断对应的AIC_SVR。
AT91C_BASE_AIC->AIC_ICCR = AT91C_ID_TC1; /* Clear timer1 interrupt */
AT91C_BASE_AIC->AIC_EOICR = 0; /* Signal end of interrupt */
//* AcknowAT91B_LEDge interrupt status
dummy = TC_pt->TC_SR;
//* Suppress warning variable "dummy" was set but never used
dummy = dummy;
count_timer1_interrupt++;
//* Read the output state
/*
if ( count_timer1_interrupt%2 )
{
AT91F_PIO_ClearOutput( AT91C_BASE_PIOB, LED3 );
}
else
{
AT91F_PIO_SetOutput( AT91C_BASE_PIOB, LED3 );
}
*/
AT91F_DBGU_Printk("timer1_interrupt:%i\n\n\r");
//TRACE_INFO("counter_timer1_interrupt:%i\n\n\r",count_timer1_interrupt);
}
//*-------------------------- External Function -------------------------------
//*----------------------------------------------------------------------------
//* Function Name : timer_init
//* Object : Init timer counter
//* Input Parameters : none
//* Output Parameters : TRUE
//*----------------------------------------------------------------------------
void timer2_init ( void )
//* Begin
{
//init the timer interrupt counter
count_timer2_interrupt = 0;
//* Open timer2
AT91F_TC_Open(AT91C_BASE_TC2,AT91C_TC_CLKS_TIMER_DIV5_CLOCK | AT91C_TC_CPCTRG,AT91C_ID_TC2);
//* Open Timer 2 interrupt
AT91F_AIC_ConfigureIt ( AT91C_BASE_AIC, AT91C_ID_TC2, TIMER2_INTERRUPT_LEVEL,AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, timer2_c_irq_handler);
AT91C_BASE_TC2->TC_IER = AT91C_TC_CPCS; // IRQ enable CPC
AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_TC2);
// AT91C_BASE_TC2->TC_RC = 0xb764;//for 1s
AT91C_BASE_TC2->TC_RC = 0xFFF8;
//* Start timer0
AT91C_BASE_TC2->TC_CCR = AT91C_TC_SWTRG ;
//* End
}
void timer1_init ( void )
//* Begin
{
//init the timer interrupt counter
count_timer1_interrupt=0;
//* Open timer1
AT91F_TC_Open(AT91C_BASE_TC1,AT91C_TC_CLKS_TIMER_DIV4_CLOCK | AT91C_TC_CPCTRG,AT91C_ID_TC1);
//* Open Timer 1 interrupt
AT91F_AIC_ConfigureIt ( AT91C_BASE_AIC, AT91C_ID_TC1, TIMER1_INTERRUPT_LEVEL,AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, timer1_c_irq_handler);
AT91C_BASE_TC1->TC_IER = AT91C_TC_CPCS; // IRQ enable CPC
AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_TC1);
// AT91C_BASE_TC1->TC_RC = 0x3fff;
AT91C_BASE_TC2->TC_RC = 0xf764;
//* Start timer1
AT91C_BASE_TC1->TC_CCR = AT91C_TC_SWTRG ;
//* End
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -