;Программа индикатора Калибратора
	PROCESSOR 16F877
	#include P16f877.inc
	__config _HS_OSC & _WDT_OFF & _PWRTE_OFF & _BODEN_OFF & _LVP_OFF & _CPD_OFF & _DEBUG_OFF & _CP_OFF
#DEFINE	  PAGE0	BCF H'03',5 ;STATUS bit 5
#DEFINE	  PAGE1	BSF H'03',5 ;STATUS bit 5
#DEFINE PAJ	PORTE,2
	ORG 0 			;Начало программы.
	GOTO    BEGIN
	ORG	0x04
;****************************************
;*** С Е К Ц И Я  П Р Е Р Ы В А Н И Я ***
;****************************************
;*** С Е К Ц И Я  П Р Е Р Ы В А Н И Я ***
	MOVWF	W_TEMP; Сохранение регистров
	SWAPF	STATUS,W
	CLRF	STATUS
	MOVWF	STATUS_TEMP
	MOVF	PCLATH,W
	MOVWF	PCLATH_TEMP
;**********************************
	CLRF	STATUS
	BCF	STATUS,C
	RLF	REG_DIN,F
	BTFSC	REG_DIN,3
	CALL	RES_DIN
	MOVF	REG_DIN,W
	MOVWF	PORTB
	BTFSC	REG_DIN,0
	MOVF	DIG1,W
	BTFSC	REG_DIN,1
	MOVF	DIG2,W
	BTFSC	REG_DIN,2
	MOVF	DIG3,W
	MOVWF	PORTD
	INCF	CNT4,F
	CALL	PREO_DIG
	CALL	READ_KEY
	BTFSC	REG_DIN,1
	CALL	COUNT_KEY
	BTFSC	REG_DIN,2
	CALL	PREO_TEMP
	BTFSC	REG_DIN,0
	INCF	CNT7,F
	BTFSC	CNT7,5
	CALL	OUT_TEMP
;**********************************
OUT_PRER
	BCF		INTCON,T0IF
	MOVF	PCLATH_TEMP,W;Восстановление регистров
	MOVWF	PCLATH
	SWAPF	STATUS_TEMP,W
	MOVWF	STATUS
	SWAPF	W_TEMP,F
	SWAPF	W_TEMP,W
	RETFIE
;*********************************
RES_DIN
	MOVLW	01h
	MOVWF	REG_DIN
	RETURN
;*********************************
PREO_DIG
	BTFSC	FLAG1,0
	RETURN
	BTFSS	CNT4,7
	RETURN
	MOVLW	0FFh
	MOVWF	PORTD
	RETURN
;*********************************
COUNT_KEY
	INCF	CNT6,F
	BTFSS	CNT6,4
	RETURN
	CLRF	CNT6
	INCF	CNT5,F
	MOVLW	D'40'
	XORWF	CNT5,W
	BTFSC	STATUS,Z
	GOTO	COUNT1
	CLRF	FLAG1
	RETURN
COUNT1
	DECF	CNT5,F	
	BSF	FLAG1,0
	RETURN
;*********************************
OUT_TEMP
	CLRF CNT7
	BCF	FLAG3,0
	RETURN
