; ========================== ここから ==============================
;	PIC16F88
;	PORTA	RA2: CS
;			RA3: CLK
;			RA4; DATA
;			RA0,1,6,7;	CdS
;	PORTB	RB0-RB7 CdS
	LIST      P=16F88
	#INCLUDE  P16F88.INC
	__CONFIG  _CONFIG1, _CP_OFF & _WDT_OFF & _BODEN_OFF & _MCLR_OFF & _PWRTE_ON & _INTRC_IO & _LVP_OFF & _CPD_OFF & _DEBUG_OFF
; serial
SER_PORT		EQU	PORTA
SER_CS		EQU	2
SER_CLK		EQU	3
SER_DATA		EQU	4
; CdS #1
CDS_01PORT	EQU	PORTA
CDS_01PIN		EQU	1
; CdS #2
CDS_02PORT	EQU	PORTA
CDS_02PIN		EQU	0
; CdS #3
CDS_03PORT	EQU	PORTA
CDS_03PIN		EQU	7
; CdS #4
CDS_04PORT	EQU	PORTA
CDS_04PIN		EQU	6
; CdS #5
CDS_05PORT	EQU	PORTB
CDS_05PIN		EQU	7
; CdS #6
CDS_06PORT	EQU	PORTB
CDS_06PIN		EQU	6
; CdS #7
CDS_07PORT	EQU	PORTB
CDS_07PIN		EQU	5
; CdS #8
CDS_08PORT	EQU	PORTB
CDS_08PIN		EQU	4
; CdS #9
CDS_09PORT	EQU	PORTB
CDS_09PIN		EQU	3
; CdS #10
CDS_10PORT	EQU	PORTB
CDS_10PIN		EQU	2
; CdS #11
CDS_11PORT	EQU	PORTB
CDS_11PIN		EQU	1
; CdS #12
CDS_12PORT	EQU	PORTB
CDS_12PIN		EQU	0


	ORG	0
	GOTO	init
; -----------------------------------------------------
; 初期処理
init

	BSF		STATUS,RP0	; BANK1
	MOVLW	B'01110100'	; 8MHz
	MOVWF	OSCCON
	MOVLW	B'0011111'	; 最高速
	MOVWF	OSCTUNE

	; PIN I/O 設定
	MOVLW	B'11101111'
	MOVWF	TRISA		;RA4は出力
	MOVLW	B'11111111'
	MOVWF	TRISB		;RB0-7は入力
	MOVLW	B'00000000'
	MOVWF	ADCON0		;AD未使用
	MOVLW	B'00000000'	
	MOVWF	ANSEL		;ANALOG未使用
	BCF		STATUS,RP0	; BANK0
	CLRF	PORTA
	CLRF	PORTB
; -----------------------------------------------------
main
cs_loop
	BTFSS	SER_PORT,SER_CS	; wait CS high
	GOTO	cs_loop
cds_01
	BTFSS	SER_PORT,SER_CS	; if CS low continue
	GOTO	cs_loop
	BTFSS	SER_PORT,SER_CLK		; wait clk up
	GOTO	cds_01
	BTFSS	CDS_01PORT,CDS_01PIN	; CdS #1
	GOTO	cds_01_off
	BCF		SER_PORT,SER_DATA		; on
	GOTO	cds_01_next
cds_01_off
	BSF		SER_PORT,SER_DATA		; off
cds_01_next
	BTFSC	SER_PORT,SER_CLK		; wait clk down
	GOTO	cds_01_next

cds_02
	BTFSS	SER_PORT,SER_CS	; if CS low continue
	GOTO	cs_loop
	BTFSS	SER_PORT,SER_CLK		; wait clk up
	GOTO	cds_02
	BTFSS	CDS_02PORT,CDS_02PIN	; CdS #2
	GOTO	cds_02_off
	BCF		SER_PORT,SER_DATA		; on
	GOTO	cds_02_next
cds_02_off
	BSF		SER_PORT,SER_DATA		; off
cds_02_next
	BTFSC	SER_PORT,SER_CLK		; wait clk down
	GOTO	cds_02_next

cds_03
	BTFSS	SER_PORT,SER_CS	; if CS low continue
	GOTO	cs_loop
	BTFSS	SER_PORT,SER_CLK		; wait clk up
	GOTO	cds_03
	BTFSS	CDS_03PORT,CDS_03PIN	; CdS #3
	GOTO	cds_03_off
	BCF		SER_PORT,SER_DATA		; on
	GOTO	cds_03_next
cds_03_off
	BSF		SER_PORT,SER_DATA		; off
cds_03_next
	BTFSC	SER_PORT,SER_CLK		; wait clk down
	GOTO	cds_03_next

cds_04
	BTFSS	SER_PORT,SER_CS		; if CS low continue
	GOTO	cs_loop
	BTFSS	SER_PORT,SER_CLK		; wait clk up
	GOTO	cds_04
	BTFSS	CDS_04PORT,CDS_04PIN	; CdS #4
	GOTO	cds_04_off
	BCF		SER_PORT,SER_DATA		; on
	GOTO	cds_04_next
cds_04_off
	BSF		SER_PORT,SER_DATA		; off
