回首頁ㄌ 向上一頁
8051簡介
                 基礎實習 串列埠實驗 時間中斷實驗 外部中斷實驗

   使用Timer1模式1輪詢法 使用Timer0模式0中斷法 使用Timer0模式0輪詢法  使用Timer0模式1中斷法  

使用Timer1模式2輪詢法  使用Timer1模式3輪詢法  使用Timer0模式3輪詢法

實習(使用Timer0模式1輪詢法

實習目的:

使用Timer0模式1( 16位元計時器 )輪詢法

材料:

              8051模擬板,電腦。

說明:   

使用計時器之中斷來延遲1秒使跑馬燈左旋

程式:

 1                   ORG           0000H

 2                   MOV          TMOD, # 0000  0001B

 3                   MOV          A, # 1111 1110B                開始時,令最右邊的  LED   

 4 LOOP:        M0V           P1,A                                    A  的內容送至  P1  

 5                   CALL         DELAY                              延時  1 

 6                   RL              A                                        A 的內容左旋  1  位元

 7                   AJMP       LOOP

 8 DEALY:    MOV           R4, # 20                              欲延時50ms*20=1000ms=  1 秒

 9 TIMER:     MOV           TL0, # < ( 65536-50000)     設定 Timer 0 工作於模式  1 (即16 位元計時器)欲設定 TIMER 1時改為#00010000                               

10                  M0V           TH0, # > (65536-50000)      可計時1us*50000=50ms                   

11                  SETB          TR0                                     起動計時器  0

12WAIT:        JBC            TF0, OVER                         ;等待  TF0 =  1 (即等待時間 

13                   JMP            WAIT                                   50ms 到 )

14 OVER:      CLR            TF0                                      ;清除 TF0  ,使  TF0=0

15                  DJNZ         R4,TIMER                            ;若時間  1 秒來到 , 則繼續計時, 時間 1秒到返回主程式       

16                   RET                  

17                    END        

實習(使用Timer1模式1輪詢法

實習目的:

使用Timer1模式1( 16位元計時器 )輪詢法

材料:

              8051模擬板,電腦。

說明:   

使用計時器之中斷來延遲1秒使跑馬燈左旋

程式:

  1                             ORG           0000H

  2                             MOV          TMOD ,  #  0001 0000B

  3                             MOV           A , #1111 1110B              開始時,令最右邊的  LED   

  4        LOOP:          MOV           P1 , A                                 A  的內容送至  P1

  5                             CALL          DELAY                           延時  1 

  6                             RL              A                                     A  的內容左旋  1  位元

  7                            AJMP         LOOP

  8       DEALY:       MOV         R4 ,  # 20              欲延時 50ms*20=1000ms= 1 秒

  9       TIMER:        MOV          TL1 , # < (65536-50000) ; 設定 Timer 0 工作於模式  1 (即16位元計時器)欲設定 TIMER 1改為#00010000                              

10                           MOV         TH0 , # > (65536-50000) 可計時  1us*50000=50ms                    

 11                          SETB         TR 1                                起動計時器  1     

 12       WAIT:        JBC           TF1 , OVER                     等待  TF1=  1 (即等待時間 

  13                        JMP           WAIT                               50ms 到 ) 

  14      OVER:       CLR           TF1                                 清除 TF1  ,使  TF1=0

  15                        DJNZ          R4 , TIMER                    若時間  1 秒來到 , 則繼續計時, 

                                                                                         時間 1秒到返回主程式          

  16                        RET

  17                        END    

 

實習(使用Timer0模式0輪詢法

實習目的:

使用Timer0模式0( 13位元計時器 )輪詢法

材料:

              8051模擬板,電腦。

說明:   

使用計時器之中斷來延遲1秒使跑馬燈左旋

程式:

   1                  ORG                0000H

   2                                     MOV                TMOD , # 0000 0000B

   3                                     MOV                A , 1111 1110B  

   4             LOOP:           MOV                P1 , A

   5                                      CALL               DELAY

   6                                       RL                    A

   7                                        AJMP                LOOP

   8               DEALY:         MOV                 R4 ,  # 20

   9               TIMER:          MOV                TL0 , # (8192-5000) .MOD . 32

  10                                       MOV                TH0 , # (8192-5000) /32

  11                                        SETB         TR 0

  12               WAIT:           JBC           TF0 , OVER

  13                                       JMP           WAIT

  14               OVER:             CLR           TF1

  15                                       DJNZ          R4 , TIMER

  16                                        RET

  17                                         END

實習(使用Timer1模式2輪詢法

實習目的:

使用Timer1模式2( 兩個獨立8位元計時器 )輪詢法

材料:

              8051模擬板,電腦。

說明:   

使用計時器之中斷來延遲1秒使跑馬燈左旋

程式:

1                             ORG                0000H

2                             MOV               A , # 1111 1110B

3                             MOV               TMOD , # 0000 0010B

4           LOOP:       MOV                P1 , A

5                             CALL               DELAY                   ;延時 1 秒

6                             RL                    A

7                             JMP                  LOOP

8      DELAY:         MOV                R3 , # 20

9                             MOV                TH0 , # ( 256-250 )

10                           MOV                TL0 , # ( 256-250 )

11    WAIT1:          SETB                TR0

12                           MOV                R4 , # 200

13   WAIT:            JBC                  TF0 , OVER           ;輪詢     250us

14                          AJMP               WAIT

15  OVER:            DJNZ               R4 , WAIT               ;計時 200*250us=50ms

16                         DJNZ               R3 , WAIT1             ;50ms*20= 1sec

17                         RET

18                          END          

實習(使用Timer0模式3輪詢法

實習目的:

使用Timer0模式3 ( 兩個獨立8位元計時器 )輪詢法

材料:

              8051模擬板,電腦。

說明:   

使用計時器之中斷來延遲1秒使跑馬燈左旋

程式:

1                                                           ORG                  0000H

2                                                           MOV                 A , # 1111 1110B

3                           LOOP:                     MOV                 P1 , A

4                                                           CALL                DELAY

5                                                           RL                     A

6                                                           JMP                   LOOP

7                           DELAY:                  MOV                 R3 , # 20

8                                                           MOV                 TMOD , # 0000 0011B

9                           TIMER1:                  MOV                 R4 , # 200

10                         TIMER :                   MOV                 TL0 , # (256-250)

11                                                         SETB                 TR0

12                         WAIT :                    JB                      TF0 , OVER

13                                                         JMP                   WAIT

14                         OVER :                    CLR                  TF0

15                                                         DJNZ                R4 , TIMER

16                                                         DJNZ                R3 , TIMER1

17                                                         RET

18                                                          END

實習(使用Timer1模式3輪詢法

實習目的:

使用Timer1模式3( 兩個獨立8位元計時器 )輪詢法

材料:

              8051模擬板,電腦。

說明:   

使用計時器之中斷來延遲1秒使跑馬燈左旋

程式:

1                                                           ORG                  0000H

2                                                           MOV                 A , # 1111 1110B

3                           LOOP:                     MOV                 P1 , A

4                                                           CALL                DELAY

5                                                           RL                     A

6                                                           JMP                   LOOP

7                           DELAY:                  MOV                 R3 , # 20

8                                                           MOV                 TMOD , # 0000 0011B

9                           TIMER1:                  MOV                 R4 , # 200

10                         TIMER :                   MOV                 TH0 , # (256-250)

11                                                         SETB                 TR1

12                         WAIT :                    JB                      TF1 , OVER

13                                                         JMP                   WAIT

14                         OVER :                    CLR                  TF1

15                                                         DJNZ                R4 , TIMER

16                                                         DJNZ                R3 , TIMER1

17                                                         RET

18                                                          END

實習(使用Timer0模式1中斷法

實習目的:

使用Timer0模式1 ( 16位元計時器 ) 中斷法

材料:

              8051模擬板,電腦。

說明:   

使用計時器之中斷來延遲1秒使跑馬燈左旋

程式:

1                        ORG         00H                                

2                        JMP          START

3                        ORG        0BH                        ;計時器0的中斷服務程式 

4                        JMP         TIMER0_T              , 起始位址一定在0000BH

5                        ORG        50H

6    START:       MOV        A , #1111 1110B         令最右邊 LED 亮

7                       MOV         P1 , A

8                       MOV         R4 , #20                     ;R4=20,以便延時50ms * 20=1秒

9                       MOV         TMOD , # 0000 0001B     ;計時器0工作於模式1

10                     MOV         TH0 , # < ( 65536-50000);設定計數值 , 以便計時 50ms

11                     MOV         TL0 , # > ( 65536-50000)

12                     SETB         EA                                   ;計時器0,中斷致能

13                     SETB         ET0                                      

14                     SETB         TR0                                  ;起動計時器 0  

15  PALSE:       JMP           PALSE                             ;暫停於本位址,等待中斷致能

16  TIMER0_T: MOV        TH0 , # < (65536-50000)   ;重新設定計數值

17                      MOV        TL0 , # >(65536- 50000)

18                      DJNZ        R4 , CONT                       ;R4-1不等於0,表示1秒未到跳至CONT

19                      MOV         R4 , 20                            ;重新設定 R4

20                      RL              A                                                         

21                      MOV          P1 , A

22   CONT:        RETI                                              ;返回主程式

實習(使用Timer0模式0中斷法

實習目的:

使用Timer0模式1 ( 13位元計時器 ) 中斷法

材料:

              8051模擬板,電腦。

說明:   

使用計時器之中斷來延遲1秒使跑馬燈左旋

程式:

1                                                  ORG               00H

2                                                  JMP                START

3                                                  ORG               0BH

4                                                  JMP                TIMER0_T

5                                                  ORG               50H

6                   START:                  MOV              A , #1111 1110B

7                                                  MOV              P1 , A

8                                                  MOV              R4 , #20

9                                                  MOV              TMOD , # 0000 0000B

10                                                MOV              TH0 , # < ( 8192-5000) / 32

11                                                MOV              TL0 , # > ( 8192-5000) . MOD .32

12                                                SETB              EA

13                                                SETB              ET0

14                                                SETB              TR0

15                 PALSE:                   JMP                PALSE

16                 TIMER0_T:             MOV              TH0 , # < (65536-50000)

17                                                 MOV              TL0 , # >(65536- 50000)

18                                                 DJNZ              R4 , CONT

19                                                 MOV               R4 , 200

20                                                 RL                   A

21                                                 MOV               P1 , A

22                  CONT:                    RETI