Interrupt on change on both edge

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
babulp
Posts: 16
Joined: 16 Jan 2022 01:48

Interrupt on change on both edge

#1 Post by babulp » 28 Mar 2022 19:33

I am using PIC16F1459, I have configured for Interrupt on change on pin RA4, the pin is pulled up. I am using IR LED and Photo Trasistor to make the pin low. with the below configuration, the Interrupt Flag is set on both rising edge and falling edge. If I set IOCAP4_bit = 0 and IOCAN4_bit = 0 then the Interrupt Flag do not set on any edge. If I set IOCAP4_bit = 0 and IOCAN4_bit = 1 then the Interrupt Flag is set on both edge.

Code: Select all

INTCON = 0b00001000;
IOCAP4_bit = 1;                 // Interrupt on Pin A4 Rising edge
IOCAN4_bit = 0;                 // No Interrupt on falling edge
IOCAF = 0;                      // Interrupt flag cleared

void interrupt() {                  // Sub Routine for Interrupt
if (IOCIF_bit) {                    // Check Interrupt Flag on A4, if ON
pr++;                               // Increase Product gone out
IOCAF = 0;                          // Clear Interrupt Flag
}
}

davegsm82
Posts: 156
Joined: 29 Mar 2011 20:35

Re: Interrupt on change on both edge

#2 Post by davegsm82 » 30 Mar 2022 21:24

Have you put a scope on the pin to see if the transition is clean in both directions? If it's noisy then it may seem like it's triggering on both, because as it transitions from high to low then there may be noise spikes which look like a transition from low to high. It's a bit like debounce on a switch.

Worst case scenario you could get it working by putting an extra test in the ISR;

Code: Select all

void interrupt() {                  // Sub Routine for Interrupt
if (IOCIF_bit) { 
IOCAF = 0;                    // Check Interrupt Flag on A4, if ON
if(porta.b4 == 0){
 pr++
}
}
}

babulp
Posts: 16
Joined: 16 Jan 2022 01:48

Re: Interrupt on change on both edge

#3 Post by babulp » 31 Mar 2022 22:25

Hi davegsm82,

Thanks for responding, I do not have any oscilloscope, but did the change you mentioned and it worked. I am using a solderless bread board, do you think this would be creating noise?

davegsm82
Posts: 156
Joined: 29 Mar 2011 20:35

Re: Interrupt on change on both edge

#4 Post by davegsm82 » 31 Mar 2022 23:35

It's probably not really anything to do with the breadboard, more to do with the nature of interfacing analogue electronics with a digital processor.

If you're not familiar with 'debounce' on switches then look that up, because I suspect what's happening is the equivalent of switch bounce in your IR sensor. I presume it's a simple photodiode, or maybe it's a IR receiver module?

If it's a receiver module then you will have less problems, but you have to use a modulated 38KHz IR beam, rather than just on or off with a simple photodiode, but the photodiode is more susceptible to noise as it's not looking for a specific frequency of light. If you're using a photodiode then you can try adjusting your pull up/down resistor, perhaps add a small capacitor to act like a debounce on a switch.

Glad it's working anyway!

babulp
Posts: 16
Joined: 16 Jan 2022 01:48

Re: Interrupt on change on both edge

#5 Post by babulp » 01 Apr 2022 00:13

I am using LTR-3208 sensor photo 940NM as receiver and IR333-A as emitter.

davegsm82
Posts: 156
Joined: 29 Mar 2011 20:35

Re: Interrupt on change on both edge

#6 Post by davegsm82 » 01 Apr 2022 22:56

Yea that's just a standard photodiode, it won't really give a clean signal to be honest.

If your transmitter has enough spare room inside/on the PCB then you can just drive your emitter LED with a 38KHz square wave pulse, then use a standard IR Receiver module, something like this;

https://www.ebay.co.uk/itm/184136821136

This will only respond to ~38KHz carrier signals and can be used to give a simple on-off signal, just put 5V in and you get your signal out. The only caveat is that it will be affected by remote controls too, but I guess your current setup will be affected also.

Dave.

babulp
Posts: 16
Joined: 16 Jan 2022 01:48

Re: Interrupt on change on both edge

#7 Post by babulp » 04 Apr 2022 12:35

Hi davegsm82,

Thank you for the information, I'll try it out but as for now I am using your software idea and is working fine.

thank you once again.

Post Reply

Return to “mikroC PRO for PIC General”