cds_04_next
	BTFSC	SER_PORT,SER_CLK		; wait clk down
	GOTO	cds_04_next

cds_05
	BTFSS	SER_PORT,SER_CS		; if CS low continue
	GOTO	cs_loop
	BTFSS	SER_PORT,SER_CLK		; wait clk up
	GOTO	cds_05
	BTFSS	CDS_05PORT,CDS_05PIN	; CdS #5
	GOTO	cds_05_off
	BCF		SER_PORT,SER_DATA		; on
	GOTO	cds_05_next
cds_05_off
	BSF		SER_PORT,SER_DATA		; off
cds_05_next
	BTFSC	SER_PORT,SER_CLK		; wait clk down
	GOTO	cds_05_next

cds_06
	BTFSS	SER_PORT,SER_CS		; if CS low continue
	GOTO	cs_loop
	BTFSS	SER_PORT,SER_CLK		; wait clk up
	GOTO	cds_06
	BTFSS	CDS_06PORT,CDS_06PIN	; CdS #6
	GOTO	cds_06_off
	BCF		SER_PORT,SER_DATA		; on
	GOTO	cds_06_next
cds_06_off
	BSF		SER_PORT,SER_DATA		; off
cds_06_next
	BTFSC	SER_PORT,SER_CLK		; wait clk down
	GOTO	cds_06_next

cds_07
	BTFSS	SER_PORT,SER_CS		; if CS low continue
	GOTO	cs_loop
	BTFSS	SER_PORT,SER_CLK		; wait clk up
	GOTO	cds_07
	BTFSS	CDS_07PORT,CDS_07PIN	; CdS #7
	GOTO	cds_07_off
	BCF		SER_PORT,SER_DATA		; on
	GOTO	cds_07_next
cds_07_off
	BSF		SER_PORT,SER_DATA		; off
cds_07_next
	BTFSC	SER_PORT,SER_CLK		; wait clk down
	GOTO	cds_07_next

cds_08
	BTFSS	SER_PORT,SER_CS		; if CS low continue
	GOTO	cs_loop
	BTFSS	SER_PORT,SER_CLK		; wait clk up
	GOTO	cds_08
	BTFSS	CDS_08PORT,CDS_08PIN	; CdS #8
	GOTO	cds_08_off
	BCF		SER_PORT,SER_DATA		; on
	GOTO	cds_08_next
cds_08_off
	BSF		SER_PORT,SER_DATA		; off
cds_08_next
	BTFSC	SER_PORT,SER_CLK		; wait clk down
	GOTO	cds_08_next

cds_09
	BTFSS	SER_PORT,SER_CS		; if CS low continue
	GOTO	cs_loop
	BTFSS	SER_PORT,SER_CLK		; wait clk up
	GOTO	cds_09
	BTFSS	CDS_09PORT,CDS_09PIN	; CdS #9
	GOTO	cds_09_off
	BCF		SER_PORT,SER_DATA		; on
	GOTO	cds_09_next
cds_09_off
	BSF		SER_PORT,SER_DATA		; off
cds_09_next
	BTFSC	SER_PORT,SER_CLK		; wait clk down
	GOTO	cds_09_next

cds_10
	BTFSS	SER_PORT,SER_CS		; if CS low continue
	GOTO	cs_loop
	BTFSS	SER_PORT,SER_CLK		; wait clk up
	GOTO	cds_10
	BTFSS	CDS_10PORT,CDS_10PIN	; CdS #10
	GOTO	cds_10_off
	BCF		SER_PORT,SER_DATA		; on
	GOTO	cds_10_next
cds_10_off
	BSF		SER_PORT,SER_DATA		; off
cds_10_next
	BTFSC	SER_PORT,SER_CLK		; wait clk down
	GOTO	cds_10_next

cds_11
	BTFSS	SER_PORT,SER_CS		; if CS low continue
	GOTO	cs_loop
	BTFSS	SER_PORT,SER_CLK		; wait clk up
	GOTO	cds_11
	BTFSS	CDS_11PORT,CDS_11PIN	; CdS #11
	GOTO	cds_11_off
	BCF		SER_PORT,SER_DATA		; on
	GOTO	cds_11_next
cds_11_off
	BSF		SER_PORT,SER_DATA		; off
cds_11_next
	BTFSC	SER_PORT,SER_CLK		; wait clk down
	GOTO	cds_11_next

cds_12
	BTFSS	SER_PORT,SER_CS		; if CS low continue
	GOTO	cs_loop
	BTFSS	SER_PORT,SER_CLK		; wait clk up
	GOTO	cds_12
	BTFSS	CDS_12PORT,CDS_12PIN	; CdS #12
	GOTO	cds_12_off
	BCF		SER_PORT,SER_DATA		; on
	GOTO	cds_12_next
cds_12_off
	BSF		SER_PORT,SER_DATA		; off
cds_12_next
	BTFSC	SER_PORT,SER_CLK		; wait clk down
	GOTO	cds_12_next

cd_loop
	BTFSC	SER_PORT,SER_CS		; wait CS low
	GOTO	cd_loop
	GOTO	cs_loop
	END
