這塊版子的LED在RD0~RD7
可變電阻(右上方)接在RA0
Switch Button(右下方)接在RB0
程式碼如下
#pragma config FOSC = INTIO67 // FOSC = INTIO67 Internal oscillator block #pragma config WDTEN = OFF, LVP = OFF // LVP = OFF Single-Supply ICSP disabled //WDTEN = OFF Watch dog timer is always disabled. SWDTEN has no effect. #include "p18f45k20.h" //inclue this mcu header void delay(); //delay function void delay() { int counter = 0; for (counter = 0; counter<1000; counter++); } void main(void) { int dummy = 0; /*init the RA0 as analog input*/ TRISA = 0x1; // set potentiometer at porta as input (only set port A0) ANSELbits.ANS0 = 1; // set RA0 to analog (=1, 0 = digital) -> this is important to potentiometer at RA0 ADCON0bits.ADON = 1; // activate A/D unit /*init the RB0 as digital input*/ TRISB = 0x1; // set bit0 of portb to input because there is the push button at RB0 ANSELHbits.ANS12 = 0; // set RB0 to digital (=0, 1= analog) -> this is important to read button at RB0 /*init the RD0..RD7(PORTD) as digital output*/ TRISD = 0; // all bits of portd are set 0 as output PORTD = 0; // deactivate all led's while (1) { if (PORTBbits.RB0 == 0) { //check if button is pressed (=0, 1= not pressed) ADCON0bits.GO_DONE = 1; // start A/D convert while(ADCON0bits.GO_DONE != 0); // wait till A/D convert done dummy = ADRESH; // copy A/D result (high part) to leds -> dummy variable is only for better debugging PORTD = dummy; } else { PORTD = PORTD<<1; // move set led if (PORTD == 0) { PORTD = 0x1; // if all led's off activate one } } delay(); } }
程式碼內容主要為
config
設定使用內建震盪器、關閉看門狗
pin define
設定使用內建震盪器、關閉看門狗
pin define
一開始遇到的困難是程式build success,按燒錄後卻沒有任何反應,
以下有二個小心得:
(1) 燒錄時Pickit 3要供電3.25V
(2)必須按下Run Project,單獨program的話,只會在燒錄過程中供電,燒完就沒電了,
當然不會亮囉。
晚安囉~
晚安囉~
參考資料:
Microchip 論壇
http://www.microchip.com/forums/m701984.aspx
http://www.microchip.com/forums/m701984.aspx