SM59R Timer2 功能使用方法 Application Note Timer 2 功能使用方法 1 適用產品:SM59R16A2 / SM59R08A2 2 Timer 2 使用概述: 2.1 不僅是一個 16-bit timer,更擁有四通道的比較模式(compare)、捕捉模式(capture)及重載 模式(reload functions). 類似 PCA(programmable counter array)功能. 2.2 Timer2 有三種操作模式: 1. Timer 2 mode 2. Event counter mode 3. Gated timer mode 3 Timer 2 相關暫存器: Mnemonic T2CON CCEN TL2 TH2 CRCL CRCH CCL1 CCH1 CCL2 CCH2 CCL3 CCH3 Description Direct Timer 2 control Compare/Capture Enable register Timer 2, low byte Timer 2, high byte Compare/Reload/ Capture register, low byte Compare/Reload/ Capture register, high byte Compare/Capture register 1, low byte Compare/Capture register 1, high byte Compare/Capture register 2, low byte Compare/Capture register 2, high byte Compare/Capture register 3, low byte Compare/Capture register 3, high byte C8h C1h CCh CDh Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Timer 2 and Capture Compare Unit T2PS CC0FR T2R[1:0] COCA COCA COC COCA COCA H3 L3 AH2 L2 H1 TL2[7:0] TH2[7:0] CAh CRCL[7:0] CBh CRCH[7:0] C2h CCL1[7:0] C3h CCH1[7:0] C4h CCL2[7:0] C5h CCH2[7:0] C6h CCL3[7:0] C7h CCH3[7:0] Bit 2 T2CM COCA L1 Bit 1 Bit 0 T2I[1:0] COCA COC H0 AL0 RESET 00h 00h 00h 00h 00h 00h 00h 00h 00h 00h 00h 00h Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A093 1 Ver. A 2008/08 SM59R Timer2 功能使用方法 Application Note Timer 2 and Capture/Compare Unit Mnemonic: T2CON 7 6 T2PS CC0FR 5 - 4 3 T2R[1:0] 2 T2CM 1 Address: C8h 0 Reset T2I[1:0] 00h T2PS: Prescaler select bit 除頻選擇 T2PS = 0 – timer 2 時脈由系統頻率產生,且預除 12 T2PS = 1 – timer 2 時脈由系統頻率產生,且預除 24 CC0FR: Select active edge:觸發緣選擇 CC0FR = 0 – 設定為負緣觸發 CC0FR = 1 – 設定為正緣觸發 T2R[1:0]: Timer 2 reload mode selection 重載模式選擇 T2R[1:0] = 0X – 重載禁能 T2R[1:0] = 10 – 模式 0;自動重載模式 T2R[1:0] = 11 – 模式 1 T2CM: Timer 2 Compare mode selection 比較模式選擇 T2CM = 0 – 模式 0 T2CM = 1 – 模式 1 T2I[1:0]: Timer 2 input selection 輸入選擇 T2I[1:0] = 00 – Timer 2 stop 停止 T2I[1:0] = 01 –Timer 2 mode: 輸入頻率預除 12 或 24 T2I[1:0] = 10 – Event counter mode:輸入頻率由外部 pin T2 產生 ( T2<=Fosc/2 ) T2I[1:0] = 11 – Gated timer mode:輸入頻率由 Gate 產生 (pin T2 =1 致能;pin T2=0 禁能) Mnemonic: CCEN 7 6 COCAH3 COCAL3 5 COCAH2 4 COCAL2 3 COCAH1 2 COCAL1 1 COCAH0 Address: C1h 0 Reset COCAL0 00h COCAH3,COCAL3: Compare/capture mode for Channel 3. COCAH3 COCAL3 Function 0 0 比較 / 捕捉模式禁能 0 1 當 pin CC3 有正緣訊號時,執行捕提模式 1 0 比較模式致能 1 1 當暫存器 CCL3 寫入任意值時,執行捕提模式 COCAH2,COCAL2: Compare/Capture mode for Channel 2. COCAH2 COCAL2 Function 0 0 比較 / 捕捉模式禁能 0 1 當 pin CC2 有正緣訊號時,執行捕提模式 1 0 比較模式致能 1 1 當暫存器 CCL2 寫入任意值時,執行捕提模式 Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A093 2 Ver. A 2008/08 SM59R Timer2 功能使用方法 Application Note COCAH1,COCAL1: Compare/Capture mode for Channel 1. COCAH1 COCAL1 Function 0 0 比較 / 捕捉模式禁能 0 1 當 pin CC1 有正緣訊號時,執行捕提模式 1 0 比較模式致能 1 1 當暫存器 CCL1 寫入任意值時,執行捕提模式 COCAH0,COCAL0: Compare/Capture mode for CRC register (Channel 0) COCAH0 COCAL0 Function 0 0 比較 / 捕捉模式禁能 0 1 當 pin CC0 有正緣訊號時,執行捕提模式 1 0 比較模式致能 1 1 當暫存器 CRCL 寫入任意值時,執行捕提模式 Timer mode T2I(Timer 2 input selection)輸入選擇=[0:1]: timer 2時脈由系統頻率Fosc產生,且預除12或24 Event counter mode T2I(Timer 2 input selection)輸入選擇=[1:0]: 輸入頻率由外部pin T2產生 ( T2<=Fosc/2 ) Gated timer mode T2I(Timer 2 input selection)輸入選擇=[1:1]:輸入頻率由Gate產生,pin T2為致能引腳. pin T2=1,致能gated mode. pin T2=0,禁能gated mode. Reload of Timer 2 (Timer 2的重載模式) 重載模式有以下兩種: Mode 0: 每當Timer 2的TH2/TL2溢位,TH2/TL2即會由CRCH/CRCL自動重載(自動重載模式) Mode 1: 每當 pin T2EX 有一負緣訊號輸入,TH2/TL2 即會由 CRCH/CRCL 做一次重載(即 event mode);並可由 IEN1 設定,產生外部重載中斷(external reload). (EXF2: Timer 2 external reload flag, must be cleared by software) Compare function (Timer 2的比較模式) 四個獨立的比較位元,SFR TH2/TL2與CCHX/CCLX內容做比較.比較模式可由T2CM選擇mode 0 或1,CC0~CC3比較的結果會相對應輸出至P1.2~P1.5. Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A093 3 Ver. A 2008/08 SM59R Timer2 功能使用方法 Application Note Compare Mode 0 當設定[COCAHx:COCALx]=[1:0]為比較模式時: 1. 當SFR TH2/TL2等於CCHX/CCLX(或CRCH/CRCL)時,CCx輸出為low to high; 2. 當TH2/TL2溢位時,CCx輸出為high to low; 3. 此模式對於P1.2~P1.5寫入訊號皆是無效的,因輸入訊號和寫入栓鎖器是沒有聯接的. Fig. 8-1: Compare mode 0 function Compare Mode 1 當SFR TH2/TL2等於CCHX/CCLX(或CRCH/CRCL)時,CCx輸出會做一次的反相;當TH2/TL2 溢位時狀態並不會改變. Fig. 8-2: Compare mode 1 function Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A093 4 Ver. A 2008/08 SM59R Timer2 功能使用方法 Application Note 4 以下是 Timer 2 中斷有關的致能旗標: Mnemonic IEN0 IEN1 Description Interrupt Enable 0 register Interrupt Enable 1 register Direct Bit 7 Bit 6 Bit 5 Interrupt Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 RESET A8h EA - ET2 ES0 ET1 EX1 ET0 EX0 00h B8h EXEN2 - IEIIC - IEEEI IEADC IESPI - 00h 1 ET0 Address: A8h 0 Reset EX0 00h Interrupt Enable 0 register(IEN0) Mnemonic: IEN0 7 6 EA - 5 ET2 4 ES0 3 ET1 2 EX1 EA: EA = 0 : disable all interrupt. EA = 1 : enable all interrupt. ET2: ET2 = 0 : disable Timer 2 overflow or external reload interrupt. ES0: ES0 = 0 : disable Serial channel 0 interrupt. ET1: ET1 = 0 : disable Timer 1 overflow interrupt. EX1: EX1 = 0 : disable external interrupt 1. ET0: ET0 = 0 : disable Timer 0 overflow interrupt. EX0: EX0 = 0 : disable external interrupt 0. Interrupt Enable 1 register(IEN1) Mnemonic: IEN1 6 7 EXEN2 5 IEIIC 4 - 3 IEEEI 2 IEADC 1 IESPI Address: B8h 0 Reset 00h EXEN2: Timer 2 reload interrupt enable EXEN2 = 0 : disable Timer 2 external reload interrupt. IEIIC: IIC interrupt enable. IEIICS = 0 : disable IIC interrupt. IEEEI: EEI interrupt enable IEEEI = 0 : disable EEI interrupt IEADC : A/D converter interrupt enable IEADC = 0 : disable ADC interrupt. IESPI: SPI interrupt enable. IESPI = 0 : disable ISPI interrupt. Interrupt request register(IRCON) Mnemonic: IRCON 5 7 6 IICIF EXF2 TF2 4 - 3 EEIIF 2 ADCIF 1 SPIIF Address: C0h 0 Reset 00h EXF2: Timer 2 external reload flag, must be cleared by software. TF2: Timer 2 overflow flag, must be cleared by software. IICIF: IIC interrupt flag, must be cleared by software. EEIIF: EEI interrupt flag, must be cleared by software. ADCIF: A/D converter interrupt flag, must be cleared by software SPIIF: SPI interrupt flag, must be cleared by software. Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A093 5 Ver. A 2008/08 SM59R Timer2 功能使用方法 Application Note 以下是 Timer 2 相對應的中斷向量表: Table 11-1: Interrupt vectors IE0 – External interrupt 0 TF0 – Timer 0 interrupt Interrupt Vector Address 0003h 000Bh Interrupt Number *(use Keil C Tool) 0 1 IE1 – External interrupt 1 0013h 2 TF1 – Timer 1 interrupt RI0/TI0 – Serial channel 0 interrupt 001Bh 0023h 3 4 TF2/EXF2 – Timer 2 interrupt SPIIF – SPI interrupt 002Bh 004Bh 5 9 ADCIF – A/D converter interrupt EEIIF – Expanded External Interrupt IICIF – IIC interrupt RI1/TI1 – Serial channel 1 interrupt 0053h 005Bh 006Bh 0083h 10 11 13 16 Interrupt Request Flags *See Keil C about C51 User’s Guide about Interrupt Function description Timer 2 中斷應用參考程序: (1) 中斷致能設罝: IEN0 |= 0xA0; //Enable interrupt All & Timer 2 interrupt IEN1 |= 0x80; //Enable Timer 2 external reload interrupt (2) 中斷程序表示: void T2_interrupt(void) interrupt 5 using 1 { if(EXF2==1) { EXF2=0; //T2EX - external interrupt } if(TF2==1) { TF2=0; //over flow interrupt } P2 =~P2; //detect interrupt freq. } Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A093 6 Ver. A 2008/08 SM59R Timer2 功能使用方法 Application Note 5 Timer 2 範例程式的流程圖: 流程圖及範例程式中的設定可參考下表: Entry SyncMOS Timer 2 function < Step1. > 1. MCU init 2. LCD init < Step2. > 1. ScanKeypad(SW) 2. OSD on LCD SW = 0~15 False < SW = 00 > < SW = 01 > Ture . . . . . Compare Reload Mode Mode SW Function Input sel descrip. 0 init T2CON 0 1 init T2CON 0 0 internal freq. mode input freq.= Fosc / 12 2 init T2CON 0 1 internal freq. mode input freq.= Fosc / 12 3 init T2CON 1 4 init T2CON 1 0 internal freq. mode input freq.= Fosc / 12 5 init T2CON 1 1 internal freq. mode input freq.= Fosc / 12 6 init T2CON 0 0 Stop Disable internal freq. mode input freq.= Fosc / 12 Disable internal freq. mode input freq.= Fosc / 12 Stop 7 init T2CON 0 0 Event counter mode 8 init T2CON 0 0 Gated timer mode Enable: pin T2 = high; Disable:pin T2 = low 9 init T2CON 1 1 Gated timer mode Enable: pin T2 = high; Disable:pin T2 = low 10 CCx set write CCHx/CCLx 11 CCx set write CCHx/CCLx 12 CCEN set all disable 13 CCEN set capture at pin CCx edge signal 14 CCEN set all compare enable 15 CCEN set capture on write operation into reg. CCLx(or CRCL) External freq. from pin T2 < SW = 14 > < SW = 15 > 範例程式的操作及量測可參考以下: No. User operate Effect: Ex1. Reset ÆSW =0: Timer 2 interrupt 65.535ms Ex2. Reset ÆSW =1: Timer 2 interrupt 1ms Ex3. Reset ÆSW =2ÆSW =14ÆSW=10 Timer 2 interrupt 65.535ms Ex4. Reset ÆSW =2ÆSW =14ÆSW=11 Pin CC3 =2ms; CC2 =3ms; CC1 =6ms ; CC0 =10ms; Timer 2 interrupt 65.535ms Pin CC3 =10ms; CC2 =6; CC1 =3ms; CC0 =2ms; Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A093 7 Ver. A 2008/08 SM59R Timer2 功能使用方法 Application Note 6 Timer 2 應用的範例程式 Description Main program //此範例應用包括 timer 2、LCD 及 4x4 Switch Key 來呈現 timer 2 的過程及結果 //Fosc = internal 12MHz //==================================================================== // // S Y N C M O S T E C H N O L O G Y // //==================================================================== #include "..\h\SM59R16A2.h" #include "..\h\SM59R16A2_ExtraDef.h" #include "..\MISC\Delay.h" #include "..\MISC\4x4Keypad.h" #include "..\LCD\LCD16x2.h" unsigned char SW = 0x00, int_EXF2 = 0; unsigned int int_TF2 = 0; void T2_interrupt(void) interrupt 5 using 1 { if(TF2==1) { TF2=0; //over flow interrupt int_TF2++; } if(EXF2==1) { EXF2=0; //T2EX - external interrupt int_EXF2++; } P2 =~P2; //detect interrupt freq. } void init_Timer2_T2CON( unsigned char T2_T2PS, unsigned char T2_CC0FR, unsigned char T2_T2R, unsigned char T2_T2CM, unsigned char T2_T2I ) { T2CON = 0x00; T2CON |= (T2_T2PS | T2_CC0FR | T2_T2R | T2_T2CM | T2_T2I); } void MCU_init(void) { TH2 = 0xEC; //5ms=1us*5000 at 12Mhz/12 TL2 = 0x77; CRCH = 0XFC; //1ms=1us*1000 at 12Mhz/12 CRCL = 0x17; IEN0 |= 0xA0; //Enable interrupt All & Timer 2 interrupt IEN1 |= 0x80; //Enable Timer 2 external reload interrupt } void OSD(void) { PrintLcdStrLX( 1, 0, "SW: T2CON: "); //Delay10mSec(1); Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A093 8 Ver. A 2008/08 SM59R Timer2 功能使用方法 Application Note SetCursorAddr(1, 3); PrintLcdDec(SW); SetCursorAddr(1,14); PrintLcdHex(T2CON); PrintLcdStrLX( 2, 0, "TF2: SetCursorAddr(2, 4); PrintLcdHex(int_TF2); SetCursorAddr(2,14); PrintLcdHex(int_EXF2); EXF2: "); //Delay10mSec(1); } void main(void) { Delay10mSec(2); //wait for system stable MCU_init(); LCD_Init(); Delay10mSec(2); //wait for LCD init while(1) { SW = ScanKeypad(); OSD(); //fuction set if SW =0~15 //fuction idle if SW =255 //On LCD display switch(SW) { case 0: init_Timer2_T2CON( T2_CLK12, T2_FALL_EDGE, T2_REL_DIS, T2_CMP_MODE0, T2_INF ); //1. Timer 2 interrupt 65.535ms //2. TH2&TL2 count-16bit from 0x0000 to 0xFFFF //3. if CCEN set compare mode and CCHx/CCLx(or CRC) give value // Compare mode 0 CCX will output break; case 1: init_Timer2_T2CON( T2_CLK12, T2_FALL_EDGE, T2_REL_MODE0, T2_CMP_MODE0, T2_INF ); //Timer 2 interrupt 1ms //Auto Reload Mode 0:TH2&TL2 will reload from CRCH&CRCL when overflow break; case 2: init_Timer2_T2CON( T2_CLK12, T2_FALL_EDGE, T2_REL_MODE1, T2_CMP_MODE0, T2_INF ); //1. Timer 2 interrupt 65.535ms //2. TH2&TL2 count-16bit from 0x0000 to 0xFFFF //3. if CCEN set compare mode and CCHx/CCLx(or CRC) give value // Compare mode 0 CCX will output break; case 3: init_Timer2_T2CON( T2_CLK12, T2_FALL_EDGE, T2_REL_DIS, T2_CMP_MODE1, T2_INF ); //Timer 2 interrupt 65.535ms //TH2&TL2 count-16bit from 0x0000 to 0xFFFF break; case 4: Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A093 9 Ver. A 2008/08 SM59R Timer2 功能使用方法 Application Note init_Timer2_T2CON( T2_CLK12, T2_FALL_EDGE, T2_REL_MODE0, T2_CMP_MODE1, T2_INF ); //Timer 2 interrupt 1ms //Auto Reload Mode 0:TH2&TL2 will reload from CRCH&CRCL when overflow break; case 5: init_Timer2_T2CON( T2_CLK12, T2_FALL_EDGE, T2_REL_MODE1, T2_CMP_MODE1, T2_INF ); //Timer 2 interrupt 65.535ms //TH2&TL2 count-16bit from 0x0000 to 0xFFFF break; case 6: init_Timer2_T2CON( T2_CLK12, T2_FALL_EDGE, T2_REL_MODE0, T2_CMP_MODE0, T2_STOP ); //Timer 2 Stop break; case 7: init_Timer2_T2CON( T2_CLK12, T2_FALL_EDGE, T2_REL_MODE0, T2_CMP_MODE0, T2_EXF ); //External freq. from pin T2 //Fosc/2 >= pin T2 break; case 8: init_Timer2_T2CON( T2_CLK12, T2_FALL_EDGE, T2_REL_MODE0, T2_CMP_MODE0, T2_GATED ); //if pin T2 = high // Timer 2 enable // Timer 2 interrupt 1ms // Auto-reload //else if pin T2 = low // Timer 2 Disable break; case 9: init_Timer2_T2CON( T2_CLK12, T2_FALL_EDGE, T2_REL_MODE1, T2_CMP_MODE1, T2_GATED ); //if pin T2 = high // Timer 2 enable // Timer 2 interrupt 65.535ms //else if pin T2 = low // Timer 2 Disable break; case 10: //CCx set CCH3 = 0xF8; //2ms CCL3 = 0x30; CCH2 = 0xF4; //3ms CCL2 = 0x48; CCH1 = 0xE8; //6ms CCL1 = 0x90; CRCH = 0xD8; //10ms CRCL = 0xF0; break; case 11: //CCx set CCH3 = 0xD8; //10ms CCL3 = 0xF0; CCH2 = 0xE8; //6ms Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A093 10 Ver. A 2008/08 SM59R Timer2 功能使用方法 Application Note CCL2 = 0x90; CCH1 = 0xF4; CCL1 = 0x48; CRCH = 0xF8; CRCL = 0x30; break; case 12: CCEN = 0x00; break; case 13: CCEN = 0x55; & falling edge at pin CC0 break; case 14: CCEN = 0xAA; break; case 15: CCEN = 0xFF; CCL3/CCL2/CCL1/CRCL break; default: break; //3ms //2ms //all mode disable //capture on rising edge at pin CC3/CC2/CC1/CC0 //all compare enable, compare mode 0:CCX output //capture on write operation into register } Delay10mSec(8); //SW delay time } } Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A093 11 Ver. A 2008/08