;*********************************
;*********************************
W_TEMP	EQU	070h
STATUS_TEMP	EQU	071h
PCLATH_TEMP	EQU	072h
CNT1	EQU	073h
CNT2	EQU	074h
CNT3	EQU	075h
CNT4	EQU	076h
CNT5	EQU	077h
CNT6	EQU	078h
CNT7	EQU	079h
;
REG_DIN	EQU	20h
TEMPER_LO	EQU	021h
TEMPER_HI	EQU	022h
CTR	EQU	023h
BCD1	EQU	024h
BCD2	EQU	025h
BCD3	EQU	026h
BYTE_LO	EQU	027h
BYTE_HI	EQU	028h
DIG1	EQU	029h
DIG2	EQU	02Ah
DIG3	EQU	02Bh
UST_LO	EQU	02Ch
UST_HI	EQU	02Dh
FLAG_KEY	EQU	02Eh
TEMP_LO	EQU	030h
TEMP_HI	EQU	031h
FLAG1	EQU	032h
FLAG2	EQU	033h
TEMP1	EQU	034h
FLAG3	EQU	035h
;**********************************
HEXLED
	ADDWF	PCL,F
	;         HGFEDCBA
	RETLW	B'11000000'; 0
	RETLW	B'11111001'; 1
	RETLW	B'10100100'; 2
	RETLW	B'10110000'; 3
	RETLW	B'10011001'; 4
	RETLW	B'10010010'; 5
	RETLW	B'10000010'; 6
	RETLW	B'11111000'; 7
	RETLW	B'10000000'; 8
	RETLW	B'10010000'; 9
	RETURN
;*********************************
PREO_TEMP
	BTFSS	FLAG1,0
	GOTO	PREO2
	MOVF	TEMPER_LO,W
	MOVWF	BYTE_LO
	MOVF	TEMPER_HI,W
	MOVWF	BYTE_HI
	GOTO	PREO1
PREO2
	MOVF	UST_LO,W
	MOVWF	BYTE_LO
	MOVF	UST_HI,W
	MOVWF	BYTE_HI
PREO1
	CALL	BINBCD
	MOVF	BCD3,W
	MOVWF	BCD1
	MOVLW	B'00001111'	
	ANDWF	BCD1,W	
	CALL	HEXLED
	MOVWF	DIG3
	BTFSC	FLAG2,0
	BCF	DIG3,7
	SWAPF	BCD3,W
	MOVWF	BCD1
	MOVLW	B'00001111'	
	ANDWF	BCD1,W	
	CALL	HEXLED
	MOVWF	DIG2
	MOVF	BCD2,W
	MOVWF	BCD1
	MOVLW	B'00001111'	
	ANDWF	BCD1,W	
	CALL	HEXLED
	MOVWF	DIG1
	RETURN
;*********************************
READ_KEY
	CLRF	CNT3
	BTFSC	FLAG_KEY,0
	GOTO	READ1
	BTFSC	PORTE,0
	RETURN
	CLRF	CNT4
	CLRF	CNT5
	BSF	FLAG_KEY,0
	BTFSS	PORTE,1
	GOTO	READ2
	INCF	UST_LO,F
	BTFSC	STATUS,Z
	CALL	PROV1
	RETURN
READ2
	MOVF	UST_LO,F
	BTFSC	STATUS,Z
	CALL	PROV2
	DECF	UST_LO,F
	BTFSC	UST_HI,0
	RETURN
	MOVLW	D'200'
	SUBWF	UST_LO,W
	BTFSC	STATUS,C
	RETURN
	MOVLW	D'200'
	MOVWF	UST_LO
	RETURN
READ1
	BTFSS	PORTE,0
	RETURN
	CLRF	FLAG_KEY
	RETURN
;
PROV1
	BTFSC	UST_HI,0
	DECF	UST_LO,F
	BTFSS	UST_HI,0
	INCF	UST_HI,F
	RETURN
;
PROV2
	BTFSS	UST_HI,0
	INCF	UST_LO,F
	BTFSC	UST_HI,0
	DECF	UST_HI,F
	RETURN
;*********************************
;*********************************
;*********************************
;*********************************
;*********************************
;*********************************
BEGIN
	MOVLW	B'01000001'
	MOVWF	ADCON0; АЦП ВКЛЮЧЕН
	PAGE1    
	MOVLW	B'10000100'
	MOVWF	ADCON1^80;АЦП ВКЛЮЧЕН
	MOVLW	B'00000011'
	MOVWF	TRISE^80;ПОРТ E НА ВХОД
    MOVLW	0x00
	MOVWF	TRISC^80;ПОРТ С НА ВЫХОД
	MOVWF	TRISD^80;ПОРТ D НА ВЫХОД
	MOVWF	TRISB^80;ПОРТ B НА ВЫХОД
	MOVLW	B'00111111'
	MOVWF	TRISA^80;ПОРТ А НА ВХОД
	PAGE0
	CLRF	PORTC
	CLRF	PORTD
	MOVLW	01h
	MOVWF	PORTB
	MOVWF	REG_DIN
