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

📄 timer_rev1_6.c

📁 电动机控制程序
💻 C
字号:
// module title: timer.c
// revision:     1.2 
// date:         15/11/2003
 
#include <AT892051.h>
#include <absacc.h>

//#define XTAL12MHz
#define   XTAL18_432MHz
//#define XTAL22_1184MHz
//#define  XTAL24MHz

#define ACTIVATE   		1
#define DEACTIVATE 		0
#define Flashing_ON		1
#define Flashing_OFF	0
#define True			1
#define False			0
#define uchar unsigned char

/************************************************
*	Local  Data	: Create software timer  		*
************************************************/
unsigned int idata	tmr0, tmr1, tmr2;
unsigned int idata	Flash_CNT = 250;		// flashing at 2Hz

//************************************************
//	Local  Data	: Create Bit Addressable Data 	*

bit		Timer0State =0;
bit		Timer1State =0;
bit		Timer2State =0;

//External Idefinition
sbit	Flashing = P1^3;		//uP running status

/************************************
*	Local Function Prototype 		*
************************************/
void	Enable_Timer(char timer_name, unsigned int timer_count);
void	Disable_Timer(char timer_name);
void	Init_Sys_Timer(void);
void	msDelay(unsigned int timer_count);
uchar 	STimerStillCounting(uchar idxStimer);

//**************************************************************************
void Enable_Timer(char timer_name, unsigned int timer_count)
{
	EA = 0;           // Disable 8051 Interrupt 
	switch (timer_name)	{   
		case 0 : 	tmr0 = timer_count;
					Timer0State=ACTIVATE;
					break;
		case 1 : 	tmr1 = timer_count;
					Timer1State=ACTIVATE;
					break;
		case 2 : 	tmr2 = timer_count;
					Timer2State=ACTIVATE;
					break;
	} // end switch
	EA = 1;           // Enable 8051 Interrupt 
}

//**************************************************************************
void Disable_Timer (char timer_name)
{
	EA=0;			/* Disable 8051 Interrupt */
	switch (timer_name)
	{  	case 0 : 	Timer0State=DEACTIVATE;
					break;                      
		case 1 : 	Timer1State=DEACTIVATE;
					break;
		case 2 : 	Timer2State=DEACTIVATE;
					break;
	}
	EA=1;           // Enable 8051 Interrupt 
} // end func.

//***********************************************************************************
void 	Init_Sys_Timer (void)
{
  TMOD = 1;    			// TIMER MODE 

  #ifdef XTAL12MHz
		TH0 = -1000/256;        // 1 msec with 12 MHz clock		
		TL0 = -1000%256;		
  #endif
  #ifdef XTAL22_1184MHz
 		TH0 = -1843/256;        // 1 msec with 12 MHz clock		
		TL0 = -1843%256;		       
  #endif
  #ifdef XTAL24MHz
 		TH0 = -2000/256;        // 1 msec with 12 MHz clock		
		TL0 = -2000%256;		        
  #endif
  #ifdef XTAL18_432MHz
		TH0 = -1517/256; 
		TL0 = -1517%256;
  #endif

  tmr0 = tmr1 = tmr2 = 0;
  ET0 = 1;        			// Enable HW Timer0's Interrupt	     
  Enable_Timer(0, Flash_CNT); // Enable Flashing of the running LED
  EA	= 1;					// Enable 8051 Interrupt   		     
  TR0	= 1;					// Start HW Timer0 to count
}  // end func.

//***********************************************************************************
void	msDelay(unsigned int timer_count)
{
  Enable_Timer(2, timer_count);    // delay for sometime specified by timer_cnt
  while( Timer2State );
  Disable_Timer(2);
} // end func.

//**********************************************************************************
void 	Timer0_ISR (void) interrupt 1 using 1
{
  TR0 = 0;						//Disable counting
  #ifdef XTAL12MHz
		TH0 = -1000/256;        /* 1 msec with 12 MHz clock		*/
		TL0 = -1000%256;		/*                              */
  #endif
  #ifdef XTAL22_1184MHz
 		TH0 = -1843/256;        // 1 msec with 12 MHz clock		
		TL0 = -1843%256;		       
  #endif
  #ifdef XTAL24MHz
 		TH0 = -2000/256;        // 1 msec with 12 MHz clock		
		TL0 = -2000%256;		       
  #endif
  #ifdef XTAL18_432MHz
		//TH0 = -1517/256; TL0 = -1517%256;
  		TH0 =0xfa; TL0 = 0x0e;
  #endif

  TR0 = 1;                // Enable counting again
  if (Timer0State) { 
		if ( tmr0 )
			tmr0--;
		else {
			tmr0 = Flash_CNT;
			Flashing = !Flashing;
		} // end else
  } // end if
		
  if (Timer1State) {
		if (tmr1) 
			tmr1--;
		else 
			Timer1State = DEACTIVATE;
  } // end 

  if (Timer2State) {
		if (tmr2)
			tmr2--;
		else 
			Timer2State = DEACTIVATE;
  } // end if
} // end isr

//*********************************************************************
uchar 	STimerStillCounting(uchar idxStimer)
{ //stmr0,stmr1,stmr2;

  switch(idxStimer) {
	 case 0: return ( (uchar) Timer0State);
			 break;
	 case 1: return ( (uchar) Timer1State);
			 break;
	 case 2: return ( (uchar) Timer2State);
			 break;
  } // end switch
  return ( False );
} // end func.

⌨️ 快捷键说明

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