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

Multiple PWM using PICBasic or C need help!!!

Status
Not open for further replies.

picBasic_Freak

New Member
Hello there,anyone of you know how to produce atleast 3 output of PWM signal using PICBasic or C?I cannot do it in the normal way by using timer because there is a significant delay when i just simply use the following code:

Loop: High 0 ' Turn on LED connected to PORTB.0
Pause 5 ' Delay for 5 miliseconds

Low 0 ' Turn off LED connected to PORTB.0
Pause 5 ' Delay for 5 miliseconds

High 1 ' Turn on LED connected to PORTB.1
Pause 5 ' Delay for 5 miliseconds

Low 1 ' Turn off LED connected to PORTB.1
Pause 5 ' Delay for 5 miliseconds

Goto loop ' Go back to loop and blink LED forever
End
if i am using the the above code i will not manage to produce simultaneous pulse with duty cycle of 50% for each of the output pins.Can anyone please help out of this ? Thanks :D The PIC i am using is PIC16F628 and this is limited to just 1 output of PWM signal if i followed the PWM module of the PIC >_<
 

Eclipsed

New Member
Do you mean 5 milliseconds(pause 5) or 500 milliseconds(pause 500)? Your comments are in conflict with your code.Also do you want different periods for each PWM?
 

ivancho

New Member
Write to the whole port instead of one bit at a time..

So do something like
Portb = %0000011
pause 5
Portb = %0000000
.....etc.

Ivancho
 

ivancho

New Member
How do you want the user to change the duty cycle? A switch pehaps, so they can choose between 2 duty cyles?

Duty Cycle is the ratio of ON to OFF. The thing to have in mind is that with PWM you don't change the period, so the ON time + the OFF time will always be the same.


And define your DutyCycle to be set by the user. And define the OFFDelay in terms of the ONDelay. If DutyCycle is a byte variable you can assign 100 to be 100% Duty Cycle and 0 to be 0%. the OffDelay will simply be the Period - ONdelay. And the ONDelay would be (Period * Duty Cycle)/100.


So if your period is 100ms a 25% Duty Cycle would be 25ms ON and 75ms OFF. The ONdelay = 100ms * 25 = 2500/100 = 25 ms. Your OFFDelay = Period - ONDelay (100ms - 25ms) = 75ms

Period = 100ms
ONDelay = (Period * DutyCycle ) /100
OFFDelay = Period - ONDelay

Portb = %0000111
pause ONDelay
Portb = %0000000
Pause OFFDelay

If you want different Duty Cycles it gets tricky.
 

Rescyou

New Member
Algo

A simple Algorithm:


---------------------------------

Start

Clear all variables

max = 255 ; This is the number of points in the cycle

Loop counter from 1 to max

Turn on led 1
Turn on led 2
Turn on led 3

;Variable1&2&3 are your duty factors

variable1 = max - counter ; This dims the led from 255 down to 1
variable2 = counter ; This brightens the led from 1 to 255
variable3 = max - counter

loop counter1 from 1 to max

if variable1 = counter1 then Turn off led 1
if variable2 = counter1 then Turn off led 2
if variable3 = counter1 then Turn off led 3

Complete counter1 loop

Complete counter loop

Go back to start

-------------------------------------------------------------

Though this is a pretty basic version, it works pretty decent. I can get 21 LEDS running individual duty factors from a 20mhz 16f876 with no noticeable flicker.

There are more advanced ways of doing this also with interupts as this one consumes massive processing power and must be pretty much dedicated to doing this.

Basically what is happening here is that you turn everything on at the start of the cycle and then shut the LEDs off at the appropriate spot within the cycle as designated by their duty and the LEDS remain off until the full 1-255 cycle is complete. You don't have to use 255 either, you can go much higher if you have only a few leds.

If you want to do some different fades etc. you can do something like this:

---------------------------------

Start

Clear all variables

max = 255

Loop sequence from 1 to 2

Loop counter from 1 to max

Turn on led 1
Turn on led 2
Turn on led 3

If sequence = 1 then

variable1 = max - counter
variable2 = counter
variable3 = max - counter

End the If

If sequence = 2 then

variable1 = counter
variable2 = max - counter
variable3 = counter

End the If

loop counter1 from 1 to max

if variable1 = counter1 then Turn off led 1
if variable2 = counter1 then Turn off led 2
if variable3 = counter1 then Turn off led 3

Complete counter1 loop

Complete counter loop

Complete sequence loop

Go back to start

-----------------------------------------------------------------


R.
 

GraveYard_Killer

New Member
Hello there,anyone of you know how to produce atleast 3 output of PWM signal using PICBasic or C?I cannot do it in the normal way by using timer because there is a significant delay when i just simply use the following code:
i tried it to pic16f877, it has one pwm output, and i was able to produce 8 PWM outputs via NPN transistor switching for 8 leds.
 

0mega

New Member
some pics have PWM built in, you just give it a duty cycle (0 = 0, 1023 = 100) , eg. 18F452 does, but many others do too
 

thead9

New Member
After some research myself I found this code and you can change the portd.2 to any port and pin and it works as pwm. I used it on a pic 16f877A to dim an led with a a2d from with a pot on pin 1. You can change the value of dutyCycle to any value to get different outputs.

pwm portd.2, dutyCycle, 10
 
Status
Not open for further replies.

EE World Online Articles

Loading
Top