4 channel Light saber driver design, may the light be with you.

50 posts / 0 new
Last post

Pages

Texas_Ace
Texas_Ace's picture
Offline
Last seen: 1 hour 36 min ago
Joined: 03/24/2016 - 07:44
Posts: 4775
Location: Everything is brighter in Texas
4 channel Light saber driver design, may the light be with you.

This is a breakout of a topic started in the Texas Avenger driver thread. Toykeeper is looking to make a driver for a light saber with 4 channels to run RGBA LED’s.

Since it is technically using LED’s and not plasma, I figured it qualifies as an “other LED flashlight”. It could quite easily be used to light up your walk to the bathroom at night, in epic fashion no less….lol

ToyKeeper wrote:
I’m not exactly in a hurry. For now, I’m using my Astrolux S41 in the empty hilt. It’s just the right size, shape, and weight to counter-balance the blade for easier spinning, and as a bonus I can turn it on to make the blade light up. The way it barely fits into the back half of the handle, with the back end press-fit into the pommel, it’s almost like it was made for this purpose.

For the RGBA build though, space is going to be tight. In the back half, there’s just barely enough room for an 18650 cell in a compact battery holder. Then there’s a switch section with a narrower inner diameter, and all the wires will get crammed in there. The front section is 25.5mm internally and long, but needs to hold the driver, emitter pill, and as much blade as possible.

Alternately, the driver could be long and thin (~20mm x ~60mm) and be placed on the back side of the battery holder. This makes driver placement and layout easier but makes the wiring harder. Five long wires from driver to emitter in that case, instead of strapping it directly onto the back of the pill.

… and because the entire hilt is one solid piece (except pommel), I need to make the wires long enough to do the soldering outside then cram the wires in. Long enough that I can pull it out for later reflashing without un-soldering anything. It’ll be fun getting everything to fit.

Note the two screws near the front, one to hold the emitter pill in place and one to hold the blade.

This barely fits into the back half. I could increase available space a little by soldering wires directly onto a battery and omitting the holder.

Would like to make it use a forward clicky, but if I can’t find a suitable one I may have to go with an e-switch. The downside is more complicated code and relying on the charge port’s weak contact spring while power is on. It would send all current through a weak connection and I’ve had issues with that on other sabers. Like, random blade dimming or shutting off when it gets hit. Was planning to bypass that if I can.

FWIW, here is the emitter setup… (this one is 3x royal blue 1x red, but I’ll be doing RGBA) I think I can probably put the driver directly onto the back of the pill, with only + and – wires going to the driver.

ToyKeeper wrote:
Texas_Ace wrote:
That emitter pill, the wires normally go out the bottom I assume, is there a reason those wires could not go directly to the driver (I think thats what you were saying?)

The pill appears to be about 20mm internally, I should be able to make up a 4 channel board in that space, the issue is how many 7135’s you would need, to fit 12 of them would be quite interesting.

That said I think it would be doable, just. All you would need is 4x banks of 3× 7135’s with the SOIC8 150mil pads + C1 + R5. The FET parts are not needed , if there is room I can try to fit C2 and the LVP could use the internal reference correct?

Plus pads for all the channels + power + e-switch + ground.

I should be able to fit 7× 7135’s on one side, possibly 8 if you can remove that lip at the bottom of the driver / sand it down for more clearance so I can put the 7135’s right on the edge of the driver.


The pill has no “internally” measurement. It’s solid except for the wire holes, and it doesn’t screw into its shell all the way. So, the driver can also be up to 25.4mm in diameter; no need to fit it inside a pill. The wires would ideally go directly from the driver to the emitters, with the solid heat sink between. Which means the driver will likely be held in place by its five short forward wires. To expose the MCU, it’ll need to be on the back side somewhere.

I’m assuming there will be no room for an e-switch unless we drop the voltage divider and voltage sense pin, and instead use the internal ref/VCC method which has been tossed around but never tried. No OTC either, for the same reason, unless it uses Mike C’s combined-pin method.

Pins:

  1. Reset (reserved)
  2. PWM2B
  3. PWM2A
  4. GND
  5. PWM1A
  6. PWM1B
  7. Voltage sense and/or e-switch and/or OTC
  8. VCC (and maybe voltage sense)

