 ;for 12f675
 ;REGISTER DESCRIPTION

TMR0     EQU H'01'
PC       EQU H'02'
STATUS   EQU H'03'
FSR      EQU H'04'
INTCON   EQU H'0B'
OPTI     EQU H'81'
PCLATH   EQU H'0A'
PIE1     EQU H'8C'
PIR1     EQU H'0C'
OSSCAL   EQU H'90'
IOCB     EQU H'96'
CMCON    EQU H'19'
ADCON0   EQU H'1F'
ANSEL    EQU H'9F'
ADRESH   EQU H'1E'
T1CON    EQU H'10'
EEADR    EQU H'9B'
EEDATA   EQU H'9A'
EECON1   EQU H'9C'
EECON2   EQU H'9D'

;EECON1 REGISTER
RD        EQU H'0'
WR        EQU H'1'
WREN      EQU H'2'
WRERR     EQU H'3'

 ;IN\OUT REGISTER

GPIO     EQU H'05'
GPIO0    EQU H'0'
GPIO1    EQU H'1'
GPIO2    EQU H'2'
GPIO3    EQU H'3'
GPIO4    EQU H'4'
GPIO5    EQU H'5'

;DRIVIN IN\OUT REGISTER

TRISIO   EQU H'85'
;OPTI    REGISTER BITS

PS0      EQU H'0'
PS1      EQU H'1'
PS2      EQU H'2'
PSA      EQU H'3'
TOSE     EQU H'4'
TOCS     EQU H'5'
GPPU     EQU H'7'
        ;STATUS REGISTER BITS

C        EQU H'0'
DC       EQU H'1'
Z        EQU H'2'
PD       EQU H'3'
TO       EQU H'4'
RP0      EQU H'5'
       ;INTCON REGISTER BITS

RAIE     EQU H'3'
INTE     EQU H'4'
T0IE     EQU H'5'
PEIE     EQU H'6'
GIE      EQU H'7'
T0IF     EQU H'2'
  ;ADCON0 REGISTER BITS

ADON     EQU H'0'
GODONE   EQU H'1'
CHS0     EQU H'2'
CHS1     EQU H'3'
VCFG     EQU H'6'
ADFM     EQU H'7'
  ;PIR1
ADIF     EQU H'6'
 ;PORT INIT

INITG    EQU B'00011100'      ; 1= INPUT  0= OUTPUT
 ;MEMORY DESCRIPTION
M1       EQU H'20'            ;LOW BITE AC MEASURING
M2       EQU H'21'            ;HIGH BITE AC MEASURING
M3       EQU H'22'
M4       EQU H'23'
COUNT1   EQU H'24'            ; count1
STATST   EQU H'25'            ; STATUS TEMP
WT       EQU H'26'            ; W TEMP
M8       EQU H'27'            ; RESAULT
M9       EQU H'28'            ;INCREMENT TO ADC CALLING
M10      EQU H'29'
M11      EQU H'2A'            ;DECREMENT TO DELAY LOW
M12      EQU H'2B'            ;DECREMENT TO DELAY MID
M13      EQU H'2C'            ;DECREMENT TO DELAY HIGH 
M14      EQU H'2D'
M15      EQU H'2E'            ;HIGH BOND OF AC
M16      EQU H'2F'            ;LOW  BOND OF AC
M17  EQU H'30'
M18  EQU H'31'
M19  EQU H'32'
M20  EQU H'33'
M21  EQU H'34'
M22  EQU H'35'
M23  EQU H'36'
M24  EQU H'37'
M25  EQU H'38'
M26  EQU H'39'
M27  EQU H'3A'
M28  EQU H'3B'
M29  EQU H'3C'
M30  EQU H'3D'
M31  EQU H'3E'
M32  EQU H'3F'

  ;WORK BEGIN
          ORG H'00'           ;TO BEGIN
          GOTO BEG
          ORG H'04'           ;TO INTERRUPT

   ;INTERRUPT
          BCF    INTCON,GIE         ;INTERRUPT OFF
          MOVWF   WT          ; W > TO W-TEMP
          SWAPF   STATUS,0    ; STATUS >W WITH SWAPF
          MOVWF   STATST      ; TO STATUS TEMP

          BTFSC   M32,1       ;SINCRO TO ZER0
          GOTO    HJO         ;IF ZERO GOTO MEASURING OF MIDDLE AC LEVEL
          CALL    AIZM1       ;SEARCH ZERO 
          GOTO    JGT
          
HJO
          DECFSZ M9,1        ; WHILE 255 STEPS GOES ADC CALLING
          GOTO   GTJ
          CALL   AIZM         ;128- MEASUREMENT
          MOVLW   D'128'      ; WHEN 128 STEPS GONE
          MOVWF   M9          ; NEW STEPS
          MOVF   M2,0         ; PRE-RESAULT TO M8
          BCF    STATUS,C
          RLF    M2,0         ; FULL RESAULT - 1\128 OF SUMM 128 MEAS.
                              ; LOWEST BIT LOST!
          MOVWF  M8           ; SAVE RESALT
          CLRF   M1           ; - AND PREPARE TO ANOTHER MEASURING
          CLRF   M2
          
          
          BCF    M32,1        ;FALL FLAG OF AC ZERO TO BEGIN ANOTHER MEASURING               
                              ;WITH ZERO SEARCH
          GOTO   JGT
GTJ
          CALL   AIZM
