PWM does not work with Interrupt

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

Moderators: Benj, Mods

Post Reply
User avatar
FCCHow
Posts: 27
Joined: Fri Jul 28, 2006 10:40 am
Contact:

PWM does not work with Interrupt

Post by FCCHow »

HI, guys,

Brief history:
I have started casually using Flowcode3 and PICStart Plus during the Elektor Electronics era and even after upgrading to Flowcode6, I find this version is still quite palatable for me. Now with Flowcode9, I need to catch up on how to use the newer versions properly before I bite the bullet.

I have two main issues but I will start with this one first. I have just gotten a toy Ghostbusters Proton Pack and wanted to upgrade the electronics in it. All seems well and good until I learned about using interrupts. There is this feature which, when I press a switch (or from an activated relay to give a high signal from another PIC since I do not know how to use a PIC to control another PIC or pass data yet), it would go into the Interrupt on the PIC16F883. Let's call this interrupt, Overheat.

When the Overheat Interrupt is called, the LEDs on Ports B and C lights stops at their current state. But for the life of me, I could not figure out how to ramp up and down the LEDs via PWM for Ports A0 to A3. Once the interrupt ends, Ports B and C continues their sequence under the main program which, the PWM works normally. Try as I might, the PWM just does not work during Interrupt where macros Ovr_Cyc_up and Ovr_Cyc_Down were called. Thanks to benj, who amended JWalthall;s PIC12F629 PWM routine from more than 14 years ago which was a real life saver.

So, I am not sure where or which settings I have not gotten right.

Edit: 2347, 08072021
My apologies, I have removed the file as my chart looked so embarrassingly amateurish.
Last edited by FCCHow on Thu Jul 08, 2021 4:49 pm, edited 1 time in total.
What can I take apart, today?

chipfryer27
Valued Contributor
Valued Contributor
Posts: 617
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: PWM does not work with Interrupt

Post by chipfryer27 »

Hi

V3.... that is going back in time indeed. Happy to recommend the latest version.

Being honest I can't remember that far back with regards to what features are included in V3, or rather what current features are not included. Opening the chart up in a later version may not give the true picture (I only have v5 onwards on this machine). I should have v3 on an older machine I can check with but I won't be able to do so until the weekend.

However I did open it in v5 and I see a couple of concerns.
You have set the clock to be internal but forgotten to include a C-code block at the very beginning to actually set the clock.
This should do it osccon = 0x70;

I see your interrupt routines are quite long and not only are there delays in them, you are calling further interrupts from within the interrupts. This could be your problem. Interrupt routines should be as short as possible and generally it's a bad idea to interrupt an interrupt.

Rather than try and modify "old" technology I'd recommend you download the free 30 day trial of the latest version of Flowcode and rewrite using it. I'm sure it will be far easier for you in the long run and you will get up to date support from all the community, not just those old enough to remember v3 <s>

I will try and look out my v3 copy to have a proper look though.

Hope this helps a little.
Regards

User avatar
FCCHow
Posts: 27
Joined: Fri Jul 28, 2006 10:40 am
Contact:

Re: PWM does not work with Interrupt

Post by FCCHow »

Thank you, chipfryer27,

To be honest, I do not have much experience with C even after all these years, or even with any sort of programming language at all. I was so happy to get Flowcode to do what is required since the stuff that I do is just, well, there's not much participation among other Engineers, even more than a Decade ago.

But yes, this is my poor excuse of creating a chart. I do get chastised by my lecturer long ago for my unconventional solutions. But I will too, look at the interrupt within the interrupt error as well. And I will have to start familiarising myself with Flowcode 6 before jumping to 9 as well.

Thanks,
Chow
What can I take apart, today?

chipfryer27
Valued Contributor
Valued Contributor
Posts: 617
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: PWM does not work with Interrupt

Post by chipfryer27 »

Hi

I am by no means a programmer. That is why I use Flowcode as do many others. However there are occasions you need to use a little bit of "actual coding" which is why they have the C-Code Blocks.

Without Flowcode I'd have been pretty much lost when I started with PIC's. From the tutorials and (in my opinion) the excellent support from the forum, I quickly learned how to create (to me) quite complex code. Each version of FC brings more to the table and that's not something you can always say about all software. There is a learning curve, but I don't believe it's a steep curve when considering FC.

You have V6 so if I were you I'd use it to update your code even if it means a rewrite. Far superior to V3 and I'm sure Matrix will agree with that :) I'm happy to help out if I can if you get stuck on something, as I'm sure other forum members will be too.

Unconventional solutions are usually the most interesting :)

Regards

chipfryer27
Valued Contributor
Valued Contributor
Posts: 617
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: PWM does not work with Interrupt

Post by chipfryer27 »

Hi

As you have a copy of V6, I knocked up the attached example to illustrate how easy it is to control PWM outputs and utilise an interrupt in newer versions.

It isn't an updated version of your code (and isn't intended to be) and uses default values in components. The target chip is the popular 16F1939 which has five PWM modules available

I stress again it's just an example of using PWM and an Interrupt.

The code first enables four PWM outputs then enables a button to be an Interrupt before entering the Main loop.

The loop has a Switch Decision that branches according to the value of the variable "Switch".

If 0 then the outputs of the PWMs ramp up to maximum before ramping back down (repeating).

If the button is pressed then the program performs the Macro "Interrupt_Routine". The routine only contains one calculation which is to set the value of Switch to 1.

In the next loop the program will branch to Switch position 1, which sets all PWM outputs to zero, waits five seconds then sets the Switch value back to 0. This has the effect of halting the PWM mid ramp, setting outputs to 0, then after five seconds it continues where it left off.

Hope this is of some help.
Attachments
V6_Example.fcfx
(13.25 KiB) Downloaded 266 times

User avatar
FCCHow
Posts: 27
Joined: Fri Jul 28, 2006 10:40 am
Contact:

Re: PWM does not work with Interrupt

Post by FCCHow »

Dear chipfryer27,

Thank you for taking the time to sort this out for me.
Wow. This is amazing! I was originally thinking along something like this but could not figure out how it could be done. Which was why I used Interrupt as a last resort.

Thank you once again.
What can I take apart, today?

chipfryer27
Valued Contributor
Valued Contributor
Posts: 617
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: PWM does not work with Interrupt

Post by chipfryer27 »

Hi

Glad to have been of some small help.

Keep us posted on progress and I hope it all goes well.

Regards

Post Reply