Please look over my code


Results 1 to 11 of 11

Threaded View

  1. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,682


    Did you find this post helpful? Yes | No

    Default Re: Please look over my code

    three things
    1. these two pieces don't have proper labels ,relying on the good nature of the compiler is probably not a good idea



    open
    pause 1000 'Orange LED on
    high gpio.1
    pause 18500
    low gpio.1
    goto InputButton

    close
    pause 1000 'Blue LED on
    high gpio.2
    pause 19000
    low gpio.2
    goto InputButton
    2 spaghetti code like that is very difficult to debug or follow the logic of

    3 rmw issue possibilities abound

    for what its worth try it this way .it may look more complicated but it compiles to smaller code that's more predictable imo
    ps not sure what you are doing with gpio.0 , if it is a sensor indicating gate open/closed or moving state you need to add it back in to the code




    Code:
    CMCON=%00000111
    ANSEL=%00000000
    
    @ DEVICE pic12F675, XT_OSC
    _NOCLKOUT
    @ DEVICE pic12F675, WDT_ON
    @ DEVICE pic12F675, PWRT_ON
    @ DEVICE pic12F675, MCLR_OFF
    @ DEVICE pic12F675, BOD_ON
    
    DEFINE OSC 4
    
    trisio= %111001
    btn var gpio.3          ; assume low is active
    gate_state var byte  ; 0=closed ,1= open , 2=moving
    open_gate var gpio.1
    close_gate var gpio.2
    button_count var byte   ;counter to bebounce btn and ensure hold time
    gpio_shadow var byte     ;shadow gpio  to eliminate rmw issues
    'initialise
    open_gate  = 0
    close_gate = 0
    gate_state = 0    ;assume closed on power up
    button_count=0
    gpio_shadow=0
    
    mainloop:
    gosub Check4Button
    if (button_count > 30) and (gate_state.1 != 1)  then gosub move_gate
    pause 100
    goto mainloop
    Check4Button:
    if btn=0 then
     button_count=button_count+1   ;if button is active add to count
    else
     button_count=0    ;reset count
    endif
    return
     
    move_gate:
    pause 1000  ; not sure if this is required
    gate_state.1=1  ;gate now moving
    if  gate_state.0 = 0 then ;its closed so open the gate
        gpio_shadow=gpio_shadow | 2 ; high gpio.1   Orange LED on 
        ; same as gpio_shadow.1=1  if you prefer
        gate_state.0 = 1
     else       ; close the gate
        gpio_shadow=gpio_shadow | 4      'high gpio.2 Blue LED on
        ;same as gpio_shadow.2=1      if you prefer
        gate_state.0 = 0
    endif
    gpio=gpio_shadow
    pause 18500
    gpio_shadow=gpio_shadow & !6 
    ;same as gpio_shadow.1=0 : gpio_shadow.2=0 if you prefer
    gpio=gpio_shadow
    'while ! btn  ; wait for button release if needed
    'wend
    button_count=0
    gate_state.1=0
    return
    Last edited by richard; - 27th March 2016 at 04:47. Reason: typo as usual

Similar Threads

  1. avr code to code for PIC
    By JasonMcG in forum General
    Replies: 1
    Last Post: - 11th September 2014, 01:57
  2. Serial problem between BasicStamp code and PBP code
    By AllanZilkowsky in forum mel PIC BASIC Pro
    Replies: 22
    Last Post: - 6th April 2014, 03:15
  3. Working code but my layman approach uses too much code space
    By Christopher4187 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 14th December 2012, 21:44
  4. Code: Why is this code greater than 2000 words?
    By DrDreas in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 1st June 2007, 20:51

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