1 / 20

Timer

Timer. TON: Timer 開關 1:ON 0:OFF PR1: Timer 計數值設定 TSIDL: 休眠模式 1: 休眠 0: 不休眠 TMR1: Timer 內計數值 TGATE: 閘控模式選擇 TSYNC: 選擇 16 位元同步或非同步計數器 TCS: 選擇計數源 0: 內頻 1: 外頻 TCKKSP: 除頻 00 = 1:1 prescale value 01 = 1:8 prescale value 10 = 1:64 prescale value

Télécharger la présentation

Timer

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Timer

  2. TON: Timer 開關 1:ON 0:OFF PR1: Timer計數值設定 TSIDL: 休眠模式 1:休眠 0:不休眠 TMR1: Timer內計數值 TGATE: 閘控模式選擇 TSYNC: 選擇16位元同步或非同步計數器 TCS: 選擇計數源 0:內頻 1:外頻 TCKKSP: 除頻 00 = 1:1 prescale value 01 = 1:8 prescale value 10 = 1:64 prescale value 11 = 1:256 prescale value

  3. timer1

  4. /* timer計數 */ • #include <p30F4011.h> //呼叫p30F4011(IC)指令巨集 • #include <timer.h> • unsigned int i=0; • unsigned int x=0x20,y=0; • _FWDT(WDT_OFF); //關閉看門狗 • void Init_Timer1( void ) • { • T1CON = 0; //TIMER1 OFF • IFS0bits.T1IF = 0; //中斷旗標 • IPC0bits.T1IP = 7; //中斷優先權設定 • IEC0bits.T1IE = 1; //中斷觸發暫存器 • T1CONbits.TCS = 0; • T1CONbits.TCKPS=1; //TCKPS<1:0>: Timer Input Clock Prescale Select bits • //11 = 1:256 prescale value • //10 = 1:64 prescale value • //01 = 1:8 prescale value • //00 = 1:1 prescale value • PR1 = 1250; // ((10MHz*8[選擇的8倍頻])/4[內部除頻])/8/1250=2KHz 中斷為0.5ms • T1CONbits.TON=1; //TIMER1 ON • }

  5. /* timer中斷副程式 */ • void _ISR _T1Interrupt(void) • { • if(i==2000) //0.5ms*2000=1s • { • PORTB =0x1c+x; • x+=0x20; • if(x>0x130) • x=0x00; • i=0; • } • else • i++; • IFS0bits.T1IF = 0 ; //清除中斷旗標 • } • /* 主程式 */ • int main(void) • { • TRISB = 0x0000 ; // 0B 0000 0000 0000 0000 • PORTB=0x1c; // "1"=>input • // "0"=>output • Init_Timer1(); • while(1); • }

  6. timer2

  7. /* timer2 */ • #include <p30F4011.h> //呼叫p30F4011(IC)指令巨集 • #include <timer.h> • int IntCount=0; • _FWDT(WDT_OFF); //關閉看門狗 • void Init_Timer2( void ) • { • T2CON = 0; //TIMER2 OFF • IFS0bits.T2IF = 0; //中斷旗標 • IPC1bits.T2IP = 7; //中斷優先權設定 • IEC0bits.T2IE = 1; //中斷觸發暫存器 • T2CONbits.TCS = 0; • T2CONbits.TCKPS=1; //TCKPS<1:0>: Timer Input Clock Prescale Select bits • //11 = 1:256 prescale value //10 = 1:64 prescale value • //01 = 1:8 prescale value • //00 = 1:1 prescale value PR2 = 1250; // ((10MHz*8[選擇的8倍頻])/4[內部除頻])/8/1250=2KHz 中斷為0.5ms • T2CONbits.TON=1; // TIMER2 ON • }

  8. /*timer2 中斷 */ • void _ISR _T2Interrupt(void) • { • IntCount++; • if (IntCount<20) • PORTBbits.RB0=1; //PORTB的RB0輸出 • else if (IntCount<40) • PORTBbits.RB0=0; • else • IntCount=0; • IFS0bits.T2IF = 0; //清除中斷旗標 • } • int main(void) • { • TRISB = 0x0000 ; // 0B 0000 0000 0000 0000 • // "1"=>input • // "0"=>output • Init_Timer2(); • while(1); • }

  9. timer3

  10. /* timer3 */ • #include <p30F4011.h> //呼叫p30F4011(IC)指令巨集 • #include <timer.h> • int IntCount=0; • _FWDT(WDT_OFF); //關閉看門狗 • void Init_Timer3( void ) • { • T3CON = 0; //TIMER3 OFF • IFS0bits.T3IF = 0; //中斷旗標 • IPC1bits.T3IP = 7; //中斷優先權設定 • IEC0bits.T3IE = 1; //中斷觸發暫存器 • T3CONbits.TCS = 0; • T3CONbits.TCKPS=1; //TCKPS<1:0>: Timer Input Clock Prescale Select bits • //11 = 1:256 prescale value //10 = 1:64 prescale value • //01 = 1:8 prescale value • //00 = 1:1 prescale value PR3 = 1250; // ((10MHz*8[選擇的8倍頻])/4[內部除頻])/8/1250=2KHz 中斷為0.5ms • T3CONbits.TON=1; // TIMER3 ON • }

  11. /*timer3 中斷 */ • void _ISR _T3Interrupt(void) • { • IntCount++; • if (IntCount<20) • PORTBbits.RB0=1; //PORTB的RB0輸出 • else if (IntCount<40) • PORTBbits.RB0=0; • else • IntCount=0; • IFS0bits.T3IF = 0; //清除中斷旗標 • } • int main(void) • { • TRISB = 0x0000 ; // 0B 0000 0000 0000 0000 • // "1"=>input • // "0"=>output • Init_Timer3(); • while(1); • }

  12. timer4

  13. /* timer4 */ • #include <p30F4011.h> //呼叫p30F4011(IC)指令巨集 • #include <timer.h> • int IntCount=0; • _FWDT(WDT_OFF); //關閉看門狗 • void Init_Timer4( void ) • { • T4CON = 0; //TIMER4 OFF • IFS1bits.T4IF = 0; //中斷旗標 • IPC5bits.T4IP = 7; //中斷優先權設定 • IEC1bits.T4IE = 1; //中斷觸發暫存器 • T4CONbits.TCS = 0; • T4CONbits.TCKPS=1; //TCKPS<1:0>: Timer Input Clock Prescale Select bits • //11 = 1:256 prescale value //10 = 1:64 prescale value • //01 = 1:8 prescale value • //00 = 1:1 prescale value PR4 = 1250; // ((10MHz*8[選擇的8倍頻])/4[內部除頻])/8/1250=2KHz 中斷為0.5ms • T4CONbits.TON=1; // TIMER4 ON • }

  14. /*timer4 中斷 */ • void _ISR _T4Interrupt(void) • { • IntCount++; • if (IntCount<20) • PORTBbits.RB0=1; //PORTB的RB0輸出 • else if (IntCount<40) • PORTBbits.RB0=0; • else • IntCount=0; • IFS1bits.T4IF = 0; //清除中斷旗標 • } • int main(void) • { • TRISB = 0x0000 ; // 0B 0000 0000 0000 0000 • // "1"=>input • // "0"=>output • Init_Timer4(); • while(1); • }

  15. timer5

  16. /* timer5 */ • #include <p30F4011.h> //呼叫p30F4011(IC)指令巨集 • #include <timer.h> • int IntCount=0; • _FWDT(WDT_OFF); //關閉看門狗 • void Init_Timer5( void ) • { • T5CON = 0; //TIMER5 OFF • IFS1bits.T5IF = 0; //中斷旗標 • IPC5bits.T5IP = 7; //中斷優先權設定 • IEC1bits.T5IE = 1; //中斷觸發暫存器 • T5CONbits.TCS = 0; • T5CONbits.TCKPS=1; //TCKPS<1:0>: Timer Input Clock Prescale Select bits • //11 = 1:256 prescale value //10 = 1:64 prescale value • //01 = 1:8 prescale value • //00 = 1:1 prescale value PR5 = 1250; // ((10MHz*8[選擇的8倍頻])/4[內部除頻])/8/1250=2KHz 中斷為0.5ms • T5CONbits.TON=1; // TIMER5 ON • }

  17. /*timer5 中斷 */ • void _ISR _T5Interrupt(void) • { • IntCount++; • if (IntCount<20) • PORTBbits.RB0=1; //PORTB的RB0輸出 • else if (IntCount<40) • PORTBbits.RB0=0; • else • IntCount=0; • IFS1bits.T5IF = 0; //清除中斷旗標 • } • int main(void) • { • TRISB = 0x0000 ; // 0B 0000 0000 0000 0000 • // "1"=>input • // "0"=>output • Init_Timer5(); • while(1); • }

  18. 請設計一個2秒向右移的跑馬燈,並顯示在LED上請設計一個2秒向右移的跑馬燈,並顯示在LED上

More Related