Page 1 of 1

Hz flash using interupt

Posted: Thu Dec 17, 2020 11:25 am
by siliconchip
hi all
im trying to get my head around using Interrupts and how they work, therefore ive done a short program to try to simulate a led flashing at 1Hz but im having problems, the Interrupt is 15.2 Hz does this mean as im toggling the output its flashing 7.5Hz on and 7.5Hz off im confused on what many of you will find easy please pardon my ignorance

bob

Re: Hz flash using interupt

Posted: Thu Dec 17, 2020 12:22 pm
by Benj
Hi Bob,

Yes an interrupt firing at 15.2 Hz will run the selected macro 15.2 times a second.

Your program looks ok but will likely be flashing at a rate of 0.5Hz. 1second on and 1 second off.

The problem with your program is you are also calling the pop macro within your main loop, remove the macro call from the loop and it should run as expected.

Re: Hz flash using interupt

Posted: Thu Dec 17, 2020 2:40 pm
by siliconchip
hi benj
thanks for the reply in simulation the led seems to flicker quick certainly more than 1 times a second even with the macro removed from the loop ??,also in some form of code wouldn't the timer macro need calling to perform the function rather than leaving it out

bob

Re: Hz flash using interupt

Posted: Thu Dec 17, 2020 5:49 pm
by medelec35
Hi Bob,
To toggle output at a more precise frequency, take a look here.
siliconchip wrote:
Thu Dec 17, 2020 2:40 pm
in simulation the led seems to flicker quick certainly more than 1 times a second even with the macro removed from the loop ??
Yes, simulation runs much faster than on real hardware so that is normal.
siliconchip wrote:
Thu Dec 17, 2020 2:40 pm
also in some form of code wouldn't the timer macro need calling to perform the function rather than leaving it out
No the interrupt call macro (pop in your case) is automatically called 15.259 times every second or once every 1/15.259 = 65.5ms.
It is handled by the microcontroller so you don't need to call it as Ben has stated.

Re: Hz flash using interupt

Posted: Fri Dec 18, 2020 8:00 pm
by siliconchip
Hi martin
As always thanks for the reply, i removed the "pop" macro call from the main loop and added the relevant osccon details for speed and tested in hardware and found i had roughly a 1 second flash, i then used the example in the link you provided for toggling and found this was flashing a lot quicker ie half the frequency for both on and off using my base frequency of 15Hz im guessing you would need some thing like a johnson counter ie the cmos cd4017 to divide the output to something suitable for your application if you couldnt get the desired frequency from the toggle method ??

Bob

Re: Hz flash using interupt

Posted: Fri Dec 18, 2020 8:10 pm
by medelec35
Your welcome Bob.
siliconchip wrote:
Fri Dec 18, 2020 8:00 pm
im guessing you would need some thing like a johnson counter ie the cmos cd4017 to divide the output to something suitable for your application if you couldnt get the desired frequency from the toggle method ??
That should not be necessary.
Just using the count and and additional variable should do it .
What frequency did have in mind?
I will edit your flowchart toggle at the stated duration.

Re: Hz flash using interupt

Posted: Fri Dec 18, 2020 9:04 pm
by siliconchip
Hi martin
Ive not a particular frequency in mind but it would be nice to select a desired frequency from 1 to 1500 or something like that but i guess if possible would get pretty involved also just out of interest could you alter the code as you suggested to say 5Hz

Bob