Code:
	'****************************************************************'
*  Name    : UNTITLED.BAS                                      *
'*  Author  : richard                                           *
'*  Notice  : Copyright (c) 2021 caveat emptor                  *
'*          : All Rights Reserved                               *
'*  Date    : 28/09/2021                                        *
'*  Version : 1.0                                               *
'*  Notes   :  18f1320                                          *
'*          :                                                   *
'****************************************************************
#CONFIG
  CONFIG  OSC=INTIO1, FSCM=ON, IESO=ON, PWRT=OFF, BOR=ON, BORV=27, WDT=ON
  CONFIG  WDTPS=512, MCLRE=ON, STVR=ON, LVP=OFF, DEBUG=OFF, CP0=OFF, CP1=OFF
  CONFIG  CPB=OFF, CPD=OFF, WRT0=OFF, WRT1=OFF, WRTC=OFF, WRTB=OFF, WRTD=OFF
  CONFIG  EBTR0=OFF, EBTR1=OFF, EBTRB=OFF
#ENDCONFIG
DEFINE OSC 8
DEFINE DEBUG_REG PORTA      ;debug
DEFINE DEBUG_BIT 0       
DEFINE DEBUG_BAUD 9600
DEFINE DEBUG_MODE 0
SSW var PortA.7
red var latB.5   ;use lat to avoid rmw
blue var latB.7     
green var latB.6
enable1 var latB.2
enable2 var latB.3
Startswitch var PortA.3
runtime var word
Thousandths var word     ; can measure upto 655 seconds in 10mS quanta
Seconds var word
now     var word
machine_state var byte ; possible states [ 0 lid up, 1 waiting for start button, 2 running, 3 ended]
old_state var byte
start    var byte
timer1_reload con    45543 ;10mS
DEFINE INTHAND   ticker          
goto   overasm
asm
ticker      ;tmr1 isr
    MOVE?CT 0, T1CON, TMR1ON ; 1 stop timer
    MOVLW LOW(_timer1_reload) ; 1 Add TimerReload to the 
    ADDWF TMR1L,F ; 1 value in Timer1
    BTFSC STATUS,C ; 1/2
    INCF TMR1H,F ; 1
    MOVLW HIGH(_timer1_reload) ; 1
    ADDWF TMR1H,F ; 1
    MOVE?CT 1, T1CON, TMR1ON ; 1 start timer
    banksel  _Thousandths
    INCF _Thousandths
    BTFSC STATUS,C 
    INCF _Thousandths+1
    BANKSEL PIR1      
    BCF PIR1   ,0     
    RETFIE            
    endasm 
overasm:
OSCCON = %01110000 
adcon1=$7f
TRISA = %11111000
TRISB = %00010011
PortB = 0
T1CON=0
TMR1H=$B1     ;10mS
TMR1L=$e7
PIE1.0=1 ' Enable TMR1 Interrupts
INTCON.6=1 ' Enable all unmasked Interrupts' 
lata.0=1   ;debug
clear
pause 1000          ;debug
Debug "Start",13 ,10   ;debug
'post
latB = 32
pause 200
latB =64
pause 200
latB =128
pause 200
latB =0 
 ;isr begin
T1CON=1   ;timer on
INTCON.7=1  
 
main:
    T1CON=0
    now  =  thousandths    ; get current time
    T1CON=1
    start=start<<1 + startswitch  ;debounce startswitch
    if SSW  then machine_state = 0     ; lid up emergency stop ??
    if old_state !=  machine_state then       ; manage changes of state
        Debug 13 ,10 , "new state "     ;debug
        select case   machine_state            ; the new state
        case 0                   ;lid up ,all stop
             latB = latB &  ~204    ; blue,green,ena,enb all off
             Debug dec 0                 ;debug
        case 1                   ;lid down read runtime then we are good to go
            runtime = 0    ; default if incorrect sw setting ie 0 2 3 16 17  
            if (PortB&19) == 18 then     ;runtime is sec * 100    read from dipsw on b0,1,4  
                runtime = 500                 '5
            elseif (PortB&19) == 19 then
                runtime = 9000                '90
            elseif (PortB&19) == 1 then
                runtime = 12000               '120
            endif  ;  validate  that  rt sw setting is valid  1 18 or 19 as not  all posdibilites have a result
            if   runtime == 0 then  machine_state = 0   ; abort invalid runtime  red slow flash
            if   machine_state then  red = 1                ;red on solid
            Debug dec 1,"rt = ",dec  runtime    ;debug
        case 2                       ; start button pressed
           latB =  140      ;ena,enb,blue on red off
           Seconds =  now          ;note starting time
           Debug dec 2
        case 3                     ;time up
           latB = 64      ;ena,enb,blue,red off  green on
           Debug dec 3                ;debug
        end select 
        old_state =  machine_state  
    endif
    select case   machine_state      ; manage current state
        case 0               ;monitor lid sw rapid flash red led
           if now//20==0 then  red = !red
           if ssw==0  then machine_state =1 
        case 1                   ;lid down monitor start button 
            if start == 255 then machine_state = 2
        case 2                ; running is time up ?
            if  now - Seconds > runtime then machine_state = 3 '[the beauty of unsigned subtraction}
    end select
    pause 10
goto main
end
 
				
Bookmarks