Continue to Site

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

Status
Not open for further replies.
I think you're on your own from here.....

Ain't nobody crazy enough to try and unravel this crap.
 
I have to agree so am thinking I have lots of room on the pic so why not just put the sequences in the code from top to bottom and a short delay between sequences. No need for a timer or interrupt etc.
Thanks for your honest opinion.
 
I wanted to try a suggested loop again (had a failed attempt but lets try again)
Code:
While TRUE
Index = 1
Repeat
   Index = Index +1
   led2 = 1
    DelayMS(500)
  
   led2 = 0
   DelayMS(500)
 
 Until  Index >= 10
 DelayMS(5000)        //indicates the change over from timed out
 
 Index = 1
 Repeat
   Index = Index +1 
   led1 = 1
    DelayMS(500)
  
   led1 = 0
   DelayMS(500)

 Until  Index >= 5//4294967200
 DelayMS(5000)
  
  Wend
Found where my coding was at fault (nothing new)
here is the SIMPLE LOOP that works and tried seeing if BYTE instead of LONGWORD worked the same.
 
here is the SIMPLE LOOP that works and tried seeing if BYTE instead of LONGWORD worked the same.
The only difference is how many times the repeat-until loop can run (the 'until index >=' statement).
A byte (8-bits), word (16-bits), and longword (32-bits) all have different maximum values.

*sigh*
 
Yes I made several testing sequences using BYTE vers LONGWORD
After experimenting with it, I found a happy medium and smooth sequence transition.
I inserted a long delay after turning off all the ports so I can visualize when the sequence changes.
I need to get my phone and make a video to share with friends n family.
I actually got a dimming effect.
don't *sigh* be happy as this may be the end of this thread?
 
//4294967200 is a comment, not a math statement. 4294967200 = 2^32. It's anyone's guess what MrDEB thinks it means.
 
I did give him the solution for smooth fading etc. back in posts 2 & 5 of this thread, which I though he originally had working.
 
yes the post # 86 is a comment. The 4294967200 is the maximum that longword will accept (I looked it up in the help file)
will reread post 2 & 5
 
4294967200 is almost 2^32. A longword will accept up to 2^32 - 1, or 4294967295.
If you increment a count past that, it'll wrap around to 0, so watch out what you compare it to.

I think it's a new programming construct... "repeat... until (almost forever)"
 
4294967200 is almost 2^32. A longword will accept up to 2^32 - 1, or 4294967295.
Sorry, blurry vision while waking up way too early

Helping MrDEB with his code is like sitting under a pineapple tree on Easter Island on the 5th Sunday of February eating broiled chocolate bunny during a cyclone while debating economic theory with the moai with Amelia Earhart in a coconut bra serving Mai Tais based on Victor J. Bergeron's* recipe. Drink lots – it's going to get worse.

*Better known as Trader Vic.
 
I reread post #2 & 5 and after adding the nessary code I ran it then it dawned on me that it looks like it is maybe using the PWM on the chip and not just software plus I now have working boards with all 70 leds on them to use as testing. Thanks for the suggestion but it dosen't seem to dim. Just sits there nice and bright.

Code:
{
*****************************************************************************
*  Name    : UNTITLED.BAS                                                   *
*  Author  : [select VIEW...EDITOR OPTIONS]                                 *
*  Notice  : Copyright (c) 2022 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 5/21/2022                                                      *
*  Version : 1.0                                                            *
*  Notes   :  copy n pasted from
Report
rjenkinsgb                                                              *
*          :                                                                *
*****************************************************************************
}
Device = 18F43K22
Clock = 8

// int osc and IO pin libraries
Include "intosc.bas"
#option DIGITALIO_INIT = true       // automatically call setalldigital
Include "setdigitalio.bas"
Include "convert.bas"
DIM LED1 AS PORTA.0
DIM LED2 AS PORTB.0
DIM LED3 AS PORTC.0
DIM LED4 AS PORTD.0
DIM Time1 AS BYTE
DIM Time2 AS BYTE
DIM Time3 AS BYTE
DIM Time4 AS BYTE
dim counter as byte
led1 = 0
led2 = 0
led3 = 0
led4 = 0

Time1 = 10
Time2 = 25
Time3 = 50
Time4 = 75

cOUNTER = 0
trisa=0
trisb=0
trisc=0
trisd=0

While TRUE

   // LED One control
   if (Time1 > counter) then
   led1=1
   else
   led1=0
   endif

   // LED Two control
   if (Time2 > counter) then
   led2=1
   else
   led2=0
   endif
 
   // LED Three control
   if (Time3 > counter) then
   led3=1
   else
   led3=0
   endif

 // Other LEDs here.
 

// Increment PWM cycle counter  every loop
   cOUNTER = (cOUNTER + 1)

// Check for overflow
   If cOUNTER > 100 Then
   cOUNTER = 0

// Add the fades etc. here.

   End If
 
 Wend
 
MrDEB,

This is a simple loop. Can you think your way through it? Step by step, from Counter = 1 to counter = 100. Is each LED on or off at each step from 1 to 100? SHOW YOUR WORK. What happens if you set Time1 = 0? Time1 = 100?

And for the love of God, please make cOUNTER either counter or Counter. It's like fingernails on a chalk board!

Code:
Time1 = 10
Time2 = 25
Time3 = 50
Time4 = 75

Counter = 0

While TRUE

   // LED One control
   if (Time1 > Counter) then
           led1=1
       else
           led1=0
   end if

   // LED Two control
   if (Time2 > Counter) then
           led2=1
       else
          led2=0
   end if
 
   // LED Three control
   if (Time3 > Counter) then
           led3=1
       else
           led3=0
   end if

   // LED Four control
   if (Time4 > Counter) then
           led4=1
       else
           led4=0
   end if
 

// Increment PWM cycle counter  every loop
   Counter = Counter + 1

// Check for overflow
   If Counter > 100 Then
       Counter = 0
   End If
 
 Wend
 
And the rest? Explain what the code is doing. It's simple.
 
It's software PWM.

The max period is counter = 100.
There's a "timer" for each of the 4 LEDS in the example...
Code:
Time1 = 10
Time2 = 25
Time3 = 50
Time4 = 75

These set the ON time of the LED as 'counter' counts from 0 to 100.
For example, since 'Time2 = 25' LED2 is ON for 25% of the time (25 out of 100 counts).

There are no delays anywhere so the loop runs as fast as it will.
The brightness won't change unless you change Time1, Time2, etc. They control the ON time.
You'll have to add Time5, Time6, etc and code for each of your LEDS.

rjenkinsgb may not fully understand how this game works. I bet he thought you'd take the code, figure out how it works, and modify it to suit your needs.
 
I was hoping MrDEB would take the trouble to figure that out.

Note that he said all the LEDs are the same brightness, which is impossible.
 
I was hoping MrDEB would take the trouble to figure that out.
You funny guy! After seeing "it dawned on me that it looks like it is maybe using the PWM on the chip" I figured we'd be waiting a loooong time for that to happen.

Note that he said all the LEDs are the same brightness, which is impossible.
I would think you'd be able to see it, but maybe the refresh is so fast it's hard to tell the difference?
 
It's true the refresh rate will be very high, but I don't think that should matter. Visually, an LED off 90% of the time should look quite different than one that's on 75% of the time, no?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top