Controlling Relays


Closed Thread
Results 1 to 11 of 11

Hybrid View

  1. #1

    Default Controlling Relays

    Good Morning,
    I have been playing around with MicroCode Studio this morning trying to learn something today. Anyhow I have built a serial keypad board and a serial relay board for experimenting with PicBasic Pro. In this way I can learn as I build. On the relay board I have some relays set to be momentary and the rest I am just toggling. When I send from the keypad to the relays that I want to be on only when the key is pressed they just stay on. When I press the one that toggle they work great. I thought that each time data was received and then branched the output would change as they do with the ones set to toggle. While holding down the switch for the relays that toggle they switch on, off, on, etc.... but the one for momentary they stay on constant. Any ideas what I am doing wrong? Thanks for all your help.
    The Pic I am using is a 16f628a i was using a 16f84a. Here is the code.

    define osc 4
    INCLUDE "MODEDEFS.BAS"
    @ DEVICE MCLR_ON, INTRC_OSC, WDT_Off, LVP_OFF, BOD_OFF, PWRT_ON, PROTECT_Off
    CMCON=7
    keydata VAR byte
    serpin VAR porta.1
    PORTA = 0
    PORTB = 0
    trisa = %00000010
    trisb = %00000000

    PAUSE 100 ' settle time

    loop:

    gosub loop1
    branch keydata, [rly1,rly2,rly3,rly4,rly5,rly6,rly7,rly8]
    GOTO loop

    rly1:
    HIGH PORTB.6
    pause 50
    GOTO loop

    rly2:
    HIGH PORTB.1
    pause 50
    GOTO loop

    rly3:
    HIGH PORTB.2
    pause 50
    GOTO loop

    rly4:
    HIGH PORTB.3
    pause 50
    GOTO loop

    rly5:
    HIGH PORTB.4
    pause 50
    GOTO loop

    rly6:
    toggle PORTB.5
    pause 250
    GOTO loop

    rly7:
    TOGGLE PORTB.6
    PAUSE 250
    GOTO loop

    rly8:
    toggle PORTB.0
    pause 250
    goto loop

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by tazntex View Post
    gosub loop1
    branch keydata, [rly1,rly2,rly3,rly4,rly5,rly6,rly7,rly8]
    Looks like some of the program is missing...probably leaving out some important information.

    Did you try it with LEDs first instead of relays?
    Does it work the way you'd expect it to work?
    Do you have flyback diodes across the coils of the relays?

  3. #3
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    1. Where is Loop1 ?
    2. When do you turn off the leds that you turn on initially?

    ==============
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  4. #4


    Did you find this post helpful? Yes | No

    Default Controlling Relays

    Thanks for replying,
    The relays that toggle work wonderful, and the ones that I want on only when I am pressing the switch, they just stay on. the relay circuits from their corresponding Pic output are Identical with a diode flyback protection.
    Thanks for helping
    Here is the code again with Loop1,
    define osc 4
    INCLUDE "MODEDEFS.BAS" 'Serial communication mode definition file
    @ DEVICE MCLR_ON, INTRC_OSC, WDT_Off, LVP_OFF, BOD_OFF, PWRT_ON, PROTECT_Off
    CMCON=7
    keydata VAR byte
    serpin VAR porta.1 'serial input pin
    PORTA = 0
    PORTB = 0
    trisa = %00000010
    trisb = %00000000

    PAUSE 100 ' settle time

    loop:

    gosub loop1
    branch keydata, [rly1,rly2,rly3,rly4,rly5,rly6,rly7,rly8]
    GOTO loop

    rly1:
    HIGH PORTB.6
    pause 50
    GOTO loop

    rly2:
    HIGH PORTB.1
    pause 50
    GOTO loop

    rly3:
    HIGH PORTB.2
    pause 50
    GOTO loop

    rly4:
    HIGH PORTB.3
    pause 50
    GOTO loop

    rly5:
    HIGH PORTB.4
    pause 50
    GOTO loop

    rly6:
    toggle PORTB.5
    pause 250
    GOTO loop

    rly7:
    TOGGLE PORTB.6
    PAUSE 250
    GOTO loop

    rly8:
    toggle PORTB.0
    pause 250
    goto loop




    loop1:
    SERIN serpin,N2400,[254],keydata
    return

  5. #5


    Did you find this post helpful? Yes | No

    Default When do you turn off the leds that you turn on initially?

    My thinking is that while keeping the switch on for the momentary relay to stay on the data being branch would be the same so the relay would stay on. When I release the switch then the data being branched changes so the relay would turn off. Is this correct?

  6. #6
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Default

    Yes,


    on each loop, you must check which keys are released and de-energize the corresponding 1-5 relays. ( the last ones do not need it ...)

    the PIC outputs must be considered as Bistable ones ... and you must write every change you want !!!

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  7. #7


    Did you find this post helpful? Yes | No

    Default what value is Keydata if no key pushed ??? good question !

    The value of Keydata is 0 if no key is pressed: here is how I've set that,
    if keyin = nokey then keydata = %00000000

    Thank you for your help.

  8. #8


    Did you find this post helpful? Yes | No

    Default This is what I am using to send to the relay board

    define osc 4
    INCLUDE "MODEDEFS.BAS"
    CMCON = 7
    serpin var porta.3
    nokey con %11111111
    preamble con %01010101
    synch con 254
    keyin var portb
    keydata var byte
    trisb = %11111111
    trisa = %00010111
    key1 VAR PORTB.0
    key2 VAR PORTB.1
    key3 VAR PORTB.2
    key4 VAR PORTB.3
    key5 VAR PORTB.4
    key6 VAR PORTB.5
    key7 VAR PORTB.6
    na VAR PORTB.7




    findkey:
    clear
    pause 50
    if key1 = 0 then sw1
    if key2 = 0 then sw2
    if key3 = 0 then sw3
    if key4 = 0 then sw4
    if key5 = 0 then sw5
    if key6 = 0 then sw6
    if key7 = 0 then sw7
    if key8 = 0 then sw8

    goto findkey

    sw1:
    keydata = 1
    goto transmit
    sw2:
    keydata = 2
    goto transmit
    sw3:
    keydata = 3
    goto transmit
    sw4:
    keydata = 4
    goto transmit
    sw5:
    keydata = 5
    goto dout
    sw6:
    keydata = 6
    goto transmit
    sw7:
    keydata = 7
    goto transmit
    sw8:
    keydata = 8
    goto transmit




    transmit:

    serout serpin,N2400,[preamble,synch,keydata]
    pause 50
    if keyin = nokey then keydata = %00000000
    goto findkey


    end

  9. #9
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Post

    Quote Originally Posted by tazntex View Post

    rly1:
    HIGH PORTB.6
    pause 50
    GOTO loop
    Hi,

    You have to de-energize your relays ... that's all

    for relay one ... what value is Keydata if no key pushed ??? good question !



    Alain
    Last edited by Acetronics2; - 28th January 2008 at 16:47.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

Similar Threads

  1. Replies: 10
    Last Post: - 17th February 2012, 07:19
  2. SMS control for Relays
    By charudatt in forum Code Examples
    Replies: 15
    Last Post: - 2nd May 2009, 00:08
  3. PIC relays control
    By flipper_md in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 1st October 2008, 16:03
  4. High End Pre with PIC 16F876A
    By abidr in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 8th May 2008, 08:13
  5. Controlling a DC solenoid
    By The Master in forum Off Topic
    Replies: 2
    Last Post: - 23rd December 2007, 01:26

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts