;**********************************************************************
;   This file is a basic code template for assembly code generation   *
;   on the PIC16F628A. This file contains the basic code              *
;   building blocks to build upon.                                    *
;                                                                     *
;   Refer to the MPASM User's Guide for additional information on     *
;   features of the assembler (Document DS33014).                     *
;                                                                     *
;   Refer to the respective PIC data sheet for additional             *
;   information on the instruction set.                               *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Filename:	    xxx.asm                                           *
;    Date:                                                            *
;    File Version:                                                    *
;                                                                     *
;    Author:                                                          *
;    Company:                                                         *
;                                                                     *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Files Required: P16F628A.INC                                     *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes:                                                           *
;                                                                     *
;**********************************************************************

	list      p=16f628A           ; list directive to define processor
	#include <p16F628A.inc>       ; processor specific variable definitions

	errorlevel  -302              ; suppress message 302 from list file

	__CONFIG   _CP_OFF & _DATA_CP_OFF & _LVP_OFF & _BOREN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _HS_OSC ; 20.000 MHz 

; '__CONFIG' directive is used to embed configuration word within .asm file.
; The lables following the directive are located in the respective .inc file.
; See data sheet for additional information on configuration word settings.

;***** VARIABLE DEFINITIONS
w_temp        EQU     0x7E        ; variable used for context saving 
status_temp   EQU     0x7F        ; variable used for context saving

TP1           equ     0x22         
TP2           equ     0x23
RADR          equ     0x24
RDATA         equ     0x25
RCONFIG       equ     0x26
RIRCONFIG     equ     0x27
TPIRL         equ     0x28
TPIRH         equ     0x29
COMP_Pause    equ     0x2A
P1            equ     0x2B
P2            equ     0x2C
P3            equ     0x2D 
;**********************************************************************
	ORG     0x000             ; processor reset vector
	goto    main              ; go to beginning of program
	
	ORG     0x004             ; interrupt vector location
	movwf   w_temp            ; save off current W register contents
	movf	STATUS,w          ; move status register into W register
	movwf	status_temp       ; save off contents of STATUS register

; isr code can go here or be located as a call subroutine elsewhere

	movf    status_temp,w     ; retrieve copy of STATUS register
	movwf	STATUS            ; restore pre-isr STATUS register contents
	swapf   w_temp,f
	swapf   w_temp,w          ; restore pre-isr W register contents
	retfie                    ; return from interrupt


main             clrw 
                 call    Pause1
                 clrf    TP1
                 clrf    TP2
                 clrf    P1
                 clrf    P2
                 clrf    P3
                 clrf    RADR
                 clrf    RDATA
                 clrf    RCONFIG   
                 clrf    RIRCONFIG  
                 clrf    TPIRL     
                 clrf    TPIRH  
                 clrf    COMP_Pause  
                 call    Ports
                 call    IR_G
                 goto    Process_Start
;********************************************************************** 
;*************************** Ports VRCON ****************************** 
;********************************************************************** 
Ports            clrf    PORTA
                 clrf    PORTB
                 bsf     STATUS,RP0
                 bcf     STATUS,RP1
                 movlw   b'00011111'
                 movwf   TRISA  
                 movlw   0x00
                 movwf   TRISB
                 movlw   b'10101100'
                 movwf   VRCON  
                 bcf     STATUS,RP0
                 return
;********************************************************************** 
;*************************** Comparators_I **************************** 
;********************************************************************** 
Comparators_I    bcf     INTCON,GIE
                 bcf     INTCON,PEIE
                 clrf    CMCON
                 bsf     CMCON,1

                 btfsc   RCONFIG,7
                 bsf     CMCON,5
                 btfss   RCONFIG,7
                 bcf     CMCON,5
                 btfsc   RCONFIG,6
                 bcf     CMCON,4
                 btfss   RCONFIG,6
                 bsf     CMCON,4
                
                 movlw   0x0A
                 movwf   COMP_Pause
