Using Interrupts

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Post Reply
Chad528
Posts: 9
Joined: Wed Sep 19, 2007 4:43 pm
Contact:

Using Interrupts

Post 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

saschech@gmx.de
Posts: 714
Joined: Wed Jan 31, 2007 12:41 pm
Has thanked: 1 time
Been thanked: 26 times
Contact:

int

Post 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

Chad528
Posts: 9
Joined: Wed Sep 19, 2007 4:43 pm
Contact:

email

Post by Chad528 »

chad528@cox.net

I am using the 16F877A

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Post 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

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Post by echase »

I am using a 16F886

MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

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

User avatar
Steve
Matrix Staff
Posts: 3426
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

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

MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

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

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Re: Using Interrupts

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

User avatar
Steve
Matrix Staff
Posts: 3426
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Using Interrupts

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

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Re: Using Interrupts

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

User avatar
Steve
Matrix Staff
Posts: 3426
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Using Interrupts

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

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Re: Using Interrupts

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

User avatar
Steve
Matrix Staff
Posts: 3426
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Using Interrupts

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

Post Reply