2011年2月18日 星期五

DIY - PIC:PIC18F4550 LED Delay 程式 (十八)

DIY - PICPIC18F4550 LED Delay 程式 (十八)

Microchip C18 Libraries 內有一個指令周期副程式 (#include ),祇要將指令周期副程式包括在程式內,程式便可以用不同數目的指令周期功能。

LED Delay 流程圖
Delay1TCY TCY 是代表指令周期 (Instruction Cycle) PIC18F4550 芯片使用了 20MHz 振盪器,內部的頻率為 48MHz TCY 等於 4/48MHz = 1/12 us
指令周期副程式的 x 是代表可以乘以次數 ( unsigned char unit )而次數由 0 2550 等於 256 

Delays.h Functions
  • Delay1TCY ();              // Delay 1/12 us (等於一個 NOP指令)
  • Delay10TCYx (60);      // Delay 10 x 60 x 1/12 = 600/12 = 50 us
  • Delay100TCYx (120);  // Delay 100 x 120 x 1/12 = 1ms
  • Delay1KTCYx (240);   // Delay 1000 x 240 x 1/12 = 20 ms
  • Delay10KTCYx (0);     // Delay 10000 x 256 x 1/12 = 213.3 ms

LED Delay 程式:
//***** Includes ******
#include            // P18F4550.h
#include                 // Delay Subroutine

//***** Define *****
#define LED0 PORTAbits.RA0           // Define LED=PORTA RA0

//***** Program Start Here *****
void main(void)
{
    TRISA = 0b00000000;                   // Setup PORTA as Output

    while(1)
    {
    LED0=0;                                        // LED OFF
            Delay10KTCYx(240);           // 10,000x240x1/12us = 200ms
            Delay10KTCYx(240);           // 10,000x240x1/12us = 200ms
            Delay10KTCYx(240);           // 10,000x240x1/12us = 200ms
            Delay10KTCYx(240);           // 10,000x240x1/12us = 200ms
            Delay10KTCYx(240);           // 10,000x240x1/12us = 200ms
           
    LED0=1;                                        // LED ON
            Delay10KTCYx(240);           // 10,000x240x1/12us = 200ms
            Delay10KTCYx(240);           // 10,000x240x1/12us = 200ms
            Delay10KTCYx(240);           // 10,000x240x1/12us = 200ms
            Delay10KTCYx(240);           // 10,000x240x1/12us = 200ms
            Delay10KTCYx(240);           // 10,000x240x1/12us = 200ms
           
    }
}

2011 02 18 天氣報告
氣溫:13.6 @ 23:00 
相對濕度:百分之92% 
天氣:有薄霧及幾陣雨

沒有留言:

張貼留言