【中断代码】

 1 #include <reg52.h>
 2 
 3 #define uchar unsigned char
 4 #define uint  unsigned int
 5 
 6 uint count;
 7 void delay(uint z)
 8 {
 9     uint x,y;
10     for(x = z; x > 0; x--)
11         for(y = 114; y > 0 ; y--);
12 }
13 
14 /*中断服务特殊功能寄存器配置*/    
15 void init()
16 {
17     
18     TMOD = 0x01;  //定时器16为计数工作模式
19     TH0 =0x4b;
20     TL0 =0xfd; //50ms
21     ET0 = 1; //开定时器0中断
22     TR0 = 1;//启动定时器0
23     EA = 1;    //开总中断
24 }
25 
26 void main()
27 {
28     init();
29     while(1)
30     {
31         P1 = 0xff;
32     }
33 }
34 
35 /*中断服务程序*/
36 void timer0() interrupt 1
37 {
38     TH0 =0x4b;
39     TL0 =0xfd; //50ms
40     count++;
41     if (count == 4)
42     {
43         P1 = 0;
44         delay(5);
45         count = 0;    
46     }
47 }

 

posted @ 2018-03-17 13:16  Justice-V  阅读(275)  评论(0)    收藏  举报