#include //#include #pragma config PLLDIV = 5 // Divide by 5 (20 MHz oscillator input) #pragma config CPUDIV = OSC1_PLL2 //[Primary Oscillator Src: /1][96 MHz PLL Src: /2] #pragma config USBDIV = 2 // USB clock source comes from the 96 MHz PLL divided by 2 #pragma config FOSC = HSPLL_HS //HS oscillator, PLL enabled (HSPLL) #pragma config FCMEN = OFF //Fail-Safe Clock Monitor disabled #pragma config IESO = OFF //Fail-Safe Clock Monitor disabled #pragma config PWRT = OFF //PWRT disabled #pragma config BOR = ON //Brown-out Reset enabled in hardware only (SBOREN is disabled) #pragma config BORV = 3 //Minimum setting #pragma config VREGEN = ON //USB voltage regulator enabled #pragma config WDT = OFF //[Watchdog Timer Enable] disabled (control is placed on the SWDTEN bit) #pragma config WDTPS = 32768 //Watchdog Timer Postscale Select bits 1:32768 #pragma config MCLRE = ON //MCLR pin enabled; RE3 input pin disabled #pragma config LPT1OSC = OFF //Timer1 configured for higher power operation #pragma config PBADEN = OFF //PORTB<4:0> pins are configured as digital I/O on Reset //#pragma config CCP2MX = ON #pragma config STVREN = ON //Stack full/underflow will cause Reset #pragma config LVP = OFF //Single-Supply ICSP disabled //#pragma config ICPRT = OFF // Dedicated In-Circuit Debug/Programming #pragma config XINST = OFF // Extended Instruction Set #pragma config CP0 = OFF //Block 0 (000800-001FFFh) is not code-protected #pragma config CP1 = OFF //Block 1 (002000-003FFFh) is not code-protected //#pragma config CP2 = OFF //#pragma config CP3 = OFF #pragma config CPB = OFF //Boot block (000000-0007FFh) is not code-protected //#pragma config CPD = OFF #pragma config WRT0 = OFF //Block 0 (000800-001FFFh) is not write-protected #pragma config WRT1 = OFF //Block 1 (002000-003FFFh) is not write-protected //#pragma config WRT2 = OFF //#pragma config WRT3 = OFF #pragma config WRTB = OFF // Boot block (000000-0007FFh) is not write-protected #pragma config WRTC = OFF //Configuration registers (300000-3000FFh) are not write-protected //#pragma config WRTD = OFF #pragma config EBTR0 = OFF //Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks #pragma config EBTR1 = OFF //Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks //#pragma config EBTR2 = OFF //#pragma config EBTR3 = OFF #pragma config EBTRB = OFF //Boot block (000000-0007FFh) is not protected from table reads executed in other blocks #define _XTAL_FREQ 48000000 //__delay_ms #define SW1 PORTEbits.RE0 #define SW2 PORTEbits.RE1 #define LED PORTEbits.RE2 void InitInterTimer0 (void); int n; unsigned char gM=0; unsigned short gCT=0,gSP=0,gTN=200; void init(void) { ADCON1 = 0b00001111; //全チャンネルがデジタルに設定され、VDDとVSSが選択 TRISA = 0b00011100; //A0〜A4を入力に設定 TRISB = 0b00000000; //出力に設 TRISC = 0b00110000; //D-,D+(USB用)RC4とRC5を入力に設定 TRISD = 0b00000000; //D0〜D7を出力に設定 TRISE = 0b00000011; //SW1,2=INPUT LED=OUTPUT LATA = 0b00000000; LATB = 0b00000000; LATC = 0b00000000; LATD = 0b00000000; LATE = 0b00000000; } void main(void) { init(); InitInterTimer0(); // 割込み全体の許可 GIE = 1; n = 20; while(1) { for ( int i = 0; i < n; i++ ) __delay_ms( 2 ); //10mS LED = !LED; if(SW2 == 0) { gM=1; n = n+1; if (n >= 20) { n = 1; } gSP--; if(gSP<0){gSP=0;} } if(SW1 == 0) { gM=0; n = n-1; if (n <= 1) { n = 20; } gSP++; if(gSP>99){gSP=99;} } } } // タイマ0割込み設定関数 void InitInterTimer0 (void) { // クロック源を内部クロックに設定 T0CS = 0; // プリスケーラをタイマ0に割り当て PSA = 0; // プリスケーラ値を16に設定=割込み周波数=48MHz/(4*256*16)=2930Hz T0CONbits.T0PS0 = 1;//PIC18F4553 T0CONbits.T0PS1 = 1;//PIC18F4553 T0CONbits.T0PS2 = 0;//PIC18F4553 // TMR0レジスタをクリア TMR0 = 0x0000; // タイマ0割込みフラグをクリア T0IF = 0; // タイマ0割込みの許可 T0IE = 1; } void interrupt SYS_InterruptHigh(void) { if(T0IF == 1) { switch (gM) { case 0: if(gCTgTN){gCT=0;} // タイマ0割込みフラグをクリア T0IF = 0; } }