CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

pic18F26k40 encoder

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
georpo



Joined: 18 Nov 2008
Posts: 281
Location: Athens, Greece.

View user's profile Send private message

pic18F26k40 encoder
PostPosted: Wed Oct 09, 2024 1:54 pm     Reply with quote

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: 19529

View user's profile Send private message

PostPosted: Thu Oct 10, 2024 1:24 am     Reply with quote

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.

View user's profile Send private message

PostPosted: Thu Oct 10, 2024 1:37 am     Reply with quote

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: 19529

View user's profile Send private message

PostPosted: Thu Oct 10, 2024 2:32 am     Reply with quote

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.

View user's profile Send private message

PostPosted: Thu Oct 10, 2024 2:35 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group