;******************************************************************************************
;  PIC 16F88 Sample Program
;   Project      :
;   Program Name :
;   Programer    :
;   Create date  :  Version 1.0    H19.06.01
;   History      :
;******************************************************************************************
;  (1)Description
;     
;******************************************************************************************
	LIST	P=PIC16F88,ST=OFF,R=DEC
	INCLUDE	"P16F88.inc"	
	__CONFIG _CONFIG1 , _CP_OFF & _CCP1_RB0 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_OFF & _PWRTE_OFF & _WDT_OFF & _INTRC_IO
;	__CONFIG _CONFIG1 , _CP_OFF & _CCP1_RB0 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_OFF & _PWRTE_OFF & _WDT_OFF & _HS_OSC
	__CONFIG _CONFIG2 , _IESO_OFF & _FCMEN_OFF
;
LEDLST	EQU	20H
LEDY	EQU	21H
WAT1	EQU	22H
WAT2	EQU	23H
LPM	EQU	24H
LPCNT	EQU	25H

	ORG	0
	GOTO	START
	ORG	4
	GOTO	START
START
; ===========================================
;   1.Initialized
; ===========================================
;  (1)Initialized Port 
; Bank0での設定
	BANKSEL	PORTA	; PortAのBankを選択する
	CLRF	PORTA	; PortAの初期化
	CLRF	PORTB	; PortBの初期化

; Bank1での設定
	BANKSEL	ANSEL	; ANSELのBankを選択する(Bank1)
	MOVLW	B'01111110'	;内部OSC選択,内部OSCに8MHzを選択
	MOVWF	OSCCON
	MOVLW	B'00000000'	; 全てをDegitalに設定
	MOVWF	ANSEL	; Degital Inputに設定(Bank1)
	MOVLW	B'11100000'
	MOVWF	TRISA	; PortAのI/O設定(Bank1)
	MOVLW	B'00000000'
	MOVWF	TRISB	; PortBのI/O設定(Bank1)
; Bank0に戻す
	BANKSEL	PORTA	; PortAのBankを選択する
;
;  (2)Initialized LED-list
	MOVLW	B'00000001'
	MOVWF	LEDLST
	MOVWF	PORTB
; 
	MOVLW	B'00000001'
	MOVWF	LEDY 
	MOVWF	PORTA
; ===========================================
;   2.Main loop
; ===========================================
	MOVLW	H'26'
	MOVWF	LPCNT	
MAIN
;  (1)Wait push-sw
	MOVLW	H'01'
	ADDWF	LPCNT,F
	MOVFW	LPCNT
	XORLW	H'E4'
	BTFSS	STATUS,Z
	GOTO	MAIN2
	MOVLW	H'26'
	MOVWF	LPCNT	
MAIN2
	BTFSS	PORTA,5
	GOTO	MAIN
;  (2)Demo loop
	MOVLW	H'BE'
MAIN3
	CALL	MVLED
	CALL	SETLED
	CALL	WAT
	DECFSZ	LPM,F
	GOTO	MAIN3
;  (3)Main loop
	MOVFW	LPCNT
	MOVWF	LPM
MAIN4
	CALL	MVLED
	CALL	SETLED
	CALL	WATW2
	DECFSZ	LPM,F
	GOTO	MAIN4

	GOTO	MAIN

;******************************************************************************************
;   Function Name : MVLED
;   Title         : 
;   Input         :
;   Output        :
;******************************************************************************************
MVLED
	MOVFW	LEDLST
	XORLW	H'01'
	BTFSC	STATUS,Z
	GOTO	MVLED1
; 1.Case LEDLST != 1
	RRF	LEDLST,F
	RETURN
MVLED1
; 2.Case LEDLST = 1
	MOVLW	H'80'
	MOVWF	LEDLST
	MOVFW	LEDY
	XORLW	H'04'
	BTFSC	STATUS,Z
	GOTO	MVLED3
; 
	MOVFW	LEDY
	XORLW	H'10'
	BTFSC	STATUS,Z
	GOTO	MVLED2
; 2-1)Case LEDLST = 1 AND ( LEDY != 10 AND LEDY != 04 )
	RLF	LEDY,F
	RETURN
MVLED2
; 2-2)Case LEDLST = 1 AND LEDY = 10
	MOVLW	H'01'
	MOVWF	LEDY
	RETURN
MVLED3
; 2-3)Case LEDLST = 1 AND LEDY = 04
	MOVLW	H'20'
	MOVWF	LEDLST
	MOVLW	H'08'
	MOVWF	LEDY
	RETURN

;******************************************************************************************
;   Function Name : SETLED
;   Title         : 
;   Input         :
;   Output        :
;******************************************************************************************
SETLED
	MOVFW	LEDLST
	MOVWF	PORTB
	MOVFW	LEDY
	MOVWF	PORTA
	RETURN

;******************************************************************************************
;   Function Name : WATW2
;   Title         : 
;   Input         :
;   Output        :
;******************************************************************************************
WATW2
	CALL	WAT
	CALL	WAT
	CALL	WAT
	CALL	WAT
	RETURN

;******************************************************************************************
;   Function Name : WAT
;   Title         : 
;   Input         :
;   Output        :
;******************************************************************************************
WAT
	MOVLW	H'20'
	MOVWF	WAT2
WATB
	MOVLW	H'FF'
	MOVWF	WAT1
WATA
	DECFSZ	WAT1,F
	GOTO	WATA
	DECFSZ	WAT2,F
	GOTO	WATB
	RETURN

	END