• 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.

Using TIMER LOOPS for SERVO control 18LF4431

camerart

Active Member
Thread starter #1
Hi,
I'm programming SERVO out PINS on PIC 18L4431.
I need either a LOOP approximately 20ms with 6x Servo times of 2ms each, leaving 8ms for the control program.
OR 6x LOOPs of 2ms within a 20ms LOOP. (Which has been suggested as the best option)

TMR1 is being used for another purpose.

I use Oshonsoft. Are there any suggestions? I only need an outline please.

Camerart
 

camerart

Active Member
Thread starter #3
Use timer 5 with special event trigger to generate pulses.

Mike.
Hi M,

You didn't say which of the options in #1 you are referring to, I'm hoping it's the second, where there is a 2ms LOOP within a 20ms LOOP, please clarify.

I've just looked at Timer5 in the D/S, and it's mighty complicated.

Is this correct: The MAIN LOOP program is running and an interrupt diverts to a SERVO LOOP with the SERVO timings inside and sets SERVO 1 then goes back to the MAIN LOOP for the remainder of the 20ms, then increments to SERVO 2 and repeats, till 6x SERVOs, then starts again?

C.
 

Pommie

Well-Known Member
Most Helpful Member
#4
You can do it various ways,
1. use interrupts to pulse the pins. Not recommended.
2. Wait for the TMR5IF to be set and add an amount to PR5 to time the next event.

So something like,
make sure timer5 is counting microseconds. If running at 8MHz (2MHz instruction speed) then prescaler = 2.
Set pin high and add servo position (in uS) to PR5. So 1500 for centre position.
Wait for TMR5IF.
Clear TMR5IF.
Set pin low and add 2000 - servo position to PR5.
Wait for TMR5IF.
Clear TMR5IF.
Repeat for next servo until all servos done.

Add 20,000 (20mS) minus 2mS per servo to PR5.
The TMR5IF will be set once it's time to start again.
If you have 6 servos then this time will be 8mS which you can use to do your other stuff.

Note, if your servo position can go to 2mS then you need to up the time per servo to something like 2.5mS.

Mike.
Edit, you don't need the special events trigger as timer5 has a period register.
 

camerart

Active Member
Thread starter #5
You can do it various ways,
1. use interrupts to pulse the pins. Not recommended.
2. Wait for the TMR5IF to be set and add an amount to PR5 to time the next event.

So something like,
make sure timer5 is counting microseconds. If running at 8MHz (2MHz instruction speed) then prescaler = 2.
Set pin high and add servo position (in uS) to PR5. So 1500 for centre position.
Wait for TMR5IF.
Clear TMR5IF.
Set pin low and add 2000 - servo position to PR5.
Wait for TMR5IF.
Clear TMR5IF.
Repeat for next servo until all servos done.

Add 20,000 (20mS) minus 2mS per servo to PR5.
The TMR5IF will be set once it's time to start again.
If you have 6 servos then this time will be 8mS which you can use to do your other stuff.

Note, if your servo position can go to 2mS then you need to up the time per servo to something like 2.5mS.

Mike.
Edit, you don't need the special events trigger as timer5 has a period register.
Hi M,
I'll give it a go, thank you.

Previously, I had a none working program, that I can modify, but it uses TMR0, I presume this is ok? Is there something special about TMR5, that I need?
C.
 

Pommie

Well-Known Member
Most Helpful Member
#6
I miss read the data sheet and thought it worked like the Special Events Trigger of the PWM module - it doesn't. When Timer5 reaches the value in PR5 it resets to zero and sets the bit TMR5IF. This means you can use it to time any event up to 65.5 mS. What speed is your crystal?

Mike.
 

camerart

Active Member
Thread starter #7
I miss read the data sheet and thought it worked like the Special Events Trigger of the PWM module - it doesn't. When Timer5 reaches the value in PR5 it resets to zero and sets the bit TMR5IF. This means you can use it to time any event up to 65.5 mS. What speed is your crystal?

Mike.
Hi M,
Me too! I chose the PIC because I thought the same.
I'll carry on with the TMR0 timer program, which now will save a lot of effort.
EDIT: I've been using the INT OSC 4 or 8MHz depending on the results. At the moment I'm using less time to make the simulator quicker, and will be more accurate, once I get sensible results.
Thanks,
C.
 
Last edited:

Pommie

Well-Known Member
Most Helpful Member
#8
I didn't say timer5 is not suitable. In fact timer5 is far more suitable than timer0 as it has a period register. You just use it differently than timer1 with the Special Events Trigger.

If you set intosc to 8MHz then you need to set the prescaler to two in order for timer5 to count μS.

Mike.
 

Latest threads

EE World Online Articles

Loading

 
Top