Trying to count from 2 counters Timer 0 and Timer 1

Discuss PIC and electronic related things

Moderators: David Barker, Jerry Messina

Post Reply
CHRISHODDYSON
Registered User
Registered User
Posts: 9
Joined: Tue Jul 03, 2007 8:55 pm
Location: SOUTHAFRICA

Trying to count from 2 counters Timer 0 and Timer 1

Post by CHRISHODDYSON » Tue Dec 30, 2014 1:33 pm

//Timer 0 works perfectly and counts every pulser transistion
//Timer 1 works only when i run my pulser wheel at a high speed
//Any Help would be appreciated

Device = 18F4685
Clock = 25

Config
CP0 = ON,
CP1 = ON,
CP2 = ON,
CP3 = ON,
WDT = OFF

#option LCD_DATA = PORTD.4
#option LCD_RS = PORTD.3
#option LCD_EN = PORTD.2
#option KEYPAD_PORT = PORTB

Include "LCD.bas"
Include "utils.bas"
Include "convert.bas"
Include "I2C.bas"
Include "keypad.bas"
Include "owx.bas"
Include "EEPROM.bas"
Include "usart.bas"
Include "String.bas"
Include "math.bas"



Dim timer0_ON As T0CON.7
Dim timer0_OFF As T0CON.7

Dim timer1_ON As T1CON.0
Dim timer1_OFF As T1CON.0

Dim timer3_ON As T3CON.0
Dim timer3_OFF As T3CON.0

Dim TMR3IE As PIE2.1 // TMR1 Interrupt Enable
Dim TMR3IF As PIR2.1

Dim TMR1IE As PIE1.0 // TMR1 Interrupt Enable
Dim TMR1IF As PIR1.0 // TMR1 Interrupt Flag
Dim TMR1ON As T1CON.0 // TMR1 Count Enable
Dim Timer1 As TMR1L.AsWord // A quick way of creating a Word Alias
Dim ps As LongWord

Dim CounterL As Word
Dim CounterL2 As Word

Dim fuelmls As Float



Sub set_timer0()



INTCON.5 = 0 //timer 0 interupt enable
T0CON.7 = 1 //sets timer0 on
T0CON.6 = 0 //sets to 16 bit timer
T0CON.5 = 1 //sets transition on T0CKI pin
T0CON.4 = 1 //sets hight to low transtion
T0CON.3 = 1 // turns off prescaler
T0CON.2 = 0
T0CON.1 = 0 //sets prescaler to 1:256
T0CON.0 = 0

INTCON.5 = 1 //timer 0 interupt enable

End Sub



Sub set_timer1()



TMR1IE = 0 //timer 1 interupt enable
TMR1IF = 0
T1CON.7 = 0 //16 BIT MODE
T1CON.6 = 0 //system clock is deriverd from another source 25MHz Xtal //sets to 16 bit timer
T1CON.5 = 0 //sets transition on T0CKI pin
T1CON.4 = 0 //PRE SCALLER
T1CON.3 = 0 // OSC OFF
T1CON.2 = 1 //DONT SYNCRONIZE
T1CON.1 = 1 //EXTERNAL CLCK
T1CON.0 = 1 //sets timer0 on 1 ENABLES 0 OFF

TMR1IE = 1
INTCON.6 = 1
INTCON.7 = 1

End Sub


//START

Input(PORTA.4)
Input(PORTC.0)
set_timer0()
set_timer1()

TMR0H = 0 // clear Timer0 registers
TMR0L = 0
TMR1H = 0 // clear Timer0 registers
TMR1L = 0

CounterL = 0
CounterL2 = 0

Input(PORTC.0)
Input(PORTA.4)

While TMR1L= 0 Or TMR0L = 0 //wait for the pulser to go high and initialy startup


If TMR0L <> 0 Then
Break
EndIf
If TMR1L <> 0 Then
Break
EndIf

Wend

While(true)

timeoutcount = 0


If TMR0L <> 0 Then
CounterL = 0
timer0_OFF = 0
CounterL.byte1 = TMR0H
CounterL.byte0 = TMR0L
PS = PS + CounterL
TMR0H = 0 // clear Timer0 registers
TMR0L = 0
CounterL = 0
timer0_ON = 1
Input(PORTA.4)

EndIf
If TMR1L <> 0 Then
CounterL2 = 0
timer1_OFF = 0
CounterL2.byte1 = TMR1H
CounterL2.byte0 = TMR1L
PS = PS + CounterL2
TMR1H = 0 // clear Timer0 registers
TMR1L = 0
CounterL2 = 0
timer1_ON = 1
TMR1IF = 0
TMR1IE = 1
Input(PORTC.0)

