Jump to content

Recommended Posts

Hello,

I need a PWM Output on 10 Pins.

Do anybody write a PWM output on variouse pin's?

I wrote the folling code:

while (j <= 4)

{

 while ( i <= 255)

 {

   set_bit(portb, 1);

   delay_us(200);

   clear_bit(port. 1);

   delay_us(55);

   i=i+1;

 }

 i=0;

 j=j+2;

}

 

This code shoud genertae a output frequenze, with e.g. 1Khz with a output for 72,5%on an 27,5%off.

But the output is not so, why?

 

thanks

Prefekt

Share this post


Link to post
Share on other sites
Guest RDH8

You might consider using Timer interupts.

In the interupt routine you can change the timeout of the timer and also cycle the pin off or on.

 

You could also create a timout thats much higher speed than the PWM you are looking for and put a counter in the interupt code that when it counts down to zero cycles the I/O pin.

 

And if you want 10 pins then you will need 20mHz to handle it.

Share this post


Link to post
Share on other sites
I mean I am getting the wrong duty cycle

 

I'd guess that the processing time of the loop instructions is skewing your duty cycle. The number of assembler instructions between "delay_us(55);" and "set_bit(portb, 1);" comes out to about, say, 12. At 4MHz, that would be, what, 15us? That would give you an off time of 70us instead of 55us, so your duty cycle would be more like 74%, with a frequency of about 3kHz.

 

How about something like this:

main()
{
  unsigned char value1=55;
  unsigned char value2=128;
  unsigned char i=0;
  while (1)
  {
    i++;
    if (i<value1)
    {
      set_bit(portb, 1);
    }
    else
    {
      clear_bit(portb, 1);
    }
    if (i<value2)
    {
      set_bit(portb, 2);
    }
    else
    {
      clear_bit(portb, 2);
    }
  }
}

 

You should be able to add as many values as you like. The frequency will drop but the duty cycles should stay reasonably consistent.

Share this post


Link to post
Share on other sites

i made a nice simple pwm signal wich is able to have multiple output and if im not mistaken you can have a much pins as you want, i use 3 pins and it works great w/o having an external clock:

 

#define rood  output_low_port_b(3) 
#define groen output_low_port_b(1)
#define blauw output_low_port_b(2)
#define ruit  output_high_port_b(3) 
#define guit output_high_port_b(1)
#define buit output_high_port_b(2)


void rgb(int r, int g, int b)
{	
 
for (freq = 200; freq > 0; freq--)
 {
 
 if(r > 0)
 {rood; r=r-1;}
 else{ruit;}
 if(g > 0)
 {groen; g=g-1;}
 else{guit;}
 if(b > 0)
 {blauw; b=b-1;}
 else{buit;}
 }

}

 

it runs this loop 200 times and if you set r = 100; than a 100 times output_low_port_b(3) will be set and a 100 times output_high_port_b(3) wich will give a dutycycle of 50%. and offcourse the same for g and b. so if you want a cycle of 10% than you just say r=20; and the for loop will keep the outport for 20 cycles high and low for 180 cycles. i used a command vcc led btw so outport high and low are inverted.

 

i hope you find this somewhat usefull, or some1 else <_<

Yod

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...