Interrupt, timer

 

 


8051 一共有 5 個中斷源及 2 個等級

Interrupt         Flag     Interrupt Handler Address
0  External 0     IE0      0x0003h
1  Timer 0        TF0      0x000Bh
2  External 1     IE1      0x0013h
3  Timer 1        TF1      0x001Bh
4  Serial         RI/TI    0x0023h 

Ext0 > T0 > Ext1 > T1 > Serial

TCON(88H)

         
0  IT0      1: 1-->0 (觸發方式)
1  IE0
2  IT1     
3  IE1
4  TR0
5  TF0      啟動
6  TR1
7  TF1

IE SFR (A8h)

Bit   Name Address    Explanation of Function
7     EA   AFh        Global Interrupt Enable/Disable
6     -    AEh        Undefined
5     -    ADh        Undefined
4     ES   ACh        Enable Serial Interrupt
3     ET1  ABh        Enable Timer 1 Interrupt
2     EX1  AAh        Enable External 1 Interrupt
1     ET0  A9h        Enable Timer 0 Interrupt
0     EX0  A8h        Enable External 0 Interrupt 

IP SFR (B8h)

Bit Name  Address        Function
7   - -   Undefined
6   - -   Undefined
5   - -   Undefined
4   PS    BCh           Serial Interrupt Priority
3   PT1   BBh           T1 Interrupt Priority
2   PX1   BAh           Ext1 Interrupt Priority
1   PT0   B9h           T0 Interrupt Priority
0   PX0   B8h           Ext0 Interrupt Priority 

TMOD

T1:

Gate=0  <-- 不使用外部源
C/t < Counter(1) / timer(0)
M1
M0

T0:

Gate
C/t
M1
M0

 

M1M0
00       13 bit
01       16 bit (65536)
10       8 auto THx-->TLx
11       T1 分成兩個獨立的 8 bit

 


 

 

2 Clock = 1 State

6 State = 1 Mech

 

Clock:12MHz
Period: 1us

 

 


Ext0

 

EA=1;
EX0=1
IT0=1;

 

void int0() interrupt 0 using 1
{
    Do something
}

 

Timer

16bit - timer1

TMOD=0x01;
TH0=(65536 - time)/16;
TL0=(65536 - time)%16;
EA=1;
ET0=1;
TR0=1;

C:

void timer0 interrupt 1
{
    TH0=0;
    TL0=0;
    Do Something
}

 

Creative Commons license icon Creative Commons license icon