Comparators_I_P  decfsz  COMP_Pause,f
                 goto    Comparators_I_P
     
                 bcf     PIR1,CMIF
                 bsf     STATUS,RP0
                 bcf     PIE1,CMIE
                 bcf     STATUS,RP0
                 bsf     INTCON,GIE
                 bsf     INTCON,PEIE
                 return  
;********************************************************************** 
;*************************** Comparators_B **************************** 
;********************************************************************** 
Comparators_B    bcf     INTCON,GIE
                 bcf     INTCON,PEIE
                 clrf    CMCON
                 bsf     CMCON,1
                 bsf     CMCON,3

                 movlw   0x0A
                 movwf   COMP_Pause
Comparators_B_P  decfsz  COMP_Pause,f
                 goto    Comparators_B_P

                 bcf     PIR1,CMIF
                 bsf     STATUS,RP0
                 bcf     PIE1,CMIE
                 bcf     STATUS,RP0
                 bsf     INTCON,GIE
                 bsf     INTCON,PEIE
                 return 
;********************************************************************** 
;***************************    IR_G     ****************************** 
;********************************************************************** 
IR_G             clrf    CCP1CON     
                 clrf    TMR2
                 movlw   0x45 ;69
                 movwf   CCPR1L    
                 clrf    INTCON    
                 bsf     STATUS,RP0
                 bcf     TRISB,3   
                 clrf    PIE1
                 movlw   0x8A ;138
                 movwf   PR2
                 bcf     STATUS,RP0                                        
                 clrf    PIR1
                 movlw   0x2C
                 movwf   CCP1CON
                 movlw   b'0000100'
                 movwf   T2CON
                 return 
;********************************************************************** 
;***************************   EEPROM    ****************************** 
;********************************************************************** 
W_EEPROM         movf    RDATA,W
                 bsf     STATUS,RP0 
                 bsf     EECON1,WREN 
                 movwf   EEDATA 
                 bcf     STATUS,RP0
                 movf    RADR,W
                 bsf     STATUS,RP0 
                 movwf   EEADR
                 bcf     INTCON,GIE
                 movlw   0x55
                 movwf   EECON2
                 movlw   0xAA
                 movwf   EECON2
                 bsf     EECON1,WR
                 bsf     INTCON,GIE
                 bcf     STATUS,RP0
                 call    R_EEPROM           
                 subwf   RDATA,W
                 btfss   STATUS,Z   
                 goto    W_EEPROM
                 return
;**********************************************************************
R_EEPROM         movf    RADR,W
                 bsf     STATUS,RP0
                 bcf     STATUS,RP1
                 movwf   EEADR
                 bsf     EECON1,RD
                 movf    EEDATA,W 
                 bcf     STATUS,RP0
                 return 
;**********************************************************************
;
; RCONFIG -> 0bit - Next
;            1bit - Setting 
;            2bir - IR_PAUSE
;            3bit - Logic-Fun   
;   LDE1     7bit -    (IR) NOT(IR)        
;   LED2     6bit -    (IN) NOT(IN)       
;   LED3     5bit - Fun(OR) Fun(AND) 
;   LED4     4bit -   (OUT) NOT(OUT)
; Action LED ------     Low High
;
;----------------------------------------------------------------------
;
; RIRCONFIG -> IR pause time 
;
;********************************************************************** 
;************************* Process_Start ****************************** 
;********************************************************************** 
Process_Start    bcf     PORTB,7
                 bcf     PORTB,6
                 bcf     PORTB,5
                 bcf     PORTB,4
                 call    Pause1
                 bsf     PORTB,7
                 call    Pause1
                 bcf     PORTB,7
                 bsf     PORTB,6
                 call    Pause1
                 bcf     PORTB,6
                 bsf     PORTB,5
                 call    Pause1
                 bcf     PORTB,5
                 bsf     PORTB,4
                 call    Pause1
                 bcf     PORTB,4
                 call    Pause1
;********************************************************************** 
;**************************** Var Load ******************************** 
;********************************************************************** 
                 movlw   0x00
                 movwf   RADR
                 call    R_EEPROM
                 movwf   RCONFIG
                 movlw   0x01
                 movwf   RADR
                 call    R_EEPROM
                 movwf   RIRCONFIG 
