Interrupt Issues, PIC12HV615 HI-TECH Compiler

Status
Not open for further replies.

brutc001

New Member
hi, i've been trying to enable interrupt on change for GPIO pins 0 and 1 except it doesn't seem to be working...

Could someone take a look at my code and tell me where i'm going wrong, thank you.

Code:
#include <pic.h>
#include <htc.h>

void init(void)
{
    TRISIO=0b00000011;            //set GPIO<1:0> as inputs
    GPIO=0b11111111;              //set GPIO high
    IOC=0b00000011;               //enable ICO GP0 and GP1
    INTCON = 0b10001000;          //enable interrupt on change and global interrupts
}

volatile unsigned char zero;      //initilise global var flag zero
volatile unsigned char one;       //initilise global var flag one
#define SET 1
#define UNSET 0
#define _XTAL_FREQ 8000000

main()
{
    init();                       //initilise registers
    zero=UNSET;                   //unset flag
    one=UNSET;                    //unset flag
    ei();                         //enable interrupts
    while(1)                      //wait for a flag to be set by interrupt
    {
        if(zero == SET)
        {
            GP4=0;                //set GP4 low
            zero=UNSET;           //unset flag
        }
        if(one == SET)
        {
            GP4 = 1;              //set GP4 high
            one=UNSET;            //unset flag
        }

    }

}

void interrupt my_isr(void)
{
    di();                         //disable interrupts
    if(GP0 == 0)                  //set correct flag
       zero=SET;
    if(GP1 == 0)
       one=SET;
    __delay_us(90);
    ei();                         //enable interrupts
}

pins are held high and get pulled down to trigger interrupt, well that was the aim anyway
 
Last edited:
Firstly, when you post code please put [code] before it and [/code] after it so it comes out as,
Code:
#include <pic.h>
#include <htc.h>

void init(void)
{
    TRISIO=0b00000011; //set GPIO<1:0> as inputs
    GPIO=0b11111111; //set GPIO high
    IOC=0b00000011; //enable ICO GP0 and GP1
    INTCON = 0b10001000; //enable interrupt on change and global interrupts
}

volatile unsigned char zero; //initilise global var flag zero
volatile unsigned char one; //initilise global var flag one
#define SET 1
#define UNSET 0
#define _XTAL_FREQ 8000000

main()
{
    init(); //initilise registers
    zero=UNSET; //unset flag
    one=UNSET; //unset flag
    //ei(); //enable interrupts
    while(1) //wait for a flag to be set by interrupt
    {
        if(zero == SET)
        {
            GP4=0; //set GP4 low
            zero=UNSET; //unset flag
        }
        if(one == SET)
        {
            GP4 = 1; //set GP4 high
            one=UNSET; //unset flag
        }
    }
}


void interrupt my_isr(void)
{
    //di(); //disable interrupts
    if(GP0 == 0) //set correct flag
        zero=SET;
    if(GP1 == 0)
        one=SET;
    __delay_us(90);
    //ei(); //enable interrupts
}

Note I commented out the ei and di functions as they are not needed, you've already done it elsewhere and it's automatic in interrupts.

What is needed is to turn off the analogue inputs by writing zero to ANSEL. You also need to clear the interrupt flag at the end of the interrupt (intcon.0).

I only gave your code a quick look at so there may be other things wrong.

Mike.
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…