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

HOW TO generate 3pwm output using one pwm in a pic microcontroller

Status
Not open for further replies.

Anniyan_x

Member
hi !!

im able to use pic16f873 and set it to pwm mode, but it can controll only one led,the problem is i wan to controll 3led using that one available pwm output, i wan to do RGB color changing project for my car dashboard.i very new to pic..i noe assembly only abit..any idea how to use that one pwm to controll rgb leds and get the color i want myb by pressing switch..i got alot of info by search,buit im still abit unclear...
 

Papabravo

Well-Known Member
Forget about using the hardware PWM. The idea is to use a timer and a bit of programing to control the number of port lines that you desire. It is however more difficult to get to 0% and 100% duty cycle with a software PWM. Any value in the 10% to 90% range should be practical.

If you have a list of events LED{1,2,3} = ON or OFF you make a schedule with the time to the next event. Set a Timer for that amount of time and do the events when the appropriate time interval expires.
 

Sceadwian

Banned
For LED's a software PWM will allow control of more devices (up to the limit of your I/O pins) But at a lower frequency. considering you're talking LED's there's no reason not to do it in software on the device you currently have. As long as the refresh rate is over 100hz perceivable flicker will be low. Even a 1khz software PWM using a single timer and a few registers should be easy enough to implement.
 

Anniyan_x

Member
Hmm pic 16f737 and 767 tht offer three 10 bit hardware pwm generators would be a nice solution and easy for me....but a'way i hav in hand 873.. i think i should find more resouces on generating PWM to control 3 r,g,b led using software PWM.i not yet grasp the technique of using software PWM and still not clear abt it..actuacly i just need the switch into differents colors. like blue thn press a swicth mybe change to other color like purple. thanks guys..anyway any ideas or codes is available for me to study and understand would greatly appriciated..
 

philba

New Member
sw pwm is pretty simple when you think about what PWM is. You are just timing when the output pin is high and then when it is low.

If you google for it, you will get a page full of spot-on hits.
 

Anniyan_x

Member
does the period effects the color or on/off duty cycle effects the color??

if al 3 led hav same period but different duty cycle it will only effect the brightness of a same color or???

different periods ofr each led will produces different colors??

any1??
 

Sceadwian

Banned
Period means absolutely nothing, and needs only be high enough to overcome human persistance of vision. The higher the better. 100hz general isn't noticed when directly looked at but flicker can be seen during motion or through peripheral vision. I'd try to keep it at or above 1khz to avoid someone being bothered by flicker. Duty cycle will effect the perceived brightness of the individual LED's, but will be different for each colour (different LED's have very different intensity outputs at the same rated current) For decent colour control you're going to want at least 10bits for your duty cycle. 8 won't really cut it as 100% duty cycle on the dimmest of the colours will likley be the same relative brightness as 60% duty cycle on the brightest of the colours.
 

Anniyan_x

Member
hi everyone...i need any help suggestion, example codes and any other information regarding my pic project as im new to pic and neeeeeeeeeeeeds alot help.im doin in assembly language



input:- a rotary switch/encoder

output:- 3 led, red green blue



the pic must read the rotary switch and vary the leds to generate different colors according as been preset



im tryin to built a simple RGB controller and drive the red blue green leds using software pwm as the device i hv supports only 1hardware PWM.after goin thru alot net surfs, i plan to use TMR0 TMR1 TMR2 as the period registers for the pwm and change the TMR0 TMR1 TMR2 value to varying the generated colors. i must hv a rotary switch/encoder were i use it to select the preset colors which i want. approx 20 colors. i plan to read the rotary switch in the main prosess and assign values to the TMR0 TMR1 TMR2 and thn wait for interupt myb!!. whn the interupts occurs,il reset or set the leds on shedule one by one. after tht il go bck to the main processing. as example tmr0 overflow will set/reset red led , tmr1 overflow will set/reset green led/ tmr2 will set/reset blue led. wat im having difficulties is how and where do i write the updated values of tmr0 tmr1 tmr2 in main process or in THE ISR, how to reload the new values into the tmr0 tmr1 tmr 2......or if i use one timer only is it possible to vary the RGB leds to generate different colors...i thought of reading the rotary switch and thn place 3 look up tables for the 3 timers and get the values from the look up table one by one and write into the timer registers to set the periods in the main processing.but i dont noe how to make it happen in assembly. im still in flow chart process but im stuck in middle coz im not sure am i doin it the right way or is there any better and easier way. i hav to update the timers registers according to the rotary switch/encoder and thrn generate the desired output...hope u al can get wat im tryin to meant and pls if hv any suggestion and no matter wat suggestion,or any example codes on generatimg software pwm and ant related releven example i would glady want it.!!! thanking al in advance



ANNIYAN
 

Anniyan_x

Member
hello sceadwin...so if i use the same period for thr 3leds and only varying the duty cycles of each led it is ok already right?? il get different variations of colors.so periods are just to avoid flickers. i hv posted my project above and would suggest any changes coz i thought of using 3 different periods for each led..so now it could use only only 1 timer to set the period for a 3leds and just vary the duty cycle...or i thought of using al 3 timers to set and reset each led,as mention above to vary the duty cycle.
 

Sceadwian

Banned
Yes, 1 timer for three leds is fine. In fact, a single timer could handle three LED's and a myriad number of other tasks easily. You should be able to get 8 bits of resolution per LED for each 2mhz of the device that's doing the PWM if the code is efficient using a RISC based processor (PIC or AVR) That's with 8 LEDs at approximatly 1khz
 

Anniyan_x

Member
Sceadwian said:
For LED's a software PWM will allow control of more devices (up to the limit of your I/O pins) But at a lower frequency. considering you're talking LED's there's no reason not to do it in software on the device you currently have. As long as the refresh rate is over 100hz perceivable flicker will be low. Even a 1khz software PWM using a single timer and a few registers should be easy enough to implement.
hello sceadwian...let say i use 1timer for all 3 leds and not 3timers as i previously thought of,so in thr ISR i must set/reset each led with different duty cycle each time.let say i just 20 different colors. so now the period for all 3 led is the same rite,the timer now act as period....and only thing i have to do in make the duty cycle vary for each led.can u giv me an idea on how vary the duty cycle for each led..i mean the ontime and offtime of the led should now different to create variations..im just not clear on it.u said even 1khz software PWM using single timer and few registers should easy enough to implement.can just explain abit more in details, i hav a picture in my mind but it would be a great if could spend abit time on explaining abit more in detail..i realy greatly appriciated it:) ...abit more!! thanks sceadwian...:)
 

Sceadwian

Banned
Create three variables (or use three registers) and set them to the duty cycle you want.
Also create and clear a master PWM variable.
Set the timer up to simply free run and enable the timer overflow interupt. If your timer is running at clock speed you'll get an interupt routine every 256 clock cycles on an 8bit timer with no prescaler.
When the ISR triggers compare the master PWM, if it's zero turn on all of your PWM I/O lines and immediatly return from the ISR after increasing the master PWM variable 1. If it's not 0 compare it with each in turn of the LED registers. If it's equal, turn that particular I/O line off and go to the next LED compare line, the last line should increase the master PWM variable.. That's about it. You just have to make sure that worst case scenario your ISR routine executes in less than 256 cycles.
 
Last edited:
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top