Timers and Interrupts

Post here to teach people how to do something.

Moderators: Chuckt, Garth, bitfogav

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Post by bitfogav » Thu Jun 03, 2010 6:06 am

yeah I think you have to use this sort of code for timer 2, this checks the flag bit and if your inturrupt was caused by the timer 2 then the next 'goto' line will be read..

Code: Select all

		
btfss	PIR1,TMR2IF	; Flag set if TMR2 interrupt
goto	(user file)		; Jump if not timed out

User avatar
sdudley
Moderator
Moderator
Posts: 337
Joined: Sun Mar 28, 2010 1:33 pm
Location: Florida, U.S.A.
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Post by sdudley » Thu Jun 03, 2010 6:08 am

You understood correctly. On a PIC 16f648a "any" interrupt will cause the Program Counter to jump to address 0x0004.

The programmer has to write code to figure out "which" interrupt occured (i.e. check the flags).

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Post by brad » Thu Jun 03, 2010 6:12 am

sdudley wrote:You understood correctly. On a PIC 16f648a "any" interrupt will cause the Program Counter to jump to address 0x0004.

The programmer has to write code to figure out "which" interrupt occured (i.e. check the flags).
Okay I think I get it. So we are running our code, then out of the blue an interrupt occurs which jumps to 0x0004.

We then have a little piece of code there that checks all the interrupt flags which will then jump to the relevant piece of code.

User avatar
sdudley
Moderator
Moderator
Posts: 337
Joined: Sun Mar 28, 2010 1:33 pm
Location: Florida, U.S.A.
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Post by sdudley » Thu Jun 03, 2010 6:17 am

brad wrote:Okay I think I get it. So we are running our code, then out of the blue an interrupt occurs which jumps to 0x0004.

We then have a little piece of code there that checks all the interrupt flags which will then jump to the relevant piece of code.
Coerrect! However, only if you are using multiple interrupt sources such as a timer to count something as well as a port pin to cause an interrupt do you need to test and see what caused the interrupt.

If you are only using one interrupt source then you already know what caused the interrupt and there is no need to check.
Last edited by sdudley on Thu Jun 03, 2010 6:38 am, edited 1 time in total.

User avatar
sdudley
Moderator
Moderator
Posts: 337
Joined: Sun Mar 28, 2010 1:33 pm
Location: Florida, U.S.A.
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Post by sdudley » Thu Jun 03, 2010 6:33 am

You have already used interrupts in your code but probably didn't think about it as an interrupt.

Anytime you bit test to see if a user pressed a button or something like that you are checking for an interrupt. The only difference here is that you can set the PIC up to automatically tell you an interrupt happend (whatever it might be... a button press, a register counted down to zero, etc.).

This just saves the programmer from having to continually look for an event.

You can also disable the interrupt from happening and manually check the interrupt flag yourself. For instance:

If you want to check and see if a user pressed a button on PORTB, 0 (I believe that is an interrupt source on the PIC16f648a) but you want to wait until some code finishes running first, then you can simply clear the interrupt "enable" bit and check that interrupt flag when you are ready.

Does that make sense?

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Post by bitfogav » Thu Jun 03, 2010 9:19 am

Its funny that you have posted this tutorial because the last few nights I have been trying to mulitplex some 7 seg displays using interrupts, Its been a nightmere but I think I have it done now :)

User avatar
sdudley
Moderator
Moderator
Posts: 337
Joined: Sun Mar 28, 2010 1:33 pm
Location: Florida, U.S.A.
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Post by sdudley » Thu Jun 03, 2010 9:22 am

bitfogav wrote:..Its been a nightmere but I think I have it done now :)
What you're saying is... I'm a horrible teacher.

I understand. :?

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Post by bitfogav » Thu Jun 03, 2010 9:26 am

HaHa.. noo not at all :)

I had to setup Postscalers and Prescalers :( for the Timer, my head has been baffled trying to work out what I needed to set it correctly to display the digits :)

User avatar
sdudley
Moderator
Moderator
Posts: 337
Joined: Sun Mar 28, 2010 1:33 pm
Location: Florida, U.S.A.
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Post by sdudley » Thu Jun 03, 2010 9:41 am

bitfogav wrote:HaHa.. noo not at all :)

I had to setup Postscalers and Prescalers :( for the Timer, my head has been baffled trying to work out what I needed to set it correctly to display the digits :)
Nice save, but really... I understand! :wink:

Seriously though, setting up the prescaler/postscaler, and finding the correct interrupt intervals is no big deal if you use a PIC timer calculator such as the one I mentioned.

There are several available for download on the net but the one I posted a link to is the best I have found and it ROCKS! It even writes the code for you! The code snippet he gives you is in basic, not in assembly (yet) but if you read it you will completely understand it.


*EDIT*

Here is the link again http://pictimer.picbingo.com/

Here is another delay calculator for coming up with precise delay times using delay routines:
http://www.biltronix.com/picloops.html
Last edited by sdudley on Thu Jun 03, 2010 10:06 am, edited 1 time in total.

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Post by bitfogav » Thu Jun 03, 2010 9:45 am

Ahh nice one! Thanks Stacy :)

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Post by bitfogav » Fri Jun 04, 2010 7:23 am

Looking forward to you implementing this interrupt with PWM :)

User avatar
sdudley
Moderator
Moderator
Posts: 337
Joined: Sun Mar 28, 2010 1:33 pm
Location: Florida, U.S.A.
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Post by sdudley » Fri Jun 04, 2010 3:20 pm

bitfogav wrote:Looking forward to you implementing this interrupt with PWM :)
I have finished with the code and will hopefully be able to add the PWM tutorial this weekend. I just need to go through and comment the code as well as create the images for the tutorial. It is very simple and is a continuation of this timer / interrupt tutorial so it should be easy to understand. That is, if you managed to get any useful information out of this one.

I also plan to bring an O-Scope home from work and make a video comparing the DSO Nano to a nice two channel bench top. I will probably just use the PWM tutorial for the video so I can show how the calculated pulse widths compare to the actual measured duty cycles.

I just hope I have enough time with all the other stuff going on.

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Post by bitfogav » Sat Jun 05, 2010 2:40 am

Im sure it will be very useful, I want to look into PWM but I have not yet gone into any projects using it with microchips. alot of systems I work on use PWM, eg Fan motors etc.. So im quite intrested in learning more about it :)

And while we are on the subject of PWM, I would like to see how you made that laser project :)

User avatar
sdudley
Moderator
Moderator
Posts: 337
Joined: Sun Mar 28, 2010 1:33 pm
Location: Florida, U.S.A.
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Post by sdudley » Sun Jun 06, 2010 2:10 am

bitfogav wrote:...And while we are on the subject of PWM, I would like to see how you made that laser project :)
Video 1:
http://www.youtube.com/watch?v=DI62BC2Iwjo

Video 2:
http://www.youtube.com/watch?v=WdfocKhgUY4

Video 3:
http://www.youtube.com/watch?v=7kcZzrcge0g

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Post by brad » Sun Jun 06, 2010 8:06 am

I havent seen those videos in nearly a year. I forgot how cool they came out :)

Post Reply
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Who is online

Users browsing this forum: No registered users and 11 guests