Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Counter in the PIC18F2550

Status
Not open for further replies.
Quick question.
Is there a counter function implemented in the PIC18F2550. Like for example use it with a motor encoder signal? And if there is, do I need to use certain pins or can i use say A0 and A1 and then configure the counter to it.

Lot of thanks in advance

Kansai
 
There are two capture/compare/PWM modules on that chip. CCP1 is on RC2 and CCP2 is on RB3. However, you probably want to use those modules to PWM your motors. So, you could use the three interrupt pins (B0-B3) or use the spare timer. You would have to use timers 0 and 1 or 3 as 1 & 3 share the same input pin. Another way would be to use the interrupt on change function of B4-B7 but that gets a little complicated.

Mike.
 
Hi Kansai and Mike,

@ Kansai, as pointed to by Mike there are 4 Timers in the 18f2550 with 2 pins of hardware available for use as Counters(Timer0,1,3) as pointed out by Mike. If you have to take an input from a motor encoder, i would advise you to go with the CCP Module, using the Compare Function you can detect the correct encoding value. If you just want to control the motor than you can use its PWM Feature/ EECP for direction as well as speed control.
 
Last edited:
Thank you guys for the reply. In the case I am seeing I think maybe I will use the CCP module (although I am not clear yet how, since the encoder has two signals, so should I use both? -I am not thinking very clearly now...) this because the motors in this project are controlled by other means and I only have to take the encoder iputs

However, what happens if I want to control the motors with both PWM signals. How can then I process the encoders??

Thanks again
 
It maybe possible (assuming you can juggle the design to leave a timer for PWM) to use a little external logic to do the quadrature decode.

A XOR gate (7486) can be used to double the amount of pulses from the A & B phase and this signal can be fed into a timer clock input, with the timer being set up as a counter.

A D type flip flop (7474) can be have its input on one phase (say A) and its clock input on the the other (say phase B). The output of the flip flop will be the direction of the motor (as one phase will lead the other) and this output can be fed into an intterupt on change pin.

You need one final timer to read the counter and clear it every period in time. You then have pulses per time unit and you can do some closed loop control.

I used this technique sometime ago, maybe worth bearing in mind. Note this decoder is very crude. a state machine in a CPLD would be a far nicer solution (plus you could get 4x decode instead of 2x) but this does work :).

If i remember right this method does lose one pulse on the change between directions. My memory is hazy though ;)
 
Thank you guys for the reply. In the case I am seeing I think maybe I will use the CCP module (although I am not clear yet how, since the encoder has two signals, so should I use both? -I am not thinking very clearly now...) this because the motors in this project are controlled by other means and I only have to take the encoder iputs

However, what happens if I want to control the motors with both PWM signals. How can then I process the encoders??


Thanks again

If you already have the PWM signals ready than why use a Microcontroller to take in the encoder input and than send it again to drive the motors unless you want to do some processing which you show by the statement in bold is not what you want. If you have two encoder outputs than you need to decide how you want to 'combine' them as you put it. How do you want to control the motors using both signals? Different duty cycles? Yes, you can with a microcontroller. Can you tell us how the encoder is working? How the two PWM signals differ?
 
Status
Not open for further replies.

Latest threads

Back
Top