Page 1 of 1

Using Interrupts

Posted: Sat Oct 27, 2007 10:22 pm
by Chad528
I want to flash an LED On and Off every 500ms during the execution of my program to show that the program is running without impacting the rest of the program timing. I was thinking of using the TMR0 interrupt. I created the interrupt and put in the LED routine into the macro. However, I do not know how to use the interrupt properly with the variable I created. Can you help me think of a routine I can use to help me solve this problem.

Thanks,
Chad

int

Posted: Sun Oct 28, 2007 11:18 am
by saschech@gmx.de
Hallo
If you send mi the e-mail-adress , i send a small demo.
What type of pic you are using?

regards wolfgang

email

Posted: Sun Oct 28, 2007 8:26 pm
by Chad528
chad528@cox.net

I am using the 16F877A

Posted: Fri Nov 09, 2007 11:16 am
by echase
Any chance of sending it to me too? I will use it to intermittently beep a buzzer, with mark space ratio varying according to the situation. Thanks.

echase@gmx.net

Posted: Fri Nov 09, 2007 11:17 am
by echase
I am using a 16F886

Posted: Sat Nov 10, 2007 4:52 pm
by MJU
Same thing here...

I made a macro that contains a flip-flop.
The only purpose is to have a variable that changes every 0.5 sec from on to off state.
When the controller runs I have a signal that i can use to flash led's if there's need to.

My goal is to control a tempearture, between certain values.
If temp is less than X degrees, the led should burn.
If the temp is less than Y degrees, the led should flash..

I want to control this flashing with an AND function.
If temp < Y AND flash = 1 then LED0 should burn.
After 0.5 sec the variable flash in the macro flashcontrol is 0, so when in the main program the control "If temp < Y AND flash = 1 then LED0=1" is false because flash = 0. The led dims for 0.5 sec.

But the strange thing is that if there is no delay in main, it doesn't flash..
It seems that without a delay in main, the timer overflow doesn't work in Flowcode???
I have enabled the interupt before a while1 loop.
The settings for the interupt seem ok by me..

I have returned my eblock because of a fault on the programmerprint, so i can't test it in real..

Posted: Sun Nov 11, 2007 9:34 pm
by Steve
For that kind of "and" (i.e. a logical "and") you will need to use "&&" instead. The word "AND" is for a "bitwise" AND.

Also, be aware that the timer interrupts do not simulate very well in Flowcode. The use of a delay icon will probably make things simulate better, but you should not need it when running on the chip.

Posted: Sun Nov 11, 2007 9:44 pm
by MJU
steve wrote:For that kind of "and" (i.e. a logical "and") you will need to use "&&" instead. The word "AND" is for a "bitwise" AND.

Also, be aware that the timer interrupts do not simulate very well in Flowcode. The use of a delay icon will probably make things simulate better, but you should not need it when running on the chip.
Okay, thought so, but my programmerprint had a problem when I got it, had to send it back..

Re: Using Interrupts

Posted: Thu Apr 17, 2008 9:57 am
by echase
I want to operate a buzzer for very short pulses every ten seconds or so, like the ones in smoke alarms that bleep intermittently when the battery is going flat. Which timer/interrupt would I use and how? I am already using one interrupt for reading switches on Port B.

I also need to operate it with different mark space ratios and frequencies so the timer needs variable timings. The rest of the software needs to carry on regardless whilst the buzzer is pulsing on and off.

It’s a 16F886 using one of the Port C outputs.

Re: Using Interrupts

Posted: Thu Apr 17, 2008 2:26 pm
by Steve
You could use the PWM output to generate the appropriate tones. You will be able to manipulate the mark:space ratio and the frequency. However, you may not have as much control over the frequency range as you would like. But if you could accept the range of frequencies, then I think that this would be the easiest approach.

You would also need a timer that activated the PWM output at the appropriate points (e.g. every 10 seconds for 250 ms). You would not be able to use timer2 (this is used by the PWM output), but you could use either timer0 or timer1. I'd suggest using timer0 because this is implemented by default in Flowcode.

In the timer0 interrupt, you would need to increment a "counter" variable to give you a range of 10 seconds. If the interrupt frequency was 75Hz, then 250ms would be when the variable reached 19 (ish) and 10 seconds would be when the variable reached 750 (you would need to use an INT variable).

You would have a variable that enabled the output of the sound. The timer0 interrupt routine would check this variable and if it was set to make the sound, then it would start incrementing the counter variable. If this counter was less than 19, then enable the PWM output and if it was greater than 19, disable it. If the counter was above 750, then set it to zero.

You would change the mark:space ratio and frequency of the PWM output inside your main program as and when the change was required. Do not do this within your timer interrupt - you need this to be as succinct as possible.

I hope this gives you some ideas.

Re: Using Interrupts

Posted: Thu Apr 17, 2008 5:49 pm
by echase
I was not clear. The buzzer generates its own tone so don’t need the PWM for that. All I need it a timer to turn the buzzer on and off with a regular pattern to give varying patterns of bleeps.

Re: Using Interrupts

Posted: Thu Apr 17, 2008 8:41 pm
by Steve
My previous post should still help - just ignore the bit about the pwm.

I'm not exactly sure what you are wanting, but if your timer0 was set up for 75Hz and you had a counter variable, then you could create a pattern of bleeps by checking the value of this counter in the interrupt routine and setting the buzzer output on or off appropriately.

For example, you might want the buzzer to come on for 200ms, then off for 400ms, then on for 200ms, then off for 400ms, then on for 200ms and then off. In this situation, the counter increments by 1 every 1/75 seconds, so 200ms = a count of 15.

If the count was less than 15, or between 45 and 60, or between 90 and 105, turn on the buzzer. Turn it off otherwise. If it gets to 750, set the counter back to zero.

Re: Using Interrupts

Posted: Thu Apr 17, 2008 9:33 pm
by echase
That would work well but I am already using timer0 for measuring seconds and an interrupt for RB Port Change so are there any interrupts left available to me? Maybe the custom interrupt? Not sure how to use that.

Re: Using Interrupts

Posted: Fri Apr 18, 2008 9:31 am
by Steve
Yes - you could use timer1 or timer2 via a custom interrupt (or by editing the FCD file). i think this has been tackled in previous forum posts.