software PWM

Status
Not open for further replies.
Lets try BIT BANGING/ MULTIPLEXING
have been able to separate the blue and white CONST arrays
seems to look alright.
wanting to avoid overloading pins
Code:
{
*****************************************************************************
*  Name    : UNTITLED.BAS                                                   *
*  Author  : [select VIEW...EDITOR OPTIONS]                                 *
*  Notice  : Copyright (c) 2022 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 5/18/2022                                                      *
*  Version : 1.0                                                            *
*  Notes   : 70 total LEDs blue and white                                                               *
*          : thoughts to improve = add a counter and increase then decrease
           : the delay just for one color  = white?                                                               *
*****************************************************************************
}
'Program: CHRISTMAS STAR:

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

Const BLUE_A(8)As Byte=  (%00000000,
                          %00000000,
                          %00100000,  //
                          %00010000,    //
                          %00000000,
                          %00000000,
                          %00000000,
                          %00000000)                                       
                          
Const BLUE_B(8)As Byte=  (%10000000,  //
                          %01000000,    //
                          %00100000,  //
                          %00010000,    //
                          %00001000,  //
                          %00000000,
                          %00000000,
                          %00000000)
                          
                          
Const BLUE_C(8) As Byte=  (%10000000, //
                           %01000000,   //
                           %00100000, //
                           %00000000,
                           %00001000,     //
                           %00000100,   //
                           %00000010, //
                           %00000000)                             
                          
Const BLUE_D(8) As Byte=  (%10000000,
                           %01000000,//
                           %00100000,  //
                           %00010000,    //
                           %00000000,
                           %00000000,
                           %00000000,
                           %00000001) //
                                                                                                            

                        
                                                            
Const WHITE_A(8)As Byte=(%10000000,
                          %01000000,
                          %00000000,
                          %00000000,
                          %00001000,
                          %00000100,
                          %00000010,
                          %00000001)                                       
                          
Const WHITE_B(8)As Byte= (%00000000,
                          %00000000,
                          %0000000,
                          %00000000,
                          %00000000,
                          %00000100,
                          %00000010,
                          %00000001)
                          
                          
Const WHITE_C(8) As Byte= (%00000000,
                           %00000000,
                           %00000000,
                           %00010000,
                           %00000000,
                           %00000000,
                           %00000000,
                           %00000001)                             
                          
Const WHITE_D(8) As Byte= (%10000000,
                           %00000000,
                           %00000000,
                           %00000000,
                           %00001000,
                           %00000100,
                           %00000010,
                           %00000000)
                                                    
CONST BLUE_E(3)AS BYTE =  (%00000100,
                           %00000010,
                           %00000001)                           
                          
                          
//-------------------------------------
// start of main program
//-------------------------------------
main:
// init hdw
               // 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   
'Counter = 100
'Counter_up = 150
'Counter_down = 150
setalldigital
While true
for x = 0 to 2

    PORTE = BLUE_E(x)// colums 8 (L-R the X is the scan
    DelayUS(500)
Next
FOR Z = 0 TO 7
    PORTA = WHITE_A(Z)
    DELAYUS(500)
     PORTE=%000
    PORTA=%00000000
    PORTB = WHITE_B(Z)
    DELAYUS(500)
    PORTB=%00000000 
    PORTC = WHITE_C(Z)
    DELAYUS(500)
    PORTC=%00000000
    PORTD = WHITE_D(Z)
    DELAYUS(500)
    PORTD=%00000000 
  NEXT 
 Wend
 
I'm not sure what you're going for, and maybe you know this. For the nested for/next loops, they may not be doing what you expect. For each value of m, the z loop executes 3 times. A little reformatting should make this clearer. So the Port E LEDs change 3 times before the rest of the ports change once. Or, stated another way, the Port E LEDs are running 3x faster than the others.

Code:
For m = 0 To 7
    for z = 0 to 2

        '***********************************************
        'This part executes 3 times
        PORTE.bits(z)=1       // only 3 pair of LEDS
        DelayuS(counter)
        PORTE.bits(z)=0
        DelayuS(counter)

       '***********************************************
        'Before this part changes once
        porta.bits(m)=1
        portb.bits(m)=1
        portc.bits(m)=1
        portd.bits(m)=1
        delayus(counter_up)
        porta.bits(m)=0
        portb.bits(m)=0
        portc.bits(m)=0
        portd.bits(m)=0
        delayus(counter-10)  'this value never changes
    next
next
 
I am trying short snippets of code to get the blue LEDs to appear ON while the white LEDs are going from LOW to HIGH brightness.
I need to multi-task somehow? changing the delay time on the LEDs.
Looking for suggestions from members that know how to multitask. There was a tutorial on the old DIGITAL DIY SITE BUT??
 
want to turn the blue leds full brightness with long delay and the white leds with varying brightness by alternating the delayus(xxx)
 
