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.

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

software PWM

For The Popcorn

Active Member
Guess you ignored the description of what's happening.

Imagine that.
 

For The Popcorn

Active Member
Ok, so what happens here? Led1On starts at 0. When the Counter value equals 0, three things happen:
¤ LED1 is turned on
¤ LED1On is set to 20
¤ LED1Off is set to 10

When the Counter = 10, LED1 will be turned off.

When the Counter = 20, the sequence repeats.

LED1 is on half the time, off half the time, so the brightness is 50%. This is kind of crude; making it a subroutine (do not call this a sub route. That is gibberish.) would be cleaner.

I have explained the process so YOU can write your own code to have multiple LEDs operating independently at the same time. A number of people have explained this to you in this and many other threads. Sorry, I don't have any crayons so I can't explain it in simpler terms. Instead of blindly pasting code you don't have any clue about, read the explanations and study the code until you understand it. Read the entire post – I showed the process in little pieces so you could understand it.
 

For The Popcorn

Active Member
Because part of my quote is hidden and requires a click to see, here is the entire thing in one view. I don't want you to overlook the explanation AGAIN.

This LED is at 50% brightness, or is dimmed 50%. You have confused the word DIM and word FADE. Dim is a level, fade is an action.

SmartSelect_20220525-042057_Edge.jpg
 

For The Popcorn

Active Member
Thinking about this in the shower (yes, I have given this way too much thought I know), there's another approach to this, still using the counter approach.

Use a limited number of steps for the counter, say 250 or maybe 1000. Treat each step as a line on a player piano, read 64 bits from an array (probably 2 arrays) at each step and light the LEDs accordingly.

Yes, I'm being scarce with words. I'm done explaining things in great detail, trying to help those who refuse to do anything beyond pasting snippets together. I'm saying this for myself and if you have a modicum of coding knowledge, you'll understand it.

Setting up 250 or a thousand lines would be a bit tedious and not for those of short attention span. It would be pretty straight forward using a spreadsheet. With a little work, the wrap-around would be seamless.
 

MrDEB

Well-Known Member
added some LED process indicators
yes the code increments COUNTER but your dealing with POV so the LED appears on constantly
I might be wrong but to get any fade or dimming the LED needs to go off
I tried different variables and still no change of LED brightness
Did you even run your code? Post #108 works perfectly fade in and out.
Code:
While 1 = 1

   // some code will go here
   If Counter = LED1On Then         //first pass led1 = 0
        LED1 = 1                    // led1 is on
        LED1On = Counter + 20
        LED1Off = Counter + 10      //first pas led1 = 10
   End If
   //xxxxxxxxxxxxxxxxxxxxxxxx
   // led testing indicators
   if counter = 20 then
   led2=1                          // process indicator
   end if
   if counter = 10
   then led3 = 1
   end if
    delayms(1000)         //slow it down to see what is happening
    //xxxxxxxxxxxxxxxxxxxxxxxxxx
 
   If Counter = LED1Off Then
       LED1 = 0
   End If

    Inc (Counter)
    If Counter > 4000 Then  //   4000000000
          Counter = 0
          led3 = 0          // process indicator
    End If

Wend
 

For The Popcorn

Active Member
Trust me. You are indeed wrong.

First – I wasn't writing your d****ed code. I was writing code to show you how to do something you seem unable to grasp. Once you understand the principle, the rest is simple.

Second – The title of this thread is PWM. Do you understand what Pulse Width Modulation is? Please describe the difference between PWM and my example code for LED1? Go ahead, I'm dying to hear.

And by the way, your idea about POV is wrong. Totally wrong. If you compare the brightness of my LED1 example to a full-on LED you'll see the difference. Or not. Don't care.
 

For The Popcorn

Active Member
And you know what? If you believe POV is the problem, increase the time to slow it down. I've explained exactly what's happening so pull your head out and prove it.
 

For The Popcorn

Active Member
My apologies. There were two oversights in post #114.

¤ I failed to mention the need to initialize the LEDOn variables. Nothing happens if you fail to do this.

¤ I failed to consider the nonlinear response of the eye to light. It's approximately logarithmic, so changing the brightness from 100% to 50% is virtually undetectable to the eye.

