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

MrDEB

Well-Known Member
still won't compile It hangs on
Sub Fade(bright As Word, dimmer As Word, stop As Word, A As Byte, B As Byte, C As Byte, D As Byte)
Dim dir As Boolean
Code:
{
*****************************************************************************
*  Name    : UNTITLED.BAS                                                   *
*  Author  : [select VIEW...EDITOR OPTIONS]                                 *
*  Notice  : Copyright (c) 2022 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 6/1/2022                                                       *
*  Version : 1.0                                                            *
*  Notes   :                                                                *
*          :                                                                *
*****************************************************************************
}
Device = 18F43K22
Clock = 8

// int osc and IO pin libraries
Include "intosc.bas"
#option DIGITALIO_INIT = true       // automatically call setalldigital
Include "setdigitalio.bas"

// hardware
Dim INDEX  As LongWord  //4294967295
Dim Counter As Word
Dim Counter_up As Word
Dim Counter_down As Word
Dim m As Byte
Dim w As Byte
Dim x As Byte
Dim y As Byte
Dim z As Byte
Dim Bright_1 As LongWord        // a wider range of settings FOR A SMOOTHER TRANSITION
Dim Dimmer_1 As LongWord
Dim Bright_2 As LongWord        // a wider range of settings
Dim Dimmer_2 As LongWord
main:
// init hdw
     // bright is initial ON  time
// dimmer is initial OFF time
// stop is end value (loop until bright = stop)
// A, B, C, and D are port values
Sub Fade(bright As Word, dimmer As Word, stop As Word, A As Byte, B As Byte, C As Byte, D As Byte)
    Dim dir As Boolean
 
    // figure out direction and account for 'repeat-until' end
    If (bright < dimmer) Then
        dir = true                // increase ON time
        stop = stop + 1
    Else
        dir = false                // increase OFF time
        stop = stop - 1
    EndIf
 
    Repeat
        PORTA = A            // ON time
        PORTB = B
        PORTC = C
        PORTD = D
        DelayUS(bright)
      
        PORTA = 0            // OFF time
        PORTB = 0
        PORTC = 0
        PORTD = 0
        DelayUS(dimmer)
 
        If (dir) Then
            bright = bright + 1        // increase ON time
            dimmer = dimmer - 1        // decrease OFF time
        Else
            bright = bright - 1        // decrease ON time
            dimmer = dimmer + 1        // increase OFF time
        EndIf
    Until (bright = stop)
End Sub
// LED port - all outputs 2 LEDs per port in parallel 330 ohm resistor
TRISA = 0
TRISB = 0
TRISC = 0
TRISD = 0
TRISE = 0
   PORTE=%000
   PORTD=%00000000
   PORTC=%00000000
   PORTA=%00000000
   PORTB=%00000000   
     // bright is initial ON  time
// dimmer is initial OFF time
// stop is end value (loop until bright = stop)
// A, B, C, and D are port values

main:
While (true)
    // fade brightness up
    Fade(1, 5000, 3000, %11001111, %00000111, %00010001, %10001110)

    // fade brightness down
    Fade(5000, 1, 1, %11001111, %00000111, %00010001, %10001110)
End While
 

tumbleweed

Active Member
Here... this compiles and is complete.
I added fade outputs for PORTE.0-PORTE.2

PORTE.3 (MCLR) is input-only, so the chip can't handle anymore outputs.

Code:
device = 18F43K22
clock = 8

// int osc and IO pin libraries
include "intosc.bas"
#option DIGITALIO_INIT = true       // automatically call setalldigital
include "setdigitalio.bas"


// bright is initial ON  time
// dimmer is initial OFF time
// stop is end value (loop until bright = stop)
// A, B, C, D and E are port values
sub Fade(bright as word, dimmer as word, stop as word, A as byte, B as byte, C as byte, D as byte, E as byte)
    dim dir as boolean
  
    // figure out direction and account for 'repeat-until' end
    if (bright < dimmer) then
        dir = true                // increase ON time
        stop = stop + 1
    else
        dir = false                // increase OFF time
        stop = stop - 1
    endif
  
    repeat
        PORTA = A            // ON time
        PORTB = B
        PORTC = C
        PORTD = D
        PORTE = E and %111  // only three bits (RE0-RE2)
        delayus(bright)
      
        PORTA = 0            // OFF time
        PORTB = 0
        PORTC = 0
        PORTD = 0
        PORTE = 0
        delayus(dimmer)
  
        if (dir) then
            bright = bright + 1        // increase ON time
            dimmer = dimmer - 1        // decrease OFF time
        else
            bright = bright - 1        // decrease ON time
            dimmer = dimmer + 1        // increase OFF time
        endif
    until (bright = stop)