You've gone through this before. A DELAY statement stops EVERYTHING. Stops all program execution.

So you set up a loop. Turn on the blue LEDs. Start a counter. Do what you will with the white LEDs. Increment the counter at the end of the loop.

When you've gone through the loop enough times (i.e., counter > some value), turn off the blue LEDs.
 
The problem is all the LEDs to remain on but at different delay time.
I solved the CONST issue of only 3 bytes instead of 8 like all the other LEDs. Will call it BIT MANIULAPTION? THIS SOLVED THE ISSUE OF PORTE BEING EITHER TOO BRIGHT OR TO DIM COMPARED TO THE OTHER LEDs OUPS! CAPS LOCK ON.
Code:
CONST BLUE_E(8)AS BYTE =  (%00000100,
                                                %00000010,
                                                 %00000001,
                                                 %00000000,
                                                  %00000000,
                                                  %00000100,
                                                   %00000010,
                                                   %00000001)
 
Woe, what a conundrum. If only there was some way to solve this dilemma!

Hmmm. How to do it? Such a challenge.

Perhaps one group of LEDs could toggle at one value of the loop counter, another group at a different value, the third group at a third value, the fourth group at a fourth value, the fifth group at yet another value, the sixth at...... well, you either get it or you don't.

And the number of loops could be set using a random number generator......
 
I increased the number of bytes for the blue leds to 16 and it made the blue seem brighter
left the white at 8 bytes
seems to have a better affect on the brightness of the leds vers just changing the delayus(xxx)
 
The 8 bits of a byte each turn one group of LEDs on or off. %00000001 turns on the first group. %00000011 turns on the first and second.

So I don't have a clue what you ARE doing or what you are INTENDING to do. A port is 8 bits wide. Writing 16 bits to it certainly doesn't make it more powerful.

I don't know what kind of spaghetti you're making, but it ain't gonna stick.

 
I think he's just creating additional sequences in a const table (ie array) for the blue leds, and now he's up to 16 entries in the table. They repeat values, so it's getting brighter.

No matter what advice you give, there's never going to be anything more complicated than a big set of "PORT = X; DELAY" statements.
 
Drat. I misinterpreted bytes as bits.

You are absolutely correct of course. Any effort to explain the "how" of something is wasted bits.
 
Laying in bed this morning I got the idea to go back to 8 bytes in all the CONST arrays then insert a port "blank" arrays so the entire port is turned off DELAYUS(5) say every 5 seconds. Basically same as increasing or decreasing the amount of time the LEDs ( blue or white) using a delay but not (hopefully affecting the other color). Will try this this morning. The 16 byte seems to work ok. But all it is doing is adding more delay I think??
 
I gave up about 10 years ago, figured it would change but it didn't.

Mike.
BTW, it's a train wreck thing.
 
Just write random numbers to each port at random untervals for each port. A couple dozen lines of code should do it
 
stepping back and working on a timer interrupt to change between the different subroutes.
experimenting with the ISR timer module.
want about 5 seconds between subs
 
the timer is not working as hoped. suggestion for a long ISR timer? like 5 seconds between sequences(sub routes) I have been trying to figure out what each value does but just adding more spaghetti to the pot.
Code:
{
*****************************************************************************
*  Name    : UNTITLED.BAS                                                   *
*  Author  : [select VIEW...EDITOR OPTIONS]                                 *
*  Notice  : Copyright (c) 2022 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 5/20/2022                                                      *
*  Version : 1.0                                                            *
*  Notes   :                                                                *
*          :                                                                *
*****************************************************************************
}
{
*****************************************************************************
*  Name    : UNTITLED.BAS                                                   *
*  Author  : [select VIEW...EDITOR OPTIONS]                                 *
*  Notice  : Copyright (c) 2022 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 5/18/2022                                                      *
*  Version : 1.0                                                            *
*  Notes   : 70 total LEDs blue and white                                                               *
*          : thoughts to improve = add a counter and increase then decrease
           : the delay just for one color  = white?
           added ist 5-20-22                                                              *
*****************************************************************************
}
'Program: CHRISTMAS STAR:

Device = 18F43K22
Clock = 8

// int osc and IO pin libraries
Include "intosc.bas"
#option DIGITALIO_INIT = true       // automatically call setalldigital
Include "setdigitalio.bas"
Include "ISRTimer.bas"
// hardware
'Dim INDEX  As LongWord
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 SUB_COUNT As LongWord
Dim led1 As PORTD.1
Dim led2 As PORTD.7
Dim led3 As PORTA.1
Dim index As LongWord

Const Timer1 = 0,
      Timer2 = 1
      
      
Const BLUE_A(8)As Byte=  (%00000000,
                          %00000000,
                          %00100000,  //
                          %00010000,    //
                          %00000000,
                          %00000000,
                          %00000000,
                          %00000000)                                       
                          
