| Swordfish Code Snippet - TMR0 |
|
|
| Saturday, 04 April 2009 10:19 | |
|
TMR0 is an 8 byte timer/counter that can be pre-scaled to change vary its incrementing cycle. It is quite different in regards to setting up compared to the 16F's, but here's an example.
Device = 18F452 Clock = 20 Dim mS As Word, TMR0ON As T0CON.7, T08BIT As T0CON.6, T0CS As T0CON.5, T0SE As T0CON.4, PSA As T0CON.3, TMR0IF As INTCON.2, TMR0 As TMR0L, TMR0IE As INTCON.5, TMR0_Event As Boolean Interrupt TMR0_Interrupt() Save(0) // Backup system variables If TMR0IF = 1 Then // Check if a TMR0 Interrupt occurred TMR0IF = 0 // Clear the interrupt Inc(mS, 1638) // Increment the mS counter (scale of 1000) If mS >= 10000 Then // Working with a scale of 1000, so this mS = mS - 10000 // checks if 10mS has elapsed TMR0_Event = True EndIf EndIf Restore // Backup system variables End Interrupt Sub TMR0_Initialize() TMR0ON = 0 // Disable TMR0 T08BIT = 1 // Ensure TMR0 is working in 8 Bit mode T0CS = 0 // Ensure TMR increments from internal clock T0SE = 0 // Only used if external source is selected PSA = 0 // Ensure the Clock source uses the Prescaler T0CON.0 = 0 // Set the Prescaler bits T0CON.1 = 0 // T0CON.2 = 1 // TMR0 = 0 // Clear the TMR0 register TMR0IE = 1 // Enable TMR0 Interrupts Enable(TMR0_Interrupt) // Enable the TMR0 Interrupt Handler TMR0ON = 1 // Enable TMR0 to increment End Sub // Start Of Main Program... mS = 0 // Reset the mS counter TMR0_Event = False // Clear the TMR0 Event Flag TMR0_Initialize // Setup and enable TMR0 Low(PORTB.0) // Make PORTB.0 and output and set it low While True While TMR0_Event = False // Wait for 10mS to elapse Wend // TMR0_Event = False // Reset the Event Flag Toggle(PORTB.0) // Toggle PORTB.0 Wend // Loop forever
The result, a very accurate interrupt;
Note the PIC's power supply/oscillator are not shown
Comments (3)
Joomla components by Compojoom
|
|
| Last Updated ( Tuesday, 14 April 2009 10:37 ) |
Whos Online
- andyo
- Jon Chandler
- MrDEB
Forum Activity
LCD / 18F1320 - mrdeb Saturday, 20 March 2010 06:21 - [3 replies]
A 'throw-away" PIC board - mrdeb Thursday, 18 March 2010 06:19 - [32 replies]
Credit Where Credit's Due - jon chandler Tuesday, 16 March 2010 12:01 - [9 replies]
Marching LEDs - mrdeb Saturday, 13 March 2010 22:54 - [7 replies]
USB 8 Channel Servo Controller - andyo Saturday, 13 March 2010 01:19 - [2 replies]
Dedicated Servo Controller - graham Friday, 12 March 2010 17:58 - [0 replies]
Must have....delta temperature glowies! - graham Friday, 12 March 2010 17:31 - [3 replies]
Recent Comments
- 2010-03-20 07:58:45 andyo
Nice. The TAP-28 boards are looking good. When I've had a buzzi...
- 2010-03-19 22:05:12 ozbec...
The author (Hop) suggests to "set the target PIC's configuratio...
- 2010-03-17 22:58:18 Jon C...
Graham, Thanks for fixing the spacing on the array. This is how i...
- 2010-03-17 21:14:34 ASDne...
Thanks Graham, It is a problem with Flowcode because the demo wa...
- 2010-03-17 19:50:32 Graha...
Looks like you've pretty much solved every minor issue that was enc...
- 2010-03-17 11:42:03 Jon C...
The low-cost servo does have one other feature. The origina...
- 2010-03-17 10:56:44 Graha...
I am not familiar with flowcode, though did you try powering the bo...
- 2010-03-15 21:47:55 ASDne...
Hello there, I am still trying to get the LCD to work but I am pro...



Thanks for another helpful article. One question though. Why is there an if statement in your interrupt?
TMR0IF must be 1 if the interrupt was triggered, correct? Making the if statement that exists in TMR0_Interrupt redundant?
Thanks