PDA

View Full Version : Reset timer / encoder transition



DJEE
- 19th June 2007, 23:08
I need to write a subroutine for my main program where I am looking for a transition from an encoder where there is one transition per second. I am new to PIC programming and I am having trouble figuring out how to use the timers in a 16F628A to accomplish this task. My programming experience has been mostly with PLC’s and I could easily figure out how to do it with the timers or counters in a PLC, but I just haven’t figured out how to do it with a PIC. I am using an external RC oscillator which runs at 6 KHz. The direction I planned to go was to reset a timer and clear the interrupt flag and then look for the edge of a transition on the encoder input. If I see a transition within a 3 second window, I would reset the timer to keep it from overflowing. If I don’t see a transition in this 3 second window then I would go into an ISR. I wanted to use the timer so that it could be running in the background while I also monitored the encoder and some other inputs. The other inputs could determine that it is okay to not get a transition from the encoder. This seems like it should be a simple task but I am have trouble figuring out how to have the timer run in the background while I monitor the encoder input transitions and continue to scan other inputs at the same time. I have used the Search feature on this forum to look at different timer examples but I don’t seem to find any example that will work for me.

mister_e
- 20th June 2007, 18:30
Why 6KHz? Not sure if the PIC may run as this slow... Internal is free, and gives 1-2 more i/o.

i need another cofee...

DJEE
- 20th June 2007, 19:01
The 6KHz oscillator is to get under the 10KHz limit to avoid FCC testing of our product. I do not need a fast clock to handle the inputs involved and clock accuracy is not critical. I have the PIC running at 6KHz and it is running the other portions of my program okay. I have just not yet been able to think of a way to use the PIC timers to confirm that I am getting a transition on 1 input while I am also monitoring other inputs. I need to see that the input has changed within a 3 or 4 second window. It seems like it should be simple but I am not used to this type of timer. I am used to a timer that I could start and then reset with each transition so that the timer does not time out. If it does time out I would go to a fault handling routine. I need this timer running in the background while I am checking other inputs. I am sure it can be done I just haven't worked with PIC's before.

Bruce
- 21st June 2007, 18:11
It may seem a lot harder than it actually is.

Try this. We're using Timer0 configured as a counter. When your encoder
makes a transition on RA4/TOCKI (Timer0 clock input pin), it increments
the Timer0 count.

Real simple. Runs in the background all on its own, etc,,.



TRISA.4 = 1 ' RA4/T0CKI = input to TMR0 counter
TRISB.0 = 0 ' RB0 = output for LED
CMCON = 7 ' All digital

' Assign prescaler to WDT for 1:1 prescale on TMR0
' TMR0 clock will be from your external input on RA4/T0CKI
' Increment on high-to-low transitions
OPTION_REG = %00111000

' If you prefer, then increment on low-to-high transitions
' OPTION_REG = %00101000

Main:
TMR0 = 0 ' Clear TMR0 count before start

Loop:
WHILE TMR0 = 0 ' Wait for high-to-low transition
WEND ' on RA4/T0CKI
PORTB.0 = 1 ' LED on to indicate transition seen
PAUSE 500
PORTB.0 = 0 ' LED off
GOTO Main ' Start over

END
Timer0 configured as a counter can count your encoder transitions in the
background, and your main code can do whatever else it needs to in the
mean time.

You can set it to increment on high-to-low or low-to-high transitions by
flipping a single bit in the OPTION register.

Just look in your datasheet at the OPTION register & Timr0 sections for
how/why it works. It's really simple once you tinkered with it a bit.

DJEE
- 22nd June 2007, 14:37
Bruce, thanks for the idea. Your idea would need some adjusting to make it run in the background in my application, but it has got me to thinking about using the timer as a counter and setting it just a couple of counts from overflowing and then my program will need to see it overflow because of the transitions or else jump to a fault routine. I am just used to the smooth operation of timers and counters in PLC programming and the PIC timers just seem awkward to use.