12f675 to latch output while it does something else


Closed Thread
Results 1 to 25 of 25
  1. #1
    Join Date
    Apr 2014
    Location
    Petaling Jaya, Selangor MALAYSIA
    Posts
    14

    Default 12f675 to latch output while it does something else

    Hi every1,

    I'm very new to programming PIC micro controllers. I've been banging my head against the wall the last few days, is there any way I can get an output to stay on while the processor does something else...? here is the deivce:

    1 input (button)
    2 outputs (relay and led)

    light flashes when on stand by (easy)
    when the button is pressed high relay (easy)
    at the same time flash LED to show process(.....not so easy as the relay shuts down)
    after 30s led stays on and relay stays on (...........again I can only get 1 output to come on at a time)
    while waiting for button to be pressed again, led stays on and relay stays on
    when button is pressed again flash led 1minute (to allow proper shutdown)
    relay goes LOW and led goes back to standby mode.

    I can't seem to get th erelay to LATCH ON. I gone thro the manual cover to cover. cant seem to find anything to solve this problem.
    can someone help....?

  2. #2
    Join Date
    Apr 2014
    Location
    Petaling Jaya, Selangor MALAYSIA
    Posts
    14


    Did you find this post helpful? Yes | No

    Default Re: 12f675 to latch output while it does something else

    Ohhhh.... yes I need the relay to stay on indefinitely from HIG RELAY command until a LOW RELAY command is executed

  3. #3
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: 12f675 to latch output while it does something else

    Post your code and schematic then I can help.

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,569


    Did you find this post helpful? Yes | No

    Default Re: 12f675 to latch output while it does something else

    Hi,
    As with most things there are more than one way to do it. Personally I like to code things like this using a state machine since it allows you divide the code into easily identifiable "blocks" (or states).
    The following compiles without errors but I don't have access to any hardware to actually test it at the moment.

    Code:
    LED VAR PortB.0                 ' Output for LED
    Relay VAR PortB.1               ' Outpur for relays
    Btn VAR PortB.2                 ' Input for button
    
    Pushed CON 1                    ' Logic level when button is pressed
    
    Standby CON 0                   ' States for the state machine...
    Delay CON 1                     ' ...which helps us keep track...
    Engaged CON 2                   ' ...of what we're doing.
    Shutdown CON 3
    
    State VAR BYTE                  ' State machine variable
    Time VAR BYTE                   ' To keep track of time
    
    TRISB = %11111100               ' Tow outputs at the bottom.
    
    Main:
        Select Case State
    '-----------------------------------------------------------------
            Case Standby
            ' In standby mode we blink the LED at 1Hz while checking
            ' if the button is pressed. Note that the response time
            ' for the button will be up to 1 second. This can be fixed
            ' later if needed.
            ' When the button is pressed we turn on the relay and
            ' prepare our 30 second timer.
            
                LED = 1
                PAUSE 500
                LED = 0
                PAUSE 500
                
                IF Btn = Pushed THEN
                    Relay = 1
                    Time = 30
                    State = Delay
                ENDIF
    
    '-----------------------------------------------------------------
            Case Delay
            ' Now we're supposed to blink the LED for 30 seconds
            ' while the relay is ON. The LED blinks at 1Hz, each cycle
            ' we decrement the Time variable. When it reaches 0 the
            ' 30 seconds delay is complete, the LED is turned on solid
            ' and we switch to next state. 
                LED = 1
                PAUSE 500
                LED = 0
                PAUSE 500
                Time = Time - 1
                
                IF Time = 0 THEN
                    Led = 1
                    State = Engaged
                ENDIF
    
    
    '-----------------------------------------------------------------            
            Case Engaged
            ' Now the relay is on, the LED is on.
            ' We're supposed to wait for the button to be pressed.
            ' At which time there should be 60 seconds delay.
                If Btn = Pushed THEN
                    Time = 60
                    State = Shutdown
                ENDIF
    
    
    '-----------------------------------------------------------------        
            Case Shutdown
            ' Now the LED should blink for 60 seconds after which the
            ' relay is turned OFF and the whole thing starts over.
                LED = 1
                PAUSE 500
                LED = 0
                PAUSE 500
                Time = Time - 1
                
                If Time = 0 THEN
                    Relay = 0
                    State = Standby
                ENDIF
    '-----------------------------------------------------------------
            END SELECT
    
    GOTO Main
    /Henrik.

    EDIT: Missed the fact that you're using a 12F part. Change TRIS and PORT as needed.
    Last edited by HenrikOlsson; - 19th September 2014 at 08:54.

  5. #5
    Join Date
    Apr 2014
    Location
    Petaling Jaya, Selangor MALAYSIA
    Posts
    14


    Did you find this post helpful? Yes | No

    Default Re: 12f675 to latch output while it does something else

    this is the prog:

    INIT:

    PAUSE 1000 'INTERNAL CLOCK RESET 75ms
    clear 'SET REGISTER TO ZERO

    FOR TIMES = 3 TO 1 STEP -1
    HIGH LED 'INDICATOR FOR MCU POWER-UP
    PAUSE 50 'INDICATOR FOR MCU POWER-UP
    LOW LED 'INDICATOR FOR MCU POWER-UP
    PAUSE 750 'INDICATOR FOR MCU POWER-UP
    NEXT TIMES


    Start:

    clear 'SET REGISTER TO ZERO

    Standby: 'WAIT FOR BUTTON TO STARTUP
    Button SWITCH,1,255,0,b1,1,STARTUP 'Check Button 1 (Skip to heartbeat if Not Pressed)

    Heartbeat:
    'HEART BEAT INDICATOR ON LED
    For DUTYCYCLE = 0 to 255 STEP 3 'THIS IS THE DELAY 255 MAX. COUNT LESS FASTER
    PWM LED, DUTYCYCLE, 1 'THE NUMBER WILL DETERMINE SPEED SMALLER FASTER
    Next
    For DUTYCYCLE = 255 to 0 Step -2
    PWM LED, DUTYCYCLE, 1 'THE NUMBER WILL DETERMINE SPEED SMALLER FASTER
    Next

    GOTO STANDBY


    startup:

    HIGH relay ' SWITCH ON POWER TO PROJECTOR
    ' OPTION TO SEND IR TO PROJECTOR
    ' OPTION TO SEND RS232 TO PROJECTOR

    'TESTING WITH TIME =20
    FOR TIMES = 20 TO 1 STEP -1 '1m FOR PROJECTOR WARMUP
    HIGH LED 'INDICATOR FOR SYSTEM START-UP
    PAUSE 250 'INDICATOR FOR SYSTEM START-UP
    LOW LED 'INDICATOR FOR SYSTEM START-UP
    PAUSE 250 'INDICATOR FOR SYSTEM START-UP
    NEXT TIMES


    running: 'WAIT FOR INTERUPT TO SHUTDOWN
    Button SWITCH,1,255,0,B1,1,SHUTDOWN 'Check Button 1 (Skip to ledup if Not Pressed)
    'SHUTDOWN IF BUTTON IS PRESSED
    ledup:
    HIGH LED 'INDICATOR FOR SYSTEM RUNNING
    GOTO RUNNING 'LOOP UNTIL BUTTON IS PRESSED



    shutdown:

    ' OPTION TO SEND IR TO PROJECTOR
    ' OPTION TO SEND RS232 TO PROJECTOR
    'TESTING WITH TIME =20
    FOR TIMES = 20 TO 1 STEP -1 '120s (2m) FOR PROJECTOR COOLDOWN
    HIGH LED 'INDICATOR FOR SYSTEM START-UP
    PAUSE 250 'INDICATOR FOR SYSTEM START-UP
    LOW LED 'INDICATOR FOR SYSTEM START-UP
    PAUSE 250
    NEXT TIMES
    LOW RELAY 'SHUT OFF POWER TO PROJECTOR


    GOTO STANDBY





    END

    regards

  6. #6
    Join Date
    Apr 2014
    Location
    Petaling Jaya, Selangor MALAYSIA
    Posts
    14


    Did you find this post helpful? Yes | No

    Default Re: 12f675 to latch output while it does something else

    Hi Henrik,

    i tried it, but the relay will not stay ON.

  7. #7
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: 12f675 to latch output while it does something else

    Check your wiring for a short between LED and RELAY

  8. #8
    Join Date
    Apr 2014
    Location
    Petaling Jaya, Selangor MALAYSIA
    Posts
    14


    Did you find this post helpful? Yes | No

    Default Re: 12f675 to latch output while it does something else

    tried various ports on the MCU. it is sitting on the PICEasy5 board

  9. #9
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: 12f675 to latch output while it does something else

    My thinking is that flashing the LED pin affects the RELAY pin therefore they must be connected in some way.

  10. #10
    Join Date
    Apr 2014
    Location
    Petaling Jaya, Selangor MALAYSIA
    Posts
    14


    Did you find this post helpful? Yes | No

    Default Re: 12f675 to latch output while it does something else

    Quote Originally Posted by EarlyBird2 View Post
    My thinking is that flashing the LED pin affects the RELAY pin therefore they must be connected in some way.
    it seems it is not possible to have 2 outputs HIGH at the same time....? i tried to lite up 2 leds:

    HIGH LED1
    HIGH LED2

    only LED22 lights up, then i did:

    HIGH LED1
    PAUSE 1000
    HIGH LED2

    i see led1 light up (1s, as expected) then it goes off and led 2 lights up is there any LATCH ON command (maybe LATCH ON LED1) to set it high indefinitely led1 while we send another command to light up LED2....? how can we high LED1 until we send a LOW LED1, in the mean time we are doing other things like flash LED2, send IR/ RS232, etcetc..........?

    I'm new at this please excuse some of my silly questions.

  11. #11
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,982


    Did you find this post helpful? Yes | No

    Default Re: 12f675 to latch output while it does something else

    When LED2 is ON, does it stay ON for ever?

    Ioannis

  12. #12
    Join Date
    Apr 2014
    Location
    Petaling Jaya, Selangor MALAYSIA
    Posts
    14


    Did you find this post helpful? Yes | No

    Default Re: 12f675 to latch output while it does something else

    Quote Originally Posted by Ioannis View Post
    When LED2 is ON, does it stay ON for ever?

    Ioannis
    yes that does. I initially thought maybe there was a problem with the out put pin., i swap to other pins, same result, still not satisfied, i ran my scope on the the pins, yup, th efirst led switches ON briefly and then OFF as the second led lights up.

    hhhhhmmmmmmmmmm (rubbing chin........) how do i get it to stay ON..............?

  13. #13
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,982


    Did you find this post helpful? Yes | No

    Default Re: 12f675 to latch output while it does something else

    OK, this is not logic at all.

    Please post all the test code that you used to light the two led's.

    There is something that we are missing here.

    Ioannis

  14. #14
    Join Date
    Apr 2014
    Location
    Petaling Jaya, Selangor MALAYSIA
    Posts
    14


    Did you find this post helpful? Yes | No

    Default Re: 12f675 to latch output while it does something else

    Quote Originally Posted by Ioannis View Post
    OK, this is not logic at all.

    Please post all the test code that you used to light the two led's.

    There is something that we are missing here.

    Ioannis
    Standby: 'WAIT FOR BUTTON TO STARTUP
    Button SWITCH,1,255,0,b1,1,STARTUP 'Check Button 1 (Skip to heartbeat if Not Pressed)

    Heartbeat:
    'HEART BEAT INDICATOR ON LED
    For DUTYCYCLE = 0 to 255 STEP 3 'THIS IS THE DELAY 255 MAX. COUNT LESS FASTER
    PWM LED, DUTYCYCLE, 1 'THE NUMBER WILL DETERMINE SPEED SMALLER FASTER
    Next
    For DUTYCYCLE = 255 to 0 Step -2
    PWM LED, DUTYCYCLE, 1 'THE NUMBER WILL DETERMINE SPEED SMALLER FASTER
    Next

    GOTO STANDBY


    startup:

    HIGH relay ' SWITCH ON POWER TO PROJECTOR
    ' OPTION TO SEND IR TO PROJECTOR
    ' OPTION TO SEND RS232 TO PROJECTOR

    'TESTING WITH TIME =20
    FOR TIMES = 20 TO 1 STEP -1 '1m FOR PROJECTOR WARMUP
    HIGH LED 'INDICATOR FOR SYSTEM START-UP
    PAUSE 250 'INDICATOR FOR SYSTEM START-UP
    LOW LED 'INDICATOR FOR SYSTEM START-UP
    PAUSE 250 'INDICATOR FOR SYSTEM START-UP
    NEXT TIMES


    running: 'WAIT FOR INTERUPT TO SHUTDOWN
    Button SWITCH,1,255,0,B1,1,SHUTDOWN 'Check Button 1 (Skip to ledup if Not Pressed)
    'SHUTDOWN IF BUTTON IS PRESSED
    ledup:
    HIGH LED 'INDICATOR FOR SYSTEM RUNNING
    GOTO RUNNING 'LOOP UNTIL BUTTON IS PRESSED



    shutdown:

    ' OPTION TO SEND IR TO PROJECTOR
    ' OPTION TO SEND RS232 TO PROJECTOR
    'TESTING WITH TIME =20
    FOR TIMES = 20 TO 1 STEP -1 '120s (2m) FOR PROJECTOR COOLDOWN
    HIGH LED 'INDICATOR FOR SYSTEM START-UP
    PAUSE 250 'INDICATOR FOR SYSTEM START-UP
    LOW LED 'INDICATOR FOR SYSTEM START-UP
    PAUSE 250
    NEXT TIMES
    LOW RELAY 'SHUT OFF POWER TO PROJECTOR

    GOTO STANDBY

    END

    you see I'm tring to create a delay power switch, to allow for projector to cool down before the power is cut off. normally in my office, they just hit the power button on the remote control and throw the switch as they leave the room, usually done in the same breath. i want to create a delay so.... the power will be cut off only after 2m, to allow for cooling of the lamp, this will make the lamp last longer before the next change is required.
    so here we have 3 states:
    standby: power realy is OFF, LED gives a heartbeat light
    startup: power relay is ON, LED Flashes for 30s (does not allow you to switch OFF the power)
    running: power relay is ON, LED is ON
    shutdown: LED flashes (2m to allow cooling) relay OFF
    go to standby wait for next cycle

    very simple requirement, but I cannot get the relay to go 'ON' at the same time LED to go "ON"

  15. #15
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,569


    Did you find this post helpful? Yes | No

    Default Re: 12f675 to latch output while it does something else

    Hi,
    it seems it is not possible to have 2 outputs HIGH at the same time....? i tried to lite up 2 leds:
    Sounds like you're getting bit by the ever so clasic read-modify-write issue caused by not having analog functionallity on related pins turned off.
    Code:
    CMCON = 7   ' Turn off comparator on 12F675
    ANSEL = 0    ' None of the I/O connected to the ADC
    /Henrik.

  16. #16
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,982


    Did you find this post helpful? Yes | No

    Default Re: 12f675 to latch output while it does something else

    What Henrik stated must be placed on top of your program.

    Try it and reply the results.

    Ioannis

  17. #17
    Join Date
    Apr 2014
    Location
    Petaling Jaya, Selangor MALAYSIA
    Posts
    14


    Did you find this post helpful? Yes | No

    Default Re: 12f675 to latch output while it does something else

    Quote Originally Posted by Ioannis View Post
    What Henrik stated must be placed on top of your program.

    Try it and reply the results.

    Ioannis
    Ok will do that we are out of the office already, GMT +8 here. That will be the first thing I try on Monday. Will keep you posted. Thanks Philip. Have a great weekend guys

  18. #18
    Join Date
    Apr 2014
    Location
    Petaling Jaya, Selangor MALAYSIA
    Posts
    14


    Did you find this post helpful? Yes | No

    Default Re: 12f675 to latch output while it does something else

    Thanks henrik, Ill try that on Monday. Have a great weekend..!

    Philip

  19. #19
    Join Date
    Apr 2011
    Location
    Welches, Oregon
    Posts
    198


    Did you find this post helpful? Yes | No

    Default Re: 12f675 to latch output while it does something else

    Quote Originally Posted by AVGuru View Post
    How do i get it to stay ON..............?
    I don't think there's anything of substance I can add here, only chiming in to answer implicitly: There is no command or statement to make a pin or port "stay" on. Pin states are "set and forget" in that they go high or low only as commanded - and remain so, until commanded to change. Some statements include changing input/ output or high/ low as part of the assembly (documented), but port state is a switch and commanded - at some level.

    So, for anyone following on after looking for a solution to a similar problem: check your code, check your configuration, check your power supply; the pin will remain set (or cleared) as initially commanded unless some other influence holds sway.

  20. #20


    Did you find this post helpful? Yes | No

    Default Re: 12f675 to latch output while it does something else

    In addition to what others suggested. Are you using a transistor to buffer the pic
    output from the relay coil? If not you may be exceeding the current capacity of
    the output pin. This could cause heating and result in the pic resetting. Also inductive
    voltage spikes can cause resets. A simple schematic would help of your relay connection.

  21. #21
    Join Date
    Apr 2014
    Location
    Petaling Jaya, Selangor MALAYSIA
    Posts
    14


    Did you find this post helpful? Yes | No

    Default Re: 12f675 to latch output while it does something else

    Quote Originally Posted by HenrikOlsson View Post
    Hi,


    Sounds like you're getting bit by the ever so clasic read-modify-write issue caused by not having analog functionallity on related pins turned off.
    Code:
    CMCON = 7   ' Turn off comparator on 12F675
    ANSEL = 0    ' None of the I/O connected to the ADC
    /Henrik.

    Henrik, thanks a million, we managed to get it to work. Alto port 1 out does not light the led. It ok I already know why. I can't see how I missed out the config for comparator. Thanks again....!

  22. #22
    Join Date
    Apr 2014
    Location
    Petaling Jaya, Selangor MALAYSIA
    Posts
    14


    Did you find this post helpful? Yes | No

    Default Re: 12f675 to latch output while it does something else

    Hi henrik, I seem to have lost A.0 & A.1 & A.3 as outputs....? I tried to swap the led around the port, but they won't light up.

    A.2 is the LED
    A.4 is the RELAY and
    A.5 is the Button

  23. #23
    Join Date
    Apr 2014
    Location
    Petaling Jaya, Selangor MALAYSIA
    Posts
    14


    Did you find this post helpful? Yes | No

    Default Re: 12f675 to latch output while it does something else

    Quote Originally Posted by mark_s View Post
    In addition to what others suggested. Are you using a transistor to buffer the pic
    output from the relay coil? If not you may be exceeding the current capacity of
    the output pin. This could cause heating and result in the pic resetting. Also inductive
    voltage spikes can cause resets. A simple schematic would help of your relay connection.
    Hi mark, thanks for the heads up on that, Yes I'm using a 2N2222 for the output to relay on open emitter, as well as a diode across the relay to stop it from chattering.

  24. #24
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,569


    Did you find this post helpful? Yes | No

    Default Re: 12f675 to latch output while it does something else

    Hi,
    Hi henrik, I seem to have lost A.0 & A.1 & A.3 as outputs....? I tried to swap the led around the port, but they won't light up.
    Not sure I understand what this means.
    On the 12F675 you have GPIO.0 etc. Make sure you reference that and make sure you set/clear TRISIO accordingly.

    /Henrik.

  25. #25
    Join Date
    Apr 2014
    Location
    Petaling Jaya, Selangor MALAYSIA
    Posts
    14


    Did you find this post helpful? Yes | No

    Default Re: 12f675 to latch output while it does something else

    Quote Originally Posted by AVGuru View Post
    Hi mark, thanks for the heads up on that, Yes I'm using a 2N2222 for the output to relay on open emitter, as well as a diode across the relay to stop it from chattering.
    THANK YOU all. got it to work on the PICEASY board, now to put it in a box and on the wall.


    thanks again

Similar Threads

  1. 12f675 GPIO Problem. No output on GPIO.2
    By tasmod in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 2nd June 2014, 16:57
  2. Replies: 11
    Last Post: - 13th January 2013, 11:25
  3. How to latch an output for a pre-defined time
    By Dennis in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 21st December 2009, 19:20
  4. 12F675 output dependency
    By Kristjan in forum General
    Replies: 4
    Last Post: - 5th May 2007, 01:44
  5. open drain output as digital output?
    By droptail in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 10th November 2006, 04:11

Members who have read this thread : 0

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

Tags for this Thread

Posting Permissions

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