- #include "reg51.h"
- typedef unsigned char BYTE;
- typedef unsigned int WORD;
- //-----------------------------------------------
- /* define constants */
- #define FOSC 11059200L
- #define T1MS (65536-FOSC/12/1000) //1ms timer calculation method in 12T mode
- /* define SFR */
- sbit ET2 = IE^5;
- sfr T2CON = 0xc8; //timer2 control register
- sbit TF2 = T2CON^7;
- sbit TR2 = T2CON^2;
- sfr T2MOD = 0xc9; //timer2 mode register
- sfr RCAP2L = 0xca;
- sfr RCAP2H = 0xcb;
- sfr TL2 = 0xcc;
- sfr TH2 = 0xcd;
- sbit LED = P2^0; //work LED, flash once per second
- /* define variables */
- WORD count; //1000 times counter
- //-----------------------------------------------
- /* Timer2 interrupt routine */
- void tm2_isr() interrupt 5 using 1
- {
- TF2 = 0;
- if (count-- == 0) //1ms * 1000 -> 1s
- {
- count = 1000; //reset counter
- LED = ! LED; //work LED flash
- }
- }
- //-----------------------------------------------
- /* main program */
- void main()
- {
- RCAP2L = TL2 = T1MS; //initial timer2 low byte
- RCAP2H = TH2 = T1MS >> 8; //initial timer2 high byte
- TR2 = 1; //timer2 start running
- ET2 = 1; //enable timer2 interrupt
- EA = 1; //open global interrupt switch
- count = 0; //initial counter
- while (1); //loop
- }
复制代码 |