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-A094 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-A094 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-A094 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-A094 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-A094 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-A094 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-A094 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-A094 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-A094 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 CCL2 = 0x90; Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A094 10 Ver. A 2008/08 SM59R Timer2 功能使用方法 Application Note 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-A094 11 Ver. A 2008/08