Trying to count from 2 counters Timer 0 and Timer 1
Posted: 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
//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