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

📄 main.c

📁 Embedded C 这本书的范例光碟程式
💻 C
字号:
/*------------------------------------------------------------------*-

   Main.c

  ------------------------------------------------------------------

   Simple timer ISR demonstration program.


   COPYRIGHT
   ---------

   This code is associated with the book:

   EMBEDDED C by Michael J. Pont 
   [Pearson Education, 2002: ISBN: 0-201-79523-X].

   This code is copyright (c) 2001 by Michael J. Pont.
 
   See book for copyright details and other information.

-*------------------------------------------------------------------*/

#include <Reg52.H>

#define INTERRUPT_Timer_2_Overflow 5

// Function prototype
// NOTE: ISR is not explictly called and does not require a prototype
void Timer_2_Init(void);

/* --------------------------------------------------------------- */

void main(void)
   {
   Timer_2_Init();  // Set up Timer 2

   EA = 1;          // Globally enable interrupts
   
   while(1);        // An empty Super Loop
   }

/* --------------------------------------------------------------- */

void Timer_2_Init(void)
   {
   // Timer 2 is configured as a 16-bit timer,
   // which is automatically reloaded when it overflows
   //
   // This code (generic 8051/52) assumes a 12 MHz system osc.  
   // The Timer 2 resolution is then 1.000 祍
   //
   // Reload value is FC18 (hex) = 64536 (decimal)
   // Timer (16-bit) overflows when it reaches 65536 (decimal)
   // Thus, with these setting, timer will overflow every 1 ms
   T2CON   = 0x04;   // Load Timer 2 control register

   TH2     = 0xFC;   // Load Timer 2 high byte
   RCAP2H  = 0xFC;   // Load Timer 2 reload capt. reg. high byte
   TL2     = 0x18;   // Load Timer 2 low byte
   RCAP2L  = 0x18;   // Load Timer 2 reload capt. reg. low byte

   // Timer 2 interrupt is enabled, and ISR will be called 
   // whenever the timer overflows - see below.
   ET2     = 1;

   // Start Timer 2 running
   TR2   = 1;        
   }

/* --------------------------------------------------------------- */

void X(void) interrupt INTERRUPT_Timer_2_Overflow
   {
   // This ISR is called every 1 ms

   // Place required code here...
   }

/*------------------------------------------------------------------*-
  ---- END OF FILE -------------------------------------------------
-*------------------------------------------------------------------*/

⌨️ 快捷键说明

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