2011年2月14日 星期一

DIY - PIC:PIC18F4550 LED 程式 (十六)

DIY - PICPIC18F4550 LED 程式 (十六)

情人節快樂!

PIC18F4550 LED 的程式是用 Delay Loop 來作時間的廷遲,將 PORTA RA0 的狀態由OFF ON 改變,然後再由 ON OFF 改變,循環不息,LED 燈便會閃動不停。

LED程式流程圖
LED 程式:
//***** Include ******
#include  

//***** Define *****
#define LED0 PORTAbits.RA0        // LED0 is PORTA RA0

void delay(void)
{
    unsigned int i;                                // max i=65535
    for(i=0; i<50000; ++i);                 // 50000 90ms
}

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

    while(1)
    {
    LED0=0;                                    // LED OFF
            delay();                               // Call Delay
    LED0=1;                                    // LED ON
            delay();                               // Call Delay
    }
}

//***** Configuration bits ******
// Oscillator 20MHz, CPU=48MHz, USB=48MHz
#pragma config PLLDIV=5                        // PLL Prescaler Selection bits 20MHz/5
#pragma config CPUDIV=OSC1_PLL2    // CPU System clock postscaler OSC1/PLL2
#pragma config USBDIV=2                       // USB Clock Selectiob bit From 96MHz/2
#pragma config FOSC=HSPLL_HS          // Oscillator Selection bit HS,PLL,USB
#pragma config FCMEN=OFF                  // Fail-Safe Clock Monitor Enable bit
#pragma config IESO=OFF                       // Int/Ext Oscillator Switchover bit

#pragma config PWRT=ON                       // Power-up Timer Enable bit
#pragma config BOR=OFF                        // Brown-out Reset Enable bits
#pragma config BORV=0                           // Brown-out Voltage bits 0=Max 3=Min
#pragma config VREGEN=ON                  // USB Voltage Regulator Enable bit

#pragma config WDT=OFF                        // Watchdog Timer Enable bit
//#pragma config WDTPS=32768               // Watchdog Timer Postscale Select bit 1:32768
//#pragma config MCLRE=ON                   // MCLR Pin Enable bit ON=RE3 disable,MCLR
//#pragma config LPT1OSC=ON                // Low-power Timer 1 Oscillator Enable bit
#pragma config PBADEN=OFF                  // PORTB A/D Enable bit OFF=Digital
#pragma config STVREN=OFF                  // Stack Full/Underflow Reset Enable bit
#pragma config LVP=OFF                          // Single-Supply ICSP Enable bit
//#pragma config ICPRT=OFF                    // Dedicated ICSP Enable bit
//#pragma config XINST=OFF                    // Extended Instruction Set Enable bit
#pragma config DEBUG=OFF                    // Background Debugger Enable bit
程式內的 #pragma參數,請參考PIC18 Configuration Settings Addendum 

如果程式編譯後,沒有錯誤,程式便可以通過 MCD2 QL1201燒寫在 MCU 上。 

2011 02 14 天氣報告
氣溫:10.1 @ 23:00 
相對濕度:百分之85% 
天氣:天氣寒冷,有微雨

沒有留言:

張貼留言