;**************************************************************************
; DMX93.ASM
;	Bbridge heater control software for dmx93 heating doll.
;	作者とその関係者はこのプログラムの運用とその結果およびその影響に
;	ついて、一切の責任を負いません。
;**************************************************************************

        LIST P=16F84, R=DEC

	#include	"16cxx.h"

RamTop   EQU     0x10

;--------------------------------------------------------------------------
;	Variables
;--------------------------------------------------------------------------

;
;	pointers
;
RamTop		EQU	0x10

wk0		EQU	RamTop+0x0
wk1		EQU	RamTop+0x1
wk2		EQU	RamTop+0x2

rc		EQU	RamTop+0x3
rc2		EQU	RamTop+0x4

SyncFlg		EQU	RamTop+0x5

TimCnt0		EQU	RamTop + 0x6
TimCnt1		EQU	TimCnt0 + 0x1
TimCnt2		EQU	TimCnt0 + 0x2
TimCnt3		EQU	TimCnt0 + 0x3

PA		EQU	RamTop+0xa
PB		EQU	RamTop+0xb
Reserv1		EQU	RamTop+0xc

TimCnt		EQU	RamTop+0xf

;
;	Bits
;
#define		SilBit	_porta,0
#define		MonBit	_porta,1

;
;	values
;
TimSet		EQU	0xFF - 30
QueSiz		EQU	0x10

;--------------------------------------------------------------------------
; Program Code
;--------------------------------------------------------------------------
;--------------------------------------------------------------------------
;   Set the reset vector here.  If you are using a PIC16C5X device, use:
;               ORG     <last program memory location>
;   Otherwise, use:
;               ORG     0
;--------------------------------------------------------------------------

                ORG     0       
                GOTO    Init

;--------------------------------------------------------------------------
; Interrupt Program
;--------------------------------------------------------------------------
		ORG	4
Interrupt
		movwf	wk0		;w -> wk0
		swapf	wk0,F		;sawp(wk0) -> wk0

		swapf	_status,w	;swap(status) -> w
		movwf	wk1		;wk1 <- w

		movf	_fsr,w		;fsw -> w
		movwf	wk2		;w -> wk2

;	reset interrupt source
		bcf	_rtif		;0 -> rtif

;	Set monitor bit output
;		bsf	MonBit		;1 -> MonBit

;	increment time counter
		incfsz	TimCnt0,f
		goto	IncEnd
		incfsz	TimCnt1,f
		goto	IncEnd
		incfsz	TimCnt2,f
		goto	IncEnd
		incf	TimCnt3,f
IncEnd		movlw	TimSet
		movwf	_tmr0

;	incremet time counter 2 
		incf	TimCnt,w
		andlw	11111111B
		movwf	TimCnt

;	set sync flag
		bsf	SyncFlg,0	;1 -> SuncFlag

;	reset monitor bit output
;		bcf	MonBit		;

;	restore registors
		movf	wk2,w		;wk2 -> w
		movwf	_fsr		;w -> fsr

		swapf	wk1,w		;wk1 -> w
		movwf	_status		;w -> status

		swapf	wk0,w		;wk0 -> w

;	end of interrupt pocessing
		retfie	

;--------------------------------------------------------------------------
; Main Program
;--------------------------------------------------------------------------

                ORG     H'50'

;--------------------------------------------------------------------------
Init
;--------------------------------------------------------------------------


;--------------------------------------------------------------------------
;	init memory
;	clear all memory
;--------------------------------------------------------------------------
		movlw	RamTop
		movwf	_fsr
clrloop		clrf	_indf
		incf	_fsr,f
		movf	_fsr,w
		sublw	RamTop + 0x30
		btfss	_z
		goto	clrloop

;	init clock
		movlw	TimSet
		movwf	_tmr0

;	init port buffer
;		movlw	0xFF
;		movwf	PrvPB

;--------------------------------------------------------------------------
;	init register
;--------------------------------------------------------------------------

