timer.c

来自「uCOS-II for AT91M55800A完整实例」· C语言 代码 · 共 88 行

C
88
字号

/*
 * $Revision: 1.3 $
 */

#include "..\CPU\config.h"
#include "timer.h"

static volatile unsigned int ms_ctr = 0;
static volatile unsigned int tick = 0;

extern int test_number;
//static void ProcessInput(void);
//extern void OSTickISR(void);
extern void __Tick(void);
//***************************************************************
 /* Timer interrupt handler */
__irq __arm void heartbeat_irq(void)
{
                      // Called at 1000 Hz rate.
  __AIC_IVR = 0;      // Debug variant of vector read, protected mode is used.

  TimerBeat();        // Call timer callback function.
    
  __TC_SRC0;          // Read timer/counter 0 status register.
  __AIC_EOICR = 0;    // Signal end of interrupt to AIC.
}
//-------------------------------------------------------------------------
void AT91InitTimer()
{
  __TC_IDRC0 = 0xff; // Disable all timer/counter 0 interrupts.
  __AIC_SVR6 = (unsigned long)&__Tick;
  //__AIC_SVR6 = (unsigned long)&heartbeat_irq; // Timer/counter 0 interrupt vector.
  __AIC_SMR6 = 0x26; // SRCTYPE=1, PRIOR=6. Timer/counter 0 interrupt edge-triggered at prio 6.

  __AIC_ICCR_bit.tc0irq = 1; // Clears timer/counter 0 interrupt.
  __AIC_IECR_bit.tc0irq = 1; // Enable timer/counter 0 interrupt.
  
  __TC_CMRC0 = 0x00004002; // Capture mode, CPCTRG=1, TCCLKS=2 (/32).
  __TC_RCC0 = AT91_MCK / 32 / 200; // Set RC (compare register), 1 ms interval.
  __TC_CCRC0 = 1; // Enable the clock.
  __TC_CCRC0 = 5; // Software trigger.
  __TC_CCRC0 = 1; // Clear trigger.
}
//-----------------------------------------------------------------------------
void AT91StartTimer()
{
  __AIC_ICCR = 0x40; // Clears timer/counter 0 interrupt.
  __TC_SRC0; // Read timer/counter 0 status register to clear flags.
  __TC_IERC0_bit.cpcs = 1; // Interrupt on RC compare.
}
//----------------------------------------------------------------------
void TimerBeat(void)
{
  // Called at 1000 Hz rate.
  ms_ctr++; // Sleep counter.
  tick++; // Button debounce counter.
 // ProcessInput(); // Check buttons.
}
//--------------------------------------------------
void Sleep(int milliseconds)
{
  ms_ctr = 0;
  while (ms_ctr < milliseconds) ;
}
int SleepEx(int milliseconds)
{
  if(ms_ctr>=milliseconds)
  {
    ms_ctr = 0;
    return 1;
  }
  else
   return 0;
}

/*
static void ProcessInput(void)
{
  //Scan buttons with 50 ms interval
  if (tick >= 50)       
  {
 //    ButtonScan();
     tick = 0;
  }
}*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?