thanks for the feedback and kind words ,but I must confess its a bit fluky I was working on replacing my roller door pgm as you posted your code . I'm also using the exact relay setup you posted in the other thread
went from this rather vague uncommented spaghetti code written 10+ years ago
to this ( with plenty of comments for my future self)Code:up VAR gpio.1 lm VAR gpio.4 dwn var gpio.2 cl var gpio.3 tr var gpio.0 b1 var byte option_reg.5=0 ' gpio 2 not tock option_reg.6=1 ' weak pullups ? trisio = %100111 '1,2 output rest i/p low up ; motor off low dwn main: if tr=1 then main pause 80 if tr= 1 then main b1=0 input gpio.4 if lm=0 then high dwn b1=3 pause 1000 else high up endif move: pause 250 b1 = b1 +1 if cl=0 or lm=0 then still if b1 >100 then still 'taken too long just give up goto move still: low dwn low up if tr=0 then still pause 5000 goto main end
in order to make a door open indicator to feed into the auto light on off switch and the alarm system.
it bench tests ok but its not installed yet , its not a direct replacement of the old chip might need a new pcb
Code:;pic12f683 #CONFIG __config _INTOSCIO & _WDT_ON & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOD_ON & _IESO_ON & _FCMEN_ON #ENDCONFIG DEFINE OSC 4 ' up VAR gpio.1 ' lm up VAR gpio.4 || cl ' lm down VAR gpio.5 ' dwn var gpio.2 ' cl var gpio.4 over current ' tr var gpio.3 maxrun con 2000 ; 2000 = 100 sec liftoff con 500 ; .5 sec time to move off limit sw settle con 1500; 1.5 sec time to let door settle before checking closed state ; lets current overload signal deactivate if that stopped door press_threshold con 20 ; 1 sec long_press_threshold con 60 ;3 sec CMCON0=%00000111 ANSEL=%00000000 wpu=48 ;pullups OPTION_REG.7=0 ;pullups trisio= %111000 trigger var gpio.3 ; low is active from button or wireless door_state var byte ; 0=closed ,1= open , 2=moving open_led var gpio.0 ;lit when door open , flashes when moving open_door var gpio.1 ;relay up close_door var gpio.2 ;relay down up_lim VAR gpio.4 ; low is active limit switches current overload is wire nor'ed to this pin too dn_lim VAR gpio.5 ; low is active trigger_cnt var byte ;counter TR_ACT VAR BYTE RUN_COUNT VAR word gpio_shadow var byte ;shadow gpio to eliminate rmw issues 'initialise open_door = 0 close_door = 0 TR_ACT=0 gosub chk_door trigger_cnt=0 RUN_COUNT=0 mainloop: gosub Chk_trigger if TR_ACT and (door_state.1 != 1) then gosub move_door ;has button been on for 3 seconds IF door_state.1=1 THEN ;is the DOOR in motion open_led =! open_led ;flash led while DOOR moving ****** this violates gpio shadow and may cause rmw error IF(gpio&48) != 48 then RUN_COUNT=0 IF RUN_COUNT THEN ;should the motion continue ? RUN_COUNT = RUN_COUNT -1 ELSE GOSUB STOP_door ENDIF else gosub chk_door ; has door been opened or closed manually ENDIF pause 50 goto mainloop Chk_trigger: ; after trigger_count gets to press_threshold we act on trigger release or when long_press_threshold reached if trigger=0 then Trigger_cnt=trigger_cnt+1 ;if trigger is active add to count IF trigger_cnt > long_press_threshold THEN TR_ACT= 2 ELSE if trigger_cnt > press_threshold THEN ;press_threshold met IF trigger_cnt > long_press_threshold THEN TR_ACT= 2 ;FORCE CLOSE ACTION WITH LONG TRIGGER >3 SEC ELSE TR_ACT= 1 ENDIF ELSE ;press_threshold not met TR_ACT= 0 trigger_cnt=0 ;reset count ENDIF endif return move_door: IF TR_ACT=2 THEN door_state.0 = 1 ;FORCE CLOSE ACTION door_state.1=1 ;door now moving if door_state.0 = 0 then ;its closed so open the door gpio_shadow.0=1 ;WILL SET open_led = ON gpio_shadow=gpio_shadow | 2 door_state.0 = 1 else ; close the door gpio_shadow=gpio_shadow | 4 door_state.0 = 0 ;show closed door open_led = Off gpio_shadow.0=0 ; WILL SET open_led = FF endif TR_ACT= 0 gpio=gpio_shadow ;apply to gpio pause liftoff ;get door off limit sw RUN_COUNT=maxrun;0 ;set timer for movement return STOP_door: gpio_shadow=gpio_shadow & ~6 ;kill movement gpio=gpio_shadow ;apply to gpio trigger_cnt=0 ;reset everything ready for next TRIGGER door_state.1=0; NOW not moving pause settle ;let door settle chk_door: door_state.0 = dn_lim ; see if its closed open_led = dn_lim gpio_shadow = gpio return




Bookmarks