Hz flash using interupt

An area to discuss 8-bit PIC specific problems and examples

Moderator: Benj

Post Reply
siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Hz flash using interupt

Post 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
Attachments
1Hz flash.fcfx
(9.57 KiB) Downloaded 168 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Hz flash using interupt

Post 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.

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: Hz flash using interupt

Post 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

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Hz flash using interupt

Post 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.
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: Hz flash using interupt

Post 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

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Hz flash using interupt

Post 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.
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: Hz flash using interupt

Post 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

Post Reply