;**********************************************************************  
                 call    Load_RIRPAUSE
                 call    W_RIRPAUSE 
                 call    Pause1
                 bcf     PORTB,7
                 bcf     PORTB,6
                 bcf     PORTB,5
                 bcf     PORTB,4
                 call    Pause1
                 call    W_LED
;********************************************************************** 
;*************************   Process   ******************************** 
;********************************************************************** 
Process          bcf     RCONFIG,0 
                 bcf     RCONFIG,1  

                 call    Comparators_B
                 call    Button_N      ;Next 
                 call    Button_S      ;Setting

                 btfsc   RCONFIG,0
                 goto    Process_Set_RCONFIG  ;Setting LOGIC CONFIG
                 btfsc   RCONFIG,1
                 goto    Process_Set_RIRPAUSE ;Setting IR pause CONFIG 
                 call    Comparators_I 

                 bcf     RCONFIG,3
                 btfsc   RCONFIG,5
                 goto    Process_F_AND

                 btfsc   CMCON,7
                 bsf     RCONFIG,2

                 btfsc   RCONFIG,2
                 call    IR_PAUSE

                 btfsc   CMCON,6
                 bsf     RCONFIG,3

                 call    Process_F_OUT
                 goto    Process 

Process_F_AND    btfsc   CMCON,7
                 bsf     RCONFIG,2

                 btfsc   RCONFIG,2
                 call    IR_PAUSE

                 btfss   RCONFIG,3
                 goto    Process_F_AND_1

                 bcf     RCONFIG,3
                 btfsc   CMCON,6
                 bsf     RCONFIG,3

Process_F_AND_1  call    Process_F_OUT 
                 goto    Process 
;********************************************************************** 
Process_F_OUT    btfsc   RCONFIG,4 
                 goto    Process_F_NOTOUT
                 btfsc   RCONFIG,3
                 bcf     PORTB,2  
                 btfss   RCONFIG,3
                 bsf     PORTB,2 
                 return

Process_F_NOTOUT btfsc   RCONFIG,3
                 bsf     PORTB,2  
                 btfss   RCONFIG,3
                 bcf     PORTB,2 
                 return
;********************************************************************** 
;*********************** Process_Set_Start **************************** 
;**********************************************************************
Process_Set_RCONFIG 
                 bcf     PORTB,7
                 bcf     PORTB,6
                 bcf     PORTB,5
                 bcf     PORTB,4
                 call    Pause1
                 bsf     PORTB,4
                 call    Pause1
                 bcf     PORTB,4
                 bsf     PORTB,5
                 call    Pause1
                 bcf     PORTB,5
                 bsf     PORTB,6
                 call    Pause1
                 bcf     PORTB,6
                 bsf     PORTB,7
                 call    Pause1
                 bcf     PORTB,7
                 call    Pause1
;********************************************************************** 
                 call    W_LED
;********************************************************************** 
;*************************  Process_IR ******************************** 
;**********************************************************************
Process_IR       bcf     RCONFIG,0    
                 bcf     RCONFIG,1

                 call    Button_N      ;Next
                 call    Button_S      ;Setting

                 btfsc   RCONFIG,0
                 goto    Process_IN

                 btfsc   RCONFIG,1 
                 call    Process_IR_BitL
                 btfsc   RCONFIG,1 
                 call    Process_IR_BitH                
                 goto    Process_IR

Process_IR_BitL  btfss   RCONFIG,7   ;IR Setting 
                 return
                 bcf     RCONFIG,7
                 bcf     PORTB,7
                 goto    Process_IR            
 
Process_IR_BitH  btfsc   RCONFIG,7
                 return
                 bsf     RCONFIG,7
                 bsf     PORTB,7
                 goto    Process_IR
;********************************************************************** 
;*************************  Process_IN ******************************** 
;**********************************************************************
Process_IN       bcf     RCONFIG,0    
                 bcf     RCONFIG,1

                 call    Button_N      ;Next
                 call    Button_S      ;Setting

                 btfsc   RCONFIG,0
                 goto    Process_Fun

                 btfsc   RCONFIG,1 
                 call    Process_IN_BitL
                 btfsc   RCONFIG,1 
                 call    Process_IN_BitH                
                 goto    Process_IN