end sub

main:
// init hdw
// LED port - all outputs 2 LEDs per port in parallel 330 ohm resistor
// 18 blue leds, 16 white leds = 34 total
TRISA = 0
TRISB = 0
TRISC = 0
TRISD = 0
TRISE = 0       // RE3 is input-only

PORTA = 0
PORTB = 0
PORTC = 0
PORTD = 0
PORTE = 0

while (true)
    // fade brightness up
    Fade(1, 5000, 3000, %11001111, %00000111, %00010001, %10001110, %101)

    // fade brightness down
    Fade(5000, 1, 1, %11001111, %00000111, %00010001, %10001110, %101)
end while
 

MrDEB

Well-Known Member
I printed out both and compare them. find out where I made a mistake
post #152 works great.
I need to get 2 blue leds off but all the others are white
a really nice fade.
 

For The Popcorn

Active Member
May I ask a silly question? If you're going to fade all the blue LEDs as a group, why connect them to individual port pins? Use a MOSFET, driven from a PWM pin to run as many fricken' LEDs as you want, each with its own current-limiting resistor.
 

For The Popcorn

Active Member
I suggested the same thing 7 pages ago (post #43).

Mike.

I'm old – do you think my memory is that good?!?

Based on the design of the board, I wrongly assumed that MrDEB had something more interesting in mind than treating one of the colors as a monolithic group.
 

MrDEB

Well-Known Member
hit me over the head, went back and looked at post#43 and yes I could have, sholud have BUT then I am limited in the number of patterns.
I grabbed my DMM from the garage (my new one would not read current, looked for a fuse but directions say where but meter and directions are not the same).
Anyway, using battery power = 4.5volts
white = 61ma
blue = 60ma
these leds were assembled by JLCPBC and are package 603
1/2 volt shouldn't change much. Using a wall wart adapter (cellphone charger) and the Christmas star as Christmas gifts this year. Star is 290mm tall x 185mm wide
 

tumbleweed

Active Member
That extra 1/2 volt will probably only increase it by about 10%, so you're probably looking at less than 150mA total.

I'd leave it the way it is.
 

MrDEB

Well-Known Member
I just realized that MAYBE JUST MAYBE I grabbed a star I assembled using the 560 ohm resistor arrays and not the 320 ohm arrays.
I assembled 8 of the stars but only 2 have the 560 ohm resistor arrays. (that's all Digikey had was 25) it takes 9 arrays per star.
Going to make new measurements today
 

For The Popcorn

Active Member
35 pairs of LEDs draw 121mA total. Vf ~3v.

If R = 560, current per group ~ 3.6mA × 35 groups ~ 126mA.

Yep, the board you tested has 560 ohm resistors.


So.......

With 330 ohm resistors, current per group will be ~ 6.1mA × 35 groups ~ 214mA total current draw with all LEDs on.
 

MrDEB

Well-Known Member
in process of routing a new 50% smaller star and curious bing these LEDs are 603 pkg. what is the rule? or proper method to change the trace from one side at the led pads.. ALL the LEDs are on top layer and inserting a via on the led pad?
 

For The Popcorn

Active Member
NO VIAS IN PADS.

Why don't you get this version working before laying out a new board? You might learn what works with this design and what doesn't.
 

MrDEB

Well-Known Member
I have the 10 boards I had made (been waiting about 4 months for correct resistor arrays)
These boards are acceptable but have a few changes like a smaller statr and distributing the leds better.
they look specular just adjusting the led patterns. Tumbleweeds suggestion in post #162 works great BUT I want to add a few lines of code to include dimming etc of the blue leds. That's on tap for tomorrow.
 

For The Popcorn

Active Member
Point. Missed. Completely.
 

MrDEB

Well-Known Member
didn't miss the point. The boards work but like most anything can be improved.
Have a new board basically done but it looks like spaggiti. Board is too small.
 

For The Popcorn

Active Member
Flashy!
 

Latest threads

New Articles From Microcontroller Tips

Top