I’m hoping it won’t need an e-switch, but if so, that’s another pad or two on the back side of the driver. It’s weird having the switch between the battery and driver.

The driver will probably be pressed against the thinner switch section, which might make some of the layout awkward. Here’s the general idea:

The rear “button” there would actually be a charge port with its third pin unused, and the front button would need to be a clicky/latching switch. But an e-switch could go there instead, if I use all three charge port pins and connect a couple more wires to the driver.

Sorry this is getting so off-topic. This really has nothing to do with the TA driver any more.

Opps, I see what I did wrong, the cylinder is what holds the optic to the LED, I thought the emitter screwed on the other way around.

25mm I could do for sure.
25.4mm max diameter.
4x channels of 3× 7135s each.
Attiny25 pads? Or 85?
C1, C2, R5, D1
Pads for each channel ground out
Pads for OTC or E-switch
Pads for power and ground input

In that sized board that should actually not be too hard.

Know guns, know peace, know safety. No guns, no peace, no safety.

The ultimate result of shielding men from the effects of folly is to fill the world with fools. --Herbert Spencer , English Philosopher (1820-1903)

Texas Avenger Driver Series

My LED Test series - XP-L2 V5  - Nichia 219C 90+ CRILatticebright "XM-L"XHP35 & PWM efficiency - XHP50 - XP-L V5 - XM-L2 U2 - XP-G3 S5  - XP-L HI V2 - Oslon Square & direct comparison to Djozz tests - Nichia 319A - Nichia 219B 9080 CRI - Nichia 219C D320 - Nichia 229AT - XHP70.2 P2 - XHP50.2 J4 - Samsung IH351D

Complete collection of all my LED test Charts

Edited by: Texas_Ace on 09/24/2016 - 08:20
ToyKeeper
ToyKeeper's picture
Offline
Last seen: 8 hours 44 min ago
Joined: 01/12/2013 - 14:40
Posts: 6504
Location: (469219) 2016 HO3

Thanks for making a thread for this! Smile

I’m hoping to fit a tiny85, but could probably get by with a 25 if necessary. It’ll just reduce the number or complexity of the modes, or maybe prevent onboard config menus. The code isn’t really formed yet.

Also, still debating about an e-switch or clicky switch, primarily because the e-switch would mean depending on a cheap charge port for the current path and I’ve had some issues with that on other sabers. Er, unless the driver was able to handle (block?) charge current without issues. The plan was to break current via clicky switch before connecting a charger, so the driver wouldn’t be at risk during charging. Alternately, the charge port can cut current, which works well… but when it’s connected it’s a really weak connection. So I can bypass it to avoid the weak point. But then I either need a clicky switch or a driver which doesn’t mind being connected while charging.

I’ve never attempted to charge a battery with the driver in the circuit, so I have no idea what to expect. Is that difficult? An e-switch would otherwise be easier and offers a richer UI, but I don’t want to fry the driver or rely on a weak connection.

Perhaps I should just use all three charge port pins as they’re intended, and not worry about the current going through a weak spring. I can replace it if it fails. Or I could maybe even just cram a clicky switch into the pommel instead of the chunk of lead I was planning to put there. Weight can be added by wrapping the battery in lead foil, perhaps.

… done thinking out loud now. ;P

Texas_Ace
Texas_Ace's picture
Offline
Last seen: 1 hour 36 min ago
Joined: 03/24/2016 - 07:44
Posts: 4775
Location: Everything is brighter in Texas

Ok, if I have 25mm to work with I should have room to spare, so if there is anything else you need let me know. Tiny85 should be easy.

Thinking I will put the 7135’s on the top of the driver facing the LED’s and the MCU on the bottom facing the battery. Lots of extra space for pads and components this way but I will have to see how it all fits in the real world.

As far as charging it with the driver in the circuit. It appears the driver is on one “side” of the charge port and the battery is at the other “side”. So thus the current would not have to flow through the driver correct? It would simply seem the charging voltage.

So in this case the driver should be fine as long as the charger doesn’t output more then 6v (well it should never go over 4.2V for the batteries sake anyways).

In other words it should be fine to have the driver in the circuit during charging unless I am missing something. Now not sure it would work as expected while it was charging but I doubt you will be using it with a cord connected.

Know guns, know peace, know safety. No guns, no peace, no safety.

