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

400 hz (2ms) hardware pwm on 50 mhz clock speed
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Mahdi



Joined: 21 Aug 2012
Posts: 47

View user's profile Send private message

PostPosted: Sun Aug 04, 2013 11:54 am     Reply with quote

John P wrote:
It may be easy to run 4 (or any number) of PWM's on one timer, or it may not. What makes the difference is how much resolution you need in the outgoing pulse, and I don't believe you've told us that.

The way to do this is to learn to use the timer so that you can make it generate successive time intervals that add together to make exactly 2.5msec. Assuming you do want 4 PWM outputs, you make up 4 bytes (or perhaps fewer, as I'll explain) to write to an output port when each PWM bit goes low. So at time t=0, you set all the PWM outputs high. Then after the right interval, the first one goes low, then after some other interval the next one goes low (and the first one stays low) and so forth until they've all gone low at the correct times. You might have some of the PWM's with the same duty cycle, and that's the situation where you don't need all 5 interrupts: in fact you might need as few as 2, one each to start and stop the waveforms.

Anyway, when all 4 outputs are low, the last time interval you use is something that makes the total end up at 2.5msec, and that's when you set them all high and start again.

But I said it "may be easy". I claim it's simple if you don't need a very high resolution in time intervals. If that's not the case, you're going to have problems if there are PWM duty cycles which are almost, but not exactly, the same. The limit is how long it takes to get out of, and then back into, an interrupt. I can imagine solving that by staying in the interrupt and somehow measuring off the right time between transitions, but that seems like a real mess, and I'd hate to have to do it.

Anyway, that's how to do it. Tell us how it comes out.


thanks for your answer

i need duty cycle from 1 ms to 2 ms with 5us steps. so 1020/5 == 204 step.



this code generate 2 ms freq with 200 step resolution.duty=100 >> 1ms

1 ms to 2ms >> 1020/100= 10us resolution

i think that 10 us resolution Not enough for stability of quadcopter.the esc Sensitive to 4 us.likly switch to dspic series.

Code:
#use Delay(clock=50m)

#use standard_io(d)
#define preload 65470
#define red pin_d0
#define green pin_d1
#define blue pin_d2


int red_duty;
int Intcount;



#INT_TIMER1
void timer_irq()
{
    set_timer1(preload);
   
   if (Intcount < 200)
    {
      if (Intcount == red_duty)
       {
         output_low(red);
       }   

    }
   
   else if(Intcount == 200)
    {
      Intcount = 0;
      output_high(red);
    }   
    Intcount++;
}



void main()
{

    setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);
    enable_interrupts(INT_TIMER1);
    enable_interrupts(GLOBAL);
   red_duty = 63;
   Intcount = 0;

}




Please tell me, if you think about it

thanks alot friends
John P



Joined: 17 Sep 2003
Posts: 331

View user's profile Send private message

PostPosted: Mon Aug 05, 2013 6:25 am     Reply with quote

I think with your fast processor, the scheme I proposed is workable. In fact you could do it by "brute force" with an interrupt at every potential transition on the PWM output, i.e. every 5usec. It is an extremely rapid rate of interrupts, but there's time to get in, do a little work and get out again--however, it would be necessary to ensure that the compiler doesn't insert much (or any) code to slow the interrupts down. I don't know how much that would be for this processor.

You would have a choice of making up a large array where each byte contained bits for every time interval's output--200 bytes, without many changes, which wastes RAM--or you could have a smaller array which would contain successive indexes at which transitions would happen, and until the next one matched the present count, the processor wouldn't load anything to the output pins. That uses less RAM, but it takes more processing in the interrupt. There are arguments to be made about the best way to do it, but it definitely seems possible.
temtronic



Joined: 01 Jul 2010
Posts: 9426
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Aug 05, 2013 6:43 am     Reply with quote

hmm...
you said...i think that 10 us resolution Not enough for stability of quadcopter...

I've made dozens of 2 bladed helicopters and none need 10us for stability.
Even real choppers are 'lazy' control systems.

All were PID analog servo systems, using 16F877@20MHz. Some actually flying via the internet.

You will find a huge difference in prop selection! Changing the pitch just 1 # , up or down , will affect performance characteristics.

hth
jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19731

View user's profile Send private message

PostPosted: Mon Aug 05, 2013 8:20 am     Reply with quote

John P wrote:
I think with your fast processor, the scheme I proposed is workable. In fact you could do it by "brute force" with an interrupt at every potential transition on the PWM output, i.e. every 5usec. It is an extremely rapid rate of interrupts, but there's time to get in, do a little work and get out again--however, it would be necessary to ensure that the compiler doesn't insert much (or any) code to slow the interrupts down. I don't know how much that would be for this processor.

You would have a choice of making up a large array where each byte contained bits for every time interval's output--200 bytes, without many changes, which wastes RAM--or you could have a smaller array which would contain successive indexes at which transitions would happen, and until the next one matched the present count, the processor wouldn't load anything to the output pins. That uses less RAM, but it takes more processing in the interrupt. There are arguments to be made about the best way to do it, but it definitely seems possible.



It is certainly do-able on his processor, but the problem is that the sheer duration spent in the interrupts will kill his servo loop response times, which are far more important than the output rate to the ESC's.
As I said earlier, I'd actually just farm this job out to a much more basic PIC, probably using I2C to control it, and re-write the servo loop to use integer. There is no point at all in being able to update the output rate to the ESC's at 400Hz, if the control loop is running at 50Hz.
Mahdi



Joined: 21 Aug 2012
Posts: 47

View user's profile Send private message

PostPosted: Mon Aug 05, 2013 8:51 am     Reply with quote

temtronic wrote:
hmm...
you said...i think that 10 us resolution Not enough for stability of quadcopter...

I've made dozens of 2 bladed helicopters and none need 10us for stability.
Even real choppers are 'lazy' control systems.

All were PID analog servo systems, using 16F877@20MHz. Some actually flying via the internet.

You will find a huge difference in prop selection! Changing the pitch just 1 # , up or down , will affect performance characteristics.

hth
jay


my project is different from helicopter.this is quadcopter.the helicopter has one prop but the quadcopter has 4 prop.the control system of quadcopter is different from helicopter.in quadcopter you must control four prop but in helicopter you have 1 prop for thrust and it dont need high resolution.

thanks
temtronic



Joined: 01 Jul 2010
Posts: 9426
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Aug 05, 2013 9:16 am     Reply with quote

Actually my helicopters, like most real have 2 motors.
As far as controlling quad copters, it's a fairly simple task, one reason why they are more popular than 'old school' types.
I did a couple 'quads' with 877s as onboard controllers, that's why I know prop selection is very. very important.

cheers
jay
Mahdi



Joined: 21 Aug 2012
Posts: 47

View user's profile Send private message

PostPosted: Mon Aug 05, 2013 11:43 am     Reply with quote

temtronic wrote:
Actually my helicopters, like most real have 2 motors.
As far as controlling quad copters, it's a fairly simple task, one reason why they are more popular than 'old school' types.
I did a couple 'quads' with 877s as onboard controllers, that's why I know prop selection is very. very important.

cheers
jay


The prop standard determine by Manufacturer of brushless motor. For a motor of 1kg thrust use 10*4.5 of prop type. This is standard. If you choose smaller prop than the standard select, your system losses increases and if you choose bigger prop than the standard, the brushless motor bear high thrust and destroy the esc and magnetor of motor.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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