The attached PDF is a file compare between the code in post #58 and the code in post #114. The only change I made was to turn everything into lower case. The gold highlights show changes. The green highlights show additions. Hmmm. Wonder where those additions came from? So weird.I didn't add anything to the first suggestion on code,
Post #58 has the 18f2221
I didn't even start on this code??
Are you here to help or here to be easily offended by a non-consequential comment?The attached PDF is a file compare between the code in post #58 and the code in post #114. The only change I made was to turn everything into lower case. The gold highlights show changes. The green highlights show additions. Hmmm. Wonder where those additions came from? So weird.
// PWM outputs (controlled by POT1 and POT2)
dim
PWM1 as PORTA.0, // controlled by POT1
PWM2 as PORTA.1, // controlled by POT2
PWM3 as PORTA.2, // controlled by POT1
PWM4 as PORTA.3, // controlled by POT2
PWM5 as PORTA.4, // controlled by POT1
PWM6 as PORTA.5, // controlled by POT2
PWM7 as PORTA.6, // controlled by POT1
PWM8 as PORTA.7 // controlled by POT2
// LED PWM outputs (all controlled by POT3)
dim
LED1 as PORTC.0,
LED2 as PORTC.1,
LED3 as PORTC.2,
LED4 as PORTC.3,
LED5 as PORTC.4,
LED6 as PORTC.5,
LED7 as PORTC.6,
LED8 as PORTC.7
// set outputs (low to start)
low(PWM1)
low(PWM2)
low(PWM3)
low(PWM4)
low(PWM5)
low(PWM6)
low(PWM7)
low(PWM8)
low(LED1)
low(LED2)
low(LED3)
low(LED4)
low(LED5)
low(LED6)
low(LED7)
low(LED8)
// outputs controlled by POT1
if (pwm_period >= pwm1_duty) then
PWM1 = 0
PWM3 = 0
PWM5 = 0
PWM7 = 0
else
PWM1 = 1
PWM3 = 1
PWM5 = 1
PWM7 = 1
endif
// outputs controlled by POT2
if (pwm_period >= pwm2_duty) then
PWM2 = 0
PWM4 = 0
PWM6 = 0
PWM8 = 0
else
PWM2 = 1
PWM4 = 1
PWM6 = 1
PWM8 = 1
endif
// outputs controlled by POT3
if (pwm_period >= pwm3_duty) then
LED1 = 0
LED2 = 0
LED3 = 0
LED4 = 0
LED5 = 0
LED6 = 0
LED7 = 0
LED8 = 0
else
LED1 = 1
LED2 = 1
LED3 = 1
LED4 = 1
LED5 = 1
LED6 = 1
LED7 = 1
LED8 = 1
endif
// Swordfish BASIC sixteen channel software PWM using TMR2 and 3 pot ADC inputs
device = 18F2221
clock = 32
include "intosc.bas"
// do NOT include setdigitalio
// to use PORTB for the ADC all pins must be in analog mode due to the way
// these old pics map ADC inputs
include "adc.bas"
// ADC pot inputs
// used to set the PWM duty cycle for PWM1-PWM8 and LED1-LED8
// connect pot upper lug = VDD, lower lug = GND, wiper = port IO
dim
POT1 as PORTB.0, // RB0/AN12
POT2 as PORTB.1, // RB1/AN10
POT3 as PORTB.2 // RB2/AN8
// ADC channels
const
CH_POT1 = 12, // AN12
CH_POT2 = 10, // AN10
CH_POT3 = 8 // AN8
// PWM outputs (controlled by POT1 and POT2)
dim
PWM1 as PORTA.0, // controlled by POT1
PWM2 as PORTA.1, // controlled by POT2
PWM3 as PORTA.2, // controlled by POT1
PWM4 as PORTA.3, // controlled by POT2
PWM5 as PORTA.4, // controlled by POT1
PWM6 as PORTA.5, // controlled by POT2
PWM7 as PORTA.6, // controlled by POT1
PWM8 as PORTA.7 // controlled by POT2
// LED PWM outputs (all controlled by POT3)
dim
LED1 as PORTC.0,
LED2 as PORTC.1,
LED3 as PORTC.2,
LED4 as PORTC.3,
LED5 as PORTC.4,
LED6 as PORTC.5,
LED7 as PORTC.6,
LED8 as PORTC.7
// pwm duty cycles (from ADC) 0=min, 255=max
dim
pwm1_duty as byte,
pwm2_duty as byte,
pwm3_duty as byte
dim pwm_period as byte
// pwm timer TMR2
dim
TMR2IF as PIR1.bits(1),
TMR2IE as PIE1.bits(1),
TMR2ON as T2CON.bits(2)
// set IO pin directions and initial settings
sub InitIO()
// set inputs
input(POT1)
input(POT2)
input(POT3)
// set outputs (low to start)
low(PWM1)
low(PWM2)
low(PWM3)
low(PWM4)
low(PWM5)
low(PWM6)
low(PWM7)
low(PWM8)
low(LED1)
low(LED2)
low(LED3)
low(LED4)
low(LED5)
low(LED6)
low(LED7)
low(LED8)
end sub
// pwm TMR2 interrupt
interrupt tmr2_isr()
TMR2IF = 0
pwm_period = pwm_period + 1
// outputs controlled by POT1
if (pwm_period >= pwm1_duty) then
PWM1 = 0
PWM3 = 0
PWM5 = 0
PWM7 = 0
else
PWM1 = 1
PWM3 = 1
PWM5 = 1
PWM7 = 1
endif
// outputs controlled by POT2
if (pwm_period >= pwm2_duty) then
PWM2 = 0
PWM4 = 0
PWM6 = 0
PWM8 = 0
else
PWM2 = 1
PWM4 = 1
PWM6 = 1
PWM8 = 1
endif
// outputs controlled by POT3
if (pwm_period >= pwm3_duty) then
LED1 = 0
LED2 = 0
LED3 = 0
LED4 = 0
LED5 = 0
LED6 = 0
LED7 = 0
LED8 = 0
else
LED1 = 1
LED2 = 1
LED3 = 1
LED4 = 1
LED5 = 1
LED6 = 1
LED7 = 1
LED8 = 1
endif
end interrupt
main:
InitIO()
// ADC setup
ADCON1 = $00 // all pins set to analog mode, VREF = VDD/GND
ADCON2 = ADC.FRC // ADC clock = Frc
ADC.ADFM = 0 // left justify (we only use the 8 MSB's)
ADC.SetAcqTime(100) // 100us delay
pwm_period = 0
pwm1_duty = 0
pwm2_duty = 0
pwm3_duty = 0
// setup pwm timer TMR2
// 25KHz = 40us/bit -> 40us x 256 = 10240us period, ~10ms period (100Hz)
T2CON = %00000001 // T2OUTPS<3:0>=%000 (1:1), TMR2ON=0, T2CKPS<1:0>=%01 (1:4)
PR2 = 176
TMR2 = 0
TMR2IF = 0
TMR2IE = 1
TMR2ON = 1
enable(tmr2_isr)
while (true)
pwm1_duty = ADC.Read(CH_POT1) >> 8
pwm2_duty = ADC.Read(CH_POT2) >> 8
pwm3_duty = ADC.Read(CH_POT3) >> 8
end while
I'd use three buttons and one 4-bit encoder.
If you want adaptive brightness, like that on cellphone screens, you should try a CdS cell and a 500K linear pot across the power supply, with the junction feeding the LEDs common feed. As for all the 555's on board, all could be eliminated by using the internal timer in the PIC.It was suggested to get a logic-level MOSFET
As for unused pins, I wanted some flexibility as to how bright the LED strip is without having to reprogram the pic to set the desired brightness of an LED strip.
I am going to do a more intense search for a better mosfet.
The 7555 smd chips etc are relatively cheap from Tayda Electronics and there are only 3 dimming circuits on the board.
Hopefully today I can attach the sign to the PC board.
The LED common is the voltage, and there's quite a bit of current there. How would that work?you should try a CdS cell and a 500K linear pot across the power supply, with the junction feeding the LEDs common feed.
{
*****************************************************************************
* Name : UNTITLED.BAS *
* Author : [select VIEW...EDITOR OPTIONS] *
* Notice : Copyright (c) 2024 [select VIEW...EDITOR OPTIONS] *
* : All Rights Reserved *
* Date : 1/5/2024 *
* Version : 1.0 *
* Notes : *
* : *
*****************************************************************************
}
// Swordfish BASIC three channel software PWM using TMR2 and ADC inputs
Device = 18F2221
Clock = 32
Include "intosc.bas"
// do NOT include setdigitalio
// to use PORTB for the ADC all pins must be in analog mode due to the way
// these old pics map ADC inputs
Include "adc.bas"
// ADC pot inputs
// used to set the PWM duty cycle for PWM1-PWM3
// connect pot upper lug = VDD, lower lug = GND, wiper = port IO
Dim
POT1 As PORTB.0, // RB0/AN12
POT2 As PORTB.1, // RB1/AN10
POT3 As PORTB.2 // RB2/AN8
// ADC channels
Const
CH_POT1 = 12, // AN12
CH_POT2 = 10, // AN10
CH_POT3 = 8 // AN8
// PWM outputs
Dim
PWM1 As PORTA.5,
PWM2 As PORTA.6,
PWM3 As PORTA.7
// pwm duty cycles (from ADC) 0=min, 255=max
Dim
pwm1_duty As Byte,
pwm2_duty As Byte,
pwm3_duty As Byte
Dim pwm_period As Byte
// pwm timer TMR2
Dim
TMR2IF As PIR1.bits(1),
TMR2IE As PIE1.bits(1),
TMR2ON As T2CON.bits(2)
//non diming mosfets FOR GLASS, LETTERING AND ICE CUBES
// THE RED STRAW AND ICE CUBES TO HAVE DIMMING
Dim Green_strip_0 As PORTA.0
Dim Green_strip_1 As PORTA.1
Dim Green_strip_2 As PORTA.2
Dim Green_strip_3 As PORTA.3
Dim Green_strip_4 As PORTA.4
//LEDS ON PORTC
Dim LED0 As PORTC.0 //USE FOR THE "GREEN STRAW INDICATORS"
Dim LED1 As PORTC.1
Dim LED2 As PORTC.2
Dim LED3 As PORTC.3
Dim LED4 As PORTC.4
Dim LED5 As PORTC.5
Dim LED6 As PORTC.6
Dim X As Byte
// set IO pin directions and initial settings
Sub InitIO()
// set inputs
Input(POT1)
Input(POT2)
Input(POT3)
// set outputs (low to start)
Low(PWM1)
Low(PWM2)
Low(PWM3)
End Sub
// pwm TMR2 interrupt
Interrupt tmr2_isr()
TMR2IF = 0
pwm_period = pwm_period + 1
If (pwm_period >= pwm1_duty) Then
PWM1 = 0
Else
PWM1 = 1
EndIf
If (pwm_period >= pwm2_duty) Then
PWM2 = 1
Else
PWM2 = 0
EndIf
If (pwm_period >= pwm3_duty) Then
PWM3 = 1
Else
PWM3 = 0
EndIf
End Interrupt
main:
InitIO()
PORTC = %11111111 // all on for testing
PORTA.0 = 1
PORTA.1 = 1
PORTA.2 = 1
PORTA.3 = 1
PORTA.4 = 1
PORTB.3 = 1
PORTB.4 = 1
PORTB.5 = 1
// ADC setup
ADCON1 = $00 // all pins set to analog mode, VREF = VDD/GND
ADCON2 = ADC.FRC // ADC clock = Frc
ADC.ADFM = 0 // left justify (we only use the 8 MSB's)
ADC.SetAcqTime(100) // 100us delay
pwm_period = 0
pwm1_duty = 0
pwm2_duty = 0
pwm3_duty = 0
// setup pwm timer TMR2
// 25KHz = 40us/bit -> 40us x 256 = 10240us period, ~10ms period (100Hz)
T2CON = %00000001 // T2OUTPS<3:0>=%000 (1:1), TMR2ON=0, T2CKPS<1:0>=%01 (1:4)
PR2 = 176
TMR2 = 0
TMR2IF = 0
TMR2IE = 1
TMR2ON = 1
Enable(tmr2_isr)
While (true)
For X = 0 To 7
pwm1_duty = ADC.Read(CH_POT1) >> 8 //porta.5 pot b.o
pwm2_duty = ADC.Read(CH_POT2) >> 8 //porta.6 pot b.1
pwm3_duty = ADC.Read(CH_POT3) >> 8 //porta.7 pot b.2
Next
Green_strip_0 = 1 //for testing only
DelayMS(3000)
Green_strip_0 = 0
End While
PORTC = %11111111 // all on for testing
PORTA.0 = 1
PORTA.1 = 1
PORTA.2 = 1
PORTA.3 = 1
PORTA.4 = 1
PORTB.3 = 1
PORTB.4 = 1
PORTB.5 = 1
// Swordfish BASIC sixteen channel software PWM using TMR2 and 3 pot ADC inputs
// schematic https://www.electro-tech-online.com/threads/using-pwm-to-dim-led-neon-strip.165067 post #112
// v2 - programmable outputs
device = 18F2221
clock = 32
include "intosc.bas"
// do NOT include setdigitalio
// to use PORTB for the ADC all pins must be in analog mode due to the way
// these old pics map ADC inputs
include "adc.bas"
// ADC pot inputs
// used to set the PWM duty cycle for PWM1-PWM8 and LED1-LED8
// connect pot upper lug = VDD, lower lug = GND, wiper = port IO
dim
POT1 as PORTB.0, // RB0/AN12
POT2 as PORTB.1, // RB1/AN10
POT3 as PORTB.2 // RB2/AN8
// ADC channels
const
CH_POT1 = 12, // AN12
CH_POT2 = 10, // AN10
CH_POT3 = 8 // AN8
// PWM outputs (controlled by POT1 and POT2)
dim
PWM1 as PORTA.0, // controlled by POT1
PWM2 as PORTA.1, // controlled by POT2
PWM3 as PORTA.2, // controlled by POT1
PWM4 as PORTA.3, // controlled by POT2
PWM5 as PORTA.4, // controlled by POT1
PWM6 as PORTA.5, // controlled by POT2
PWM7 as PORTA.6, // controlled by POT1
PWM8 as PORTA.7 // controlled by POT2
// LED PWM outputs (all controlled by POT3)
dim
LED1 as PORTC.0,
LED2 as PORTC.1,
LED3 as PORTC.2,
LED4 as PORTC.3,
LED5 as PORTC.4,
LED6 as PORTC.5,
LED7 as PORTC.6,
LED8 as PORTC.7
// this structure holds the on-off state of the pwm and led outputs
// to turn an output on, set it to 1 (ie 'vPORTS.vPWM2 = 1')
// to turn an output off, set it to 0 (ie 'vPORTS.vPWM2 = 0')
// dimming for all outputs is still controlled by the pots
structure vPORT_t
vPWM as byte
vLED as byte
vPWM1 as vPWM.bits(0) // PWM1-PWM8 settings
vPWM2 as vPWM.bits(1)
vPWM3 as vPWM.bits(2)
vPWM4 as vPWM.bits(3)
vPWM5 as vPWM.bits(4)
vPWM6 as vPWM.bits(5)
vPWM7 as vPWM.bits(6)
vPWM8 as vPWM.bits(7)
vLED1 as vLED.bits(0) // LED1-LED8 settings
vLED2 as vLED.bits(1)
vLED3 as vLED.bits(2)
vLED4 as vLED.bits(3)
vLED5 as vLED.bits(4)
vLED6 as vLED.bits(5)
vLED7 as vLED.bits(6)
vLED8 as vLED.bits(7)
end structure
dim vPORTS as vPORT_t
// pwm duty cycles (from ADC) 0=min, 255=max
dim
pwm1_duty as byte,
pwm2_duty as byte,
pwm3_duty as byte
dim pwm_period as byte
// pwm timer TMR2
dim
TMR2IF as PIR1.bits(1),
TMR2IE as PIE1.bits(1),
TMR2ON as T2CON.bits(2)
// set IO pin directions and initial settings
sub InitIO()
// set inputs
input(POT1)
input(POT2)
input(POT3)
// set outputs (low to start)
low(PWM1)
low(PWM2)
low(PWM3)
low(PWM4)
low(PWM5)
low(PWM6)
low(PWM7)
low(PWM8)
low(LED1)
low(LED2)
low(LED3)
low(LED4)
low(LED5)
low(LED6)
low(LED7)
low(LED8)
// set all the variable states to low (off)
clear(vPORTS)
end sub
// pwm TMR2 interrupt
interrupt tmr2_isr()
TMR2IF = 0
pwm_period = pwm_period + 1
// outputs controlled by POT1
if (pwm_period >= pwm1_duty) then // set outputs off
PWM1 = 0
PWM3 = 0
PWM5 = 0
PWM7 = 0
else // set outputs to current variable setting
PWM1 = vPORTS.vPWM1
PWM3 = vPORTS.vPWM3
PWM5 = vPORTS.vPWM5
PWM7 = vPORTS.vPWM7
endif
// outputs controlled by POT2
if (pwm_period >= pwm2_duty) then // set outputs off
PWM2 = 0
PWM4 = 0
PWM6 = 0
PWM8 = 0
else // set outputs to current variable setting
PWM2 = vPORTS.vPWM2
PWM4 = vPORTS.vPWM4
PWM6 = vPORTS.vPWM6
PWM8 = vPORTS.vPWM8
endif
// outputs controlled by POT3
if (pwm_period >= pwm3_duty) then // set outputs off
LED1 = 0
LED2 = 0
LED3 = 0
LED4 = 0
LED5 = 0
LED6 = 0
LED7 = 0
LED8 = 0
else // set outputs to current variable setting
LED1 = vPORTS.vLED1
LED2 = vPORTS.vLED2
LED3 = vPORTS.vLED3
LED4 = vPORTS.vLED4
LED5 = vPORTS.vLED5
LED6 = vPORTS.vLED6
LED7 = vPORTS.vLED7
LED8 = vPORTS.vLED8
endif
end interrupt
main:
InitIO()
// ADC setup
ADCON1 = $00 // all pins set to analog mode, VREF = VDD/GND
ADCON2 = ADC.FRC // ADC clock = Frc
ADC.ADFM = 0 // left justify (we only use the 8 MSB's)
ADC.SetAcqTime(100) // 100us delay
pwm_period = 0
pwm1_duty = 0
pwm2_duty = 0
pwm3_duty = 0
// setup pwm timer TMR2
// 25KHz = 40us/bit -> 40us x 256 = 10240us period, ~10ms period (100Hz)
T2CON = %00000001 // T2OUTPS<3:0>=%000 (1:1), TMR2ON=0, T2CKPS<1:0>=%01 (1:4)
PR2 = 176
TMR2 = 0
TMR2IF = 0
TMR2IE = 1
TMR2ON = 1
enable(tmr2_isr)
while (true)
// read pots and set pwm duty cycle
pwm1_duty = ADC.Read(CH_POT1) >> 8
pwm2_duty = ADC.Read(CH_POT2) >> 8
pwm3_duty = ADC.Read(CH_POT3) >> 8
// to control an LED/PWM output, set the appropriate vPORTS
// variable to 1 (on) or 0 (off)
vPORTS.vPWM1 = 1
vPORTS.vLED1 = 1
end while
// outputs controlled by POT1
if (pwm_period >= pwm1_duty) then // set outputs off
PWM1 = 0
'PWM3 = 0 // PWM3 output no longer dimmed
PWM5 = 0
PWM7 = 0
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?