12f675 to latch output while it does something else


Closed Thread
Results 1 to 25 of 25

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    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 07:54.

  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

    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

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, 15:57
  2. Replies: 11
    Last Post: - 13th January 2013, 10: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, 18:20
  4. 12F675 output dependency
    By Kristjan in forum General
    Replies: 4
    Last Post: - 5th May 2007, 00:44
  5. open drain output as digital output?
    By droptail in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 10th November 2006, 03: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