The ultimate result of shielding men from the effects of folly is to fill the world with fools. --Herbert Spencer , English Philosopher (1820-1903)

Texas Avenger Driver Series

My LED Test series - XP-L2 V5  - Nichia 219C 90+ CRILatticebright "XM-L"XHP35 & PWM efficiency - XHP50 - XP-L V5 - XM-L2 U2 - XP-G3 S5  - XP-L HI V2 - Oslon Square & direct comparison to Djozz tests - Nichia 319A - Nichia 219B 9080 CRI - Nichia 219C D320 - Nichia 229AT - XHP70.2 P2 - XHP50.2 J4 - Samsung IH351D

Complete collection of all my LED test Charts

Texas_Ace
Texas_Ace's picture
Offline
Last seen: 1 hour 36 min ago
Joined: 03/24/2016 - 07:44
Posts: 4775
Location: Everything is brighter in Texas

Ok, I think this is what you are looking for. Let me kow if you see any changes that need to be made.

It has the voltage divider, OCT / e-switch on the same pin since there was room for it, it is 1206 footprint for the OTC.

Obiously 4x channels of 3× 7135’s

Oh yeah,

jump = 0 ohm resistor to jump a trace
BR = bleeder resistor in case you want to do any kind of lighted tailcap, figured I would toss that on there since some form of glowing LED somewhere on the saber while off could look cool, and there was room.

Top of the driver (facing saber)

Bottom of driver (facing pamel)

Know guns, know peace, know safety. No guns, no peace, no safety.

The ultimate result of shielding men from the effects of folly is to fill the world with fools. --Herbert Spencer , English Philosopher (1820-1903)

Texas Avenger Driver Series

My LED Test series - XP-L2 V5  - Nichia 219C 90+ CRILatticebright "XM-L"XHP35 & PWM efficiency - XHP50 - XP-L V5 - XM-L2 U2 - XP-G3 S5  - XP-L HI V2 - Oslon Square & direct comparison to Djozz tests - Nichia 319A - Nichia 219B 9080 CRI - Nichia 219C D320 - Nichia 229AT - XHP70.2 P2 - XHP50.2 J4 - Samsung IH351D

Complete collection of all my LED test Charts

DavidEF
DavidEF's picture
Online
Last seen: 3 min 29 sec ago
Joined: 06/05/2014 - 06:00
Posts: 5177
Location: Salisbury, North Carolina, USA

Sweet! I’m looking forward to seeing what TK comes up with now for firmware!

Reason is not automatic. Those who deny it cannot be conquered by it. Do not count on them. Leave them alone.
-Ayn Rand

ToyKeeper
ToyKeeper's picture
Offline
Last seen: 8 hours 44 min ago
Joined: 01/12/2013 - 14:40
Posts: 6504
Location: (469219) 2016 HO3

I just realized the tiny25/45/85 might not be able to do full 4-channel PWM. Wikipedia’s summary lists it as “2 × 2 (sharing 3 pins)” with a note attached: “4 pins are usable, but only 3 unique generators can be attached. The 4th pin would be the inverse of OC1B on the 3rd pin.”

So, if I understand correctly, it can independently operate channels 1+2+3 or 1+2+4, but not anything involving 3+4 unless I don’t mind them being inverted from each other. When one is on, the other is off. I think.

With a RGBA emitter, maybe I could set it up so that opposite colors (blue+amber) are on channels 3 and 4, since they should theoretically never be on at the same time. And then flip a MCU flag somewhere to tell it which one is active at any given time, on the assumption that I never want both active simultaneously.

Or perhaps only use 3 channels; a pretty full rainbow can still be made from just R+G+B.

Or maybe do RGBW in a way where it is either in a color mode or white, but not both.

Or maybe RGBA with red+amber on the shared pins, and a restriction that they must always have inverse power levels.

Or maybe try to use a tiny84, which has more channels (and more pins in general).

Or maybe do soft PWM on channels 3+4 (or just 4?), but that’s going to look a bit weird (especially on an e-switch driver) because the pulse lengths won’t be as fast or as consistent. Probably would work best on RGBW with soft PWM on white, which would make irregularities merely look like a shimmer.

I think I’ll probably have to test some ideas before I figure out what works. The simplest is to do only RGB.