EndIf


High(led2)





printDisplayDelay = printDisplayDelay + 1
If ps <> 0 Then
fuelmls = ps/100.00
Else
fuelmls = 0
EndIf

floatstring = FloatToStr(fuelmls,4,2,"0")


LCD.WriteAt(3,1,floatstring)





















Wend
CHRISO

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Trying to count from 2 counters Timer 0 and Timer 1

Post by Jerry Messina » Tue Dec 30, 2014 3:21 pm

Well, for starters you're enabling the interrupts in set_timer1(), but you haven't shown us any interrupt handlers. I'm surprised it works at all.

I'd strongly recommend that you don't directly manipulate INTCON.6 and INTCON.7... That's what 'enable()' and 'disable()' are for (one you have proper interrupt procedures, that is)

CHRISHODDYSON
Registered User
Registered User
Posts: 9
Joined: Tue Jul 03, 2007 8:55 pm
Location: SOUTHAFRICA

Re: Trying to count from 2 counters Timer 0 and Timer 1

Post by CHRISHODDYSON » Wed Dec 31, 2014 6:59 am

so I cannot use timer 1 like timer 0 to count pulses. you are saying I need to over flow then handle the interupt and add the count to my total. my overall task is to add two separate pulse inputs pulses together from two schmitt trigger opto pulse wheels.
CHRISO

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Trying to count from 2 counters Timer 0 and Timer 1

Post by Jerry Messina » Wed Dec 31, 2014 11:54 am

so I cannot use timer 1 like timer 0 to count pulses
No, all I'm saying that your set_timer1() routine has enabled interrupts, while set_timer0() doesn't.

If you're happy with the way timer0 works, then I think you should be able to get them both to operate in polled mode by changing the set_timer routines to remove the interrupt

Code: Select all

Sub set_timer0()
    INTCON.5 = 0 //timer 0 interupt enable
    INTCON.2 = 0 // timer0 interrupt flag
    T0CON.7 = 1 //sets timer0 on
    T0CON.6 = 0 //sets to 16 bit timer
    T0CON.5 = 1 //sets transition on T0CKI pin
    T0CON.4 = 1 //sets hight to low transtion
    T0CON.3 = 1 // turns off prescaler
    T0CON.2 = 0
    T0CON.1 = 0 //sets prescaler to 1:256
    T0CON.0 = 0
End Sub

Sub set_timer1()
    TMR1IE = 0 //timer 1 interupt enable
    TMR1IF = 0
    T1CON.7 = 0 //16 BIT MODE
    T1CON.6 = 0 //system clock is deriverd from another source 25MHz Xtal
    T1CON.5 = 0 //sets transition on T0CKI pin
    T1CON.4 = 0 //PRE SCALLER
    T1CON.3 = 0 // OSC OFF
    T1CON.2 = 0 // sync timer1 to internal clock
    T1CON.1 = 1 //EXTERNAL CLCK
    T1CON.0 = 1 //sets timer0 on 1 ENABLES 0 OFF
End Sub
and then also remove the INTCON statements from the the main loop

Code: Select all

While(true)
    If TMR0L <> 0 Then
        timer0_OFF = 0
        CounterL.byte1 = TMR0H
        CounterL.byte0 = TMR0L
        TMR0H = 0 // clear Timer0 registers
        TMR0L = 0
        timer0_ON = 1
        PS = PS + CounterL
        Input(PORTA.4)
    EndIf

    If TMR1L <> 0 Then
        timer1_OFF = 0
        CounterL2.byte1 = TMR1H
        CounterL2.byte0 = TMR1L
        TMR1H = 0 // clear Timer1 registers
        TMR1L = 0
        timer1_ON = 1
        PS = PS + CounterL2
        Input(PORTC.0)
    EndIf
Instead of checking for the low byte of the timer to be <> 0, it would probably be better to check the IF bit of the timer. If it's set then you know you have at least one count event. Every 256 pulses the timer low byte will be 0.

CHRISHODDYSON
Registered User
Registered User
Posts: 9
Joined: Tue Jul 03, 2007 8:55 pm
Location: SOUTHAFRICA

Re: Trying to count from 2 counters Timer 0 and Timer 1

Post by CHRISHODDYSON » Wed Dec 31, 2014 6:53 pm

Thankyou for all your help and clear guidance.
I first tried working without an interupt and it worked the same as before
Then i created an interupt to handle timer1 where i load it with FFFE and add to my pulse count on over flow and it works.
I will probably do the same for timer 0

Thank you again.
CHRISO

Post Reply