Const BLUE_B(8)As Byte=  (%10000000,  //
                          %01000000,    //
                          %00100000,  //
                          %00010000,    //
                          %00001000,  //
                          %00000000,
                          %00000000,
                          %00000000)
                          
                          
Const BLUE_C(8) As Byte=  (%10000000, //
                           %01000000,   //
                           %00100000, //
                           %00000000,
                           %00001000,     //
                           %00000100,   //
                           %00000010, //
                           %00000000)                             
                          
Const BLUE_D(8) As Byte=  (%10000000,
                           %01000000,//
                           %00100000,  //
                           %00010000,    //
                           %00000000,
                           %00000000,
                           %00000000,
                           %00000001) //
                                                                                                            

                        
                                                            
Const WHITE_A(8)As Byte=(%10000000,
                          %01000000,
                          %00000000,
                          %00000000,
                          %00001000,
                          %00000100,
                          %00000010,
                          %00000001)                                       
                          
Const WHITE_B(8)As Byte= (%00000000,
                          %00000000,
                          %0000000,
                          %00000000,
                          %00000000,
                          %00000100,
                          %00000010,
                          %00000001)
                          
                          
Const WHITE_C(8) As Byte= (%00000000,
                           %00000000,
                           %00000000,
                           %00010000,
                           %00000000,
                           %00000000,
                           %00000000,
                           %00000001)                             
                          
Const WHITE_D(8) As Byte= (%10000000,
                           %00000000,
                           %00000000,
                           %00000000,
                           %00001000,
                           %00000100,
                           %00000010,
                           %00000000)
                                                    
Const BLUE_E(8)As Byte =  (%00000100,
                           %00000010,
                           %00000001,
                           %00000000,
                           %00000000,
                           %00000100,
                           %00000010,
                           %00000001)
                          
                          
            
                          
                          
//-------------------------------------
//PATTERN SUBROUTES
Sub FLICKER_1()
index = 60000
'REPEAT

 
For x = 0 To 7
For z = 0 To 7
  PORTA = WHITE_A(x)
    DelayUS(10)
  PORTA = BLUE_A(z)
    DelayUS(50)
  PORTB = WHITE_B(x)
    DelayUS(10)
  PORTB = BLUE_B(z)
    DelayUS(50)
  PORTC = WHITE_C(x)
    DelayUS(10)
  PORTC = BLUE_C(z)
    DelayUS(50)
  PORTD = WHITE_D(x)
    DelayUS(10)
  PORTD = BLUE_D(z)
    DelayUS(50)   
  PORTE = BLUE_E(z)// colums 8 (L-R the X is the scan
    DelayUS(50)
Next   
Next
  
    
End Sub



Sub FLICKER_2()

For x = 0 To 2
    PORTE = BLUE_E(x)// colums 8 (L-R the X is the scan
    DelayUS(10)
    
For z = 0 To 7
   PORTA = WHITE_A(z)
    DelayUS(Counter_down)
    PORTB = WHITE_B(z)
   DelayUS(Counter_down)
    PORTC = WHITE_C(z)
    DelayUS(Counter_down)
    PORTD = WHITE_D(z)
    DelayUS(Counter_down)
    
    PORTA = BLUE_A(z)
     DelayUS(25)
    PORTB = BLUE_B(z)
    DelayUS(25)
    PORTC = BLUE_C(z)
     DelayUS(25)
    PORTD = BLUE_D(z)
     DelayUS(25)
    DelayMS(Counter_down - 1 )
  
    Next
    
      Next
 
   Counter_down = 50
    If index <= 10 Then Exit
  End If
 End Sub
 
// TIMER SUBROUTINE
Sub OnTimer()

   inc (counter)
  
End Sub



                        
//-------------------------------------
// start of main program
//-------------------------------------
main:
// init hdw
               // 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   
Counter = 100
Counter_up = 1
Counter_down = 10
index = 1
led1 = 0
led2 = 0
//SET UP TIMER
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

// initialise the timer module...

Timer.Initialize(2)

// initialise each timer - refresh is every 1000Hz (1ms)...

Timer.Items(Timer1).Interval = 1      // 1ms

Timer.Items(Timer1).OnTimer = @OnTimer   // timer event handler

Timer.Items(Timer2).Interval = 5000      // 5000ms, no event handler

 

// enable the timers...

Timer.Items(Timer1).Enabled = true

Timer.Items(Timer2).Enabled = true

 

// start processing all timers...

Timer.Start

 

// main program loop...

While true
If Not Timer.Items(Timer2).Enabled Then

      flicker_2()     // enable a sub routine here

      Timer.Items(Timer2).Enabled = true
      EndIf
    If  Timer.Items(Timer2).Enabled and counter >1000
    Then
    flicker_1()          // change sub route
    end if

      Timer.Items(Timer2).Enabled = true

 
      

   // background flash LED...  not needed

   High(led3)

   DelayMS(500)

   Low(led3)

   DelayMS(500)

Wend
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…