;	set bank 1 
		bcf	_irp
		bcf	_rp1
		bsf	_rp0

;	I/O Location
;
;	port	I/O
;	---------------------
;	A0	OUT	of A3
;	A1	OUT	of A2
;	A2	IN
;	A3	IN
;	B0	IN
;	B1	IN
;	B2	IN
;	B3	IN
;	B4	OUT	of B3
;	B5	OUT	of B2
;	B6	OUT	of B1
;	B7	OUT	of B0	


;	set port a I/O mode (0 for out)
		movlw	11111100B
		movwf	_trisa

;	set port b I/O mode (0 for out)
		movlw	00001111B
		movwf	_trisb

;	set up timer
		movlw	11000010B	;tmr0 active
		movwf	_option		;PS 1/8 devide

;	set bank 0 
		bcf	_rp0

;	set up interrupt
		movlw	00100000B	;tmr0 int enable
		movwf	_intcon

;	enable interrupt
		bsf	_gie		

;--------------------------------------------------------------------------
;	main loop
;--------------------------------------------------------------------------
Start
		
;	Is now for output drive off ?
		movlw	0
		subwf	TimCnt,W
		btfsc	_z
		call	OffDrv

;	Is now for input latch ?
		movlw	25
		subwf	TimCnt,W
		btfsc	_z
		call 	LatDrv

;	wait for next time slot
		call	Sync

                GOTO    Start

;--------------------------------------------------------------------------
;	Off output drive
;	all heaters are off and bridge sensing is enabled
;--------------------------------------------------------------------------
OffDrv		bsf	_porta,0
		bsf	_porta,1
		bsf	_portb,4
		bsf	_portb,5
		bsf	_portb,6
		bsf	_portb,7
		return

;--------------------------------------------------------------------------
;	Latch senser input and drive due to input status
;--------------------------------------------------------------------------
LatDrv		movf	_porta,w	;get port a
		movwf	PA		;save port a	
		movf	_portb,w	;get port b
		movwf	PB		;save port b

		BTFSS	PA,3		;if compalator is HIGH (temp is LOW) then skip
		BsF	_porta,0	;else off the heater
		BTFSC	PA,3		;if compalator is LOW (temp is HIGH) then skip
		BcF	_porta,0	;else on the heater

		BTFSS	PA,2		;if compalator is HIGH (temp is LOW) then skip
		BsF	_porta,1	;else off the heater
		BTFSC	PA,2		;if compalator is LOW (temp is HIGH) then skip
		BcF	_porta,1	;else on the heater

		BTFSS	PB,3		;if compalator is HIGH (temp is LOW) then skip
		BsF	_portb,4	;else off the heater
		BTFSC	PB,3		;if compalator is LOW (temp is HIGH) then skip
		BcF	_portb,4	;else on the heater

		BTFSS	PB,2		;if compalator is HIGH (temp is LOW) then skip
		BsF	_portb,5	;else off the heater
		BTFSC	PB,2		;if compalator is LOW (temp is HIGH) then skip
		BcF	_portb,5	;else on the heater

		BTFSS	PB,1		;if compalator is HIGH (temp is LOW) then skip
		BsF	_portb,6	;else off the heater
		BTFSC	PB,1		;if compalator is LOW (temp is HIGH) then skip
		BcF	_portb,6	;else on the heater

		BTFSS	PB,0		;if compalator is HIGH (temp is LOW) then skip
		BsF	_portb,7	;else off the heater
		BTFSC	PB,0		;if compalator is LOW (temp is HIGH) then skip
		BcF	_portb,7	;else on the heater

		retunrn

;--------------------------------------------------------------------------
;	Sync
;--------------------------------------------------------------------------
Sync	
;	Wait for next time slot
		btfss	SyncFlg,0		;check sync flag
		goto	Sync
		bcf	SyncFlg,0
		return

;--------------------------------------------------------------------------
;	end of program
;--------------------------------------------------------------------------
                END


