View previous topic :: View next topic |
Author |
Message |
georpo
Joined: 18 Nov 2008 Posts: 281 Location: Athens, Greece.
|
pic18F26k40 encoder |
Posted: Wed Oct 09, 2024 1:54 pm |
|
|
Hello,
I am using a pic18F26k40 to drive a DC motor with PWM.
The motor is used in my garage door and has a single output encoder.
So what would be the best way to measure the pulses from the encoder?
1) Interrupt on pin change and modify a variable?
2) use a timer with external clock from the encoder to hold the value?
3) any ideas?
encoder is connected to RB1
Thanks. _________________ George. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19495
|
|
Posted: Thu Oct 10, 2024 1:24 am |
|
|
A little depends on the actual speed of the shaft the encoder is on. If this
can spin at some very high rate, then the hardware timer solution becomes
the only 'practical' one. If the shaft is slower using a simple interrupt
on a pin is quite acceptable.
Obviously you code will have to reset the timer when you change direction
on the motor, and you will then have to treat the count as a -ve value.
Key thing throughout will be careful design to keep noise from the motor
drive away from the PIV. |
|
|
georpo
Joined: 18 Nov 2008 Posts: 281 Location: Athens, Greece.
|
|
Posted: Thu Oct 10, 2024 1:37 am |
|
|
At 100% PWM I saw 625Hz on the encoder output, an interrupt about every 1.6mS but I do not like the idea.
The timer is a better solution but again I will have to keep track of the overflow. I am sure 16bits can not hold the full travel of the door.
And again, when "opening the door" the timer must increment
and when "closing the door" the timer must decrement which I think is not possible.
Or else I will just have the timer count pulses and add them/subtract them from my "position" of the door.
What do you think?
Any better approach? _________________ George. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19495
|
|
Posted: Thu Oct 10, 2024 2:32 am |
|
|
Handling the overflow is easy. Just have an interrupt from the timer counter.
It'll interrupt every time the counter wraps off the end. Have this increment
a high byte of word for the counter.
If you look at several of the timer based examples (like ex_capture.c), they
handle the timer overflow exactly this way. |
|
|
georpo
Joined: 18 Nov 2008 Posts: 281 Location: Athens, Greece.
|
|
Posted: Thu Oct 10, 2024 2:35 am |
|
|
yes I know what you mean and how to do it.
I was loooking for a "better" way.
Is there an example of how to setup the timer1 as counter with external clock from RB1?
This is the first time I use this micro and has many new features for me.
As always you are very helpful and I thank you for this.
Good day!!! _________________ George. |
|
|
|