{
*****************************************************************************
* 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
Dim LETTERING_1 As PORTB.3,
LETTERING_2 As PORTB.4,
LETTERING_3 As PORTB.5
// ADC channels
Const
CH_POT1 = 12, // AN12
CH_POT2 = 10, // AN10
CH_POT3 = 8 // AN8
// PWM outputs
Dim //non diming mosfets FOR GLASS, LETTERING AND ICE CUBES
// THE RED STRAW AND ICE CUBES TO HAVE DIMMING
PWM1 As PORTA.5, //GLASS ----- DIMMING
PWM2 As PORTA.6, //ICE CUBES -- DIMMING
PWM3 As PORTA.7 //RED STRAW -- DIMMING
// 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)
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
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//GREEN STRIPS
PORTA.0 = 1
PORTA.1 = 1
PORTA.2 = 1
PORTA.3 = 1
PORTA.4 = 1
//XXXXXXXXXXXXXXXXXXXXXXXX
//LETTERING
PORTB.3 = 1
PORTB.4 = 1
PORTB.5 = 1
//SIGM LETTERS
Output(LETTERING_1)
Output(LETTERING_2)
Output(LETTERING_3)
Output(Green_strip_0) // DRINK LEVEL
Output(Green_strip_1)
Output(Green_strip_2)
Output(Green_strip_3)
Output(Green_strip_4)
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//STRAW SIPPING
Output(LED0)
Output(LED1)
Output(LED2)
Output(LED3)
Output(LED4)
//Output(LED5)
//Output(PORTC.6)
// STRAW LEDS
LED0 = 0 //ALL OFF
LED2 = 0
LED3 = 0
LED4 = 0
// 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)
//XXXXXXXXXXXXXXXXXXXXX
//SET ALL PORTS OFF
Green_strip_0 = 0
Green_strip_1 = 0
Green_strip_2 = 0
Green_strip_3 = 0
Green_strip_4 = 0
LETTERING_1 = 0
LETTERING_2 = 0
LETTERING_3 = 0
While (true)
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
// Green_strip_0 = 1 //for testing only porta.0
//DelayMS(3000)
//Green_strip_0 = 0
PORTC = %11111111
Green_strip_0 = 1
Green_strip_1 = 1
Green_strip_2 = 1
Green_strip_3 = 1
Green_strip_4 = 1
LETTERING_1 = 1
LETTERING_2 = 1
LETTERING_3 = 1
End While
If (pwm_period >= pwm3_duty) Then
PWM3 = 1
Else
PWM3 = 0
EndIf
End Interrupt
{
*****************************************************************************
* Name : UNTITLED.BAS *
* Author : [select VIEW...EDITOR OPTIONS] *
* Notice : Copyright (c) 2024 [select VIEW...EDITOR OPTIONS] *
* : All Rights Reserved *
* Date : 3/15/2024 *
* Version : 1.0 *
* Notes : *
* : *
*****************************************************************************
}
{
*****************************************************************************
* 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 : SOFTWARE PWM *
* : *
*****************************************************************************
}
Device = 18F2221
Clock = 16
// int osc and IO pin libraries
Include "intosc.bas"
#option DIGITALIO_INIT = true // automatically call setalldigital
Include "setdigitalio.bas"
Include "convert.bas"
Dim LETTERING_1 As PORTB.3,
LETTERING_2 As PORTB.4,
LETTERING_3 As PORTB.5
Dim
Glass As PORTA.5, //GLASS ----- DIMMING
ICE As PORTA.6, //ICE CUBES -- DIMMING
RED_STRAW As PORTA.7 //RED STRAW -- 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 X As Byte
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//GREEN STRIPS
PORTA.0 = 0
PORTA.1 = 0
PORTA.2 = 0
PORTA.3 = 0
PORTA.4 = 0
//LETTERING
PORTB.3 = 0
PORTB.4 = 0
PORTB.5 = 0
//SIGM LETTERS
// STRAW LEDS
LED0 = 0 //ALL OFF
LED2 = 0
LED3 = 0
LED4 = 0
//XXXXXXXXXXXXXXXXXXXXX
PORTB.0 = 0
PORTB.1 = 0
PORTB.2 = 0
//SET ALL PORTS OFF
Green_strip_0 = 0
Green_strip_1 = 0
Green_strip_2 = 0
Green_strip_3 = 0
Green_strip_4 = 0
PORTC = %00000000 // all on for testing
LETTERING_1 = 0
LETTERING_2 = 0
LETTERING_3 = 0
//XXXXXXXXXXXXXXXXXXXXXXXXXXX
//SET PORTS AS OUTPUTS
Output(LETTERING_1)
Output(LETTERING_2)
Output(LETTERING_3)
Output(PORTB.0)
Output(PORTB.1)
Output(PORTB.2)
Output(PORTB.3)
Output(PORTB.4)
Output(PORTB.5)
Output(Green_strip_0) // DRINK LEVEL
Output(Green_strip_1)
Output(Green_strip_2)
Output(Green_strip_3)
Output(Green_strip_4)
//STRAW SIPPING
Output(LED0)
Output(LED1)
Output(LED2)
Output(LED3)
Output(LED4)
// ICE
Output(Glass)
Output(ICE)
Output(RED_STRAW)
//XXXXXXXXXXXXXXXXXXXXXXXXXXXX
SetAllDigital()
While (true)
Glass = 1
ICE = 1
RED_STRAW = 1
PORTC = %11111111
Green_strip_0 = 1
Green_strip_1 = 1
Green_strip_2 = 1
Green_strip_3 = 1
Green_strip_4 = 1
LETTERING_1 = 1
LETTERING_2 = 1
LETTERING_3 = 1
End While
the code in post#146 works pretty good but will run post#148 and move/ re-lable some things, plus add 3 more mosfets if needed but not sure.
// 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
// setup pwm timer TMR2
// 40KHz = 25us/bit -> 25us x 256 = 6400us period, 6.4ms period (156Hz)
T2CON = %00000001 // T2OUTPS<3:0>=%000 (1:1), TMR2ON=0, T2CKPS<1:0>=%01 (1:4)
PR2 = 207
That normally indicates a lack of acquisition time for the ADC.Adjusting one pot affects the other two channels.
Exactly, he's either feeding the inputs from too high an impedance, or he's not allowing enough (any?) time for the sample and hold capacitor to charge/discharge - or both.That normally indicates a lack of acquisition time for the ADC.
Mike.
Or they're wired wrong.Exactly, he's either feeding the inputs from too high an impedance, or he's not allowing enough (any?) time for the sample and hold capacitor to charge/discharge - or both.
Looking at the code (with no idea if it's anything like what he might be using?) he doesn't seem to have any delays at all when switching inputs?.Or they're wired wrong.
Mike.
Edit, I've also seen this when the pin is set as an output.
You don't need a separate circuit, just debug the one you've got.Switching inputs is why I am considering a separate circuit using a 555 to generate the PWM then use the pic to control the 5v LEDs on portc and the porta outputs for controlling the dimming of the glass, ice and red straw only.
Plan to assemble the 555 circuit on a separate perfboard
The ADC.Read() function has a delay built-in after the channel is selected,...he doesn't seem to have any delays at all when switching inputs?
That doesn't sound right, but if that's the PWM freq you're getting then no wonder it's "flashing".I used a frequency meter on my DMM and the readings on the outputs of porta.5,6,7 turn off and on but getting a frequency of 35-43hz.
The 'clock=' statement is in MHz.what does clock=32 mean?
Good question...If it's not acquisition time then what could it be?
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?