DavidEF
DavidEF's picture
Online
Last seen: 3 min 29 sec ago
Joined: 06/05/2014 - 06:00
Posts: 5177
Location: Salisbury, North Carolina, USA

Not capable of four independent PWM channels?

Oh… Tired

Well, I’m sure you’ll figure something out. I’d be for using the blue and amber on 3 and 4 and never having them activated together. But, of course, it’s your light saber! Big Smile

Reason is not automatic. Those who deny it cannot be conquered by it. Do not count on them. Leave them alone.
-Ayn Rand

LightRider
LightRider's picture
Offline
Last seen: 3 weeks 3 days ago
Joined: 08/05/2015 - 09:52
Posts: 2007
Location: U.P. MI, USA

Wait… Dr Jones driver definitely uses pwm on all four colors. I can infinitely adjust each color separately including white and yet blend them all to be on at the same time. So there must be a way, no?

DavidEF
DavidEF's picture
Online
Last seen: 3 min 29 sec ago
Joined: 06/05/2014 - 06:00
Posts: 5177
Location: Salisbury, North Carolina, USA

LightRider wrote:
Wait… Dr Jones driver definitely uses pwm on all four colors. I can infinitely adjust each color separately including white and yet blend them all to be on at the same time. So there must be a way, no?

Yeah, he uses a different MCU.

Reason is not automatic. Those who deny it cannot be conquered by it. Do not count on them. Leave them alone.
-Ayn Rand

LightRider
LightRider's picture
Offline
Last seen: 3 weeks 3 days ago
Joined: 08/05/2015 - 09:52
Posts: 2007
Location: U.P. MI, USA
DavidEF wrote:
LightRider wrote:
Wait… Dr Jones driver definitely uses pwm on all four colors. I can infinitely adjust each color separately including white and yet blend them all to be on at the same time. So there must be a way, no?
Yeah, he uses a different MCU.

He uses the atiny85.

DavidEF
DavidEF's picture
Online
Last seen: 3 min 29 sec ago
Joined: 06/05/2014 - 06:00
Posts: 5177
Location: Salisbury, North Carolina, USA
LightRider wrote:
DavidEF wrote:
LightRider wrote:
Wait… Dr Jones driver definitely uses pwm on all four colors. I can infinitely adjust each color separately including white and yet blend them all to be on at the same time. So there must be a way, no?
Yeah, he uses a different MCU.

He uses the atiny85.


Sorry, I got my RGBW’s crossed… I was thinking of tterev3 for some reason, even though you said DR Jones. Facepalm

Edit: The Dr Jones RGBW driver uses software PWM.

Reason is not automatic. Those who deny it cannot be conquered by it. Do not count on them. Leave them alone.
-Ayn Rand

LightRider
LightRider's picture
Offline
Last seen: 3 weeks 3 days ago
Joined: 08/05/2015 - 09:52
Posts: 2007
Location: U.P. MI, USA

Ok. I wasn’t aware of the difference. Is software pwm an option for us?

DavidEF
DavidEF's picture
Online
Last seen: 3 min 29 sec ago
Joined: 06/05/2014 - 06:00
Posts: 5177
Location: Salisbury, North Carolina, USA

LightRider wrote:
Ok. I wasn’t aware of the difference. Is software pwm an option for us?

Yes, if somebody writes the code. That would be TK in this case. Wink

Reason is not automatic. Those who deny it cannot be conquered by it. Do not count on them. Leave them alone.
-Ayn Rand

LightRider
LightRider's picture
Offline
Last seen: 3 weeks 3 days ago
Joined: 08/05/2015 - 09:52
Posts: 2007
Location: U.P. MI, USA

Ahh… So, TK..? Wink

DavidEF
DavidEF's picture
Online
Last seen: 3 min 29 sec ago
Joined: 06/05/2014 - 06:00
Posts: 5177
Location: Salisbury, North Carolina, USA

Or, maybe you could talk DR Jones into tweaking his firmware for this new board. He may do that for a decent price. But, an open source solution would allow us to carry it forward and make changes and tweaks to our liking and to fit future boards, etc.

Reason is not automatic. Those who deny it cannot be conquered by it. Do not count on them. Leave them alone.
-Ayn Rand