Process_IN_BitL  btfss   RCONFIG,6   ;IN Setting 
                 return
                 bcf     RCONFIG,6
                 bcf     PORTB,6
                 goto    Process_IN            
 
Process_IN_BitH  btfsc   RCONFIG,6
                 return
                 bsf     RCONFIG,6
                 bsf     PORTB,6
                 goto    Process_IN
;********************************************************************** 
;************************* Process_Fun ******************************** 
;**********************************************************************
Process_Fun      bcf     RCONFIG,0    
                 bcf     RCONFIG,1

                 call    Button_N      ;Next
                 call    Button_S     ;Setting

                 btfsc   RCONFIG,0
                 goto    Process_OUT

                 btfsc   RCONFIG,1 
                 call    Process_Fun_BitL
                 btfsc   RCONFIG,1 
                 call    Process_Fun_BitH                
                 goto    Process_Fun

Process_Fun_BitL btfss   RCONFIG,5   ;Fun Setting 
                 return
                 bcf     RCONFIG,5
                 bcf     PORTB,5
                 goto    Process_Fun            
 
Process_Fun_BitH btfsc   RCONFIG,5
                 return
                 bsf     RCONFIG,5
                 bsf     PORTB,5
                 goto    Process_Fun
;********************************************************************** 
;************************* Process_OUT ******************************** 
;**********************************************************************
Process_OUT      bcf     RCONFIG,0    
                 bcf     RCONFIG,1

                 call    Button_N      ;Next
                 call    Button_S      ;Setting

                 btfsc   RCONFIG,0
                 goto    Process_Set_COEND

                 btfsc   RCONFIG,1 
                 call    Process_OUT_BitL 
                 btfsc   RCONFIG,1 
                 call    Process_OUT_BitH                
                 goto    Process_OUT

Process_OUT_BitL btfss   RCONFIG,4   ;OUT Setting 
                 return
                 bcf     RCONFIG,4
                 bcf     PORTB,4
                 goto    Process_OUT            
 
Process_OUT_BitH btfsc   RCONFIG,4
                 return
                 bsf     RCONFIG,4
                 bsf     PORTB,4
                 goto    Process_OUT
;********************************************************************** 
;*********************** Process_Set_COEND **************************** 
;**********************************************************************
Process_Set_COEND 
                 movlw   0x00
                 movwf   RADR
                 movf    RCONFIG,W
                 movwf   RDATA
                 call    W_EEPROM
                 goto    Process_Start 
;********************************************************************** 
;********************* Process_Set_RIRPAUSE ***************************
;**********************************************************************
Process_Set_RIRPAUSE 
                 bsf     PORTB,7
                 bsf     PORTB,6
                 bsf     PORTB,5
                 bsf     PORTB,4
                 call    Pause1
                 bcf     PORTB,7
                 bcf     PORTB,6
                 bcf     PORTB,5
                 bcf     PORTB,4
                 call    Pause1
                 call    Load_RIRPAUSE

Process_Set_RIRPAUSE_1
                 bcf     RCONFIG,0    
                 bcf     RCONFIG,1

                 call    Button_N      ;Next
                 call    Button_S      ;Setting

                 btfsc   RCONFIG,0
                 goto    Process_Set_IREND

                 btfss   RCONFIG,1
                 goto    Process_Set_RIRPAUSE_1

                 incf    RIRCONFIG,f 
                 movlw   0x05
                 subwf   RIRCONFIG,w
                 btfss   STATUS,Z  
                 goto    Process_Set_RIRPAUSE
                 movlw   0x01
                 movwf   RIRCONFIG
                 goto    Process_Set_RIRPAUSE
;********************************************************************** 
;*********************** Process_Set_IREND **************************** 
;**********************************************************************
Process_Set_IREND 
                 movlw   0x01
                 movwf   RADR
                 movf    RIRCONFIG,W
                 movwf   RDATA
                 call    W_EEPROM
                 goto    Process_Start 
