Hi boys/girls.

I have a task to determine if the frequency on a PIC input changes more than certain %, let’s say 1%. If that occurs, I should turn on an LED and when the frequency is back, that LED should be off again.

I am using PIC18F458 which has 4 timers, TMR0 as per manual is able to calculate pulses on RA4, i.e. able to work as a counter.

What I decided to do is to use 2 timers TMR0 and TMR1. Initially I will reset them, after that TMR0 will count input pulses up to overflow, TMR1 will count an internal frequency within that timing interval. When TRM0 overflows, I will just stop timers and keep that value of TMR1. After that reset TMR0 and TMR1 and start counting again, etc.
Comparing the current value and the subsequent value of TMR1 I can say whether we had a difference of more than 1%, i.e. this is like an indirect way.

The problems.
1. In my program the TMR0 does not work but I checked the control codes and for me they look all right.
2. I am not sure if I use overflow of TMR0 correctly in terms of providing reset for overflow bit, etc.

Anyway, the part of the program which calculates the subsequent values in TMR1 is below and the program does not go below


Define OSC 10
define HSER_TXSTA 20h
define HSER_BAUD 9600

' variables defintion
overflow var word
OverflowValue var word
TMR1valuelow var byte
TMR1valuehigh var byte
Value var word
TMR1Overflow var bit
TMR0Overflow var bit

TRISA = %11111111 ' Set all PORT A to input
TRISC = %10010000 ' Set all PORT C to output/input for RS232

' ports variables
InputSignal var PORTA.4 ‘ actually TMR0 does not work if I remove this operator
‘ in such mode TMR0 should count pulses on PORTA.4
TMR0Overflow = INTCON.2 ‘ just in case overflow flag reset for TMR0, not sure I
‘ can like that

PIR1.0 = 0 ' clear overflow flag of TMR1 just in case
INTCON.2 = 0 ' clear overflow flag of TMR0
T1CON = %10000000 ' stop TMR1, internal clock, scale 1:1, 16-bit work
T0CON = %01101000 ' stop TMR0, 8 bit, input RA4, low-high, no prescaler
TMR1L = 0 ' clear TMR1 low
TMR1H = 0 ' clear TMR1 high
TMR0L = 0 ' clear TMR0 low

main:
T0CON = %11101000 ' start TMR0, 8 bit, input RA4, low-high, no prescaler (1:1)
T1CON = %10000001 ' start TMR1 as 16-bit timer
while TMR0Overflow = 0
wend
T1CON = %10000000 ' stop TMR1
T0CON = %01101000 ' stop TMR0
TMR1valuelow = TMR1L
TMR1valuehigh = TMR1H
OverflowValue = TMR1valuehigh * 256
Value = TMR1valuelow + OverflowValue ' this is value of TMR1
‘ overflow+remaining

Hserout [dec Value]
' for checking purposes when adjust the program
' put Value to RS232 thru hserout to see TMR1 value

TMR1L = 0 ' clear TMR1 low
TMR1H = 0 ' clear TMR1 high
TMR0L = 0 ' clear TMR0 low
PIR1.0 = 0 ' clear overflow flag of TMR1
INTCON.2 = 0 ' clear overflow flag of TMR0


goto main

End

Questions.
1. Why TMR0 does not calculate anything?
2. Am I right with TMR0 overflow handling?

Thanks for comments.