;******************************************************************
;   PIC16C84
;: Aleksander I. Gurin         a_gurin@kmv.ru
;      .
;       
;   .
;() 17.08.99
;******************************************************************

	list p=16c84, r=dec
	errorlevel 0,-305
	include "C:\PROGRA~1\MPLAB\p16c84.inc";     inc-
; 

	__config   _cp_off & _wdt_on & _pwrte_off & _xt_osc

	count		equ     h'0010'	;""   
	pres		equ     h'0011'	;  
	push		equ	h'0012'	;  
	tout		equ	h'0013'	; 
	ctout		equ	h'0014'	;   
	dummy		equ	h'0015'	; - 
;					 (,-    )
	sum		equ	h'0016'	;    
	flag		equ	h'0017'	; 
;  flag
;0 - 	\
;1 - 	 -  10-  
;2 - 	/
;3 - 	 
;4 - 	,     
;5 -	,    
;6 -	,     EEPROM
;7 -	 

;#define	test		;   (     )

	org	h'00'
	goto	main
	org     h'04'
inter	bcf	intcon,3	;  
	bsf	status,5
	bcf	eecon1,4	;     EEPROM
	bcf	status,5
	bsf	flag,6
	retfie

read	clrf	count		;  
	incf	count,1	
	clrf	pres	 
	clrf	push
	bcf	flag,5
seta	comf	count,0		;   " "
	movwf	porta		;  RA
	call	rest		;     RA
scanb	movf	portb,0		;  RB
	movwf	pres
	comf	pres,1
	xorlw	0x70
	movwf	pres
	swapf	pres,1
	incf	pres,1
	decfsz	pres,1		;     -
	goto	key		;,  
	bcf	status,c	;    -
	rlf	count,1		;   
	btfss	count,4		;   -
	goto	seta
	goto	eoread		;   .

;      
;  push    (  -   
;  ):
;"1" - 09h 	"2" - 0Ah 	"3" - 0Ch
;"4" - 11h	"5" - 12h	"6" - 14h
;"7" - 21h	"8" - 22h	"9" - 24h
;"*" - 41h	"0" - 42h	"#" - 44h
;       EEPROM
;,     EEPROM   - .
;   42h   20h  EEPROM.
;    "0".

key	movf	count,0
	movwf	push
	rlf	push,1
	rlf	push,1
	rlf	push,1
	movf 	pres,0
	iorwf	push,1
	btfss	flag,5		;       "" -
	call	beep		; ""
	bsf	flag,5		;   5  flag,
;				    ""   
;				  
	goto	scanb		;...   - 
eoread	return

beep	movlw   0xff		;  
	movwf   ctout
	ifndef	test
loop1	bcf	portb,2		;  1 
	bsf	portb,1		; 2  RB
	call	rest		;...  
	bcf	portb,1		;   1 
	bsf	portb,2		; 2  RB
	call	rest		;...   
	decfsz	ctout,1
	goto	loop1
	bcf	portb,1
	bcf	portb,2
	endif
	return

rest	movlw   0x19		; 
	movwf   tout
loop2	nop	
	ifndef	test		;     
	decfsz	tout,1
	goto	loop2
	endif
	return

compare	clrf	sum		;  
	movlw   0x20
	movwf   fsr
mark02	movf	0x00,0		;   
	addwf	sum,1		;  20h-27h RAM
	incf	fsr,1		;     sum
	movlw	0x28
	xorwf	fsr,0
	btfss	status,z
	goto	mark02
	return

sec	nop			;   1 .
	ifndef	test		;     
	bsf	status,5
	movlw   0x0e		; ,    
	movwf	0x81		;    
	bcf	status,5
	clrwdt			; WDT
	sleep			;...  
	endif
	return

main	bsf	status,5
	movlw   0x00		;  RA  
        movwf   trisa
	movlw   0x70		; RB0-RB3  RB7  
	movwf   trisb		;... RB4-RB6 -  
	bcf	0x81,7
	bcf	status,5
	movlw   0x00
	movwf   intcon		; 
	movlw	0x1f
	movwf	porta
	clrf	portb
	movlw	0x03		;   
	movwf	dummy
mark00	movlw   0x20		; 8   
	movwf   fsr		; EEPROM (20h-27h)
mark01	movf	fsr,0		; ROM (20h-27h) 
	movwf	eeadr
	bsf	status,5
	bsf	eecon1,rd
	bcf	status,5
	movf	eedata,0
	movwf	0x00
	incf	fsr,1
	movlw	0x28
	xorwf	fsr,0
	btfss	status,z
	goto	mark01
	movlw	0x20
	movwf	fsr
	clrf	flag		; 
	goto	loop0
