Timer1 程式是利用 PIC18F4550 內的計時器 1 及中斷來實現,程式開始是設定計時器 1 (10ms) 及中斷旗號,程式便進入循環。當時鐘上升,計時器 1 加一直至溢滿便會觸發中斷,中斷副程式將 TimeCount 加一,當 TimeCount 小於 100 LED 不閃動,但 TimeCount 大於 100 及 小於 200 LED便會閃動,大於 200 便會重置 TimeCount,完成中斷副程式後,返回程式繼續循環。
▲ Timer1 流程圖 |
//***** Vector Remapping ************************************************* #pragma interrupt high_isr #pragma code high_vector = 0x08 void high_interrupt (void) { _asm goto high_isr _endasm } #pragma code //***** END Vector Remapping ******************************************** /************************************************************************* /************************************************************************* * Function: void main(void) * * PreCondition: None * Input: None * Output: None * Side Effects: None * Overview: Main program entry point. * Note: None ************************************************************************/ void main (void) { TRISA = 0x00; // Port A As Output // Set Timer 1 OpenTimer1 ( TIMER_INT_ON & // Interrupt: ON T1_16BIT_RW & // 16 bit mode T1_SOURCE_INT & // Use internal clock T1_PS_1_8 & // Prescaler 1:1/2/4/8 T1_OSC1EN_OFF // No oscillator ); //***** Vector Remapping ************************************************* #pragma interrupt high_isr #pragma code high_vector = 0x08 void high_interrupt (void) { _asm goto high_isr _endasm } #pragma code //***** END Vector Remapping ******************************************** /************************************************************************* /************************************************************************* * Function: void main(void) * * PreCondition: None * Input: None * Output: None * Side Effects: None * Overview: Main program entry point. * Note: None ************************************************************************/ void main (void) { TRISA = 0x00; // Port A As Output // Set Timer 1 OpenTimer1 ( TIMER_INT_ON & // Interrupt: ON T1_16BIT_RW & // 16 bit mode T1_SOURCE_INT & // Use internal clock T1_PS_1_8 & // Prescaler 1:1/2/4/8 T1_OSC1EN_OFF // No oscillator ); WriteTimer1 (59286); // Preset Timer1 value 59286 RCONbits.IPEN = 0; // Interrupt Priority Control: OFF // (RCON register IPEN bit = 0) PIE1bits.TMR1IE = 1; // Timer1 interrupt enable INTCONbits.PEIE = 1; // peripheral interrupt enable // INTCON register bit b6: low interrupt enable / disable // (When interrupt priority control) INTCONbits.GIE = 1; // all interrupt enable // INTCON register bit b7: high interrupt enable / disable // (When interrupt priority control) while (1) // wait for interrupt doing nothing { } } void high_isr (void) { PIR1bits.TMR1IF = 0; // Timer1 interrupt flag to 0 WriteTimer1 (59286); // 0.05μsec × 4 × 8 × 6250 = 10000μsec = 10msec // (FOSC = HS at the system clock 20MHz) // 256 × 256 - 6250 = 59286 // 16 bit timer overflow interrupt occurs TimeCount ++; // 10msec for each increment if (TimeCount <100) // count of 100 or less (1000msec = 10msec × 100) { LATAbits.LATA0 = 0; //RA0 = 0 LED: OFF Scope=0 } else if (TimeCount <200) // count from 100 to 200 (2000msec = 10msec × 100) { LATAbits.LATA0 = 1; // RA0 = 1 LED: ON Scope=1 } else TimeCount = 0; // Reset count = 0 } |
▲ Timer1 20ms (10msx2) |
氣溫:17.1 度 @ 23:00
相對濕度:百分之73%
天氣:大致多雲
沒有留言:
張貼留言