PIC 12F615


初めての PIC 割り込みに挑戦 (2)

割り込み処理に四苦八苦

これでもうまくいく。

/*
 * CNC Power unit
 * 
 * File:   main.c
 * Author: masato
 *
 * Created on 2017/02/05, 8:02
 */

// PIC12F615 Configuration Bit Settings
// 'C' source line config statements
// CONFIG
#pragma config FOSC = INTOSCIO  // Oscillator Selection bits (INTOSCIO oscillator: I/O function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config IOSCFS = 8MHZ    // Internal Oscillator Frequency Select (4 MHz)
#pragma config BOREN = ON       // Brown-out Reset Selection bits (BOR enabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>

static int val = 0;
static int odd = 0;
static int even = 0;
void interrupt ISR(void) {
    if (INTCONbits.GPIF) {
        if (odd) {
            val ^= 1;
            GP4 = val;
#if 0
            GP4 = val;
            GP4 = val;
            GP4 = val;
#endif
        }
#if 0
        GP0 = even & 0x01;
        GP1 = (even & 0x02) >> 1;
        GP2 = (even & 0x04) >> 2;

        even++;
#else
        GP0 = 0;
#if 0
        GP1 = 0;
        GP2 = 0;
#endif
#endif
        odd ^= 1;
        INTCONbits.GPIF = 0;    // 割り込みフラグクリア
    }
}

void main(void) {

    // レジスタセット
    TRISIO = 0b00001000;
    ANSEL =  0b00000000;
    CMCON0 = 0b00000000;
    IOCbits.IOC3 = 1;

    INTCONbits.GPIE = 1;        // GPIO変更割り込み許可
    INTCONbits.GIE =  1;        // グローバル割り込み許可

    GP0 = 0;
    GP1 = 0;
    GP2 = 0;
    GP4 = 0;
    GP5 = 0;

    while(1);

    return;
}

異なる2つのGPIOポートに出力すると良いようだが、こんな解決方法は間違ってるよね。