PDA

View Full Version : Background Pulse Counter



svjohngalt
- 6th November 2021, 20:06
I am creating an application which will need to count 2uS pulses while running a control loop.
This will be implemented with the PIC18F4680
The control loop uses DT Interrupts to control some pins used for output.
I have an input which feeds the 2uS pulses in a frequency proportional to speed and can be from 1khz to a max of 30 khz.
I want to have these pulses clock a counter and within my control loop monitor this value and when it reaches specific values execute a sub.

I am not sure if there is a way to setup the timers to be externally clocked then just read the timer register.
I also am not sure if this would impact the DT Interrupts since they use some of the timers as well.

I didn't want to just test the input pin for high and low because since the pulse is just 2uS it could get missed.
Maybe there is a way to set a flag using the 2uS pulse, I read the flag in my loop and increment my own counter then reset the flag.
Is this part of the capture and compare pins?

Thanks for any input or opinion if this is even a reasonable task to accomplish with a PIC18F.

svjohngalt
- 6th November 2021, 20:11
I should add that with the DT Interrupts, I am only using Timer 1 for an internal event timer.
I would presume this would mean anything I use timer 2 or 3 for would leave DT Interrupts unaffected under this situation.

Thanks
George

HenrikOlsson
- 7th November 2021, 09:10
TMR0 can be setup as a 8 or 16 bit counter clocked by your signal attached to the T0CKI-pin, you can choose rising or falling edge. It can also be set to interrupt on overflow so if you want to run your sub every 2000 counts simply preload the TMR0 register pair with 63536 and after 2000 counts an interrupt will occur. Remember to reload the TMR0 register pairs again, otherwise it'll keep counting from 0.

Or you can just read the count at your leasure if that's better/easier/more suitable. When using 16bit mode there's a special order in which you must read/write the TMR0 register pairs, make sure to read the datasheet.

svjohngalt
- 7th November 2021, 22:14
This turned out the be far easier than I thought it would be.
I set T0CON for $E8
TMR0ON = Enable Timer 0
T08BIT = Timer0 8 bit
T0CS = Transition on TOCK1
T0SE = Low to High transition (beginning of 2uS pulse)
PSA = Timer0 prescaler is not assigned
T0PS2:T0PS0 all 0

Then I set the register to 0
TMR0L = $00

In my control loop which enables the device which provides the 2uS speed proportional pulses I read the TMR0L and when it equals to a predetermined value the sub is called and when the sub is finished the register is again set to zero.

It counts in the background and the control loop doesn't have to consume that many cycles reading the register so it really doesn't burden the control loop.

Thanks