LightRider
LightRider's picture
Offline
Last seen: 3 weeks 3 days ago
Joined: 08/05/2015 - 09:52
Posts: 2007
Location: U.P. MI, USA
DavidEF wrote:
Or, maybe you could talk DR Jones into tweaking his firmware for this new board. He may do that for a decent price. But, an open source solution would allow us to carry it forward and make changes and tweaks to our liking and to fit future boards, etc.

I’ve been trying to get ahold of him but he hasn’t been around the forum for a while and I don’t have any other contact info for him. I would be willing to pay for a completed rgbw firmware that I could use on self built drivers. Idk if he would’ve good with that though?

DavidEF
DavidEF's picture
Online
Last seen: 3 min 29 sec ago
Joined: 06/05/2014 - 06:00
Posts: 5177
Location: Salisbury, North Carolina, USA

His driver firmwares are proprietary code. But, he used to sell pre-flashed MCUs. I would think he’d be willing to make a tweaked version for a new board if there was enough demand. How different is your board from his self-designed RGBW driver? If it’s basically the same, or not much different, his code may already work for you. It would be nice if you could get in touch with him. But, like I said above, it would be even better if someone came up with an open source RGBW firmware that was as nice as DR Jones or tterev3 code.

Reason is not automatic. Those who deny it cannot be conquered by it. Do not count on them. Leave them alone.
-Ayn Rand

DavidEF
DavidEF's picture
Online
Last seen: 3 min 29 sec ago
Joined: 06/05/2014 - 06:00
Posts: 5177
Location: Salisbury, North Carolina, USA

Reason is not automatic. Those who deny it cannot be conquered by it. Do not count on them. Leave them alone.
-Ayn Rand

LightRider
LightRider's picture
Offline
Last seen: 3 weeks 3 days ago
Joined: 08/05/2015 - 09:52
Posts: 2007
Location: U.P. MI, USA

Ya. For me, his existing firmware is great! If I had the choice I would change the mode to clicks configuration, but if I could get an MCU from him as, I would be thrilled. I have one of his drivers left and I plan on removing the programmed MCU to place on my own driver layout. I’m working on a multilayered driver to power 4 XML rgbw led’s as well as one xpg3 high cri led. It will control three banks of 7135s and one fet channel for the white. I’m going to parallel the xpg3 along with the four white dies. When testing, I can get 4amps to the xpg3 and 1amp to each white die when in parallel.

At first, I was planning on using an escutcheon modified BLF d80, but room was very very tight. I found a way to pack it in with a custom driver design, but I finally surrendered to the pk26 as a host. After receiving the pk26, I’m still not certain it will work out. Even if it never works out, it’s been fun thinking about a 40w rgbw pocketable flashlight. (40w for about 40sec that is Smile )

TK, if you want his driver board minus the MCU for testing, let me know.
(Edit: that is, dr jones driver board I am referring to.)

LightRider
LightRider's picture
Offline
Last seen: 3 weeks 3 days ago
Joined: 08/05/2015 - 09:52
Posts: 2007
Location: U.P. MI, USA

I found this instructable for software PWM. I’m not near versed enough to know what this entails, but the instructable gives a little hope for another good four individually addressable PWM controlled driver.

http://www.instructables.com/id/Software-PWM-with-arduinoattiny/

DavidEF
DavidEF's picture
Online
Last seen: 3 min 29 sec ago
Joined: 06/05/2014 - 06:00
Posts: 5177
Location: Salisbury, North Carolina, USA

Wow! Thanks LightRider!

Reason is not automatic. Those who deny it cannot be conquered by it. Do not count on them. Leave them alone.
-Ayn Rand

DavidEF
DavidEF's picture
Online
Last seen: 3 min 29 sec ago
Joined: 06/05/2014 - 06:00
Posts: 5177
Location: Salisbury, North Carolina, USA

I would love to see some action here. TK? …Have you got anything to report? …TK? …Anybody?

Reason is not automatic. Those who deny it cannot be conquered by it. Do not count on them. Leave them alone.
-Ayn Rand

ToyKeeper
ToyKeeper's picture
Offline
Last seen: 8 hours 44 min ago
Joined: 01/12/2013 - 14:40
Posts: 6504
Location: (469219) 2016 HO3

I got kind of ninja’d by other parts of life for a few months.

Also, um, I don’t actually have any driver boards to print and build. Texas_Ace, do you have these up on oshpark? I didn’t see the saber driver listed there.

