4-10 STM Timer/Counter 與比較吻合輸出 HT66F50
STM 內部結構
STM (Standard type TM)提供5種模式 Compare match Timer/Counter PWM Input Capture Single pulse output
STM 模組編號 HT66F50 為TM2 STM寬度16bit TM輸入腳位 TCK2 TM輸出腳位 TP2_0, TP2_1 透過特殊功能暫存器TMnC1 , TMnC2 設定
STM寬度16bit , TMnD[15:0] 藉由TnCK[2:0]選擇七種不同計數時脈 與TMnA[15:0] 或TNRP[7:0]比較
INT0F INT0E EMI [中斷名稱 INT0] INCT0 (fig 4-10-3) INT0F INT0E EMI [中斷名稱 INT0] INT1F INT1E EMI [中斷名稱 INT1] CP0F CP0E EMI [中斷名稱 Comparator 0] INCT1 CP1F CP1E EMI [中斷名稱 Comparator 1] MF0F MF0E EMI [中斷名稱 Multi-function 0] MF1F MF1E EMI [中斷名稱 Multi-function 1] ADF ADE EMI [中斷名稱 A/D]
MFIO (fig 4-10-3) T0PF TOPE [中斷名稱 TM0P] T0AF T0AE [中斷名稱 TM0A] T2PF T2PE [中斷名稱 TM2P] T2AF T2AE [中斷名稱 TM2A]
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 與 STM CCRA 比對吻合中斷
MFI0 特殊功能暫存器 T2AF T2PF T0AF T0PF T2AE T2PE T0AE T0PE Bit 7 6 5 4 3 2 1 0
TM2C0 控制暫存器(表2-5-18) Bit 7 6 5 4 3 2 1 0 Bit [7] ; TnPAU = 1, 暫停計數 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,停止計數
TM2C0 控制暫存器 TnPAU TnCK2 TnCK1 TnCK0 TnON - - - Bit 7 6 5 4 3 2 1 0
TM2C1 控制暫存器 Bit [7 6] ; TnM1 TnM0 = 00 , STM為比對吻合輸出模式 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為計時/計數
TM2C1 控制暫存器 Bit [5 4] ; TnIO[1:0]: TP2_0, TP2_1 為功能選擇位元 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 無作用
TM2C1 控制暫存器 TnM1 TnM0 TnIO1 TnIO0 TnOC TnPOL TnDPX TnCCLR Bit 7 6 5 4 3 2 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 比對吻合時或計數器溢位清除計數器
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
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 ;Bit [6 5 4] ; TnCK2 TnCK1 TnCK0 ;= 000 , fINT= fsys/4 MOV A,11000001B ;STM IS IN THE TIMER/COUNTER MODE, ;Bit[7 6]=11 , STM為計時/計數 MOV TM2C1,A ; Bit[0]=1, 當比較器A 比對吻合時清除計數器 ;AND TP2CNT IS CLEARED BY TP2CCRA ; COMPARE MATCH CLR T2AF ;清除MF0F 與T2AE 中斷旗標 CLR MF0F SET EMI ;中斷總開關
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
;=================================================================== ; 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
;======================================================================================== ; 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
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
當CPU執行DELAY副程式時 STM仍繼續計數 待其計數值與TM2A暫存器所設定之參數產生比對吻合時 以中斷方式讓CPU跳至014h執行