MrDEB
Well-Known Member
have checked and rechecked but still the 18f2221 won't reconoze the toggle switch is grounding the input'
Using SF basic.
curious will wire length to switch matter? wires are 16 inches long.
this code is just for testing the toggle switch.
Using SF basic.
curious will wire length to switch matter? wires are 16 inches long.
this code is just for testing the toggle switch.
Code:
{
*****************************************************************************
* Name : UNTITLED.BAS *
* Author : [select VIEW...EDITOR OPTIONS] *
* Notice : Copyright (c) 2023 [select VIEW...EDITOR OPTIONS] *
* : All Rights Reserved *
* Date : 2/22/2023 *
* Version : 1.0 *
* Notes : *
* : *
*****************************************************************************
}
{
*****************************************************************************
* Name : UNTITLED.BAS *
* Author : [select VIEW...EDITOR OPTIONS] *
* Notice : Copyright (c) 2023 [select VIEW...EDITOR OPTIONS] *
* : All Rights Reserved *
* Date : 1/21/2023 *
* Version : 1.0 *
* Notes : THIS ONE WORKS *
* : *
*****************************************************************************
}
Device = 18F2221
Clock = 8
Include "intosc.bas"
#option DIGITALIO_INIT = true
Include "setdigitalio.bas"
//12 VOLT LED STRIPS
Dim R As PORTA.0 // channel_1
Dim Y As PORTA.1 // channel_2
Dim D As PORTA.2 // channel_3
Dim E As PORTA.3 // channel_4
Dim R_1 As PORTA.4 // channel_5
Dim W As PORTA.5 // channel_6
Dim O As PORTA.7 // channel_8
Dim D_1 As PORTA.6 // channel_7
Dim Red_Candle_STRIPS As PORTB.0 // turn on mosfet Q10
Dim Flames_ENABLE As PORTB.1 // TOGGLE SWITCH PORT, input with external pullup
//CAKE SPRINKLES CATHODES CONNECTED TO PORT PINS
Dim Row_4 As PORTB.2
Dim Row_3 As PORTB.3
Dim Row_2 As PORTB.4
Dim Row_1 As PORTB.5
//CANDLE FLAMES
Dim Flame1 As PORTC.0
Dim Flame2 As PORTC.1
Dim Flame3 As PORTC.2
// SPRINKLES ANODES
Dim RED As PORTC.3
Dim WHITE As PORTC.4
Dim BLUE As PORTC.5
Dim GREEN As PORTC.6
//HEADLIGHT ON TRAIN ENGINE
Dim HEAD_LIGHT As PORTC.7 // HEAD LIGHT
Dim X As Byte
// isr timer module
// default is TMR1, 1ms refresh interval
Include "isrtimer.bas"
Const TIMER1 = 0
Const TIMER2 = 1
Dim led_step As Byte
Dim Sprinkles As Byte
// timer 1 event handler
// this routine will be called every timer1 Interval
Sub OnTimer1()
led_step = led_step + 1
Select (led_step)
Case 1: R = 1
Case 2: Y = 1
Case 3: D = 1
Case 4: E = 1
Case 5: R_1 = 1
Case 6: W = 1
Case 7: O = 1
Case 8: D_1 = 1
Case >12: // turn them all off
R = 0
Y = 0
D = 0
E = 0
R_1 = 0
W = 0
O = 0
D_1 = 0
// start over next time
led_step = 0
End Select
Red_Candle_STRIPS = 1
End Sub
Sub OnTimer2() //SECOND TIMER
Sprinkles = Sprinkles + 1
Select (Sprinkles)
Case 1: Row_4=1 Row_1=0 // turn off previous, turn on next
Case 2: Row_1=1 Row_2=0
Case 3: Row_2=1 Row_3=0
Case 4: Row_3=1 Row_4=0
Case 5: Row_4=1 Row_1=0
Case 6: Row_1=1 Row_2=0
Case 7: Row_2=1 Row_3=0
Case 8: Row_3=1 Row_4=0
Case 9: Row_4=1
Case >12:
Sprinkles = 0
End Select
End Sub
// set all IO pin directions and initial settings
Sub InitIO()
// before enabling the outputs, init all LAT registers low
// before enabling the outputs, init all LAT registers low
LATA = 0
LATB.0 = 0
LATB.1 = 1
LATB.2 = 0
LATB.3 = 0
LATB.4 = 0
LATB.5 = 0
LATB.6 = 0
LATB.7 = 0
LATC = 0
//ENABLE OUTPUTS port A, B, C
TRISA = 0
TRISB.0 = 0
TRISB.1 = 1 //input
TRISB.2 = 0
TRISB.3 = 0
TRISB.4 = 0
TRISB.5 = 0
TRISB.6 = 0
TRISB.7 = 0
TRISC = 0
RED = 1
WHITE = 1
BLUE = 1
GREEN = 1
Row_1 = 1
Row_2 = 1
Row_3 = 1
Row_4 = 1
// enable outputs, except for PORTB.1
' Input(Flames_ENABLE)
End Sub
main:
InitIO()
led_step = 0
Sprinkles = 0
{
// initialise the timer module for 2 timers...
Timer.Initialize(2)
// initialise each timer - refresh is every 1ms
Timer.Items(TIMER1).Interval = 1000 // 1000 x 1ms = 1 sec
Timer.Items(TIMER1).OnTimer = @OnTimer1 // timer event handler
Timer.Items(TIMER2).Interval = 10 // 1000 x 1ms = 1 sec
Timer.Items(TIMER2).OnTimer = @OnTimer2 // timer event handler
// enable the timers...
Timer.Items(TIMER1).Enabled = true
Timer.Items(TIMER2).Enabled = true
Timer.Start()
}
While true
// flash LED...
if Flames_ENABLE = 0 //toggle switch w/ 10K pull-down
then
FOR X = 0 TO 5
HEAD_LIGHT = 1
DELAYMS(500)
TOGGLE(HEAD_LIGHT)
DELAYMS(500)
NEXT
endif
Flame1 =1
Flame2 =1
Flame3 =1
delayms(1000)
WEND