Download
4 10 stm timer counter n.
Skip this Video
Loading SlideShow in 5 Seconds..
4-10 STM Timer/Counter 與比較吻合輸出 PowerPoint Presentation
Download Presentation
4-10 STM Timer/Counter 與比較吻合輸出

4-10 STM Timer/Counter 與比較吻合輸出

249 Vues Download Presentation
Télécharger la présentation

4-10 STM Timer/Counter 與比較吻合輸出

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. 4-10 STM Timer/Counter 與比較吻合輸出 HT66F50

  2. STM 內部結構

  3. STM (Standard type TM)提供5種模式 • Compare match • Timer/Counter • PWM • Input Capture • Single pulse output

  4. STM 模組編號 HT66F50 為TM2 • STM寬度16bit • TM輸入腳位 TCK2 • TM輸出腳位 TP2_0, TP2_1 • 透過特殊功能暫存器TMnC1 , TMnC2 設定

  5. INCT1 特殊功能暫存器 ADF MF1F MF0F CP1F ADE MF1F MF0E CP1E Bit 7 6 5 4 3 2 1 0 MF0E為 enable bits 對應 enable bits : T0PE, T0AE, T2PE, T2AE

  6. MFI0 特殊功能暫存器 T2AF T2PF T0AF T0PF T2AE T2PE T0AE T0PE Bit 7 6 5 4 3 2 1 0

  7. TM2C0 控制暫存器(表2-5-18) n=2 for HT66F50 TnPAU TnCK2 TnCK1 TnCK0 TnON TnRP2 TnRP1 TnRP0 Bit 7 6 5 4 3 2 1 0 Bit [7] ; TnPAU = 1, 暫停計數 = 0, 繼續計數 Bit [6 5 4] ; TnCK2 TnCK1 TnCK0 = 000 , fINT= fsys/4 =100, fINT= fINT = 001 , fINT= fsys =101 ,保留 = 010 , fINT= fsys/16 =110 , fINT=TCKn = 011 , fINT= fsys/64 =111 , fINT= TCKn’ Bit [3] ;TnON =1 ,開始計數 =0,停止計數

  8. TM2C0 控制暫存器 TnPAU TnCK2 TnCK1 TnCK0 TnON - - - Bit 7 6 5 4 3 2 1 0 Bit 2:0 保留

  9. TM2C1 控制暫存器 TnM1 TnM0 TnIO1 TnIO0 TnOC TnPOL TnDPX TnCCLR Bit 7 6 5 4 3 2 1 0 Bit [7 6] ; TnM1 TnM0 = 00 , STM為比對吻合輸出模式 = 01 , STM為輸入補捉模式 = 10 , STM為PWM / single pulse mode = 11 , STM為計時/計數

  10. TM2C1 控制暫存器 TnM1 TnM0 TnIO1 TnIO0 TnOC TnPOL TnDPX TnCCLR Bit 7 6 5 4 3 2 1 0 Bit [5 4] ;TnIO[1:0]: TP2_0, TP2_1 為功能選擇位元 若STM為比對吻合輸出 T2M[1:0]=00 TP2_0, TP2_1 為輸出 00: 比對吻合時,輸出不變 10: 比對吻合時,輸出高態 01: 比對吻合時,輸出低態 11: 比對吻合時,輸出轉態 若STM為比對輸入補捉 T2M[1:0]=01 TP2_0, TP2_1 為輸入 00: 在TP2_0, TP2_1輸入為正緣時,記錄TM2D 01: 在TP2_0, TP2_1輸入為負緣時,記錄TM2D 10: 在TP2_0, TP2_1輸入為負緣及正緣時,記錄TM2D 11: 停止輸入補捉功能 若STM為PWM/Single Pulse Mode,T2M[1:0]=10 00: 強制輸出為非啟動 10: PWM輸出 01: 強制輸出為啟動 11: Single Pulse Mode 若STM為計時/計數, T2M[1:0]=11 無作用

  11. TM2C1 控制暫存器 TnM1 TnM0 TnIO1 TnIO0 TnOCTnPOL TnDPX TnCCLR Bit 7 6 5 4 32 1 0 Bit [3] ;TnOC 輸出準位控制位元 若STM為比對吻合輸出 TP2M[1:0]=00 1: 首次比對吻合前,輸出維持在1 0:首次比對吻合前,輸出維持在0 若STM為PWM/Single Pulse Mode,T2M[1:0]=10 1: 輸出啟動準位為1 0: 輸出啟動準位為0 Bit [2] ; TnPOL 輸出極性控制位元 1: 輸出反向 0: 輸出不反向 Bit [0] ; TnCCLR =1, 當比較器A 比對吻合時清除計數器 =0,當比較器p 比對吻合時或計數器溢位清除計數器

  12. 4-10.ASM (fig 4-10-2) ;PROGRAM : 4-10.ASM ;FUNCTION: GENERATE TONE DO-DO BY TIME INTERRUPT SKILL OF STM #INCLUDE HT66F50.INC #INCLUDE 4-10.INC ;=========================================================== MY_DATA .SECTION 'DATA' ;==DATA SECTION== DEL1 DB ? ;DELAY LOOP COUNT1 DEL2 DB ? ;DELAY LOOP COUNT2 DEL3 DB ? ;DELAY LOOP COUNT3 COUNT DB ? ;PITCH COUNT STACK_A DB ? ;STACK BUFFER FOR ACC STACK_STATUS DB ? ;STACK BUFFER FOR PSW ;===========================================================

  13. STM is 16 bit timer MY_CODE .SECTION 'CODE' ;==PROGRAM SECTION== ORG 00H ;HT-66FX0 RESET VECTOR JMP INIT ORG 14H ;HT-66FXX STM INTERRUPT VECTOR JMP ISR_STM INIT: CLR SPK_PORTC ;CONFIG SPK_PORT AS OUTPUT MODE SET MF0E ;ENABLE [STM] INTERRUPT SET T2AE ;ENABLE T2CCRA COMPARE INTERRUPT CLR TM2C0 ;fINT=fSYS/4=1MHz MOV A,11000001B ;STM IS IN THE TIMER/COUNTER MODE, MOV TM2C1,A ;AND TP2CNT IS CLEARED BY TP2CCRA ; COMPARE MATCH CLR T2AF ;清除MF0F 與T2AE 中斷旗標 CLR MF0F SET EMI

  14. MAIN: MOV A,TAB_PITCH MOV TBLP,A ;INITIAL TABLE POINTER MOV A,15 MOV COUNT,A ;SET COUNTER FOR PITCH TABLE NEXT_PITCH: TABRDL TM2AL ;LOAD PITCH TO TP2CCRA PE-LOAD REGISTER MOV A,TBLH MOV TM2AH,A ;PERLOAD TP2CCRA FOR HIGH 8-BIT SET T2ON ;START GTPU COUNTING MOV A,40 ;DELAY 0.4 SEC FOR EACH TONE CALL DELAY CLR T2ON ;STOP STM COUNTING INC TBLP ;UPDATE TABLE POINTER SDZ COUNT ;WERE ALL PITCH PLAYER? JMP NEXT_PITCH ;NO,NEXT PITCH JMP MAIN ;YES,RESTART

  15. ;===================================================================;=================================================================== ; STM INTERRUPT SERVICE ROUTINE ;=================================================================== ISR_STM: MOV STACK_A,A ;PUSH A MOV A,STATUS MOV STACK_STATUS,A ;PUSH STATUS CLR T2AF MOV A,00010000B XORM A,SPK_PORT ;CONPLEMENT PC.4 MOV A,STACK_STATUS MOV STATUS,A ;POP STATUS MOV A,STACK_A ;POP A RETI

  16. ;========================================================================================;======================================================================================== ; PROC : DELAY ; FUNC : DEALY ABOUT ACC*10mS @fSYS=4MHz ; PARA : ACC : DELAY FACTOR ; REG : DEL1,DEL2,DEL3 ;======================================================================================== DELAY PROC MOV DEL1,A ;SET DEL1 COUNTER DEL_1: MOV A,30 MOV DEL2,A ;SET DEL2 COUNTER DEL_2: MOV A,110 MOV DEL3,A ;SET DEL3 COUNTER DEL_3: SDZ DEL3 ;DEL3 DOWN COUNT JMP DEL_3 SDZ DEL2 ;DEL2 DOWN COUNT JMP DEL_2 SDZ DEL1 ;DEL1 DOWN COUNT JMP DEL_1 RET DELAY ENDP

  17. ORG LASTPAGE ;DEFINE LASTPAGE TAB_PITCH: ;PITCH CONSTANT FOR fINT=fSYS/4 DC fSYS/(523*2*4) ;DO TONE TIME COUNSTANT DC fSYS/(587*2*4) ;RE TONE TIME COUNSTANT DC fSYS/(659*2*4) ;MI TONE TIME COUNSTANT DC fSYS/(698*2*4) ;FA TONE TIME COUNSTANT DC fSYS/(785*2*4) ;SO TONE TIME COUNSTANT DC fSYS/(880*2*4) ;LA TONE TIME COUNSTANT DC fSYS/(998*2*4) ;TI TONE TIME COUNSTANT DC fSYS/(523*2*2*4) ;DO. TONE TIME COUNSTANT DC fSYS/(587*2*2*4) ;RE. TONE TIME COUNSTANT DC fSYS/(659*2*2*4) ;ME. TONE TIME COUNSTANT DC fSYS/(698*2*2*4) ;FA. TONE TIME COUNSTANT DC fSYS/(785*2*2*4) ;SO. TONE TIME COUNSTANT DC fSYS/(880*2*2*4) ;LA. TONE TIME COUNSTANT DC fSYS/(998*2*2*4) ;TI. TONE TIME COUNSTANT DC fSYS/(523*2*4*4) ;DO. TONE TIME COUNSTANT END

  18. 當CPU執行DELAY副程式時 STM仍繼續計數 • 待其計數值與TM2A暫存器所設定之參數產生比對吻合時 以中斷方式讓CPU跳至014h執行