2011年3月1日 星期二

DIY - PIC: PIC18F4550 Timer0 模組程式 (廿十七)

DIY - PIC PIC18F4550 Timer0 模組程式 (廿十七)

Timer0 程式是利用 PIC18F4550 內的計時器 0 及中斷來實現,程式開始是設定計時器 0 (10ms) 及中斷旗號,程式便進入循環。當時鐘上升,計時器 0 加一直至溢滿便會觸發中斷,中斷副程式將 TimeCount 加一,當 TimeCount 小於 100 LED 不閃動,但 TimeCount 大於 100 小於 200 LED 便會閃動,大於 200 便會重置 TimeCount,完成中斷副程式後,返回程式繼續循環。

Timer0 流程圖
 程式:
//***** Define ***********************************************************
void high_isr (void);

//***** pragma ***********************************************************
#pragma udata

//***** Variables ********************************************************
unsigned long int TimeCount = 0;

//***** 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

        OpenTimer0
        (
               TIMER_INT_ON &         // interrupt: ON
               T0_16BIT &                     // 16 bit mode vs 8bit mode (T0_8BIT)
               T0_SOURCE_INT &       // use internal clock
               T0_PS_1_8                       // 1-bit 8 / 8 vs
                                                          // 1:1/2/4/8/16/32/128/256
        );

        WriteTimer0 (59286);             // Preset Timer0 value 59286

        RCONbits.IPEN = 0;              // Interrupt Priority Control: OFF
                                                          // (RCON register IPEN bit = 0)
        INTCONbits.TMR0IE = 1;    // Timer 0 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)
{
        INTCONbits.TMR0IF = 0;    // Timer 0 interrupt flag to 0
        WriteTimer0 (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
}

LED 在閃動
PortA RA0 的波型
2011 03 01 天氣報告
氣溫:20.2 @ 23:00
相對濕度:百分之88%
天氣:大致多雲

沒有留言:

張貼留言