open	movlw	0x0f		;  
	movwf	porta
	call	sec		;  
	call	beep		;"" 
	bsf	portb,3		;   
	call	sec		;  
mark04	bsf	porta,4		;  
	clrf	portb
	call	beep		;  ""
	goto	main		;...   
loop0	clrf	intcon		;  
	clrwdt			; WDT
	call	read		;  
mark09	movlw	0x00 
	xorwf	push,0
	btfss	status,z	;    -
	goto	mark05		;  mark05
	movlw	0x10		; RA0-RA3
	movwf	porta
loop7	movlw	0x88		;   RB
	movwf	intcon
	bsf	status,5	; WDT
	movlw   0x0f
	movwf	0x81	
	bcf	status,5
	clrwdt
	sleep	
	btfsc	intcon,0	;       -
	goto	loop0		; 
	btfss	flag,4
	goto	loop7
	incf	flag,1
	btfsc	flag,2		; 10 .   - 
	goto	main		;   
	goto 	loop7		;   -  ...
mark05	movlw	0x41		;...  ,  .
	xorwf	push,0
	btfss	status,z	;  "*" -
	goto	mark06
	goto	mark10		;  mark10
mark06	movlw	0x44
	xorwf	push,0
	btfss	status,z	;  "#" -
	goto	mark07
	goto	mark03		;  mark03
mark07	movf	push,0		;    
	xorwf	0x00		;...   EEPROM
	incf	fsr,1
	movlw	0x10		;  10-  
	movwf	flag
	goto	loop0		;...      
mark03	bcf	status,c	;   + "#"
	call	compare		;   
	incf	sum,1
	decfsz	sum,1		;   -
	goto	mark08
	goto	open		; 
mark08	decfsz	dummy		;"" -     ,-     
	goto	mark00		;  ...
;				     ! 
;				    !
	movlw	0x40		
	movwf	dummy
	clrf	porta
loop4	clrf	portb
	call	beep		;""  
	call	sec
	bsf	porta,4
	decfsz	dummy,1
	goto	loop4
	goto	main		;...   
mark10	bcf	status,c	;   + "*" -   
	call	compare		;   
	incf	sum,1
	decfsz	sum,1		;  "_"  -
	goto	main
	movlw	0x20
	movwf	fsr
	bcf	porta,4		;  
	call	beep		; ""
	bsf	porta,4		;   
loop6	clrwdt
	call	read		; 
	movlw	0x00
	xorwf	push,0
	btfss	status,z	; -  -
	goto	mark11		
	goto	loop6
mark11	movlw	0x41		;... ,  - "*"
	xorwf	push,0
	btfss	status,z	; "*" -
	goto	mark12
	goto	main		; ,   
mark12	movlw	0x44		;... ,  - "#"
	xorwf	push,0
	btfss	status,z	; "#" -
	goto	mark13
	goto	save		;...    "_"      EEPROM
mark13	movf	push,0		;   "" -     "_"
	movwf	0x00		;   RAM
	incf	fsr,1
	movlw	0x29
	xorwf	fsr,0
	btfss	status,z	;    9- 
	goto	loop6
	call	sec		;  ""     "_"
	call	beep
	call	sec
	call	beep
	goto	main
save	call	sec		;    prom
	bcf	porta,4
	call	beep
	bsf	porta,4
	clrwdt
	movlw	0x20
	movwf	fsr
loop5	bsf	status,5	;  (    EEPROM)
	movlw	0x04
	movwf	eecon1
	bcf	status,5
	bcf	flag,6
	movf	fsr,0
	movwf	eeadr
	movf	0x00,0
	movwf	eedata
	movlw	0xc0
	movwf	intcon
	bsf	status,5
	movlw	0x09
	movwf	0x81
	movlw	0x55
	movwf	eecon2
	movlw	0xaa
	movwf	eecon2
	bsf	eecon1,wr
loop8	bcf	status,5
	btfss	flag,6		;   
	goto	loop8		;  
	clrf	intcon
	clrwdt
	bcf	flag,6
	incf	fsr,1		;...       20h-27h  ROM  EEPROM
	movlw	0x28	
	xorwf	fsr,0
	btfss	status,z
	goto	loop5
	bsf	status,5
	clrf	eecon1
	clrf	eecon2
	movlw	0x0e
	movwf	0x81
	bcf	status,5
	clrwdt			; 
stop	bcf	porta,4		;  
	call	beep		; ""
	bsf	porta,4		;   
	call	sec		; 
	bcf	porta,4		;  
	call	beep		; ""
	bsf	porta,4		;   
	goto	main		;...   
end	end	


--------------------------------------------------------------------------------

: Aleksander I. Gurin. a_gurin@kmv.ru 