⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 interrupt_timer.c

📁 AT91fr40162的FLASH读写程序
💻 C
字号:
//*----------------------------------------------------------------------------
//*      ATMEL Microcontroller Software Support  -  ROUSSET  -
//*----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*----------------------------------------------------------------------------
//* File Name           : interrupt_timer.c
//* Object              : Timer interrupt management
//*                     : Use LED7 & LED8 for status interrupt
//*
//* 1.0 03/Jan/03 JPP   : Creation
//*----------------------------------------------------------------------------

#include "dtm_v3.h"
#include "dtm.h"
#include "externdef.h"
#include "lib_AT91R40008.h"


extern void timer1_asm_irq_handler(void);
extern void timer2_asm_irq_handler(void);

//* Global variable
// int count_timer0_interrupt;
// int count_timer1_interrupt;
 
 #define speed          1000			//66000>speed>2


#define TIMER0_INTERRUPT_LEVEL		7
#define TIMER1_INTERRUPT_LEVEL		5
#define TIMER2_INTERRUPT_LEVEL		5

/*-----------------*/
/* Clock Selection */
/*-----------------*/
#define TC_CLKS                  0x7
#define TC_CLKS_MCK2             0x0
#define TC_CLKS_MCK8             0x1
#define TC_CLKS_MCK32            0x2
#define TC_CLKS_MCK128           0x3
#define TC_CLKS_MCK1024          0x4


//*------------------------- Interrupt Function -------------------------------

//*----------------------------------------------------------------------------
//* Function Name       : timer2_c_irq_handler
//* Object              : C handler interrupt function called by the interrupts 
//*                       assembling routine
//* Input Parameters    : <RTC_pt> time rtc descriptor
//* Output Parameters   : increment count_timer0_interrupt
//*----------------------------------------------------------------------------
void timer2_c_irq_handler(void)			// 10us interrupt
{
    unsigned int dummy;
   static _WORD i=0;
    //* Acknowledge interrupt status
    dummy = MY_TC2_SR;
    //* Suppress warning variable "dummy" was set but never used
    dummy = dummy;

	i++;
	// 处理声音    
   	if (i%2==0) {
      	if ((gcOpenBeep)&&(gcBeepCount)) {
     	 	gcpCPLDReg[3] |= CPLD_BEEP;
     	 	pCPLD[6] = gcpCPLDReg[3];
         	gcBeepCount--;
      	}
   	}
   	else {
      	if ((gcOpenBeep)&&(gcBeepCount)&&(gcBeepCount%2!=0)) {
     	 	gcpCPLDReg[3] &=~CPLD_BEEP;
     	 	pCPLD[6]=gcpCPLDReg[3];
         	gcBeepCount--;
         	if (gcBeepCount==0) gcOpenBeep=OFF;
      	}
   	}
	
	if (i>=20) {
      	// 累计按键按下的时间
      	if (gcKeyDown==1) gcTimeOfKeyPress++;
      	// 检查背光,并关闭
      	if (gcBackLightTime==0) {
     	 	gcpCPLDReg[3] &= ~1;	pCPLD[6] = gcpCPLDReg[3];
      	}
      	else
         	gcBackLightTime--;
		
      	// 检查LED,并关闭
      	if (gcOpenLEDRed==ON) {
         	if (gcLEDRedLightTime==0) {
		    	gcpCPLDReg[2] |= CPLD_LED_R;
		    	pCPLD[4]=gcpCPLDReg[2];
            	gcOpenLEDRed=OFF;
         	}
         	else {
            	gcLEDRedLightTime--;
         	}   
	  	}
	  
	  	if (gcOpenLEDGreen==ON) {
      	 	if (gcLEDGreenLightTime==0) {
		    	gcpCPLDReg[2] |= CPLD_LED_G;
		    	pCPLD[4]=gcpCPLDReg[2];
		    	gcOpenLEDGreen=OFF;
         	}
         	else {
            	gcLEDGreenLightTime--;
	     	}	 
      	}
    	i=0;
	}
    //gwPluseMile++ ;
    //gwPluseAllow++;
    
}
//*----------------------------------------------------------------------------
//* Function Name       : timer1_c_irq_handler
//* Object              : C handler interrupt function called by the interrupts 
//*                       assembling routine
//* Input Parameters    : <RTC_pt> time rtc descriptor
//* Output Parameters   : increment count_timer1_interrupt
//*----------------------------------------------------------------------------
void timer1_c_irq_handler(void)		// 100ms unterrupt
{
    unsigned int dummy;
   	//static _DWORD dwTmp;
   	static _WORD i=0;
   	//static _DWORD dwMile;


   	//* Acknowledge interrupt status
   	dummy = MY_TC1_SR;
   	//* Suppress warning variable "dummy" was set but never used
   	dummy = dummy;
   	i++;
   	// 1秒钟
   	if (i>=10) {				//10
      	gcSecondFresh = 1;
      	gcDecimal = ~gcDecimal;
      	gcSeconds++;
      	IncreaseClock();
      	// 累加当前时间,按秒累加
      	if ((gwPluseAllow>=1) && (CSI.cStatus==S_TRAINING)) {
         	gcValidSeconds++;
         	CDTI.cCycleSecond++;
         	gcTrainFreshSecond = 1;
      	}

      	// 每10秒
      	if ((gcSeconds % 10)==0) {
         	gwPluseAllow=0;
      	}

      	// 每5秒累计里程
      	if (((gcSeconds % 5)== 0) && (CSI.cStatus==S_TRAINING)) {
         	if (gwPluseMile!=0) {
            	//dwTmp = (_DWORD)gwPluseMile;                    // 取脉冲数
            	//dwTmp *= 1000;                                  //
            	//dwTmp += gwRemainedPluse;                       // 累计上次剩余的脉冲数
            	//dwMile = dwTmp/SPB.wKParm;                          // 计算脉冲数对应的米数
            	//gwRemainedPluse = dwTmp%SPB.wKParm;                // 计算本次剩余脉冲数

            	// 累计里程
            	CDTI.wCyclePluse += gwPluseMile;
            	gcTrainFreshMile = 1;
            	gwPluseMile = 0;
         	}
      	}

      	// 累计教练和驾驶员时间,按分钟累计
      	if ((gcValidSeconds%60)==0) {     						// 刷新时间
         	gcTrainFreshTime = 1;
     	}
      	if (gcValidSeconds==60*SPB.cTimeCycle) {     			// 区分训练类型
         	gcValidSeconds=0;
			MergeDCBCDTISmallInfo(0,MCDTI);
         	//DecideTrainType(CDTI.wCyclePluse,CDTI.cCycleSecond,CDTI.wCycleMaxRunPluse,CDTI.cCycleMaxRunTime);
         	gcTrainFreshTime = 1;
         	gcUpdateCDTI = 1;									// 写CDIT
      	}
      	if (gcSeconds==60) {
          	gcSeconds=0;
          	gcMinterFresh = 1;
      	}

      	i=0;
   	}    
}

