| 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
- MrDEB
Forum Activity
simulation program - mrdeb Thursday, 11 March 2010 22:52 - [0 replies]
MARCHING ledS - mrdeb Thursday, 11 March 2010 19:13 - [3 replies]
REALISTIC LED candle flicker - mrdeb Wednesday, 10 March 2010 05:48 - [0 replies]
the LCD demo hello world - mrdeb Wednesday, 10 March 2010 05:41 - [29 replies]
USB problem - jon chandler Tuesday, 09 March 2010 11:31 - [3 replies]
Servo Module - andyo Tuesday, 09 March 2010 03:39 - [3 replies]
pic to ps2 communication - roshan Friday, 05 March 2010 01:30 - [7 replies]
Recent Comments
- 2010-03-11 21:03:32 Jon C...
If this wasn't a class project that had to be done on a PIC16F690 u...
- 2010-03-11 15:30:06 uuu
I am going to send two data from ADC to USART(async mode) to serial...
- 2010-03-05 13:24:23 mrpse...
I was thinking of battery with powered routers or powered for every...
- 2010-03-05 12:17:40 andyo
50 rooms - wow. The XBee modules are very easy to use but I'm not ...
- 2010-03-05 11:46:37 mrpse...
Thanks AndyO. What I have to do is design a device to monitor temp...
- 2010-03-05 03:08:58 andyo
The circuit used for the remote sensor can be found here: [url]http...
- 2010-03-04 07:19:06 renno
plz i need help do u know from where i could download it or how cou...
- 2010-03-04 07:16:05 renno
hello everyone i'm trying to finish my final project and i want to ...



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