Welcome, Guest
Username: Password: Secret Key Remember me

TOPIC: Hi Guys - TIMER1

Re: Hi Guys - TIMER1 10 years 2 months ago #14406

  • srspinho
  • srspinho's Avatar
  • Offline
  • Fresh Boarder
  • Posts: 1
Hi graham !

I´m sorry for the comments on the wrog place. I think the comments poluted your articles page.

If you prefer, you can remove them from there.

Well, I will try to post here what is happening with me :

I got your Sample about Timer1, compiled and run it on ISIS. The sample is perfect !

I gto the 10 Hz frequency as expected.

After that, I executed the PicMultCalc.exe to get the correct configuration for my need with the Timer1 :

Device = 18F452
Clock = 20
 
Dim
    second As Word,                // segundo
    uS As Word,
    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 = 65043,          // User defined TMR1 starting value
    TMR1ReloadVal = TMR1StartVal + 7


Sub GeraFrequencia()
    High(PORTB.0)                  // Toggle PORTB.0
    Low(PORTB.0)     
    If uS = 10000 Then
     Toggle(PORTB.1)
     uS = 0
    EndIf              //
End Sub

 
Interrupt TMR1_Interrupt()
    Save(0)                       // Back up system variables
    If TMR1IF = 1 Then
        TMR1ON = 0                // Disable TMR1
        Timer1 = TMR1ReloadVal    // Reload a new start value (includes non-counted cycles while disabled)
        TMR1ON = 1                // Enable TMR1
        TMR1IF = 0  
        GeraFrequencia()              // Clear the TMR1 Interrupt
        uS = uS + 100
    EndIf 
    Restore                       // Restore system variables
End Interrupt   
 
 
Sub TMR1_Initialize()
TMR1ON = 0                 // Disable TMR1
T1CON.1 = 0                // 1 = External clock from pin RC0/T1OSO/T1CKI (on the rising edge)
                           // 0 = Internal clock (FOSC/4)
'TRISC.0 = 1               // If External clock, then set clock as an input
'T1CON.2 = 1               // 1 = Do not synchronize external clock input
                           // 0 = Synchronize external clock input
                           // When T1CON.1 = 0;
                           //   this bit is ignored.
T1CON.4 = 0                // 11 = 1:8 prescale value
T1CON.5 = 0                // 10 = 1:4 prescale value
                           // 01 = 1:2 prescale value...
                           // 00 = 1:1 prescale value
Timer1 = TMR1StartVal      // Fill the Timer register with a starting value
 
TMR1IE = 1                 // Enable TMR1 Interrupts
TMR1ON = 1                 // Enable TMR1 to Increment
    Enable(TMR1_Interrupt) // Enable TMR1 Interrupt Handle
End Sub    
 
// Start Of Main Program...
uS = 0                             // Clear the mS counter
TMR1_Initialize                    // Setup and enable TMR1
Low(PORTB.0)                       // Make PORTB.0 an output, and set it low
Low(PORTB.1)
 
While True
    
Wend

But, as we can see in the picture below, what we get is a frequency of 7825 hz...

I really did not understand why.

Re: Hi Guys - TIMER1 10 years 2 months ago #561

  • DigitalDIY
  • DigitalDIY's Avatar
  • Offline
  • User is blocked
  • Posts: 6
  • Thanks received: 1
By preloading the timer with 65043, you are only allowing for 492 (65535 - 65043) lines of assembly code to be executed in-between interrupts.

Put another way, if an interrupt happens while the mcu is already servicing an interrupt, then it is ignored. In your case 2175 interrupts are ignored each second.

Yes ISIS is extremely stable, providing you have the latest updates.


From the article comment;
Hi Graham !

Here I am again !

Well, As I told you last week, I was using you Multi-Task code to read my car´s sensors ]define OSC 4 ' 4 Mhz Crytal

'

ASM
;T0IF = TMR0IF
;T0IE = TMR0IE
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler CCP1_INT, _Capture, PBP, yes
INT_Handler TMR1_INT, _Timer1, PBP, yes
endm
INT_CREATE ; Criate interrupt
ENDASM

Load = 65443 ' Load TMR1 to overflow every 100us
CCP1CON = %00000101 ' Capture mode - Raise Edge
T1CON = 0 ' TMR1 turned off

Capture:
If CCP1CON = %00000101 Then ' If captured a raise edge...
TMR1L = load.lowbyte ' Reload the TMR1 to overflow every 100us
TMR1H = load.highbyte
on_timeb = 0 ' Set the on-time variable to zero
T1CON.0 = 1 ' Turn on TMR1
CCP1CON = %00000100 ' Change CCP to capture on Falling edge.
PIR1.0 = 0 ' Clear the TMR1´s overflow flag
GoTo OVER_CCP ' end, Goto end of interrupt routine
EndIf
If CCP1CON = %00000100 Then ' If captured a falling edge...
T1CON.0 = 0 ' Stops TMR1
On_TimeT = On_timeT + On_timeB ' Load the on_timeT and continue adding to get the total / Clear the variable after showing in the lcd
CCP1CON = %00000101 ' Change the CCP to capture on Raise Edge
EndIf
OVER_CCP:
...[/code]

You can use TMR1 as either a Clock or a Counter at any one time. I would use TMR2 for all of your clock requirements, why, well it is a setup-and-forget type module. No reloading of values, and has little overheads. I have a guide on setting up TMR2 here.

Now you can use TMR1 exclusively to detect rise and fall times

Re: Hi Guys - TIMER1 10 years 2 months ago #562

  • DigitalDIY
  • DigitalDIY's Avatar
  • Offline
  • User is blocked
  • Posts: 6
  • Thanks received: 1
Is it ok if I remove your comments from the article now that they are in the forum?

Re: Hi Guys - TIMER1 10 years 2 months ago #563

  • srspinho
  • srspinho's Avatar
  • Offline
  • Fresh Boarder
  • Posts: 1
Hi Graham !

Thank you for your explanation !

you´re right ! Actually, double right !

I did configure the TMR2 and I got the 100us period without problems. Very, very good !

Now, I will use some intermediate variables to create multiple "Virtual" timers and TMR0 to get the speed sensor etc

Very nice !

Talking about my comments in the Article Section, yes, you can remove them from that. The discuss here in the forum is more complete ! ! !

Thank you very much !

Regards

Sérgio

Re: Hi Guys - TIMER1 10 years 2 months ago #564

  • DigitalDIY
  • DigitalDIY's Avatar
  • Offline
  • User is blocked
  • Posts: 6
  • Thanks received: 1
you´re right ! Actually, double right !

No worries srspinho, say, could you post some piccy's of your project? Maybe in a new thread would be good

Re: Hi Guys - TIMER1 10 years 2 months ago #566

  • picem
  • picem's Avatar
  • Offline
  • Fresh Boarder
could you post some piccy's of your project?


2nd that! Sounds like a cool project
Time to create page: 0.275 seconds