//*------------------------- 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;
    _DWORD dwCounter;
    
    MY_PS_PCER = (1<<TimerId);							//* First, enable the clock of the TIMER

    TC_pt->TC_CCR = AT91C_TC_CLKDIS ;				//* Disable the clock and the interrupts
	TC_pt->TC_IDR = 0xFFFFFFFF ;
	
	if(TimerId==AT91C_ID_TC2) {
		dwCounter = MCK/32;
		dwCounter /= 200;
		TC_pt->TC_RC = 65535 - (_WORD)dwCounter; 	// Time0 1000us	
	}
	else  { 										//65535>TC_RC>0
		dwCounter = MCK/1024;
		dwCounter /= 132;				
		TC_pt->TC_RC = 65535 - (_WORD)dwCounter;	// Time1 100ms(MCKKHz/speed);
	}
    //* 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
	//if(TimerId==AT91C_ID_TC1)  
		TC_pt->TC_CCR = AT91C_TC_CLKEN ;

}

//*-------------------------- External Function -------------------------------

//*----------------------------------------------------------------------------
//* Function Name       : timer_init
//* Object              : Init timer counter
//*                     : init the timer interrupt counter,TC1 and TC2
//* Input Parameters    : none
//* Output Parameters   : TRUE
//*----------------------------------------------------------------------------
void timer_init ( void )
//* Begin
{
	AT91PS_AIC pAic;
    pAic=AT91C_BASE_AIC;

    //* Open timer1
	AT91F_TC_Open(AT91C_BASE_TC1,TC_CLKS_MCK128,AT91C_ID_TC1);	//TC_CLKS_MCK128
    //* Open Timer 1 interrupt
	AT91F_AIC_ConfigureIt(AT91C_BASE_AIC, AT91C_ID_TC1, TIMER1_INTERRUPT_LEVEL,AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE, (unsigned int)timer1_asm_irq_handler); 
	AT91C_BASE_TC1->TC_IER  = AT91C_TC_CPCS;  //  IRQ enable CPC
	pAic->AIC_IECR = (0x1 << AT91C_ID_TC1) ;

    //* Open timer2
	AT91F_TC_Open(AT91C_BASE_TC2,TC_CLKS_MCK32,AT91C_ID_TC2);
    //* Open Timer 2 interrupt
	AT91F_AIC_ConfigureIt(AT91C_BASE_AIC, AT91C_ID_TC2, TIMER2_INTERRUPT_LEVEL,AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE, (unsigned int)timer2_asm_irq_handler); 
	AT91C_BASE_TC2->TC_IER = AT91C_TC_CPCS;  //  IRQ enable CPC
 	pAic->AIC_IECR = (0x1 << AT91C_ID_TC2) ;

    //* Generate interrupt by software
 	pAic->AIC_ISCR = (0x1 << AT91C_ID_TC1) ;
 	pAic->AIC_ISCR = (0x1 << AT91C_ID_TC2) ;
 	
    //* Start timer1
    AT91C_BASE_TC1->TC_CCR = AT91C_TC_SWTRG ;

    //* Start timer2
    AT91C_BASE_TC2->TC_CCR = AT91C_TC_SWTRG ;
}

⌨️ 快捷键说明

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