|
该应用为帮助清晰理解定时器中断的概念;
需要掌握51单片机IO口控制电平输出,以及中断的理论概念和定时器的理论概念。
源程序:
#include "STC15F2K60S2.H"
#define uchar unsigned char
#define uint unsigned int
sbit LED_SEL = P2^3;
sbit LED1 = P0^0;
sbit LED2 = P0^1;
sbit LED3 = P0^2;
sbit LED4 = P0^3;
sbit LED5 = P0^4;
sbit LED6 = P0^5;
sbit LED7 = P0^6;
sbit LED8 = P0^7;
uchar cnt = 0;
void Init(){
P0M1=0x00;
P0M0=0xff;
P2M1=0x00;
P2M0=0x08;
TMOD = 1;
TH0 = (65535 - 10000)/256;
TL0 = (65535 - 10000)%256;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void Timer0() interrupt 1 {
TH0 = (65535 - 10000)/256;
TL0 = (65535 - 10000)%256;
cnt++;
if (cnt == 30){
cnt = 0;
LED1 =~ LED1;
}
}
void main() {
Init();
LED_SEL = 1;
while(1);
}
|
|