Welcome, Guest
Username: Password: Secret Key Remember me

TOPIC: 1 Second Timer

Re: 1 Second Timer 6 years 1 day ago #16532

  • nerdz
  • nerdz's Avatar
  • Offline
  • Fresh Boarder
Hi Everyone! I figured I would share this little bit of Code for a 1 second timer. it requires the use of a external 32khz Crystal on T1OSO/T1OSI, but Ive found these can be bought cheaply anywhere. Some of the code is from the tutorials (which were a big help), while some of the code is from microchip data sheets; which Ive been pouring over for the last few days to make sure I understand the registers.

One thing that I didn't understand was the preloading. You preload the High Byte (TMR1H) with 0x80 (which is 128 decimal) and clear the low byte (TMR1L). Without preloading the timer would interrupt at every 2 seconds, So by preloading it to 32768, you essentially cut that in half (1000 0000 0000 0000 in binary btw).

For demonstration purposes, I have the PIC toggling PortB.7 Every Time the interrupt occurs. I timed it on my Scope and I get 1 second on, and one second off, using no delays what so ever, which is how I wanted it.

There is some code that isn't used. The interrupt routine didn't like when I removed the constants for some odd reason. I couldn't figure a way around it. The code works though. It just needs some cleaning.

Timer 1 is setup to accept an external clock, asynchronous timing.
Device = 18F2410
Clock = 8
Config OSC = INTIO67                // Use the Internal Oscillator
 


Dim
 IntCount As Byte,
 TMR1IE As PIE1.0, // TMR1 Interrupt Enable
 TMR1IF As PIR1.0, // TMR1 Interrupt Flag
// TMR1ON As T1CON.0, // TMR1 Count Enable
 Timer1 As TMR1L.AsWord // A quick way of creating a Word Alias
 
Const 
 TMR1StartVal = $80, // User defined TMR1 starting value
 TMR1ReloadVal = TMR1StartVal + 5 
 
 
Interrupt TMR1_Interrupt()
 Save(0) // Back up system variables
 If TMR1IF = 1 Then
 TMR1IF = 0 // Clear the TMR1 Interrupt
 T1Con.0 = 0 //Turn timer 1 off
 TMR1H = $80 //Preload high byte with 128
 TMR1L = 0   // Preload Low byte with 0
 T1CON.0 = 1 // timer 1 on
 IntCount = IntCount + 1

 EndIf 
 Restore // Restore system variables
End Interrupt 
 
 
Sub TMR1_Initialize()
 TRISC.0 = 1 // If External clock, then set clock as an input
 T1CON.0 = 0 // Timer 1 off
 TMR1H = $80 //Preload with 128
 TMR1L = 0   // 
 T1CON.0 = 1 // timer 1 on
 T1CON.1 = 1 // 1 = External clock from pin RC0/T1OSO/T1CKI (on the rising edge)
             // 0 = Internal clock (FOSC/4)
    
 T1CON.2 = 0 // 1 = Do not synchronize external clock input   
 // 0 = Synchronize external clock input
 // When T1CON.1 = 0;
 // this bit is ignored.
 T1CON.3 = 1
 T1CON.4 = 0 
 T1CON.5 = 0 
 // 11 = 1:8 prescale value
 // 10 = 1:4 prescale value
 // 01 = 1:2 prescale value...
 // 00 = 1:1 prescale value
 T1CON.6 = 0 
 T1CON.7 = 0
   
 TMR1IE = 1 // Enable TMR1 Interrupts

 Enable(TMR1_Interrupt) // Enable TMR1 Interrupt Handle
End Sub 
 
// Start Of Main Program...
IntCount = 0 // 
TMR1_Initialize // Setup and enable TMR1
 
While True
If IntCount = 1 Then
   High (PORTB.7)
        
End If

If IntCount = 2 then
    Low (portb.7)
    Intcount= 0
End If
 
Wend

Re: 1 Second Timer 6 years 14 hours ago #13356

  • Baldor
  • Baldor's Avatar
  • Offline
  • Expert Boarder
  • Posts: 135
  • Thanks received: 91
Are you using the cristal as an external oscilator for the pic? If not, you dont need it for de timer, you could use the internal oscilator to feed the timer. Just configure the timer to use the system oscilator, preload the timer with the apropiate value, and fire the int on overflow. I program in C, so i dont know if my conde could be of some use.

I have an small spreadsheet to calculate the value you have to preload based on system clock, multiplier and the time you want.
Aprendiz de mucho, maestro de casi nada.
Time to create page: 0.221 seconds