-
CPU结构
2. 中断系统结构
3. 结论
3.1 INT0/INT1
(1)INT0 为 CPU 管脚 P32
(2)INT1 为 CPU 管脚 P33
TCON:
IT0:
0: 低电平有效
1: 下降沿有效
IE0: 1
IE:
EX0: 1
EA: 1
定时器/计数器中断
代码:
#include "reg51.h" typedef unsigned int uint; typedef unsigned char u8; sbit p32 = P3 ^ 2; sbit p33 = P3 ^ 3; u8 code smgduan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; char dig = 0; uint sec = 0; void delay(uint ms) { while(ms--); } void initINT0() { IT0 = 1; EX0 = 1; EA = 1; } void initINT2() { IT1 = 0; EX1 = 1; EA = 1; } void initTimer0() { TMOD = 0x01; TH0 = 0x0FC; TL0 = 0x18; TR0 = 1; ET0 = 1; EA = 1; } void main() { initINT0(); initINT2(); initTimer0(); P0 = ~smgduan[dig]; while(1) { if(sec == 1000) { sec = 0; dig++; if(dig > 9) dig = 0; P0 = ~smgduan[dig]; } } } void inter0() interrupt 0 { if(p32 == 0) { delay(1000); if(p32 == 0) { dig ++; if(dig > 9) dig = 0; P0 = ~smgduan[dig]; } while(!p32); } } void inter2() interrupt 2 { if(p33 == 0) { delay(1000); if(p33 == 0) { dig --; if(dig < 0) dig = 9; P0 = ~smgduan[dig]; } while(!p33); } } void interTimer0() interrupt 1 { TH0 = 0x0FC; TL0 = 0x18; sec ++; }