Unstable INT interrupt

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Unstable INT interrupt

Post by Jan Lichtenbelt »

I try already for some time to measure a pulse time by means of INT interrupt on RB0 (PIC16F877A). The procedure is simple. Measure the number of interrupts of timer2 of the pulse. At the rising edge of the INT interrupt the timer counter is set equal to 0 and at the falling edge the timer counter gives the pulse length in timer2 time-period units. So far so good.

But I wanted the pulse length as soon as possible. But after a long time of puzzling, I found that the first peak, after starting the INT interrupt, give wrong and unstable results. To test that I extended the INT interrupt a little bit and made possible to start measuring at the wanted pulse#. See Flowcode attached.

Question: Can someone tell me why the first pulse after starting the INT interrupt, gives wrong results?

Thanks a lot for help

Kind regards

Jan Lichtenbelt


PS. The program has two timers: The regular timer2 and very fast 'timer9', but both show the same problem and is thus not essential for my question. I will tell later why I use this timer9.
Attachments
Test_INT_Interrupt_V1.fcf
(15 KiB) Downloaded 342 times

Gary Freegard
Posts: 45
Joined: Mon Nov 07, 2011 6:36 pm
Has thanked: 1 time
Been thanked: 30 times
Contact:

Re: Unstable INT interrupt

Post by Gary Freegard »

Hi Jan
I tried to do something similar in the past for a response box, measuring the time between button presses, by using Port B Interrupt on change and Timer 1 Interrupt unfortunately it didn’t always work as once the interrupt service routine was being run then other interrupts are/can be ignored (depending on their priority).

The way I got around this was to set up Timer 1 to overflow every millisecond thus causing an interrupt, the interrupt service routine would do the following:
reset the Timer 1 hi and lo bytes to the values that are required
read Port B
Check to see if Port B has changed
If Port B =1 then start counting else stop

Seeing that the accuracy of the measurement is set by the Timer then checking every millisecond in this case is acceptable.

I have attached a version for you that runs at approx 10KHz and is using RB0, hopefully this will help you in your endeavours.

Gary

PS Have a look at http://www.picbasic.co.uk/forum/showthread.php?t=9165 Have used this to get the timer values.
Attachments
Interrupt2.fcf
(11.5 KiB) Downloaded 321 times

User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Re: Unstable INT interrupt

Post by Jan Lichtenbelt »

Hi Gary,

Thanks for your help. You gave an other solution to measure pulse time. You replaced the INT-interrupt by a software(Flowcode) solution by monitoring a change of portB.

I tried your Flowcode, but I'm sorry to say, but that will not work for 1 msec pulses with a high accuracy. Increasing frequency of the timer can possible give a correct pulse length measurement. But I'm afraid that the interrupt macro is to long (take to much time).

When using the INT_interrrupt, I found a solution (as said in the first part) by using the second pulse. But the first and all other pulses must give a correct interrupt on both rising and falling edges (See C-code for details). The timer2 interrupt counter must give the pulse length. The question stays why the first peak fails to give the correct pulse length???? That is silly. That is very unexpected. Electrical the INT-interrupt will work correctly. Thus there should be a software/Flowcode/C-code problem?? But where???


Kind regards

Gary Freegard
Posts: 45
Joined: Mon Nov 07, 2011 6:36 pm
Has thanked: 1 time
Been thanked: 30 times
Contact:

Re: Unstable INT interrupt

Post by Gary Freegard »

Hi
I have made two program for you to try, Interrupt2 and Interrupt4.

Interrupt2 just uses the TMR2 interrupt at approx 76.8KHz for the counter and it polls PortB to see if there is any change at which point it will start or stop counting. Using this method I get the following:
0.1ms 8
1.0ms 77
10.0ms 770
20.0ms 1575
25.0ms 548
There is an issue around 23ms.

Interrupt4 does not use any Interrupts at all, just polls PortB and if there is a change then start or stop a counter. With this methosd I get the following readings:
0.1ms 16
1.0ms 170
10.0.ms 1710
100.0ms 17083

I have tested both programs and even the initial reading is correct (within +-10% if not less) .
Gary
Attachments
Interrupt4.fcf
(15 KiB) Downloaded 312 times
Interrupt2.fcf
(11 KiB) Downloaded 311 times

User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Re: Unstable INT interrupt

Post by Jan Lichtenbelt »

Dear Gary,

Thanks for your 2 solutions. Both use a (kind of) timer and a triggering on change of the input status of portB. In principal an interrupt on change of portB should be more accurate. That is the reason I prefer the INT-interrupt.

I hope Ben, Steve or someone else, can help me, why I get wrong results with the measurement of the first pulse using INT-interrupt.


Kind regards,

Jan

Gary Freegard
Posts: 45
Joined: Mon Nov 07, 2011 6:36 pm
Has thanked: 1 time
Been thanked: 30 times
Contact:

Re: Unstable INT interrupt

Post by Gary Freegard »

Hi Jan

I decided to have another look at your code.
here what I have done:
modified your INT_Interrupt
only using B Int for timing
runs continuously

I have tried this, using continuous and single shot pulses at 1ms and lower (0.063ms is the lowest ), I can say that the reading was correct for the first pulse and all others as long as the pulse was above 0.063ms and less than 4.3 sec.

Gary
Attachments
Test_INT_Interrupt_V1.fcf
(15 KiB) Downloaded 350 times

User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Re: Unstable INT interrupt

Post by Jan Lichtenbelt »

Hi Gary,

In the meanwhile I was puzzeling on the electronics hardware. Today I started again with the software and I saw your improvement. Sorry for my late reaction. But your simple solution works correctly.

Thanks a lot

Jan Lichtenbelt

Post Reply