可程式計數陣列功能使用方法 Application Note 可程式計數陣列(PCA)功能使用方法 1 適用產品:SM59D04G2,SM59D03G2。 2 應用說明: PCA 共有五組,每組皆可工作於以下七種模式: z 捕獲模式-正緣捕獲模式(Positive edge capture mode) 3 z 捕獲模式-負緣捕獲模式(Negative edge capture mode) z 捕獲模式-正緣及負緣捕獲模式(Both positive and negative edge capture mode) z z 計時模式(Timer mode) 高速時脈輸出模式(High Speed Output mode) z 8 位元脈寬調變模式(8-bit PWM mode) z 16 位元脈寬調變模式(16-bit PWM mode) PCA Compare/Capture mode 應用說明 3.1 捕獲模式-正緣捕獲模式(Positive edge capture mode): Capture mode 致能時,PCA 16-bit counter 計數暫存器(CCCH, CCCL)會不斷的上數,當 CC0~CC4 pin 輸入外部訊號,低電位轉換成高電位的訊號將做為捕獲條件,捕獲時產生中斷,並且由硬體將 PCA counter 存至捕獲資料暫存器 CCnD[15:0](CCnDH, CCnDL)。 圖: PCA capture mode with positive edge Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A123 1 Ver. A 2009/03 可程式計數陣列功能使用方法 Application Note 3.2 捕獲模式-負緣捕獲模式(Negative edge capture mode): Capture mode 致能時,PCA 16-bit counter 計數暫存器(CCCH, CCCL)會不斷的上數,當 CC0~CC4 pin 輸入外部訊號,高電位轉換成低電位的訊號將做為捕獲條件,捕獲時產生中斷,並且由硬體將 PCA counter 存至捕獲資料暫存器 CCnD[15:0](CCnDH, CCnDL)。 圖: PCA capture mode with negative edge 3.3 捕獲模式-正緣及負緣捕獲模式(Both positive and negative edge capture mode) Capture mode 致能時,PCA 16-bit counter 計數暫存器(CCCH, CCCL)會不斷的上數,當 CC0~CC4 pin 輸入外部訊號,高電位轉換成低電位,或由低電位轉換成高電位的訊號將做為捕獲條件,捕獲時產生中斷, 並且由硬體將 PCA counter 存至捕獲資料暫存器 CCnD[15:0](CCnDH, CCnDL)。 圖: PCA capture mode with both negative and positive edge Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A123 2 Ver. A 2009/03 可程式計數陣列功能使用方法 Application Note 3.4 計時模式(Timer mode) 當該模式致能,PCA 16-bit counter計數暫存器(CCCH, CCCL)會上數,直到PCA counter溢位時,則會產 生中斷. 圖: PCA Timer mode 3.5 高速時脈輸出模式(High Speed Output mode) 當高速時脈輸出模式(比較模式)致能,每當PCA 16-bit counter計數暫存器(CCCH, CCCL)和資料暫存器 CCnD[15:0]相同時,CCn pin則會做反相訊號輸出。 圖: PCA high speed output Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A123 3 Ver. A 2009/03 可程式計數陣列功能使用方法 Application Note 3.6 8-bit 脈寬調變模式(8-bit PWM mode) 3.6.1 8 位元 PWM 僅使用資料暫存器 CCnDL[7:0],未使用 CCnDH[15:8] 3.6.2 8 位元 PWM 訊號輸出頻率由 bit PCAC2^CCCLK 設定: CCCLK 00 01 10 11 6T Fosc/256 (Fosc/4)/256 (Fosc/12)/256 (CCCI pin frequency )/256 12T (Fosc/2)/256 (Fosc/8)/256 (Fosc/24)/256 (CCCI pin frequency )/256 3.6.3 當使用 8 位元 PCA 之 PWM 功能時需注意如下: 當 CCnDL=0x00 或 0xFF 時 CCn pin 輸出皆為 low; 當 CCnDL=0x01 時 CCn pin 輸出 low pulse 為 2 個 PWM Input clock; 當 CCnDL=0x02 時 CCn pin 輸出 low pulse 為 3 個 PWM Input clock; 當 CCnDL=0xFE 時 CCn pin 輸出 high pulse 為 1 個 PWM Input clock。 Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A123 4 Ver. A 2009/03 可程式計數陣列功能使用方法 Application Note 3.7 16-bit 脈寬調變模式(16-bit PWM mode) 3.7.1 16 位元 PWM 使用資料暫存器 CCnDL[7:0]和 CCnDH[15:8] 3.7.2 16 位元 PWM 訊號輸出頻率由 bit PCAC2^CCCLK 設定: CCCLK 00 01 10 11 6T Fosc/65536 (Fosc/4)/65536 (Fosc/12)/65536 (CCCI pin frequency )/65536 12T (Fosc/2)/65536 (Fosc/8)/65536 (Fosc/24)/65536 (CCCI pin frequency )/65536 3.7.3 當使用 PCA 之 PWM 功能時需注意如下: 4 40L PDI P Pin # 當 CCnD = 0x0000 或 0xFFFF 時 CCn pin 輸出皆為 low; 當 CCnD = 0x0001 時 CCn pin 輸出 low pulse 為 2 個 PWM Input clock; 當 CCnD = 0x0002 時 CCn pin 輸出 low pulse 為 3 個 PWM Input clock; 當 CCnD =0xFFFE 時 CCn pin 輸出 high pulse 為 1 個 PWM Input clock。 Pin Description 44L 44L PLC QFP C Pin Pin # # 3 42 4 4 5 6 7 8 43 44 1 2 3 5 6 7 8 9 Symbol P12/CCCI/EEI2 /ICP_DATA P13/CC0/EEI3 P14/CC1/EEI4 P15/CC2/EEI5 P16/CC3/EEI6 P17/CC4/EEI7 I/O i/o i/o i/o i/o i/o i/o Names Bit 2 of port 1 & PCA External clock input & EEI interrupt 2 & ICP data/command IO Bit 3 of port 1 & PCA Channel 0 & EEI interrupt 3 Bit 4 of port 1 & PCA Channel 1 & EEI interrupt 4 Bit 5 of port 1 & PCA Channel 2 & EEI interrupt 5 Bit 6 of port 1 & PCA Channel 3 & EEI interrupt 6 Bit 7 of port 1 & PCA Channel 4 & EEI interrupt 7 Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A123 5 Ver. A 2009/03 可程式計數陣列功能使用方法 Application Note 5 特殊功能暫存器說明: Mnemonic PCAC1 PCAC2 PCAH PCAL CC0CON CC0DH CC0DL CC1CON CC1DH CC1DL CC2CON CC2DH CC2DL CC3CON CC3DH CC3DL CC4CON CC4DH CC4DL Description PCA control register 1 PCA control register 2 PCA counter high byte PCA counter low byte CC0 control register CC0 data high byte CC0 data low byte CC1 control register CC1 data high byte CC1 data low byte CC2 control register CC2 data high byte CC2 data low byte CC3 control register CC3 data high byte CC3 data low byte CC4 control register CC4 data high byte CC4 data low byte Direct Bit 7 Bit 6 A1h CCR CIDL A2h CCCLK[1:0] Bit 5 PCA Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 RESET - CC4IE CC3IE CC2IE CC1IE CC0IE 00h - CC4IF CC3IF CC2IF CC1IF CC0IF 00h 9Ah PCA[15:8] 00h 9Bh PCA[7:0] 00h A3h - - - TOG0 - CC0MOD[2:0] 00h 9Ch CC0D[15:8] 00h 9Dh CC0D[7:0] 00h A4h - - - TOG1 - CC1MOD[2:0] 00h 9Eh CC1D[15:8] 00h 9Fh CC1D[7:0] 00h A5h - - - TOG2 - CC2MOD[2:0] 00h 91h CC2D[15:8] 00h 92h CC2D[7:0] 00h A6h - - - TOG3 - CC3MOD[2:0] 00h 93h CC3D[15:8] 00h 94h CC3D[7:0] 00h A7h - - - TOG4 - CC4MOD[2:0] 00h 95h CC4D[15:8] 00h 96h CC4D[7:0] 00h Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A123 6 Ver. A 2009/03 可程式計數陣列功能使用方法 Application Note 5.1 PCA 控制暫存器 1(Control register 1) Read/Write Reset Value Bit 7 CCR R/W 0 CIDL R/W 0 0 CC4IE R/W 0 CC3IE R/W 0 CC2IE R/W 0 CC1IE R/W 0 Bit 0 CC0IE R/W 0 CCR: PCA Counter Run control bit: CCR =1,啓動PCA計數,由軟體設定 CCR =0,停止PCA計數,由軟體設定 CIDL: Counter Idle control: CIDL =1,進入閒置模式,但必須要PCON[0] 同時也被致能為”1” CIDL =0,離開閒置模式 CCnIE: CC0 ~ CC4 interrupt enable. CCnIE = 1,中斷致能 CCnIE = 0,中斷禁能 5.2 PCA 控制暫存器 2(Control register 2) Mnemonic: PCAC2 7 6 CCCLK[1:0] R/W 5 - 4 CC4IF R/W 3 CC3IF R/W 2 CC2IF R/W 1 CC1IF R/W Address: A2h 0 Reset CC0IF 00h R/W CCCLK[1:0] 比較捕獲時脈來源選擇(Compare/Capture clock source select) CC0CLK[1:0] Clock source 00 Fosc 01 Fosc/4 10 Fosc/12 11 External clock input (CCCI pin) CCCLK 會和 6T 或 12T 有關,詳細請參考表一、圖一及圖二說明.相關名詞定義如下: System clock frequency(FSYS) = crystal(FCRY) or oscillator(FOSC) frequency in 6T, and System clock frequency(FSYS)= crystal(FCRY) or oscillator(FOSC) frequency divided by 2 in 12T CCnIF CC0 ~ CC4中斷旗標 (CC0 ~ CC4 interrupt flag) 當發生中斷時由硬體設置為”1”,必須由軟體清除. Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A123 7 Ver. A 2009/03 可程式計數陣列功能使用方法 Application Note 表一:計算取樣週期,以使用 Crystal 16MHz 為範例 Machine cycle 6T Fcry (MHz) 16 6T CCCLK[1:0] PCA counter increment Div Sample period(us) 00 1 system clocks 1 0.0625 16 01 2 system clocks 4 0.25 6T 16 10 12 system clocks 12 0.75 12T 16 00 1 system clocks 2 0.125 12T 16 01 2 system clocks 8 0.5 12T 16 10 6T/12T 12 system clocks 24 1.5 External clock input External clock input / 16 11 (CCCI pin) (CCCI pin) 註:CCCLK[1:0]=11 時,取樣週期會依照外部的訊號輸入而定; 其它模式取樣週期(Sample period) = 1 / (Fcry / Div) 圖. PCA clock selection at 6T mode 圖二. PCA clock selection at 12T mode Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A123 8 Ver. A 2009/03 可程式計數陣列功能使用方法 Application Note 5.3 PCA 計數暫存器 1(Counter register 1) Mnemonic: PCAH 7 6 5 4 3 PCA[15:8] R/W 2 1 Address: 9Ah 0 Reset 00h Mnemonic: PCAL 7 6 5 4 3 PCA[7:0] R/W 2 1 Address: 9Bh 0 Reset 00h 5.4 CC0CON (Compare/Capture channel 0 control register) Mnemonic: CC0CON 7 6 5 - 4 TOG0 R/W 3 - Address: A3h 2 1 0 Reset CC0MOD[2:0] 00h R/W CC0MOD[2:0] 比較/捕獲通道0模式選擇(Compare/Capture channel 0 modes select) CC0MOD[2:0] 000 001 010 011 100 101 110 111 TOG0: CC0 toggle register. Function Disable PCA channel 0 Positive edge capture mode Negative edge capture mode Both positive and negative edge capture mode Timer mode High Speed Output mode 8-bit PWM 16-bit PWM 5.5 CC0 Data register Mnemonic: CC0DH 7 6 5 4 3 CC0D[15:8] R/W 2 1 Address: 9Ch 0 Reset 00h Mnemonic: CC0DL 7 6 5 4 3 CC0D[7:0] 2 1 Address: 9Dh 0 Reset 00h Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A123 9 Ver. A 2009/03 可程式計數陣列功能使用方法 Application Note R/W 5.6 CC1CON (Compare/Capture channel 1 control register) Mnemonic: CC1CON 7 6 5 - 4 TOG1 R/W 3 - Address: A4h 2 1 0 Reset CC1MOD[2:0] 00h R/W CC1MOD[2:0] 比較/捕獲通道1模式選擇(Compare/Capture channel 1 modes select) CC1MOD[2:0] 000 001 010 011 100 101 110 111 TOG1: CC1 toggle register. Function Disable PCA channel 1 Positive edge capture mode Negative edge capture mode Both positive and negative edge capture mode Timer mode High Speed Output mode 8-bit PWM 16-bit PWM 5.7 CC1 Data register Mnemonic: CC1DH 7 6 Mnemonic: CC1DL 7 6 5 5 4 3 CC1D[15:8] R/W 4 3 CC1D[7:0] R/W 2 2 1 Address: 9Eh 0 Reset 00h 1 Address: 9Fh 0 Reset 00h 5.8 CC2CON (Compare/Capture channel 2 control register) Mnemonic: CC2CON 7 6 5 - 4 TOG2 R/W 3 - Address: A5h 2 1 0 Reset CC2MOD[2:0] 00h R/W CC2MOD[2:0] 比較/捕獲通道2模式選擇(Compare/Capture channel 2 modes select) Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A123 10 Ver. A 2009/03 可程式計數陣列功能使用方法 Application Note CC2MOD[2:0] 000 001 010 011 100 101 110 111 TOG2: CC2 toggle register. Function Disable PCA channel 2 Positive edge capture mode Negative edge capture mode Both positive and negative edge capture mode Timer mode High Speed Output mode 8-bit PWM 16-bit PWM 5.9 CC2 Data register Mnemonic: CC2DH 7 6 Mnemonic: CC2DL 7 6 5 5 4 3 CC2D[15:8] R/W 4 3 CC2D[7:0] R/W 2 2 1 Address: 91h 0 Reset 00h 1 Address: 92h 0 Reset 00h 5.10 CC3CON (Compare/Capture channel 3 control register) Mnemonic: CC3CON 7 6 5 4 3 - - - TOG3 R/W - Address: A6h Res 2 1 0 et CC3MOD[2:0] 00h R/W CC3MOD[2:0] 比較/捕獲通道3模式選擇(Compare/Capture channel 3 modes select) CC3MOD[2:0] 000 001 010 011 100 101 110 Function Disable PCA channel 3 Positive edge capture mode Negative edge capture mode Both positive and negative edge capture mode Timer mode High Speed Output mode 8-bit PWM Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A123 11 Ver. A 2009/03 可程式計數陣列功能使用方法 Application Note 111 16-bit PWM TOG3: CC3 toggle register. 5.11 CC3 Data register Mnemonic: CC3DH 7 6 5 4 3 CC3D[15:8] R/W 2 1 Address: 93h 0 Reset 00h Mnemonic: CC3DL 7 6 5 4 3 CC3D[7:0] R/W 2 1 Address: 94h 0 Reset 00h 5.12 CC4CON (Compare/Capture channel 4 control register) Mnemonic: CC4CON 7 6 5 - 4 TOG4 R/W 3 - 2 Address: A7h 1 0 Reset CC4MOD[2:0] 00h R/W CC4MOD[2:0] 比較/捕獲通道4模式選擇(Compare/Capture channel 4 modes select) CC4MOD[2:0] 000 001 010 011 100 101 110 111 TOG4: CC4 toggle register. Function Disable PCA channel 4 Positive edge capture mode Negative edge capture mode Both positive and negative edge capture mode Timer mode High Speed Output mode 8-bit PWM 16-bit PWM Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A123 12 Ver. A 2009/03 可程式計數陣列功能使用方法 Application Note 5.13 CC4 Data register Mnemonic: CC4DH 7 6 5 4 3 CC4D[15:8] R/W 2 1 Address: 95h 0 Reset 00h Mnemonic: CC4DL 7 6 5 4 3 CC4D[7:0] R/W 2 1 Address: 96h 0 Reset 00h Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A123 13 Ver. A 2009/03 可程式計數陣列功能使用方法 Application Note 6 中斷(Interrupt) 6.1 中斷向量表(Interrupt Vector Table): Interrupt Vector External interrupt 0 Timer 0 External interrupt 1 Timer 1 Serial Port Timer 2 EEI PCA interrupt 0003h 000Bh 0013h 001Bh 0023h 002Bh 0033h 003Bh Polling sequence Polling sequence Interrupt Source 6.2 使用 Keil’ C 的中斷設定範例程序: unsigned char CPH,CPL; void init(void) { IE = 0x80; IE1= 0x02; // PCAC1[7]=>PCA counter run bit // PCAC1[4]~PCAC1[0] =enable all of CC4 ~ CC0 interrupt PCAC1 = 0x9f; // Clock source = Fosc (note:12T=Fosc/2 ,6T=Fosc) PCAC2 = 0x00; } void PCA_Capture(void) interrupt 7 //vector 003Bh { //user code=================== PCAL = 0x00; PCAH = 0x00; CPH=CC0DH; CPL=CC0DL; //============================ PCAC2 = PCAC2&0xC0; //clear all flag } Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A123 14 Ver. A 2009/03 可程式計數陣列功能使用方法 Application Note 7 PCA 範例程式及說明: 7.1 PCA Capture mode 應用的範例程式: Device:SM59D04G2C25 function generator: f=2KHz,T=500us Description Main program Example: P1.3 Negative edge capture mode Crystal: 24MHz/12T, Clock source = Fosc/2, t= 0.0833333us, CC0D= 1743h T*CC0D = 496.25us Crystal: 24MHz/12T, Clock source = Fosc/8, t= 0.3333333us, CC0D= 05DFh T*CC0D = 500.9us Crystal: 24MHz/12T, Clock source = Fosc/24, t= 1us, CC0D= 01EEh T*CC0D = 496us //==================================================================== // // S Y N C M O S T E C H N O L O G Y // //==================================================================== #include "sfr.h" //#include <reg51.h> unsigned int j = 0; unsigned char CPH,CPL; void init(void) { P0 = 0xff; P1 = 0xff; P2 = 0xff; P3 = 0xff; //EA = 1; IE = 0x80; IE1= 0x02; // PCAC1[7]=>PCA counter run bit // PCAC1[4]~PCAC1[0] =enable all of CC4 ~ CC0 interrupt PCAC1 = 0x9f; //Clock source = Fosc (note:12T=Fosc/2 ,6T=Fosc) //PCAC2 = 0x00; //Clock source = Fosc/4 (note:12T=Fosc/8 ,6T=Fosc/4) //PCAC2 = 0x40; //Clock source = Fosc/12 (note:12T=Fosc/24,6T=Fosc/12) PCAC2 = 0x80; //Clock source = External clockc (note:CCCI pin) //PCAC2 = 0xc0; PCAL = 0x00; PCAH = 0x00; } /****************************************************/ Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A123 15 Ver. A 2009/03 可程式計數陣列功能使用方法 Application Note // Select_channel_mode Set /****************************************************/ // CCnCON.2 CCnCON.1 CCnC0N.0 Capture mode // 0 0 1 postive edge // 0 1 0 Negative edge // 0 1 1 postive&Negative edge // 1 0 0 Timer mode void Select_channel_mode(void) { CC0CON = 0x02; //P1.3 Negative edge capture mode CC1CON = 0x02; //P1.4 Negative edge capture mode CC2CON = 0x01; //P1.5 Postive edge capture mode CC3CON = 0x03; //P1.6 Postive & Negative capture mode CC4CON = 0x03; //P1.7 Postive & Negative capture mode } void PCA_Capture(void) interrupt 7 //vector 003Bh { /****************************************************/ // PCA Capture interrupt /****************************************************/ PCAL = 0x00; PCAH = 0x00; CPH=CC0DH; CPL=CC0DL; PCAC2 = PCAC2&0xC0; } void main(void) { unsigned int i=0, ii=0; init(); Select_channel_mode(); P3 = 0xFF; while(1) { for(i=0;i<1000; i++) { for(ii=0;ii<200; ii++) { } } P0 =CPH; P2 =CPL; } } Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A123 16 Ver. A 2009/03 可程式計數陣列功能使用方法 Application Note 7.2 PCA Timer mode 應用的範例程式: Description Main program Device:SM59D04G2C25 Crystal:24MHz/12T Clock source= Fosc/4 6T:T =1 / Clock source = 4/24MHz =0.1666666us 12T:T =2(1 / Clock source) =2*4/24MHz = 0.33333us PCA = 65536 -300(decimal) =10000 -12C = 0xFED4 300T = 100us, will detect P3 toggles about 100us //==================================================================== // // SYNCMOS TECHNOLOGY // //==================================================================== #include "sfr.h" /****************************************************/ // enable interrupt and select clock source /****************************************************/ void Initial() { IE=0x80; //enable all interrupt IE1=0x02; //enable PCA interrupt //PCAC1: CCR //PCAC1=0x9f; PCAC1=0x81; CIDL - CC4IE CC3IE CC2IE //PCAC2.7,PCAC2.6 // 0 0 // 0 1 // 1 0 // 1 1 PCAC2=0x40; PCAL =0xD4; PCAH =0xFE; Clock source Fosc Fosc/4 Fosc/12 External clock input(CCCI pin,P1.2) //clock source from Fosc/4 //software reload value //software reload value CC1IE CC0IE } /****************************************************/ // select_channel 0 ~ 4 /****************************************************/ void select_channel(void) { CC0CON=0x04; //channel"0" timer mode CC1CON=0; //channel"1" timer mode disable CC2CON=0; //channel"2" timer mode disable CC3CON=0; //channel"3" timer mode disable CC4CON=0; //channel"4" timer mode disable } void PCA_Timer(void) interrupt 7 using 0//vector 003Bh { //if (PCAC2&0x01) //{ //65536 -300(decimal) =10000 -12C = 0xFED4 Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A123 17 Ver. A 2009/03 可程式計數陣列功能使用方法 Application Note PCAL =0xD4; PCAH =0xFE; //software reload value //software reload value //} P3 = ~P3; //12T@24MHz P3 toggle per 100us PCAC2 &= 0x40; //software clear all flag } void main(void) { select_channel(); Initial(); while(1) {} } 7.3 PCA High speed output mode 應用的範例程式: Description Main program High speed pulse width Device:SM59D04G2C25 Crystal:24MHz/12T Clock source = Fosc/12 6T: T =1 /source clock = 1/(Fosc/12) = 12/24MHz =0.5us 12T: T =2(1 /source clock) = 2/(Fosc/12) =2*12/24MHz =1us 100T =100us, detect pin CC0(P1.3) toggles about 100us. //==================================================================== // // SYNCMOS TECHNOLOGY // //==================================================================== //******************************************************************** // high speed pulse width // Device:SM59D04G2C25 Crystal:24MHz/12T // 6T: 1 source clock = 1/(Fosc/12) = 12/24MHz =0.5us // 12T: 1 source clock = 2/(Fosc/12) =2*12/24MHz =1us // detect P1.3 toggle per 100us //******************************************************************** #include"sfr.h" void Initial() { IE=0x80; IE1=0x02; PCAC1=0x81; //enable all interrupt //enable PCA interrupt //PCAC1.7=>PCA counter run bit //PCAC1 enable CC0 interrupt //PCAC2.7,PCAC2.6 Clock source // 0 0 Fosc // 0 1 Fosc/4 // 1 0 Fosc/12 Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A123 18 Ver. A 2009/03 可程式計數陣列功能使用方法 Application Note // 1 1 PCAC2=0x80; PCAH =0x00; PCAL =0x00; CC0DH=0x00; CC0DL=0x64;; External clock input(CCCI pin) //clock source from Fosc/12 //software reload value //software reload value //detect P1.3 toggle per 100us // } /****************************************************/ // only for select channel at CCnCON. /****************************************************/ void Select_channel_mode() { CC0CON=0x05; //high speed mode CC1CON=0x00; //high speed mode CC2CON=0x00; //high speed mode CC3CON=0x00; //high speed mode CC4CON=0x00; //high speed mode } =>P1.3 enable =>P1.4 disable =>P1.5 disable =>P1.6 disable =>P1.7 disable /****************************************************/ // PCA High Speed interrupt /****************************************************/ void PCA_High_Speed(void) interrupt 7//vector 3B { PCAL =0x00; //software reload value PCAH =0x00; //software reload value PCAC2=PCAC2&0xC0; // PCAC2.7:6 (CCCLK is "11" representative external clock input "p1.2") } void main() { Initial(); Select_channel_mode(); while(1) {} } Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A123 19 Ver. A 2009/03 可程式計數陣列功能使用方法 Application Note 7.4 PCA PWM mode 應用的範例程式: Description Main program Device:SM59D04G2C25 Crystal:24MHz/12T //==================================================================== // // SYNCMOS TECHNOLOGY // //==================================================================== #include"sfr.h" void Select_channel(); void Initial(); void main() { Initial(); Select_channel(); while(1) { p3=~p3; } } //***************************************************************** // initial set and select clock source mode0 ~ mode3 //***************************************************************** void Initial() { p0=0xff; p1=0xff; p2=0xff; p3=0xff; PCAC1=0x9f; //PCAC1.7=>PCA counter run bit enable //PCAC1.4 ~ PCAC1.0 enable CC4 ~ CC0 interrupt //source clock = Fosc/12 PCAC2=0x80; } //***************************************************************** // select 8 bit or 16 bit PWM and select PWM output p1.3 ~ p1.7 //***************************************************************** void Select_channel(void) { CC0CON=0x06; //8bit PWM CC0DL= 0x3F; //pulse width ,low:high =64us:192us =1:3 CC1CON=0x07; //16bit PWM CC1DH= 0x3F; //pulse width ,low:high =16.386ms:49.159ms =1:3 CC1DL= 0xFF; /* CC1CON=0x06; //8bit PWM CC1DL= 0x00; //pulse width ,always low CC0CON=0x06; //8bit PWM CC0DL= 0x01; //pulse width ,low:high =2:254 Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A123 20 Ver. A 2009/03 可程式計數陣列功能使用方法 Application Note CC2CON=0x06; //8bit PWM CC2DL= 0xfd; //pulse width ,low:high =254:2 CC3CON=0x06; //8bit PWM CC3DL= 0xfe; //pulse width ,low:high =255:1 CC4CON=0x06; //8bit PWM CC4DL= 0xff; //pulse width ,always low */ /* CC0CON=0x07; //16bit PWM CC0DH= 0x00; //pulse width ,low:high =3:65533 CC0DL= 0x02; CC1CON=0x07; //16bit PWM CC1DH= 0x00; //pulse width ,low:high =2:65534 CC1DL= 0x01; CC2CON=0x07; //16bit PWM CC2DH= 0x00; //pulse width ,always low CC2DL= 0x00; CC3CON=0x07; //16bit PWM CC3DH= 0xFF; //pulse width ,low:high =65535:1 CC3DL= 0xFE; CC4CON=0x07; //16bit PWM CC4DH= 0xFF; //pulse width ,always low CC4DL= 0xFF;*/ } Specifications subject to change without notice, contact your sales representatives for the most recent information. IRFWX-A123 21 Ver. A 2009/03