;ИНИЦИАЛИЗАЦИЯ ТАЙМЕРА	
	CLRF	INTCON
	PAGE1
	MOVLW	B'10000010'
	MOVWF	OPTION_REG^80h
	PAGE0
	MOVLW	B'10100000'
	MOVWF	INTCON
	MOVLW	1h
	MOVWF	UST_HI
	MOVLW	2Ch
	MOVWF	UST_LO
	BSF	FLAG_KEY,0
	CLRF	CNT5
	CLRF	FLAG1
	CLRF	CNT6
;********************************
LOOP
	BSF	ADCON0,2
	BTFSC	ADCON0,2
	GOTO	$-1
	PAGE1
	MOVF	ADRESL^80H,W
	PAGE0
	MOVWF	TEMP_LO
	MOVF	ADRESH,W
	MOVWF	TEMP_HI
	MOVLW	D'30';******************************************* ТЕМПЕРАТУРА ОКРУЖАЮЩЕЙ СРЕДЫ 
	ADDWF	TEMP_LO,F
	BTFSC	STATUS,C
	INCF	TEMP_HI,F
;
	BCF	FLAG2,0
	MOVF	TEMP_HI,W
	SUBWF	UST_HI,W
	MOVWF	TEMP1
	MOVF	TEMP_LO,W
	SUBWF	UST_LO,W
	BTFSS	STATUS,C
	DECF	TEMP1,F
	BTFSS	TEMP1,7
	BSF	FLAG2,0
;
	BTFSC	FLAG2,0
	BSF	PAJ
	BTFSS	FLAG2,0
	BCF	PAJ
	BTFSC	FLAG3,0
	GOTO	LOOP1
	MOVF	TEMP_LO,W
	MOVWF	TEMPER_LO
	MOVF	TEMP_HI,W
	MOVWF	TEMPER_HI
	BSF	FLAG3,0
LOOP1
	CALL	DELAY10
	GOTO	LOOP

;*********************************
DELAY10
	MOVLW	D'15'
	MOVWF	CNT1
DEL100	
	MOVLW	D'219'
	MOVWF	CNT2
	DECFSZ	CNT2,F
	GOTO	$-1
	DECFSZ	CNT1,F
	GOTO	DEL100
	RETURN
;****************************************
;****************************************
;****************************************
;****************************************
;****************************************
;****************************************
;****************************************
BINBCD	
	MOVLW	.16 
    MOVWF	CTR 
	CLRF	BCD1
	CLRF	BCD2
	CLRF	BCD3
	GOTO	ST1
ADJDEC1
	MOVLW	0x33 
    ADDWF	BCD1,F
	ADDWF	BCD2,F
	ADDWF	BCD3,F
    MOVLW	0x03
    BTFSS	BCD1,3
    SUBWF	BCD1,F
    BTFSS	BCD2,3
    SUBWF	BCD2,F
    BTFSS	BCD3,3
    SUBWF	BCD3,F
	MOVLW	0x30
	BTFSS	BCD1,7
    SUBWF	BCD1,F
    BTFSS	BCD2,7
    SUBWF	BCD2,F
    BTFSS	BCD3,7
    SUBWF	BCD3,F
ST1
	RLF	BYTE_LO,F
	RLF	BYTE_HI,F
	RLF	BCD3,F
	RLF	BCD2,F
	RLF	BCD1,F
	DECFSZ	CTR,F
	GOTO	ADJDEC1
	RETURN
;****************************************
;*********************************
	END