Texas_Ace
Texas_Ace's picture
Offline
Last seen: 1 hour 36 min ago
Joined: 03/24/2016 - 07:44
Posts: 4775
Location: Everything is brighter in Texas

I never heard back from you on weather it was ok and what you wanted so I didn’t bother uploading it in case it needed changes. lol

If you think it will work for you I can upload it.

Know guns, know peace, know safety. No guns, no peace, no safety.

The ultimate result of shielding men from the effects of folly is to fill the world with fools. --Herbert Spencer , English Philosopher (1820-1903)

Texas Avenger Driver Series

My LED Test series - XP-L2 V5  - Nichia 219C 90+ CRILatticebright "XM-L"XHP35 & PWM efficiency - XHP50 - XP-L V5 - XM-L2 U2 - XP-G3 S5  - XP-L HI V2 - Oslon Square & direct comparison to Djozz tests - Nichia 319A - Nichia 219B 9080 CRI - Nichia 219C D320 - Nichia 229AT - XHP70.2 P2 - XHP50.2 J4 - Samsung IH351D

Complete collection of all my LED test Charts

ToyKeeper
ToyKeeper's picture
Offline
Last seen: 8 hours 44 min ago
Joined: 01/12/2013 - 14:40
Posts: 6504
Location: (469219) 2016 HO3

I think it’ll probably work. If you’re willing though, it’d also be nice to have one with a pin and pad for an e-switch instead of the voltage divider. I think I can probably use the inverted VCC method for measuring voltage, and an e-switch likely makes the interface easier to navigate.

Or I could maybe use the 4th PWM pin for an e-switch, and use only three colors. I’m not sure how much value there is in adding a 4th color channel, and it should be fairly easy to stick the switch wire into one of the vias on pin 2 or 3.

LightRider
LightRider's picture
Offline
Last seen: 3 weeks 3 days ago
Joined: 08/05/2015 - 09:52
Posts: 2007
Location: U.P. MI, USA

ToyKeeper wrote:
I think it’ll probably work. If you’re willing though, it’d also be nice to have one with a pin and pad for an e-switch instead of the voltage divider. I think I can probably use the inverted VCC method for measuring voltage, and an e-switch likely makes the interface easier to navigate.

Or I could maybe use the 4th PWM pin for an e-switch, and use only three colors. I’m not sure how much value there is in adding a 4th color channel, and it should be fairly easy to stick the switch wire into one of the vias on pin 2 or 3.

Is there no use for adding a little bit of white to the mix? You can get some bright tinted colors this way. But I must say, I have no idea how a saber is yo be used:)

Texas_Ace
Texas_Ace's picture
Offline
Last seen: 1 hour 36 min ago
Joined: 03/24/2016 - 07:44
Posts: 4775
Location: Everything is brighter in Texas

ToyKeeper wrote:
I think it’ll probably work. If you’re willing though, it’d also be nice to have one with a pin and pad for an e-switch instead of the voltage divider. I think I can probably use the inverted VCC method for measuring voltage, and an e-switch likely makes the interface easier to navigate.

Or I could maybe use the 4th PWM pin for an e-switch, and use only three colors. I’m not sure how much value there is in adding a 4th color channel, and it should be fairly easy to stick the switch wire into one of the vias on pin 2 or 3.

The idea was that you could use the R1/R2 pads to setup the e-switch if you didn’t need the voltage divider. I went ahead and added a dedicated set of pads for the switch though.

Here is the oshpark link: https://oshpark.com/shared_projects/X5RZNYA2

Know guns, know peace, know safety. No guns, no peace, no safety.

The ultimate result of shielding men from the effects of folly is to fill the world with fools. --Herbert Spencer , English Philosopher (1820-1903)

Texas Avenger Driver Series

My LED Test series - XP-L2 V5  - Nichia 219C 90+ CRILatticebright "XM-L"XHP35 & PWM efficiency - XHP50 - XP-L V5 - XM-L2 U2 - XP-G3 S5  - XP-L HI V2 - Oslon Square & direct comparison to Djozz tests - Nichia 319A - Nichia 219B 9080 CRI - Nichia 219C D320 - Nichia 229AT - XHP70.2 P2 - XHP50.2 J4 - Samsung IH351D

Complete collection of all my LED test Charts

