|
定時(shí)器0工作在16位定時(shí)器/計(jì)數(shù)器模式的測(cè)試程序
1. C程序:
/*------------------------------------------------------------------------------------*/
/* --- STC MCU International Limited ---------------------------------------*/
/* --- 演示STC89xx系列單片機(jī)定時(shí)器0的16位定時(shí)器/計(jì)數(shù)器模式 --*/
/*-------------------------------------------------------------------------------------*/
#include "reg51.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
//-----------------------------------------------
/* define constants */
#define FOSC 18432000L
#define T1MS (65536-FOSC/12/1000) //1ms timer calculation method in 12T mode
/* define SFR */
sbit TEST_LED = P1^0; //work LED, flash once per second
/* define variables */
WORD count; //1000 times counter
//-----------------------------------------------
/* Timer0 interrupt routine */
void tm0_isr() interrupt 1 using 1
{
TL0 = T1MS; //reload timer0 low byte
TH0 = T1MS >> 8; //reload timer0 high byte
if (count-- == 0) //1ms * 1000 -> 1s
{
count = 1000; //reset counter
TEST_LED = ! TEST_LED; //work LED flash
}
}
//-----------------------------------------------
/* main program */
void main()
{
TMOD = 0x01; //set timer0 as mode1 (16-bit)
TL0 = T1MS; //initial timer0 low byte
TH0 = T1MS >> 8; //initial timer0 high byte
TR0 = 1; //timer0 start running
ET0 = 1; //enable timer0 interrupt
EA = 1; //open global interrupt switch
count = 0; //initial counter
while (1); //loop
}
113
2. 匯編程序:
/*------------------------------------------------------------------------------------*/
/* --- STC MCU International Limited ---------------------------------------*/
/* --- 演示STC89xx系列單片機(jī)定時(shí)器0的16位定時(shí)器/計(jì)數(shù)器模式 ---*/
/*-------------------------------------------------------------------------------------*/
;/* define constants */
T1MS EQU 0FA00H ;1ms timer calculation method in 12T mode is (65536-18432000/12/1000)
;/* define SFR */
TEST_LED BIT P1.0 ;work LED , flash once per second
;/* define variables */
COUNT DATA 20H ;1000 times counter (2 bytes)
;-----------------------------------------------
ORG 0000H
LJMP MAIN
ORG 000BH
LJMP TM0_ISR
;-----------------------------------------------
;/* main program */
MAIN:
MOV TMOD,#01H ;set timer0 as mode1 (16-bit)
MOV TL0,#LOW T1MS ;initial timer0 low byte
MOV TH0,#HIGH T1MS ;initial timer0 high byte
SETB TR0 ;timer0 start running
SETB ET0 ;enable timer0 interrupt
SETB EA ;open global interrupt switch
CLR A
MOV COUNT,A
MOV COUNT+1,A ;initial counter
SJMP $
;-----------------------------------------------
;/* Timer0 interrupt routine */
TM0_ISR:
PUSH ACC
PUSH PSW
MOV TL0, #LOW T1MS ;reload timer0 low byte
MOV TH0, #HIGH T1MS ;reload timer0 high byte
MOV A, COUNT
ORL A, COUNT+1 ;check whether count(2byte) is equal to 0
JNZ SKIP
MOV COUNT, #LOW 1000 ;1ms * 1000 -> 1s
MOV COUNT+1, #HIGH 1000
CPL TEST_LED ;work LED flash
SKIP:
114
CLR C
MOV A, COUNT ;count--
SUBB A, #1
MOV COUNT, A
MOV A, COUNT+1
SUBB A, #0
MOV COUNT+1,A
POP PSW
POP ACC
RETI
;-----------------------------------------------
END |
評(píng)分
-
查看全部評(píng)分
|