View previous topic :: View next topic |
Author |
Message |
bulut_01
Joined: 24 Feb 2024 Posts: 219
|
encoder pulse measurement |
Posted: Thu Mar 13, 2025 1:19 pm |
|
|
Good day forum friends, I have a problem about encoder measurement. I have an encoder that gives 100 Hz signal, I need to understand its direction and how much it rotates, I do direction finding with logic gates, I use pic 18F, I use t1cki external counter, this counter always counts forward, I need to make it count backwards, I need a software algorithm.
My sample code below counts up. I need an idea for counting down. Since the mcu does a lot of processing, the encoder should not miss a pulse.
Code: |
setup_timer_1(T1_EXTERNAL | T1_DIV_BY_1); //enkoder
set_timer1(0);
if(GET_TIMER1() > 0 && input(direction) == 0){
enkoder_pulse = GET_TIMER1();
//set_timer1(0);
} |
|
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9444 Location: Greensville,Ontario
|
|
Posted: Thu Mar 13, 2025 2:17 pm |
|
|
which 'encoder' ?
most rotary encoders have 2 outputs ( A and B )
if A leads B, CW if B leads A CCW
CCS does supply an encoder example in the examples subfolder
If you cannot miss a pulse, I suggest 'offloading' the encoder using a chip like the US Digital LS7266(?). Been awhile might be wrong part number but nice chips to work with. |
|
 |
bulut_01
Joined: 24 Feb 2024 Posts: 219
|
|
Posted: Thu Mar 13, 2025 2:20 pm |
|
|
temtronic wrote: | which 'encoder' ?
most rotary encoders have 2 outputs ( A and B )
if A leads B, CW if B leads A CCW
CCS does supply an encoder example in the examples subfolder
If you cannot miss a pulse, I suggest 'offloading' the encoder using a chip like the US Digital LS7266(?). Been awhile might be wrong part number but nice chips to work with. |
Yes, encoder a and b are 2 pins. These are 4013 logic inputs. I determine the direction from here and a pulse comes out. I am using an external PIC counter. There is no problem with forward counting, but I have no idea about backward counting. |
|
 |
bulut_01
Joined: 24 Feb 2024 Posts: 219
|
|
Posted: Thu Mar 13, 2025 2:32 pm |
|
|
I need a forward and reverse counter algorithm. Using T1CKI external counter, I find the direction information from CD4013 IC.
T1CKI counts up continuously in hardware. I need to create a countdown algorithm in software. |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9444 Location: Greensville,Ontario
|
|
Posted: Thu Mar 13, 2025 5:03 pm |
|
|
so you could....
read the counter
read the direction
if direction is fwd, count =count+1 else count=count-1 |
|
 |
bulut_01
Joined: 24 Feb 2024 Posts: 219
|
|
Posted: Fri Mar 14, 2025 1:04 am |
|
|
Since the counter is constantly increasing by 1, saying the counter is -1 is meaningless, the number does not change and remains constant. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19763
|
|
Posted: Fri Mar 14, 2025 2:41 am |
|
|
As a comment, are you committed to your PIC?????
If not, there are PIC's that have the complete quadrature decoder circuitry
'built in', which will handle counting up and down, without needing any
external gates. Much easier.
Otherwise you have a number of choices:
1) use an interrupt, rather than the timer, and in this do count up/down
and Jay describes. This is the normal way to do this.
2) Add another gate, and feed the count when forward into your existing
timer, and the count when backwards into another. Then subtract the
second from the first to get position.
Normal PIC hardware counters are unidirectional, so your current hardware
approach is not easy to use. |
|
 |
diode_blade
Joined: 18 Aug 2014 Posts: 56 Location: Sheffield, South Yorkshire
|
|
Posted: Fri Mar 14, 2025 2:43 am |
|
|
If your using an 18F series, then as a suggestion why not use an 18F4431 which has a quadratue encoder interface, no software to write to count the pulses or check direction, which you can do by checking the direction flag bit level, just set up the registers.
Use these all the time for things like this.
Still available. |
|
 |
bulut_01
Joined: 24 Feb 2024 Posts: 219
|
|
Posted: Fri Mar 14, 2025 2:55 am |
|
|
Ttelmah wrote: | As a comment, are you committed to your PIC?????
If not, there are PIC's that have the complete quadrature decoder circuitry
'built in', which will handle counting up and down, without needing any
external gates. Much easier.
Otherwise you have a number of choices:
1) use an interrupt, rather than the timer, and in this do count up/down
and Jay describes. This is the normal way to do this.
2) Add another gate, and feed the count when forward into your existing
timer, and the count when backwards into another. Then subtract the
second from the first to get position.
Normal PIC hardware counters are unidirectional, so your current hardware
approach is not easy to use. |
2
Can you give an example with code for the second topic? |
|
 |
bulut_01
Joined: 24 Feb 2024 Posts: 219
|
|
Posted: Fri Mar 14, 2025 2:57 am |
|
|
diode_blade wrote: | If your using an 18F series, then as a suggestion why not use an 18F4431 which has a quadratue encoder interface, no software to write to count the pulses or check direction, which you can do by checking the direction flag bit level, just set up the registers.
Use these all the time for things like this.
Still available. |
unfortunately there is no 18F25K20 QEI module used |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19763
|
|
Posted: Fri Mar 14, 2025 4:28 am |
|
|
Use (say) Timer 0 and Timer 1.
Setup both to simply count on there external inputs. Add interrupts to
handle overflows on both, so you have got 32bit counters, instead of
16bit. Have the upper 18bit counter be signed.
Externally have the clock signal routed to T0CKI when forward, and
T13CKI when backwards.
Then simply subtract the T1 count from the T0 count to give position. |
|
 |
bulut_01
Joined: 24 Feb 2024 Posts: 219
|
|
Posted: Fri Mar 14, 2025 4:31 am |
|
|
Ttelmah wrote: | Use (say) Timer 0 and Timer 1.
Setup both to simply count on there external inputs. Add interrupts to
handle overflows on both, so you have got 32bit counters, instead of
16bit. Have the upper 18bit counter be signed.
Externally have the clock signal routed to T0CKI when forward, and
T13CKI when backwards.
Then simply subtract the T1 count from the T0 count to give position. |
timer0 16 bit is used for another job, I can't use it for the encoder, I only have T13CKI, I have to do my job accordingly. |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9444 Location: Greensville,Ontario
|
|
Posted: Fri Mar 14, 2025 7:18 am |
|
|
hmm, please post which '18F' PIC you're using...
there may be 'options' on how to do what you want but not all '18F' PICs are the same ! |
|
 |
bulut_01
Joined: 24 Feb 2024 Posts: 219
|
|
Posted: Fri Mar 14, 2025 8:21 am |
|
|
temtronic wrote: | hmm, please post which '18F' PIC you're using...
there may be 'options' on how to do what you want but not all '18F' PICs are the same ! |
18f25k20 |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9444 Location: Greensville,Ontario
|
|
Posted: Fri Mar 14, 2025 8:36 am |
|
|
OK, did a quik read of the datasheet...
this 'should' work
setup timer1 register pair to 0xfffe and enable it's interrupt.
when it overflows ( 1 pulse from encoder....)
have the timer1 ISR
read the 'direction' bit from the 4013 chip.
then either add or subtract 1 from 'thecount'
lastly preload the timer 1 register pair with 0xfffe
exit the ISR
at least this sounds good in my brain..... |
|
 |
|