tterev3
Offline
Last seen: 2 weeks 5 hours ago
Joined: 03/28/2014 - 11:22
Posts: 232
Location: NC, USA

Software PWM can work very well as long as you have a timer available (that is not used for anything else) and don't frequently need to suspend interrupts (such as to write to NVM) which will glitch the PWM output. It's fairly easy to implement - here's pseudocode that you would put in an 8-bit timer's overflow interrupt routine. The only overhead you'll need is a variable for the output (duty cycle) you want and a flag bit that toggles to keep track of the phase of the output.

if timer overflowed to zero (interrupt):

toggle the flag

if the flag is set:

turn on the output pin

load the timer with a value of (255 - duty cycle)

else if the flag is clear:

turn off the output pin

load the timer with the value of duty cycle

 

The way this works is by scheduling the following interrupt to occur in a variable amount of time. By loading the timer with a low value, it will take longer before overflowing past 255 again and the subsequent interrupt will scheduled further out, and vice versa for a high value. So as an example, say the timer runs at 1MHz and the duty cycle is set to 200 (of 255). The interrupt will fire and turn on the output, then load 55 into the timer. The timer then has to count up 200 cycles before firing the interrupt again, when the output is turned off. Then 200 is loaded into the timer, so it only has to count up 55 cycles before firing again and starting the process over. This gives a high output for 200us and a low output for 55us, so we get a ~4kHz PWM signal at ~78% duty.

This can be extended slightly to give better performance at the extremes (0 and 100%) by adding a test for 0 and 100% duty before setting the output:

if timer overflowed to zero (interrupt):

toggle the flag

if the flag is set:

if duty cycle is not zero, turn on the output pin

load the timer with a value of (255 - duty cycle)

else if the flag is clear:

if duty cycle is not 255, turn off the output pin

load the timer with the value of duty cycle

 

My AVR skills are rusty but I think a simple implementation would look like this, assuming you set up timer0 as an 8-bit timer with an overflow interrupt:

bool flag;

uint duty;

ISR(TIM0_OVF_vect)

{

flag=~flag;

if(flag){

output_pin=1;

TCNT0=(255-duty);

}

else{

output_pin=0;

TCNT0=duty;

}

}

DavidEF
DavidEF's picture
Online
Last seen: 3 min 29 sec ago
Joined: 06/05/2014 - 06:00
Posts: 5177
Location: Salisbury, North Carolina, USA

Thanks tterev3! Thumbs Up

Reason is not automatic. Those who deny it cannot be conquered by it. Do not count on them. Leave them alone.
-Ayn Rand

DavidEF
DavidEF's picture
Online
Last seen: 3 min 29 sec ago
Joined: 06/05/2014 - 06:00
Posts: 5177
Location: Salisbury, North Carolina, USA

TA, is this board optimized to (also) work with the new 4-channel firmware from Flintrock? I know you made this for TK, but we might like to use it in a regular flashlight too. Innocent

I’d love to see some of our firmware people comment a bit about what tterev3 said, too (ToyKeeper, Tom, Flintrock).

Reason is not automatic. Those who deny it cannot be conquered by it. Do not count on them. Leave them alone.
-Ayn Rand

Texas_Ace
Texas_Ace's picture
Offline
Last seen: 1 hour 36 min ago
Joined: 03/24/2016 - 07:44
Posts: 4775
Location: Everything is brighter in Texas

It should work fine with the right firmware although it is not really setup for a flashlight and is pretty large.

All 4 of the output pins can be PWM so it is just a matter of getting firmware that takes advantage of it.

Know guns, know peace, know safety. No guns, no peace, no safety.

The ultimate result of shielding men from the effects of folly is to fill the world with fools. --Herbert Spencer , English Philosopher (1820-1903)

Texas Avenger Driver Series

My LED Test series - XP-L2 V5  - Nichia 219C 90+ CRILatticebright "XM-L"XHP35 & PWM efficiency - XHP50 - XP-L V5 - XM-L2 U2 - XP-G3 S5  - XP-L HI V2 - Oslon Square & direct comparison to Djozz tests - Nichia 319A - Nichia 219B 9080 CRI - Nichia 219C D320 - Nichia 229AT - XHP70.2 P2 - XHP50.2 J4 - Samsung IH351D

Complete collection of all my LED test Charts

Pages