2013年5月15日 星期三

DIY - PIC:PIC18F4550 Timer0 1秒鐘計時器程式 (一百六十九)

DIY - PICPIC18F4550 Timer0 1秒鐘計時器程式 (一百六十九)

這是一個利用 Timer0 Interrupt 篇寫出來的 1 秒鐘 (1 second) 計時器程式,程式會將設定 Timer0 中斷和寫入 59286 Timer0Timer0 便會每 10ms 中斷一次,而中斷程式會用 Counter 計算 100 (10ms × 100 = 1s) 便會熄滅 RA1 LED,到 Counter計算至 200次便會點亮 RA1 LED 並重置 Counter

PIC18F4550 Timer0 1秒鐘計時器實驗
Timer0 1秒鐘計時器程式:
void main (void)
{
            
    char LCD_name[]="BWS Timer=1S RA1";
    unsigned int i;
    unsigned int timeout=0;
    char data;

    ADCON0 = 0x00;              // Disable the AD converter
    ADCON1 = 0x0F;              // Set all ports to digital
    TRISA = 0b00000000;     // Setup PORTA as Output

    // Initialise LCD
    OpenXLCD(FOUR_BIT&LINES_5X7);   // Init the LCD Display
    while(BusyXLCD());
    WriteCmdXLCD(BLINK_OFF & CURSOR_OFF ); 
    while(BusyXLCD());
    stdout = _H_USER;                            // Redirect I/O to LCD
    LcdSetLine1();                          // Put cursor on start of line 1
    putsXLCD(LCD_name);                    // Display text

        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
    {
           i=SecondCount/100;
           sprintf(glb_Buf,"Second=%1u",i); // Put Temp into Buf
           LcdSetLine2();
           putsXLCD(glb_Buf);         // Display Content
    }
} // End main

Timer0 中斷副程式:
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

        SecondCount ++;
        TimeCount ++;                        // 10msec for each increment
        if (TimeCount <100)             // count of 100 or less (1000msec = 10msec × 100)
        {
                LATAbits.LATA1 = 0;    //RA0 = 0 LED: OFF Scope=0
        }
        else if (TimeCount <200)    // count from 100 to 200 (2000msec = 10msec × 100)
        {
                LATAbits.LATA1 = 1;    // RA0 = 1 LED: ON Scope=1
        }
        else TimeCount = 0;             // Reset count = 0
}

RA1 LED 點亮
RA1 LED 熄滅

2013515 天氣報告
氣溫:28.4 @ 22:30
相對濕度:百分之84%
天氣:微雨

沒有留言:

張貼留言