Menu

toggle pin state?

Help
2015-09-06
2015-09-07
  • tony golding

    tony golding - 2015-09-06

    a simple question that i have searched and found things like using "pin = !pin" ect but obviously this does not work so i am just wondering if their is an easy quick way or command to toggle an output pin between on/off states.

    tony

     
  • Anobium

    Anobium - 2015-09-06

    If you search the Help for Toggle this will show a good method .

     
  • Anobium

    Anobium - 2015-09-06

    If you search the Help for Toggle this will show a good method .

     
  • tony golding

    tony golding - 2015-09-06

    yep seen both of those, the peek solution really doesnt make to much sense to me though.

    tony

     

    Last edit: tony golding 2015-09-06
  • tony golding

    tony golding - 2015-09-07

    ok i think ive spent too long not doing this as i seem to have forgotten how to do things.

    what i am trying to achieve is to simply control 2 outputs using one pushbutton with a brief push and release toggling output 1 and a push and hold to toggle output 2 but it seems im getting no luck at all.

    #chip 10f322, 16
    #config LVP_OFF, MCLRE_OFF, BOREN_OFF, FOSC_INTOSC
    
    set NOT_WPUEN = 0
    set WPUA.3 = 1
    
    ;Input Pin and Direction
    #define inpin PORTA.3
    #define bttn_led PORTA.2
    #define ch2 PORTA.1
    #define ch1 PORTA.0
    
    Dir inpin In
    dir bttn_led out
    dir ch1 out
    dir ch2 out
    
    dim ch_set as bit
    
    
    PORTA = b'00001111'
    wait 500 ms
    PORTA = b'00001000'
    
    
     ; Main Program starts here
     Do
    
        wait until inpin = off
        ch_select
    
        if ch_set = 0 then
        Toggle @PORTA, 1
        end if
    
        if ch_set = 1 then
        Toggle @PORTA, 2
        end if
    
        wait until inpin = on
    
    
     Loop
    
    sub ch_select
    
        wait 350 ms
    
        if inpin = off then
        ch_set = 1
        end if
    
        if inpin = on then
        ch_set = 0
        end if
    
    end sub
    
    Sub Toggle ( In DestPort As word, In DestBit )
          Poke DestPort, Peek(DestPort) xor DestBit
    End sub
    

    i tried the recommended route of the peek at the entire port but still cannot get any sort of behaviour i expect.

    tony

     

    Last edit: tony golding 2015-09-07
  • tony golding

    tony golding - 2015-09-07

    well finally got it working, gawd i think ive spent far to much time away from this

    #chip 10f322, 16
    #config LVP_OFF, MCLRE_OFF, BOREN_OFF, FOSC_INTOSC
    
    set NOT_WPUEN = 0
    set WPUA.3 = 1
    
    ;Input Pin and Direction
    #define inpin PORTA.3
    #define bttn_led PORTA.2
    #define ch2 PORTA.1
    #define ch1 PORTA.0
    
    Dir inpin In
    dir bttn_led out
    dir ch1 out
    dir ch2 out
    
    dim ch_set as bit
    
    
    LATA = b'00001111'
    wait 500 ms
    LATA = b'00001000'
    
    
     ; Main Program starts here
     Do
    
        wait until inpin = off
        ch_select
    
        if ch_set = 0 then
        Toggle @LATA, 1
        end if
    
        if ch_set = 1 then
        Toggle @LATA, 2
        end if
    
        wait until inpin = on
        wait 350 ms
    
    
     Loop
    
    sub ch_select
    
        wait 350 ms
    
        if inpin = on then
        ch_set = 0
        end if
    
        if inpin = off then
        ch_set = 1
        end if
    
    end sub
    
    Sub Toggle ( In DestPort As word, In DestBit )
          Poke DestPort, Peek(DestPort) xor DestBit
    End sub
    
     
  • Anobium

    Anobium - 2015-09-07

    Tony, well done.

    Have a look at the alternate approach using an interrupt. If you look at you Demo filder in your Great Cow Basic installation there are more interrupt examples.

    Anobium

     'I changed to 16f so I could test... sorry.
      #chip 16f88, 16
      #config  MCLRE_OFF, BOREN_OFF
    
      'set NOT_WPUEN = 0
      'set WPUA.3 = 1
    
      ;Input Pin and Direction
      #define inpin PORTb.4
      #define bttn_led PORTA.2
      #define ch2 PORTB.1
      #define ch1 PORTB.0
    
      Dir inpin In
      dir bttn_led out
      dir ch1 out
      dir ch2 out
    
      portb = b'00001111'
      wait 500 ms
      portb = b'00001000'
    
      set ch1 on
    
      On Interrupt portbchange call HandleEvent
    
       ; Main Program starts here
       Do
    
       Loop
    
      Sub HandleEvent
        if ch2 = 1 then
          ch1 = 1
          ch2 = 0
        else
          ch1 = 0
          ch2 = 1
        end if
      End sub
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.