toggle switch with pull down 10k resistor

Status
Not open for further replies.

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.
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
 
this code is just for testing the toggle switch.
And yet it contains all sorts of other stuff to make it impossible to follow.
And, no wire length won't matter if a proper pullup has been used. However, you mention a pulldown and the toggle switch grounding the input, which is it?

Mike.
 
As Mike said, a 10K pull down in parallel with a switch to ground will always be low, regardless of the switch position.

Now, if you're also trying to use the internal pullups as well, then the 10K pull down will dominate. Parameter D070 of the datasheet states that the typical pullup is a 50uA current. That into a 10K resistor is 0.5 Volts, which the uC will read as low.
 
Parameter D070 of the datasheet states that the typical pullup is a 50uA current. That into a 10K resistor is 0.5 Volts, which the uC will read as low.
That 50uA is the MIN spec. The range for the weak pullup current is 50uA to 400uA, so across 10K that's anywhere from 0.5V to 4.0V which means you have no idea what the result is.

If you want to use the internal pullup then remove the 10K pulldown and just switch the pin to open (1) or GND (0).
Otherwise, make the 10K a pullup.
 
Here is the bare necessary code and still won't recognize the switch. How to turn on just portB.1 pullups? Will give it a try.
Code:
Device = 18F2221
Clock = 8

Include "intosc.bas" 
#option DIGITALIO_INIT = true
Include "setdigitalio.bas"
Dim Led As PORTC.7
Dim Switch As portB.1
Dim x As Byte
Input(Switch) //----------set port C.7 as INPUT
Output(Led)//-------------set portB.1 as OUTPUT
While true
IF SWITCH = 0 THEN
   LED = 1
   DELAYMS(500)
    ELSE
    LED = 0
    ENDIF
WEND
 
How to turn on just portB.1 pullups?

With the 18F2221 you can't turn on just one pullup... it's all of PORTB pullups or none.
On that chip they're turned on by setting INTCON2.7 = 0

I think you've got your comments around backwards...
Code:
Dim Led As PORTC.7
Dim Switch As portB.1

Input(Switch) //----------set port C.7 as INPUT<<<<<<<<<
Output(Led)//-------------set portB.1 as OUTPUT<<<<<<<<<<
 
thanks
Back to the lab to recheck my wiring etc.
My "lab" as my wife calls it is a 1/2 bath (commode and sink).
 
Show us the switch, resistor, and PORTB.1 connections.

Since you're using the rest of PORTB as outputs, you can still use the internal pullups on PORTB for the switch on PORTB.1
If you add 'INTCON2.7 = 0' to your InitIO sub after all the TRISB statements it'll turn on only the pullup on PORTB.1
(individual internal pullups are automatically turned off when you set a port pin to output, ie 'TRISB.x = 0' or by an 'Output' statement)
 
To avoid confusion, just use the 10k resistor as a pullup and ground the pin with the pushbutton.
I.E.


Mike.
 
I tried all suggestions and checked and rechecked board connections.
THEN IT DAWNED ON ME I needed to put SETALLDIGITAL in the code
Now it works as expected.
Code:
Device = 18F2221
Clock = 8

Include "intosc.bas" 
#option DIGITALIO_INIT = true
Include "setdigitalio.bas"
Dim Led As PORTC.7
Dim Switch As PORTB.1
Dim x As Byte

Input(Switch) //----------set port B.1 as INPUT
Output(Led)//-------------set portC.7 as OUTPUT
Led = 0
setalldigital // this is needed to work code as expected
While true

If Switch = 1 Then
   Led = 0
   EndIf
      
       If Switch = 0 Then  //portb.1 is LOW
        Led = 1
       For x = 0 To 5
       DelayMS(500)
       Toggle(Led)
       DelayMS(200)
       Next
       EndIf
       DelayMS(2000)
  Wend
 
THEN IT DAWNED ON ME I needed to put SETALLDIGITAL in the code
Now it works as expected.
If that's the case then your version of setdigitalio.bas is pretty old.

for versions > 2.5 adding the '#option DIGITALIO_INIT = true' will automatically add the call for you.
Code:
#option DIGITALIO_INIT = true
Include "setdigitalio.bas"
 
You know, you could always update.

For most of what you do even the free version would work.
It has all the same features, it's just limited to 256 bytes of ram.
 
I have the paid version.
I looked on the Swordfish forum but failed to locate the SETALLDIGITAL.BAS update
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…