Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC16F1824 Comparator to timer1 counter question

Not open for further replies.


Full Member level 5
Feb 23, 2010
Reaction score
Trophy points
South Africa
Activity points
Hi all.
I have a simple problem , that I don't seem to be able to get right.
I have an LMT01 temperature sensor that provides a pulse train that is proportional to
temperature. It's an analog pulse train that has to be processed before it can be counted.
I thought I'd use the comparator in the pic to square it up and then pass on the comparator output to timer1
to count.
I cant seem to get this figured out though.
From the datasheet as I understand it , the comparator (comparator 2 in this case) can be used to GATE timer1.
I want to use it to increment timer1's count directly. How do you achieve this. I'm sure it can be done , i'm just missing something here.
I could always use the comparator interrupt to do the counting ,but the timer option just seems more elegant.


What is the frequency range of this sensor ( relation chip between temp and Freq )
or is the frequence fixe and only the duty cycle change ?

in this case you can use RB0 interrupt and Timer1 interrupt

Set timer1 in interrupt mode to count nb of overflow

Set RB0 interrupt for Rising edge interrupt
so when you get it :
Start Timer1 16 bits,( and also timer1 interrupt to count overflow of timer1..if needed )
change rB0 interrupt setting to falling edge interrupt
when you get it:
store the value of Timer1 ( duration of state =1) + Nb of Overflow * 65536 => to HIGH_STATE
RAZ nb of timer1 overflow
change RB0 interrupt setting to Rising edge interrupt
when you get it:
one cycle is finish
store Timer1 value + NB of overflow*65536 to LOW_STATE

stop all interrupts
Caculate the duty cycle
apply this Duty coefficient to calculate the temperature .

It's not a frequency change , it changes the number of pulses it sends.
The frequency of the pulses is about 80 - 90khz.
For 0 deg C it sends about 800 pulses. ( the pulse "bursts" are sent every 100mS or so)
For 30 deg C it sends about 1280 pulses
For 50 deg C it sends about 1600 pulses... etc
The pulses are not square and only a little over 1 volt Peak to peak , so you HAVE to square
and amplify them. The method you are describing won't work in my case.

You should be able to implement a level detector out of a comparator by feeding the sensor output to the one of the comparator inputs, then connect a trimpot between VDD and VSS, with its wiper fed to the other comparator input. The trimpot sets the voltage level reference which determines whether or not the sensor signal level triggers the comparator output, essentially "squaring" it up.


You can also, sync the pulse train with the timer clock using a D Flip-Flop if need be.


You need to ensure the sensor output range stays within VDD and VSS, you also may need to adjust the comparator register settings to correctly set the "polarity" of the comparator output depending on the specific implementation of the level detector.

Of course, once the correct comparator output is generated, there are several ways of counting the pulses by routing the comparator output to a specific peripheral module input or generate an interrupt.


Squaring up the pulses and all that is the easy part. That is done. It is routing (internally) the pulses to timer1 to count that I can't get right. It is easy to GATE timer1 via the comparator ,I just can't see how to count the pulses via timer 1. There is no external pins available , all the other pins are used.
I can also count the pulses via the comparators interrupt , but I wanted to see if I could use the timer , not have to be interrupted all the time.

Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to