;**********************************************************************
;**************************   Buttons   *******************************
;**********************************************************************
Button_N         btfss   CMCON,7    ;Next
                 return
Button_N_1       nop 
                 nop
                 btfsc   CMCON,7
                 goto    Button_N_1
                 bsf     RCONFIG,0
                 return
;**********************************************************************
Button_S         btfss   CMCON,6    ;Setting
                 return
Button_S_1       nop
                 nop
                 btfsc   CMCON,6
                 goto    Button_S_1
                 bsf     RCONFIG,1                 
                 return
;**********************************************************************
;*************************     W_LED     ******************************
;**********************************************************************
W_LED            btfsc   RCONFIG,4
                 bsf     PORTB,4
                 btfss   RCONFIG,4
                 bcf     PORTB,4

                 btfsc   RCONFIG,5
                 bsf     PORTB,5
                 btfss   RCONFIG,5
                 bcf     PORTB,5

                 btfsc   RCONFIG,6
                 bsf     PORTB,6
                 btfss   RCONFIG,6
                 bcf     PORTB,6

                 btfsc   RCONFIG,7
                 bsf     PORTB,7
                 btfss   RCONFIG,7
                 bcf     PORTB,7
                 return 
;********************************************************************** 
;************************** Load_RIRPAUSE ***************************** 
;********************************************************************** 
Load_RIRPAUSE    movlw  0x01          ;IR_pause 1ms
                 subwf  RIRCONFIG,w
                 btfss  STATUS,Z  
                 goto   Load_RIRPAUSE_2 
                 bsf    PORTB,7      
                 movlw  0x22
                 movwf  TPIRL
                 movlw  0x01
                 movwf  TPIRH

Load_RIRPAUSE_2  movlw  0x02          ;IR_pause 50ms
                 subwf  RIRCONFIG,w
                 btfss  STATUS,Z 
                 goto   Load_RIRPAUSE_3  
                 bsf    PORTB,6     
                 movlw  0xFF
                 movwf  TPIRL
                 movlw  0x07
                 movwf  TPIRH

Load_RIRPAUSE_3  movlw  0x03          ;IR_pause 0.5s
                 subwf  RIRCONFIG,w
                 btfss  STATUS,Z  
                 goto   Load_RIRPAUSE_4  
                 bsf    PORTB,5     
                 movlw  0xFF
                 movwf  TPIRL
                 movlw  0x43
                 movwf  TPIRH

Load_RIRPAUSE_4  movlw  0x04           ;IR_pause 1.9s
                 subwf  RIRCONFIG,w
                 btfss  STATUS,Z  
                 return 
                 bsf    PORTB,4      
                 movlw  0xFF
                 movwf  TPIRL
                 movlw  0xFF
                 movwf  TPIRH
                 return
;********************************************************************** 
;**************************** W_RIRPAUSE ****************************** 
;********************************************************************** 
W_RIRPAUSE       movf    TPIRL,W
                 movwf   TP1 
                 movf    TPIRH,W
                 movwf   TP2   
                 return
;********************************************************************** 
;****************************   Pause1  ******************************* 
;********************************************************************** 
Pause1           clrf    P1
                 clrf    P2
                 movlw   0x05 
                 movwf   P3
Pause1_1         decfsz  P1,f
                 goto    Pause1_1
                 decfsz  P2,f
                 goto    Pause1_1
                 decfsz  P3,f
                 goto    Pause1_1
                 return    
;********************************************************************** 
;***************************   IR_PAUSE   ***************************** 
;**********************************************************************
IR_PAUSE         bsf     RCONFIG,3
                 decfsz  TP1,f
                 return   
                 decfsz  TP2,f
                 return  
                 bcf     RCONFIG,2
                 call    W_RIRPAUSE
                 return 
;**********************************************************************


; initialize eeprom locations

	ORG	0x2100
	DE	0x00, 0x01, 0x02, 0x03


	END                       ; directive 'end of program'

