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.

generating Pwm Pulse in the "background". How?

Status
Not open for further replies.

kanabalize

New Member
Hello guys....

i am planning to control a servo using timer interrupt....

My question is how do we keep sending pulses to the servo using timer interrupt while doing other works at the same time?
 
yeah any idea for that?

program is running
.
.
.
.
interupt after 10 ms (for example)
set output pin high
reset timer to interupt in 5 ms
will now interupt after that 5 ms
set output pin low
reset time to interupt in 15 ms
will now interupt after that 15 ms
set output pin high again...
etc
etc
 
use timer

you can do one thing

use timer with interrupt

In it's interrupt put one counter which incremented each time interrupt is called....

now If we consider the limit of that counter is 100 then if you want to do 50% then when counter reaches to 50 you set the uc pin and reset it after 100 count...

code attached here with may help you....
 

Attachments

  • pwm.c
    1.6 KB · Views: 212
A servo needs a pulse of 1 - 2 ms every 20 ms.

If you set a timer running so that it rolls every 20 ms, then you can do this:-

Wait for the timer to roll.
Start pulse
Wait for the timer to get to the correct number to give the right length of pulse
End pulse
Do everything else
Loop to start

That will give you at least 18 ms every 20 ms to do everything else you need.
 
If you're using an 18F' device with two CCP modules then you might consider using one of them along with Timer 1 in "special event trigger" mode.

The following example provides 1 usec servo pulse width resolution but Timer 1 prescaler settings limit you to a clock of 4, 8, 16, or 32 MHz for this resolution. Basically on each compare match interrupt you toggle the Servo output pin and load the CCPRx module "compare" register pair with the servo pulse "on" time or "off" time for the next compare match interrupt. The "special event trigger" mode automatically resets the TMR1 register pair to zero after each compare match.

Code:
unsigned int servo = 1500;           // 500..2499 usecs

void interrupt()
{ if (PIR1bits.CCP1IF == 1)          // if "special event trigger"
  { PIR1bits.CCP1IF = 0;             // clear CCP1 interrupt flag
    LATB ^= 0b00000001;              // toggle Servo pin on RB0
    CCPR1 = 20000 - Servo;           // setup "off" time match value
    if(LATB & 1)                     // if "on" time
      CCPR1 = Servo;                 // setup "on" time match value
  }
}
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top