探し方が悪いのかもしれないが、PIC24F系の簡単なコードのサンプルがあまり無い。
複雑なコードは眺める前に気後れする。
ので、見つけてきて、改良して実際に動かしたものを貼ってみる。
実際に使っているPICはPIC24FJ64GB002で、電流制限抵抗とLEDを6ピンに接続する。
電源とGNDはもちろん配線したが、パスコンはつけてない。
MCLRを10kΩでプルアップしないと動かなかったので、これは接続してある。
いくつかコードを貼るが、回路的にはすべて共通だ。
※6ピンにつないだLEDを、for文による時間稼ぎによるdelayで点滅させる。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
#include "p24fj64gb002.h" #include <xc.h> // CONFIG4 #pragma config DSWDTPS = DSWDTPSF // DSWDT Postscale Select (1:2,147,483,648 (25.7 days)) #pragma config DSWDTOSC = LPRC // Deep Sleep Watchdog Timer Oscillator Select (DSWDT uses Low Power RC Oscillator (LPRC)) #pragma config RTCOSC = SOSC // RTCC Reference Oscillator Select (RTCC uses Secondary Oscillator (SOSC)) #pragma config DSBOREN = ON // Deep Sleep BOR Enable bit (BOR enabled in Deep Sleep) #pragma config DSWDTEN = ON // Deep Sleep Watchdog Timer (DSWDT enabled) // CONFIG3 #pragma config WPFP = WPFP63 // Write Protection Flash Page Segment Boundary (Highest Page (same as page 42)) #pragma config SOSCSEL = SOSC // Secondary Oscillator Pin Mode Select (SOSC pins in Default (high drive-strength) Oscillator Mode) #pragma config WUTSEL = LEG // Voltage Regulator Wake-up Time Select (Default regulator start-up time used) #pragma config WPDIS = WPDIS // Segment Write Protection Disable (Segmented code protection disabled) #pragma config WPCFG = WPCFGDIS // Write Protect Configuration Page Select (Last page and Flash Configuration words are unprotected) #pragma config WPEND = WPENDMEM // Segment Write Protection End Page Select (Write Protect from WPFP to the last page of memory) // CONFIG2 #pragma config POSCMOD = NONE // Primary Oscillator Select (Primary Oscillator disabled) #pragma config I2C1SEL = PRI // I2C1 Pin Select bit (Use default SCL1/SDA1 pins for I2C1 ) #pragma config IOL1WAY = OFF // IOLOCK One-Way Set Enable (The IOLOCK bit can be set and cleared using the unlock sequence) #pragma config OSCIOFNC = ON // OSCO Pin Configuration (OSCO pin functions as port I/O (RA3)) #pragma config FCKSM = CSDCMD // Clock Switching and Fail-Safe Clock Monitor (Sw Disabled, Mon Disabled) #pragma config FNOSC = FRCPLL // Initial Oscillator Select (Fast RC Oscillator with Postscaler and PLL module (FRCPLL)) #pragma config PLL96MHZ = ON // 96MHz PLL Startup Select (96 MHz PLL Startup is enabled automatically on start-up) #pragma config PLLDIV = DIV12 // USB 96 MHz PLL Prescaler Select (Oscillator input divided by 12 (48 MHz input)) #pragma config IESO = ON // Internal External Switchover (IESO mode (Two-Speed Start-up) enabled) // CONFIG1 #pragma config WDTPS = PS32768 // Watchdog Timer Postscaler (1:32,768) #pragma config FWPSA = PR128 // WDT Prescaler (Prescaler ratio of 1:128) #pragma config WINDIS = OFF // Windowed WDT (Standard Watchdog Timer enabled,(Windowed-mode is disabled)) #pragma config FWDTEN = OFF // Watchdog Timer (Watchdog Timer is disabled) #pragma config ICS = PGx1 // Emulator Pin Placement Select bits (Emulator functions are shared with PGEC1/PGED1) #pragma config GWRP = OFF // General Segment Write Protect (Writes to program memory are allowed) #pragma config GCP = OFF // General Segment Code Protect (Code protection is disabled) #pragma config JTAGEN = OFF // JTAG Port Enable (JTAG port is disabled) void delay(void){ int i, j; for (i=0; i<1000; i++) for (j=0; j<200; j++); } int main(void){ CLKDIV = 0x0000; TRISBbits.TRISB2 =0; while(1){ LATBbits.LATB2 = 0; delay(); LATBbits.LATB2 = 1; delay(); } } |
※割り込みでなくTimer1の値を監視して設定値になるまで待つことでLED(6ピン)を点滅する。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
#include "p24FJ64gb002.h" #include <xc.h> // CONFIG4 #pragma config DSWDTPS = DSWDTPSF // DSWDT Postscale Select (1:2,147,483,648 (25.7 days)) #pragma config DSWDTOSC = LPRC // Deep Sleep Watchdog Timer Oscillator Select (DSWDT uses Low Power RC Oscillator (LPRC)) #pragma config RTCOSC = SOSC // RTCC Reference Oscillator Select (RTCC uses Secondary Oscillator (SOSC)) #pragma config DSBOREN = ON // Deep Sleep BOR Enable bit (BOR enabled in Deep Sleep) #pragma config DSWDTEN = ON // Deep Sleep Watchdog Timer (DSWDT enabled) // CONFIG3 #pragma config WPFP = WPFP63 // Write Protection Flash Page Segment Boundary (Highest Page (same as page 42)) #pragma config SOSCSEL = SOSC // Secondary Oscillator Pin Mode Select (SOSC pins in Default (high drive-strength) Oscillator Mode) #pragma config WUTSEL = LEG // Voltage Regulator Wake-up Time Select (Default regulator start-up time used) #pragma config WPDIS = WPDIS // Segment Write Protection Disable (Segmented code protection disabled) #pragma config WPCFG = WPCFGDIS // Write Protect Configuration Page Select (Last page and Flash Configuration words are unprotected) #pragma config WPEND = WPENDMEM // Segment Write Protection End Page Select (Write Protect from WPFP to the last page of memory) // CONFIG2 #pragma config POSCMOD = NONE // Primary Oscillator Select (Primary Oscillator disabled) #pragma config I2C1SEL = PRI // I2C1 Pin Select bit (Use default SCL1/SDA1 pins for I2C1 ) #pragma config IOL1WAY = ON // IOLOCK One-Way Set Enable (Once set, the IOLOCK bit cannot be cleared) #pragma config OSCIOFNC = OFF // OSCO Pin Configuration (OSCO pin functions as clock output (CLKO)) #pragma config FCKSM = CSDCMD // Clock Switching and Fail-Safe Clock Monitor (Sw Disabled, Mon Disabled) #pragma config FNOSC = FRCDIV // Initial Oscillator Select (Fast RC Oscillator with Postscaler (FRCDIV)) #pragma config PLL96MHZ = ON // 96MHz PLL Startup Select (96 MHz PLL Startup is enabled automatically on start-up) #pragma config PLLDIV = DIV12 // USB 96 MHz PLL Prescaler Select (Oscillator input divided by 12 (48 MHz input)) #pragma config IESO = ON // Internal External Switchover (IESO mode (Two-Speed Start-up) enabled) // CONFIG1 #pragma config WDTPS = PS32768 // Watchdog Timer Postscaler (1:32,768) #pragma config FWPSA = PR128 // WDT Prescaler (Prescaler ratio of 1:128) #pragma config WINDIS = OFF // Windowed WDT (Standard Watchdog Timer enabled,(Windowed-mode is disabled)) #pragma config FWDTEN = ON // Watchdog Timer (Watchdog Timer is enabled) #pragma config ICS = PGx1 // Emulator Pin Placement Select bits (Emulator functions are shared with PGEC1/PGED1) #pragma config GWRP = OFF // General Segment Write Protect (Writes to program memory are allowed) #pragma config GCP = OFF // General Segment Code Protect (Code protection is disabled) #pragma config JTAGEN = ON // JTAG Port Enable (JTAG port is enabled) #define DELAY 4000 int main(){ TRISBbits.TRISB2 = 0; T1CONbits.TCS = 0;//Timer1 Clock Source is Internal clock T1CONbits.TCKPS = 0b11;//Timer1 Input Clock Prescale is 1:256 T1CONbits.TON = 1;//Starts 16-bit Timer1 while( 1){ PORTBbits.RB2 =1; TMR1 = 0; while ( TMR1 < DELAY){ } PORTBbits.RB2 =0; TMR1 = 0; while ( TMR1 < DELAY){ } } } |
※Timer1の割り込み処理によるLED(6ピン)の点滅。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
#include "p24FJ64gb002.h" #include <xc.h> // CONFIG4 #pragma config DSWDTPS = DSWDTPSF // DSWDT Postscale Select (1:2,147,483,648 (25.7 days)) #pragma config DSWDTOSC = LPRC // Deep Sleep Watchdog Timer Oscillator Select (DSWDT uses Low Power RC Oscillator (LPRC)) #pragma config RTCOSC = SOSC // RTCC Reference Oscillator Select (RTCC uses Secondary Oscillator (SOSC)) #pragma config DSBOREN = ON // Deep Sleep BOR Enable bit (BOR enabled in Deep Sleep) #pragma config DSWDTEN = ON // Deep Sleep Watchdog Timer (DSWDT enabled) // CONFIG3 #pragma config WPFP = WPFP63 // Write Protection Flash Page Segment Boundary (Highest Page (same as page 42)) #pragma config SOSCSEL = SOSC // Secondary Oscillator Pin Mode Select (SOSC pins in Default (high drive-strength) Oscillator Mode) #pragma config WUTSEL = LEG // Voltage Regulator Wake-up Time Select (Default regulator start-up time used) #pragma config WPDIS = WPDIS // Segment Write Protection Disable (Segmented code protection disabled) #pragma config WPCFG = WPCFGDIS // Write Protect Configuration Page Select (Last page and Flash Configuration words are unprotected) #pragma config WPEND = WPENDMEM // Segment Write Protection End Page Select (Write Protect from WPFP to the last page of memory) // CONFIG2 #pragma config POSCMOD = NONE // Primary Oscillator Select (Primary Oscillator disabled) #pragma config I2C1SEL = PRI // I2C1 Pin Select bit (Use default SCL1/SDA1 pins for I2C1 ) #pragma config IOL1WAY = ON // IOLOCK One-Way Set Enable (Once set, the IOLOCK bit cannot be cleared) #pragma config OSCIOFNC = OFF // OSCO Pin Configuration (OSCO pin functions as clock output (CLKO)) #pragma config FCKSM = CSDCMD // Clock Switching and Fail-Safe Clock Monitor (Sw Disabled, Mon Disabled) #pragma config FNOSC = FRCDIV // Initial Oscillator Select (Fast RC Oscillator with Postscaler (FRCDIV)) #pragma config PLL96MHZ = ON // 96MHz PLL Startup Select (96 MHz PLL Startup is enabled automatically on start-up) #pragma config PLLDIV = DIV12 // USB 96 MHz PLL Prescaler Select (Oscillator input divided by 12 (48 MHz input)) #pragma config IESO = ON // Internal External Switchover (IESO mode (Two-Speed Start-up) enabled) // CONFIG1 #pragma config WDTPS = PS32768 // Watchdog Timer Postscaler (1:32,768) #pragma config FWPSA = PR128 // WDT Prescaler (Prescaler ratio of 1:128) #pragma config WINDIS = OFF // Windowed WDT (Standard Watchdog Timer enabled,(Windowed-mode is disabled)) #pragma config FWDTEN = ON // Watchdog Timer (Watchdog Timer is enabled) #pragma config ICS = PGx1 // Emulator Pin Placement Select bits (Emulator functions are shared with PGEC1/PGED1) #pragma config GWRP = OFF // General Segment Write Protect (Writes to program memory are allowed) #pragma config GCP = OFF // General Segment Code Protect (Code protection is disabled) #pragma config JTAGEN = ON // JTAG Port Enable (JTAG port is enabled) volatile int flag =0; void __attribute__((__interrupt__,__no_auto_psv__)) _T1Interrupt(void){ if ( flag ==0 ){ LATBbits.LATB2 =1; } else { LATBbits.LATB2=0; } flag = flag ^ 1; IFS0bits.T1IF = 0; } int main(){ TMR1 = 0; PR1 = 0x7fff; TRISBbits.TRISB2 = 0; PORTBbits.RB2 =0; T1CONbits.TCS = 0;//Timer1 Clock Source is Internal clock T1CONbits.TCKPS = 0b01;//Timer1 Input Clock Prescale is 1:8 T1CONbits.TON = 1;//Starts 16-bit Timer1 IFS0bits.T1IF = 0; IEC0bits.T1IE = 1; while(1){ } } |
割り込みで呼び出されるルーチンの書式はまったくなじみが無く、8ビットのもの(というかXC8での書き方?)とはぜんぜん違う。
もっとも、当面は真似するだけだから構わないわけだが。
MPLAB XC16 C Compiler User’s Guide を斜めに読んだ限りでは、何もパラメタを必要とせず、値も返さない場合は、
void __attribute__((__interrupt__,__no_auto_psv__)) _T1Interrupt(void){
という感じに書けばいいらしい。
ここで、_T1Interrupt はTimer1による割り込みを意味するので、割り込みの種類によってこの部分を変えればいいんだと思う。
例えば INT0 による外部の割り込みなら _INT0Interrupt を使って、
void __attribute__((__interrupt__,__no_auto_psv__)) _INT0Interrupt(void){
と書く。
これらについては、Microchip¥xc16¥v1.23¥docs¥vector_docs¥PIC24FJ64GB002.html に一覧表が載っている。
そこまで分かれば、このような単純な割り込みなら、その他の手順はXC8で書いたものと変わらない。