Interrupts
Moderator: Benj
-
- Posts: 9
- Joined: Tue Jun 11, 2019 8:09 am
- Been thanked: 6 times
- Contact:
Interrupts
Hi, using 18 series pic with rising edge interrupt configured on RB1 / INT1 pin. The simulation is perfect but testing on hardware the count is incremented both on the rising and falling edges, the pin is tied high with 10k and brought low with external reed switch, any suggestions welcomed. Thanks, Gary.
- Attachments
-
- FC8.fcfx
- (26.67 KiB) Downloaded 139 times
-
- Valued Contributor
- Posts: 618
- Joined: Fri Jun 06, 2014 3:53 pm
- Has thanked: 184 times
- Been thanked: 195 times
- Contact:
Re: Interrupts
Hi
I've had a quick look at your program.
First thing is that I see you have the Interrupt Enable within what is in effect your main loop. The Interrupt Enable generally is called before the Main Loop however that is not your issue. Instead I think it is possibly due to switch bounce.
When a switch or relay closes it does not do so cleanly, instead it makes and breaks many times over a few milliseconds. Microcontrollers don't see that closure as a single event, instead they see each "bounce" as a valid change of state.
There are many ways to debounce a switch and a search in this forum will provide many examples. I would also, as the very first step in your Interrupt routine (Pulse_Count), disable the interrupt then process as necessary (perhaps including a delay of x-mS to allow switch to settle) and then before leaving your routine re-enable the Interrupt.
Hope this helps.
Regards
I've had a quick look at your program.
First thing is that I see you have the Interrupt Enable within what is in effect your main loop. The Interrupt Enable generally is called before the Main Loop however that is not your issue. Instead I think it is possibly due to switch bounce.
When a switch or relay closes it does not do so cleanly, instead it makes and breaks many times over a few milliseconds. Microcontrollers don't see that closure as a single event, instead they see each "bounce" as a valid change of state.
There are many ways to debounce a switch and a search in this forum will provide many examples. I would also, as the very first step in your Interrupt routine (Pulse_Count), disable the interrupt then process as necessary (perhaps including a delay of x-mS to allow switch to settle) and then before leaving your routine re-enable the Interrupt.
Hope this helps.
Regards
-
- Posts: 9
- Joined: Tue Jun 11, 2019 8:09 am
- Been thanked: 6 times
- Contact:
Re: Interrupts
Hi, ok great, delay in interrupt routine seems to do most of the trick, David from Matrix also suggested using a small cap on the input line, will add soon and recheck, much appreciated
-
- Valued Contributor
- Posts: 618
- Joined: Fri Jun 06, 2014 3:53 pm
- Has thanked: 184 times
- Been thanked: 195 times
- Contact:
Re: Interrupts
Hi
As I eluded to there are many ways to skin a cat
If you are going to rely on a delay in your IHR then I would suggest the following steps inside your routine
Disable Int1
Increment counter
Delay 50mS (to allow switch to settle and can be increased if required)
Enable Int1
Hope this helps
Regards
As I eluded to there are many ways to skin a cat
If you are going to rely on a delay in your IHR then I would suggest the following steps inside your routine
Disable Int1
Increment counter
Delay 50mS (to allow switch to settle and can be increased if required)
Enable Int1
Hope this helps
Regards