I juggled the numbers to show some noticeable differences. This video shows the results. The right-most LED is at full brightness. The next LED to the left is at 10% brightness. The third LED is all the way down to about 1%. Finally, the left-most LED is "twinkling" – on for a brief moment then off for an extended period. This is running with a 20MHz clock, with the being 2^32 counts (yes, I was going to cut it off at 1,000,000 but it's late and I forgot). If you've ever tried to take photos of LEDs, you know what a pain in the butt it is. Still, the video demonstrates the method of post #116.
Well poop. I can't upload videos here. You'll have to trust me for now until I figure out how to do so. Here's a picture to show the PWM dimming the LEDs.

SmartSelect_20220526-005245_Gallery.jpg


A key point to be made – this is with the microcontroller running flat out. If the LED is on 10% of the time, it will be dimmer than an LED on 100% of the time, no matter the frequency of the PWM

The pictures below should the same code, with only a delay added to the loop to slow the speed. At the slowest speed, the most-dimmed LED was visibly flashing, yet the brightness of the LEDs of the LEDs did not change.

The simple code is shown here. It is EXAMPLE CODE to ILLUSTRATE THE METHOD. Sorry MrDEB, no more copy and pasting my code – it's posted to teach you something, not do your project.

loop counter code.jpg


The thumbnails below are in order of decreased speed/increased loop delays.

SmartSelect_20220526-005323_Gallery.jpg

SmartSelect_20220526-005346_Gallery.jpg

SmartSelect_20220526-005407_Gallery.jpg
 

Attachments

  • SmartSelect_20220526-005407_Gallery.jpg
    SmartSelect_20220526-005407_Gallery.jpg
    70.3 KB · Views: 9

For The Popcorn

Active Member
The loop time at full speed is about 6uSec.

Therefore, the 10% PWM LED is running at 164 Hz.
 

For The Popcorn

Active Member
Please don't bother. You have missed the point, which is how to have your LEDs doing different things at the same time. My code was posted so you could understand the method. You can have fading, flashing, twinkling and anything else you want, if you take a little effort to understand the very simple basics.

I am really sorry that I went to the trouble to try and show you HOW this works; I really thought you could understand the steps as I laid them out. Instead of accomplishing anything, I wasted about 8 hours trying to make this simple and clear.

Use your own code. Make multiple LEDs do different things if you can. Doesn't matter to me what you do or how you do it. More power to you if you make yourethod work.
 

MrDEB

Well-Known Member
I understand what you are doing but IMO you made it more complex than it needs to be
have typed your code and going to run it later today
You made it more complex by adding ledoffcount etc.
 

tumbleweed

Active Member
The code in #108 can only fade one led at a time, so all the others just sit there idle.
The code in #111 can do multiple leds, but they all do the same thing.
Both methods are pretty boring/fake looking.

Dimming (ie PWM at a set level) and fading (changing PWM values) are two different things.
Coded properly, you can do both, and have all the leds doing something different.

For that to work you can't have more than 1 delay call (if any at all) in the whole loop.
If you sit in a loop fading LED1 (#108) then nothing else can happen.
 

For The Popcorn

Active Member
I understand what you are doing but IMO you made it more complex than it needs to be
have typed your code and going to run it later today
You made it more complex by adding ledoffcount etc.

Geez oh grief. Typing in my code isn't the answer. Understanding how to take action based on the counter is the key. If you refuse to understand this, you won't be able to make anything work.

As I said, if, in your humble opinion, I made it too complex, forget my posts and delete everything based on my code. Please.
 

For The Popcorn

Active Member
A brief explanation for those playing along at home....

You'll see in my code lines like

LED1 = LEDOn

LED1 = LEDOff

S1 = Pressed

With microcontrollers, LEDs typically have the anode (positive end) connected to V+, and the cathode (negative end) connected to a port pin. When that pin is high (i.e., = 1), the LED is off, and when that pin is low (i.e., = 0) the LED is on. This is because microcontroller port pins are often better at sinking current (providing a ground connection) than sourcing current (providing V+).

That's how I usually do it, so I code say

LED1 = 1 to turn the LED off and

LED1 = 0 to turn the LED on.

But some boards I use are wired the the LED anodes connected to port pins and the cathode to ground, so the code is

LED1 = 1 would turn the LED on and

LED1 = 0 would turn the LED off.

When I look at the code in the future, I might see

LED1 = 1 and think the LED is off because that's how I usually do things, and wonder why nothing makes sense because this board is wired the other way round.

To make life simpler, and remove the ambiguity, LEDOn and LEDOff are constants, depending on how the board is arranged. It's a little more typing, but it makes understanding the code so much clearer.


The same logic goes for switches, with typically have a pull-up resistor to V+, and short the point pin to ground (i.e., 0) when pressed. But not always.

Seeing "when S1 = Pressed" tells you instantly what's going on when you scan the code.

Code:
If PORTB.0 = 0 then
        PortB.7 = 1
    Else
       PortB.7 = 0
End If

//vs

If S1 = Pressed then
        LED1 = LEDOn
    Else
        LED1 = LEDOff
End If

Each of those gives the exact same result. Which is easier to understand? How about six months from now when you want to change the code? I'm lazy, but a little extra typing now makes things so much easier when troubleshooting or trying to understand code in the future.
 

MrDEB

Well-Known Member
the code in post#108 will fade in and out multiple leds. No they are not included in post#108. With some rearanging it will fade in and out multiple leds at different rates if desired
 

For The Popcorn

Active Member
the code in post#108 will fade in and out multiple leds. No they are not included in post#108. With some rearanging it will fade in and out multiple leds at different rates if desired

Awesome. Then you don't need to use my method, or my suggestions.
 

Latest threads

Top