     LIST P= 16F628
     INCLUDE "P16F628.INC"
     RADIX DEC
     ORG  0X0000
     CLRF  STATUS
     GOTO PROGRAM_START
     ORG 0X0004
     ; SAVE CONTEXT (PIC REGISTERS)
     MOVWF   W_TEMP
     SWAPF   STATUS, W
     CLRF    STATUS
     MOVWF   STATUS_TEMP
     MOVF    PCLATH, W
     MOVWF   PCLATH_TEMP
     MOVF    FSR, W
     MOVWF   FSR_TEMP
     CLRF    PCLATH
     BCF    STATUS, RP1
     BCF    STATUS, RP0
     ; SAVE CONTEXT (COMPILER'S VARIABLES)
     MOVF   FSR_TEMP, W
     MOVWF  TEMP_FSR_TEMP
     MOVF   AUX3_H, W
     MOVWF  TEMP_AUX3_H
     MOVF   AUX3_L, W
     MOVWF  TEMP_AUX3_L
     MOVF   AUX4_H, W
     MOVWF  TEMP_AUX4_H
     MOVF   AUX4_L, W
     MOVWF  TEMP_AUX4_L
     MOVF   AUX5_H, W
     MOVWF  TEMP_AUX5_H
     MOVF   AUX5_L, W
     MOVWF  TEMP_AUX5_L
     MOVF   AUX6_L, W
     MOVWF  TEMP_AUX6_L
     MOVF   AUX6_H, W
     MOVWF  TEMP_AUX6_H
     MOVF   AUX7_L, W
     MOVWF  TEMP_AUX7_L
     MOVF   AUX1_L, W
     MOVWF  TEMP_AUX1_L
     MOVF   AUX1_H, W
     MOVWF  TEMP_AUX1_H
     MOVF   AUX2_L, W
     MOVWF  TEMP_AUX2_L
     MOVF   AUX2_H, W
     MOVWF  TEMP_AUX2_H
     MOVF   AUX, W
     MOVWF  TEMP_AUX
     MOVF   S0, W
     MOVWF  TEMP_S0
     MOVF   S1, W
     MOVWF  TEMP_S1
     MOVF   S2, W
     MOVWF  TEMP_S2
     ; CHECK WHICH INTERRUPT OCCURED
     BTFSC   INTCON, INTF
     GOTO    EXT_INTERRUPT_ROUTINE
     ; RESTORE CONTEXT (COMPILER'S VARIABLES)
RESTORE_CONTEXT
     MOVF   TEMP_FSR_TEMP, W
     MOVWF  FSR_TEMP
     MOVF   TEMP_AUX3_H, W
     MOVWF  AUX3_H
     MOVF   TEMP_AUX3_L, W
     MOVWF  AUX3_L
     MOVF   TEMP_AUX4_H, W
     MOVWF  AUX4_H
     MOVF   TEMP_AUX4_L, W
     MOVWF  AUX4_L
     MOVF   TEMP_AUX5_H, W
     MOVWF  AUX5_H
     MOVF   TEMP_AUX5_L, W
     MOVWF  AUX5_L
     MOVF   TEMP_AUX6_L, W
     MOVWF  AUX6_L
     MOVF   TEMP_AUX6_H, W
     MOVWF  AUX6_H
     MOVF   TEMP_AUX7_L, W
     MOVWF  AUX7_L
     MOVF   TEMP_AUX1_L, W
     MOVWF  AUX1_L
     MOVF   TEMP_AUX1_H, W
     MOVWF  AUX1_H
     MOVF   TEMP_AUX2_L, W
     MOVWF  AUX2_L
     MOVF   TEMP_AUX2_H, W
     MOVWF  AUX2_H
     MOVF   TEMP_AUX, W
     MOVWF  AUX
     MOVF   TEMP_S0, W
     MOVWF  S0
     MOVF   TEMP_S1, W
     MOVWF  S1
     MOVF   TEMP_S2, W
     MOVWF  S2
     ; RESTORE CONTEXT (PIC REGISTERS)
     MOVF    FSR_TEMP, W
     MOVWF   FSR
     MOVF    PCLATH_TEMP, W
     MOVWF   PCLATH
     SWAPF   STATUS_TEMP, W
     MOVWF   STATUS
     SWAPF   W_TEMP, F
     SWAPF   W_TEMP, W
     RETFIE
     ; HEADER END
W_TEMP  EQU 0X7F
STATUS_TEMP EQU  0X20
PCLATH_TEMP EQU  0X21
FSR_TEMP EQU  0X22
AUX3_H EQU  0X23
AUX3_L EQU  0X24
AUX4_H EQU  0X25
AUX4_L EQU  0X26
AUX5_H EQU  0X27
AUX5_L EQU  0X28
AUX6_L EQU  0X29
AUX6_H EQU  0X2A
AUX7_L EQU  0X2B
AUX1_L EQU  0X2C
AUX1_H EQU  0X2D
AUX2_L EQU  0X2E
AUX2_H EQU  0X2F
AUX EQU  0X30
S0 EQU  0X31
S1 EQU  0X32
S2 EQU  0X33
TEMP_FSR_TEMP EQU  0X34
TEMP_AUX3_H EQU  0X35
TEMP_AUX3_L EQU  0X36
TEMP_AUX4_H EQU  0X37
TEMP_AUX4_L EQU  0X38
TEMP_AUX5_H EQU  0X39
TEMP_AUX5_L EQU  0X3A
TEMP_AUX6_L EQU  0X3B
TEMP_AUX6_H EQU  0X3C
TEMP_AUX7_L EQU  0X3D
TEMP_AUX1_L EQU  0X3E
TEMP_AUX1_H EQU  0X3F
TEMP_AUX2_L EQU  0X40
TEMP_AUX2_H EQU  0X41
TEMP_AUX EQU  0X42
TEMP_S0 EQU  0X43
TEMP_S1 EQU  0X44
TEMP_S2 EQU  0X45
PAUSES EQU  0X46
PULSES_H EQU  0X47
PULSES_L EQU  0X48
PUL_DIV_2_H EQU  0X49
PUL_DIV_2_L EQU  0X4A
ADD16
     MOVF    AUX2_L, W
     ADDWF   AUX1_L, F
     BTFSC   STATUS, C
     INCF    AUX2_H, F
     MOVF    AUX2_H, W
     ADDWF   AUX1_H, F
     RETLW   0
DIVU16
     MOVF    AUX2_H, W
     MOVWF   AUX3_H
     MOVF    AUX2_L, W
     MOVWF   AUX3_L
     CLRF    AUX4_H
     CLRF    AUX4_L
     MOVLW   0X10
     MOVWF   AUX7_L
     RLF     AUX3_L, F
     RLF     AUX3_H, F
     RLF     AUX4_L, F
     RLF     AUX4_H, F
     BCF     AUX3_L, 0
DIVU16_LOOP
     MOVF    AUX1_L, W
     MOVWF   AUX5_L
     MOVF    AUX1_H, W
     MOVWF   AUX5_H
     MOVF    AUX4_L, W
     MOVWF   AUX6_L
     MOVF    AUX4_H, W
     MOVWF   AUX6_H
     MOVF    AUX5_L, W
     SUBWF   AUX4_L, F
     BTFSC   STATUS, C
     GOTO    DIVU16_SUB_H
     BTFSC   STATUS, Z
     GOTO    DIVU16_SUB_H
     INCF    AUX5_H, F
DIVU16_SUB_H
     MOVF    AUX5_H, W
     SUBWF   AUX4_H, F
     BTFSC   STATUS, C
     GOTO    DIVU16_REM_POS
     BTFSC   STATUS, Z
     GOTO    DIVU16_REM_POS
     MOVF    AUX6_L, W
     MOVWF   AUX4_L
     MOVF    AUX6_H, W
     MOVWF   AUX4_H
     RLF     AUX3_L, F
     RLF     AUX3_H, F
     RLF     AUX4_L, F
     RLF     AUX4_H, F
     BCF     AUX3_L, 0
     DECFSZ  AUX7_L, F
     GOTO    DIVU16_LOOP
     BCF     STATUS, C
     RRF     AUX4_H, F
     RRF     AUX4_L, F
     MOVF    AUX3_H, W
     MOVWF   AUX1_H
     MOVF    AUX3_L, W
     MOVWF   AUX1_L
     RETLW   0
DIVU16_REM_POS
     RLF     AUX3_L, F
     RLF     AUX3_H, F
     RLF     AUX4_L, F
     RLF     AUX4_H, F
     BSF     AUX3_L, 0
     DECFSZ  AUX7_L, F
     GOTO    DIVU16_LOOP
     BCF     STATUS, C
     RRF     AUX4_H, F
     RRF     AUX4_L, F
     MOVF    AUX3_H, W
     MOVWF   AUX1_H
     MOVF    AUX3_L, W
     MOVWF   AUX1_L
     RETLW   0
GREU8
     MOVF    AUX1_L, W
     SUBWF   AUX, W
     BTFSC   STATUS, C
     GOTO    GREU8_CLEAR_W
     BTFSC   STATUS, Z
     GOTO    GREU8_CLEAR_W
     MOVLW   0X01
     MOVWF   AUX
     RETLW   0
GREU8_CLEAR_W
     CLRF    AUX
     RETLW   0
EQU16
     MOVF    AUX2_H, W
     SUBWF   AUX1_H, W
     BTFSS   STATUS, Z
     GOTO    EQU16_CLEAR_W
     MOVF    AUX2_L, W
     SUBWF   AUX1_L, W
     BTFSS   STATUS, Z
     GOTO    EQU16_CLEAR_W
     MOVLW   0X01
     MOVWF   AUX
     RETLW   0
EQU16_CLEAR_W
     CLRF    AUX
     RETLW   0
PROGRAM_START
     GOTO   SKIP_IR_EXT_BLOCK
EXT_INTERRUPT_ROUTINE
     BCF    STATUS, RP1
     BCF    STATUS, RP0
     MOVLW  1
     ADDWF  PULSES_L, F
     BTFSC  STATUS, C
     INCF   PULSES_H, F
     BCF    INTCON, INTF
     GOTO   RESTORE_CONTEXT
SKIP_IR_EXT_BLOCK
     BCF    STATUS, RP1
     BCF    STATUS, RP0
     ;
     ; CONFIGURE PORTS, RB0 IS ST-INPUT
     ; RB3 IS RESERVED FOR THE PWM
     ;
     MOVLW  B'00000000'
     BSF    STATUS, RP0
     MOVWF  TRISA
     MOVLW  B'00000001'
     MOVWF  TRISB
     ;
     ; WAIT A BIT
     ;
     MOVLW  33;*
     BCF    STATUS, RP0
     MOVWF  AUX2_L
DELAYLAB31;*
     MOVLW  248;*
     MOVWF  AUX1_H;*
DELAYLAB21;*
     MOVLW  185;*
     MOVWF  AUX1_L;*
DELAYLAB11;*
     DECFSZ AUX1_L, F;*
     GOTO   DELAYLAB11;*
     DECFSZ AUX1_H, F;*
     GOTO   DELAYLAB21;*
     DECFSZ AUX2_L, F;*
     GOTO   DELAYLAB31;*
     MOVLW  68;*
     MOVWF  S0;*
DELAYLAB61;*
     DECFSZ S0, F;*
     GOTO   DELAYLAB61;*
     NOP;*
     NOP;*
     ;
     ; CLEAR VARIABLES
     ;
     CLRF   PULSES_L
     CLRF   PULSES_H
     CLRF   PAUSES
     ;
     ; CONFIGUE AND ENABLE RB0 EDGE-INTERRUPT
     ;
     BSF    OPTION_REG, INTEDG
     BSF    INTCON, PEIE
     BSF    INTCON, INTE
     BSF    INTCON, GIE
     BSF    INTCON, PEIE
     ;
     ; CONFIGURE AND ENABLE PWM
     ;
     CLRF   TMR2
     MOVLW  255
     BSF    STATUS, RP0
     MOVWF  PR2
     MOVLW  0
     BCF    STATUS, RP0
     MOVWF  CCPR1L
     CLRF   CCP1CON
     MOVLW  B'00001111'
     IORWF  CCP1CON, F
     MOVLW  0
     IORWF  CCP1CON, F
     BSF    STATUS, RP0
     BCF    TRISB, 3     ;<---- CHFlashBasic bug (2 is not 3)
     BCF    STATUS, RP0
     BSF    T2CON, TMR2ON
     BSF    STATUS, RP0
     BCF    PIE1, TMR2IE
     ;
     ;
     ; MAIN LOOP
     ;
LOOP
     BCF    STATUS, RP1
     BSF    STATUS, RP0
     ; WAITING 1/4SEC PROVIDES US A 4HZ RESOLUTION
     MOVLW  16;*
     BCF    STATUS, RP0
     MOVWF  AUX2_L
DELAYLAB32;*
     MOVLW  115;*
     MOVWF  AUX1_H;*
DELAYLAB22;*
     MOVLW  206;*
     MOVWF  AUX1_L;*
DELAYLAB12;*
     DECFSZ AUX1_L, F;*
     GOTO   DELAYLAB12;*
     DECFSZ AUX1_H, F;*
     GOTO   DELAYLAB22;*
     DECFSZ AUX2_L, F;*
     GOTO   DELAYLAB32;*
     MOVLW  19;*
     MOVWF  S0;*
DELAYLAB62;*
     DECFSZ S0, F;*
     GOTO   DELAYLAB62;*
     NOP;*
     NOP;*
     NOP;*
     MOVF   PULSES_L, W
     MOVWF  AUX1_L
     MOVF   PULSES_H, W
     MOVWF  AUX1_H
     MOVF   AUX1_L, W
     MOVWF  S1
     MOVF   AUX1_H, W
     MOVWF  S2
     MOVLW  0
     MOVWF  AUX1_L
     MOVLW  0
     MOVWF  AUX1_H
     MOVF   S2, W
     MOVWF  AUX2_H
     MOVF   S1, W
     MOVWF  AUX2_L
     CALL   EQU16
     BCF    STATUS, RP1
     BCF    STATUS, RP0
     MOVLW  1
     XORWF  AUX, F
     MOVF   AUX, W
     BTFSS  AUX, 0
     GOTO   ENDIFLAB3
     ; LED-8BIT OUTPUT AT 8HZ
     ;
     MOVF   PULSES_L, W
     MOVWF  AUX1_L
     MOVF   PULSES_H, W
     MOVWF  AUX1_H
     MOVF   AUX1_L, W
     MOVWF  S1
     MOVF   AUX1_H, W
     MOVWF  S2
     MOVLW  2
     MOVWF  AUX1_L
     MOVLW  0
     MOVWF  AUX1_H
     MOVF   S2, W
     MOVWF  AUX2_H
     MOVF   S1, W
     MOVWF  AUX2_L
     CALL   DIVU16
     BCF    STATUS, RP1
     BCF    STATUS, RP0
     MOVF   AUX1_L, W
     MOVWF  PUL_DIV_2_L
     MOVF   AUX1_H, W
     MOVWF  PUL_DIV_2_H
     MOVF PUL_DIV_2_L,W;*
     ANDLW B'11110110';*
     MOVWF PORTB;*
     MOVF PUL_DIV_2_L,W;*
     ANDLW B'00001001';*
     MOVWF PORTA;*
     ;
     ; PWM AT 4HZ
     ; THE NEXT LINE IS TO OVERCOME RCX-10K PULL-UP
     MOVF   PULSES_L, W
     MOVWF  AUX1_L
     MOVF   PULSES_H, W
     MOVWF  AUX1_H
     MOVF   AUX1_L, W
     MOVWF  S1
     MOVF   AUX1_H, W
     MOVWF  S2
     MOVLW  0X00
     MOVWF  AUX1_L
     MOVLW  0X1
     MOVWF  AUX1_H
     MOVF   S2, W
     MOVWF  AUX2_H
     MOVF   S1, W
     MOVWF  AUX2_L
     CALL   ADD16
     BCF    STATUS, RP1
     BCF    STATUS, RP0
     MOVF   AUX1_L, W
     MOVWF  PULSES_L
     MOVF   AUX1_H, W
     MOVWF  PULSES_H
     MOVF PULSES_H,W;*
     ANDLW B'00000011';*
     MOVWF PULSES_H;*
     ANDLW B'00000011';*
     MOVF    PULSES_H, W
     MOVWF   AUX1_H
     MOVF    PULSES_L, W
     MOVWF   AUX1_L
     BTFSC   AUX1_L, 0
     BSF     CCP1CON, 4
     BCF     CCP1CON, 4
     BTFSC   AUX1_L, 1
     BSF     CCP1CON, 5
     BCF     CCP1CON, 5
     RRF     AUX1_H, F
     RRF     AUX1_L, F
     RRF     AUX1_H, F
     RRF     AUX1_L, F
     MOVF    AUX1_L, W
     MOVWF   CCPR1L
     CLRF   PULSES_L
     CLRF   PULSES_H
     GOTO   ELSELAB3
ENDIFLAB3
     BCF    STATUS, RP1
     BCF    STATUS, RP0
     ; WAIT 1SEC TO RESET IF 0 HZ
     INCF   PAUSES, F
     MOVF   PAUSES, W
     MOVWF  S1
     MOVLW  2
     MOVWF  AUX
     MOVF   S1, W
     MOVWF  AUX1_L
     CALL   GREU8
     BCF    STATUS, RP1
     BCF    STATUS, RP0
     MOVF   AUX, W
     BTFSS  AUX, 0
     GOTO   ENDIFLAB4
     CLRF   PAUSES
     MOVLW  B'00000000'
     MOVWF  PORTA
     MOVLW  B'00000000'
     MOVWF  PORTB
     MOVLW  0
     MOVWF  CCPR1L
     MOVLW  B'00001111'
     ANDWF  CCP1CON, F
     MOVLW  0
     IORWF  CCP1CON, F
     BSF    STATUS, RP0
     BCF    TRISB, 2
     BCF    STATUS, RP0
     CLRF   PULSES_L
     CLRF   PULSES_H
ENDIFLAB4
     BCF    STATUS, RP1
     BCF    STATUS, RP0
ELSELAB3
     BCF    STATUS, RP1
     BCF    STATUS, RP0
     ;
     GOTO   LOOP
     END