JGT

          
          SWAPF   STATST,0    ; STATUS COME BACK
          MOVWF   STATUS
          SWAPF   WT,1        ; W COME BACK
          SWAPF   WT,0
          BCF     INTCON,T0IF
          BSF     INTCON,GIE  ;INTERRUPT ON

          RETFIE





          ORG H'100'
BEG
          CLRWDT
        ;register installation

         CLRF STATUS
         CLRF T1CON
         MOVLW   B'00000111'
         MOVWF   CMCON             ;comparators off

         BSF     STATUS,RP0        ;BANK 1 SELECT
         BCF     ANSEL,0           ;out (relay KEY)
         BCF     ANSEL,1           ;OUT (COMMON)
         BCF     ANSEL,2           ;IN DIGITAL (ADJ MODE)
         BSF     ANSEL,3           ;IN ANALOG  (AC LEVEL)
         BSF     ANSEL,4            ;F/16 - OSC ADC
         BCF     ANSEL,5
         BSF     ANSEL,6
         CLRF    PIE1              ;PERIFERIAL INTERRUPT DISABLE
         MOVLW   INITG
         MOVWF   TRISIO          ;PORT INIT TO IN\OUT
         BSF     OPTI,PS0        ;PRESCALER VALUE BIT0
         BSF     OPTI,PS1        ;PRESCALER VALUE BIT1
         BSF     OPTI,PS2        ;PRESCALER VALUE BIT2
         BSF     OPTI,PSA        ;PRESCALER ASSIGMENT WDT
         BCF     OPTI,TOCS       ;TAKT TMR0 INTERNAL OSC
         BSF     OPTI,GPPU       ;RESISTORS OFF
         BCF     STATUS,RP0      ;BANK 0 SELECT
         BSF     INTCON,GIE      ;ENABLE INTERRUPT
         BSF     INTCON,T0IE     ;ENABLEINTERRUPT BY TMR0
         BCF     ADCON0,VCFG     ;INTERNAL ADC-REF
         BSF     ADCON0,ADON     ;ADC ON
         BCF     ADCON0,ADFM     ;LEFT JUSTIFIED
         BCF     GPIO,GPIO5      ;
         BCF     GPIO,GPIO1      ;
         BCF     GPIO,GPIO0      ;RELAY OFF
         MOVLW   D'128'          ;COUNT NUMBER
         MOVWF   M9              ;DECREMENT OF COUNTER  
         CLRF M8                 ;MIDDLE OF AC LEVEL RESALT
         CLRF M1                 ;AIZM RESALT LOW
         CLRF M2                 ;AIZM RESALT HIGH
         MOVLW D'255'            ;ON DELAY LOW DECREMENT
         MOVWF M11
         MOVLW D'155'            ;ON DELAY MIDDLE DECREMENT
         MOVWF M12
         MOVLW D'2'              
         MOVWF M13               ;ON DELAY HIGH DECREMENT
         MOVLW D'10'
         MOVWF M30               ;PROGRAM KEY DELAY INCREMENT

         MOVLW D'101'
         MOVWF M16
         MOVF M16,0
         ADDLW D'4'
         MOVWF M15
         
         CLRF  M32              ;FLAG OF ZERO SEARCHING

         BSF    ADCON0,CHS0     ;GPIO4 - AC IZM
         BSF    ADCON0,CHS1




ALG
         
         CLRWDT
                         ; ON\OFF RELAY BY M8  - MIDDLE OF AC LEVEL
         MOVF  M8,0
         BCF STATUS,C
         SUBWF M15,0
         BTFSS STATUS,C
         GOTO  OFER      ; OFF RELAY BECAUSE AC TOO HIGH
         
FGRT                     ;
         MOVF  M8,0
         BCF STATUS,C
         SUBWF M16,0
         BTFSC STATUS,C
    
         GOTO  ONER       ;ON RELAY BECAUSE AC TOO SMALL
         GOTO  ALG      



AIZM                      ; MEASURING PARTIAL COUNTS OF AC
         CLRWDT
         BSF  GPIO,GPIO5  ;TEST BEG
         BSF  ADCON0,GODONE
GH0
         BTFSC ADCON0,GODONE
         GOTO  GH0
         MOVF  ADRESH,0
         BCF   STATUS,C
         ADDWF M1,1
         BTFSC STATUS,C
         INCF  M2,1
         BCF GPIO,GPIO5   ;TEST END
         RETURN
AIZM1                     ;SEARCHIG ZERO TO SINCRONYZE
         CLRWDT
         BCF  INTCON,GIE
         BSF  ADCON0,GODONE
GH1
         BTFSC ADCON0,GODONE
         GOTO  GH1
         
         ;BCF   STATUS,Z
         MOVF  ADRESH,0
         ;BTFSC STATUS,Z
         BCF   STATUS,C
         SUBLW D'03'
         BTFSC STATUS,C
         BSF   M32,1
         BTFSS M32,1
         GOTO  AIZM1
         BSF  INTCON,GIE
         RETURN

OFER                                   ; ON RELAY PROGRAM
         MOVLW D'255'
         MOVWF M11
         MOVLW D'155'
         MOVWF M12
         MOVLW D'2'
         MOVWF M13
         BCF   GPIO,GPIO0
         GOTO FGRT
ONER                                   ; OFF RELAY WITH DELAY PROGRAM
         DECFSZ M11
         GOTO ALG
         MOVLW D'255'
         MOVWF M11
         DECFSZ M12
         GOTO ALG
         MOVLW D'155'
         MOVWF M12
         DECFSZ M13
         GOTO ALG
         MOVLW D'2'
         MOVWF M13
         BSF   GPIO,GPIO0
         GOTO ALG

 








         END


