6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sat Nov 09, 2024 5:50 pm

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: PWM
PostPosted: Mon Dec 11, 2017 1:22 am 
Offline

Joined: Thu Mar 10, 2016 4:33 am
Posts: 180
I'm thinking about a project where I need to pulse a LED, and want to use a 6502 as the microcontroller. Most modern microcontrollers have a PWM peripheral, but I don't think there are any PWM chips for the 6502. It looks like I could do this in software though. One way I came up with is to use a 65C22 and the timers to generate interrupts at the correct times to turn the LED on and off. So far this looks like the easiest way to go. Does anyone have any other ideas, or has tried this?


Top
 Profile  
Reply with quote  
 Post subject: Re: PWM
PostPosted: Mon Dec 11, 2017 1:57 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8538
Location: Southern California
There are a couple of approaches on the circuit potpourri page of the 6502 primer, under http://wilsonminesco.com/6502primer/potpourri.html#DAC .  If nine levels are enough, you can use the 6522's shift register directly.  Another way (not shown there) would be to make an R-2R ladder connected to a parallel port's output bits and follow it with an op amp.  PWM is usually just a way to get around not having a D/A converter; but D/A's, particularly in lowish resolutions, are easy to implement in analog circuitry (but not so much on a digital IC).  The last time I used PWMs, it was for audio, and I had to make analog filters to keep the PWM carrier out of the amplifiers' inputs so the amplifiers wouldn't waste a lot of battery power and get hot trying to drive the carrier into the speakers, two at 39kHz and one at 78kHz.  That said, PWM would nevertheless get around the nonlinearities of an LED.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
 Post subject: Re: PWM
PostPosted: Mon Dec 11, 2017 2:49 am 
Offline

Joined: Thu Mar 10, 2016 4:33 am
Posts: 180
Thanks Garth, that gives me some good starting points.


Top
 Profile  
Reply with quote  
 Post subject: Re: PWM
PostPosted: Mon Dec 11, 2017 11:10 am 
Offline

Joined: Tue Sep 03, 2002 12:58 pm
Posts: 334
You don't want a DAC for fading an LED. They're dependent on current, not voltage, and not very linear. Trying to get a variable current through one will just burn power in the analogue parts of your circuit. Everyone uses PWM for this. The LED is always either fully on, or fully off, and the ratio between them gives the brightness (well, that's not linear either, but it's a lot easier to deal with).

If the 6502 is doing nothing but driving the LED, that's a) a waste of a 6502, but b) a very easy task for it. You don't need timers, just a loop incrementing a value, and a compare to tell you when to turn the LED on.

If it's doing other things as well, then put your code on a regular timer interrupt. It's easiest to have the LED on for N interrupts then off for M-N. But with a bit more work you can distribute the 'on' cycles evenly throughout the whole, since that reduces flicker and lets you use a lower frequency interrupt. You want something like Bresenham's algorithm for that: it tells you when to step to get N steps more or less evenly distributed among M.


Top
 Profile  
Reply with quote  
 Post subject: Re: PWM
PostPosted: Mon Dec 11, 2017 2:41 pm 
Offline

Joined: Wed Jan 08, 2014 3:31 pm
Posts: 578
@John, if you built the classic resistor DAC out of eight 1K resistors, wouldn't that provide a variable current too? The resistance would vary all the way down to 125 ohms which would be bright, but a single pin would be 1K and dim. If you used different resistor values for each pin you could probably get a smoother transition between brightness levels as well.

As for varying the brightness of an LED. I agree that if you are only pulsing an LED than use a delay loop. But blinking an LED is often a stepping stone to more complex projects like generating servo or stepper motor control pulses. Since delay loops usually won't scale for complex projects it's probably worth learning more sophisticated techniques.


Top
 Profile  
Reply with quote  
 Post subject: Re: PWM
PostPosted: Mon Dec 11, 2017 4:36 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8538
Location: Southern California
Here are a couple of ways to handle it.  Yes, the LED is a current device, not voltage; but the op amp can just as easily put out a current as a voltage.  Here's the idea:

Attachment:
R2R_LEDdriver.gif
R2R_LEDdriver.gif [ 25.43 KiB | Viewed 1727 times ]

The resistors in the ladder need to be matched within about 1% for the six bits shown here.  I find that 5% carbon-film resistors are usually within 1% anyway, so if you have a bunch, just measure them with your DMM and you might find a set that are within half percent of a center value.  I kept it up at 10K and 20K because the pin drivers of the 65C22 aren't exactly zero ohms, but more like 50 ohms which is 0.5% of 10K.  You could go higher, like 15K and 30K, 18K and 36K, or 75K and 150K, all of which are standard 5% values.  1% values will give more choices.  The LM358's or LM324's maximum input bias current is 250nA (45nA typ), and 250nA times 75K is about 1/2 LSB here, so I wouldn't go any higher than that.

The top 2R resistor is tied to ground so the output of the ladder goes 0 to 2.5V*(63/64) which is 2.46V which will be reflected on the emitter resistor, leaving at least about two and a half volts for the LED at max brightness which is fine for red or yellow.  Green won't get quite all the way to full brightness, meaning approximately the last five codes (out of 64) won't result in any increase in brightness.  Blue will be worse.  IIRC, red is around 1.7V, yellow around 2.2V, green around 2.7V, and blue around 3.3V.  Raising the power supply voltage at the LED's anode is one way to do the job.  Another is to add another R-2R pair at the top of the ladder and ground that 2R as well, then cut the 220 ohm resistor down to 100 or 120 ohms.  As it is, with a red or yellow LED, you'll get a maximum current of about 11mA which is very bright with modern LEDs.  I never run them that high for indicators in our products. The days when you needed 20mA are gone.

Several things here are flexible, especially to the right of the ladder.  The LM358 is a common, inexpensive dual op amp available in an 8-pin DIP.  The LM324 is the quad version, in a 14-pin DIP.  You only need the one section here, and the other(s) is (or are) left for other purposes.


For a software-implemented PWM, one way is discussed in the 65(c)22 data sheet, where it's talking about using T1 in free-run mode and making it invert PB7 on each time-out, and generating an interrupt with every time-out.  What it does not clarify is that you need to write x1xxxxxx to the ACR before writing to the counters, and you need to write directly to the T1 counters to get T1 started, and then you can write to the latches after that.  I don't know if all brands are the same (I suspect they are), but we had quite a time of getting this going in the late 1980's until an applications engineer sent us some sample code.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
 Post subject: Re: PWM
PostPosted: Tue Dec 12, 2017 3:56 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8538
Location: Southern California
I should add something.  The LM358 or '324 can only pull up to about 1.25V from the power-supply rail (ie, 3.75V with 5V rail) even at this light load, which is just barely enough to get you to max brightness.  Power the op amp instead with the voltage you have before the 5V regulator, whether 7.5V, 9V, 12V, whatever.  It can handle over 30V.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


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 post attachments in this forum

Search for:
Jump to: