;===============================================================
; PROGRAMMER: WALK TOGETHER
; PROJECT: HEARTBEAT ANALYZER
; TITLE: ADCX.asm
; DATE: 28/02/2009
;===============================================================
LIST P=16F877A
ERRORLEVEL -302
ERRORLEVEL -305
#INCLUDE <P16F877A.INC>
__CONFIG 0X3F32 ; HS OSC
; WATCH DOG OFF
; POWER UP TIMER ON
; BROWN OUT OFF
; LVP OFF
; FLASH PROGRAM WRITE OFF
; DATA EE READ PROTECT OFF
; CODE PROTECT OFF
;=============================== DEFINE CONSTANT =====================================
X1 EQU H'20'
X2 EQU H'21'
X3 EQU H'22'
Z1 EQU H'23'
Z2 EQU H'24'
Z3 EQU H'25'
TWOTWO EQU H'26'
;========================================= RESET VECTOR ============================
ORG 0X000
GOTO MAIN
;========================================= INTERRUPT VECTOR ==========================
ORG 0X004
GOTO MAIN ;IF INTERRUPT IS NOT USED, SET TO MAIN
;================================ INITIALIAZATION =====================================
ORG 0X100
MAIN CALL DELAY1
CALL INITIALIZE_IO ;MAKE ALL INPUT OUTPUT
CALL SETUP_ADC ;SETUP FOR ADC
CALL BANK0 ;OPEN BANK0
;================================MAIN PROGRAM ======================================
CALL DELAY1
MOVLW D'2'
MOVWF TWOTWO
ADC0 CLRF Z3
MOVLW D'255'
MOVWF Z1
MOVLW D'50'
MOVWF Z2
ADC
CALL READ_ADC1
SUBLW D'55'
BTFSS STATUS,C
GOTO ADC_ON
BTFSC Z3,0
GOTO ADC
DECFSZ Z1
GOTO ADC
MOVLW D'255'
MOVWF Z1
DECFSZ Z2
GOTO ADC
MOVLW D'1'
MOVWF Z3
GOTO ADC
ADC_ON
BTFSC Z3,0
GOTO TEST_BEATM
DECFSZ TWOTWO
GOTO $+9
MOVLW D'2'
MOVWF TWOTWO
BSF LED7
BTFSS SIGNAL
GOTO $+3
BCF SIGNAL
GOTO $+2
BSF SIGNAL
CALL DELAY_BEAT
BCF LED7
TEST_BEATM
MOVLW D'255'
MOVWF Z1
MOVLW D'5'
MOVWF Z2
TEST_BEATC
DECFSZ Z1,1
GOTO TEST_OFF1
MOVLW D'255'
MOVWF Z1
DECFSZ Z2,1
GOTO TEST_BEATC
GOTO ADC0
TEST_OFF1
CALL READ_ADC1
SUBLW D'55'
BTFSS STATUS,C
GOTO TEST_BEATM
GOTO TEST_BEATC
;============================= SUBROUTINE =========================================
INITIALIZE_IO
CALL BANK1 ;CHANGE TO BANK 1
MOVLW B'11111111'
MOVWF TRISA
MOVLW B'00000000'
MOVWF TRISB
MOVLW B'01001001'
MOVWF TRISC
MOVLW B'00000000'
MOVWF RISD
MOVLW B'00000000'
MOVWF TRISE
CALL BANK0 ;CHANGE TO BANK 0
MOVLW B'00000000'
MOVWF PORTA
MOVLW B'00000000'
MOVWF PORTB
MOVLW B'00110000'
MOVWF PORTC
MOVLW B'00000000'
MOVWF PORTD
MOVLW B'00000000'
MOVWF PORTE
RETURN
SETUP_ADC
MOVLW B'10000000' ;FOSC/32
;CHANNEL 0
;ADC NOT ACTIVE
;ADC OFF
MOVWF ADCON0
CALL BANK1
MOVLW B'01000100' ;LEFT JUSTIFIED
;RA0, RA1, RA3 AS ADC
MOVWF ADCON1
CALL BANK0
RETURN
READ_ADC1
CALL BANK0
MOVLW B'10000001' ;CONFIGURE TO AN0
MOVWF ADCON0
MOVLW D'50'
MOVWF X1
DECFSZ X1
GOTO $-1 ;DELAY A WHILE
BSF ADCON0,2 ;START CONVERSION
BTFSC ADCON0,2 ;WAIT UNTIL ADC COMPLETED
GOTO $-1
MOVF ADRESH,W ;MOVE ADC VALUE TO WORKING REGISTER
BCF ADCON0,ADON ;OFF ADC MODULE
RETURN
READ_ADC2
CALL BANK0
MOVLW B'10001001' ;CONFIGURE TO AN1
MOVWF ADCON0
MOVLW D'50'
MOVWF X1
DECFSZ X1
GOTO $-1 ;DELAY A WHILE
BSF ADCON0,2 ;START CONVERSION
BTFSC ADCON0,2 ;WAIT UNTIL ADC COMPLETED
GOTO $-1
MOVF ADRESH,W ;MOVE ADC VALUE TO WORKING REGISTER
BCF ADCON0,ADON ;OFF ADC MODULE
RETURN
READ_ADC3
CALL BANK0
MOVLW B'10011001' ;CONFIGURE TO AN3
MOVWF ADCON0
MOVLW D'50'
MOVWF X1
DECFSZ X1
GOTO $-1 ;DELAY A WHILE
BSF ADCON0,2 ;START CONVERSION
BTFSC ADCON0,2 ;WAIT UNTIL ADC COMPLETED
GOTO $-1
MOVF ADRESH,W ;MOVE ADC VALUE TO WORKING REGISTER
BCF ADCON0,ADON ;OFF ADC MODULE
RETURN
;============================= CHANGE BANK ROUTINE ==================================
BANK0
BCF STATUS,RP0 ; SELECT BANK 0
BCF STATUS,RP1
RETURN
BANK1
BSF STATUS,RP0 ; SELECT BANK 1
BCF STATUS,RP1
RETURN
BANK2
BSF STATUS,RP1
BCF STATUS,RP0 ; SELECT BANK 2
RETURN
BANK3
BSF STATUS,RP1
BSF STATUS,RP0 ; SELECT BANK 3
RETURN
;============================= DELAY SUBROUTINE ======================================
;============================= DELAY =================================================
DELAY
MOVLW D'5'
MOVWF X3
MOVLW D'60'
MOVWF X2
MOVLW D'255'
MOVWF X1
DECFSZ X1
GOTO $-1
DECFSZ X2
GOTO $-5
DECFSZ X3
GOTO $-9
RETURN
;============================= DELAY 1 ===============================================
DELAY1
MOVLW D'30'
MOVWF X3
MOVLW D'100'
MOVWF X2
MOVLW D'255'
MOVWF X1
DECFSZ X1
GOTO $-1
DECFSZ X2
GOTO $-5
DECFSZ X3
GOTO $-9
RETURN
;============================= DELAY 2 ================================================
DELAY2
MOVLW D'100'
MOVWF X3
MOVLW D'255'
MOVWF X2
MOVLW D'255'
MOVWF X1
DECFSZ X1
GOTO $-1
DECFSZ X2
GOTO $-5
DECFSZ X3
GOTO $-9
RETURN
;============================= DELAY 3 ================================================
DELAY3
MOVWF X3
MOVLW D'50'
MOVWF X2
MOVLW D'100'
MOVWF X1
DECFSZ X1
GOTO $-1
DECFSZ X2
GOTO $-5
DECFSZ X3
GOTO $-9
RETURN
;============================= DELAY_BEAT ===========================================
DELAY_BEAT
MOVLW D'2'
MOVWF X3
MOVLW D'255'
MOVWF X2
MOVLW D'255'
MOVWF X1
DECFSZ X1
GOTO $-1
DECFSZ X2
GOTO $-5
DECFSZ X3
GOTO $-9
RETURN
;============================= DELAY_BEAT_OFF ========================================
DELAY_BEAT_OFF
MOVLW D'2'
MOVWF X3
MOVLW D'255'
MOVWF X2
MOVLW D'255'
MOVWF X1
DECFSZ X1
GOTO $-1
DECFSZ X2
GOTO $-5
DECFSZ X3
GOTO $-9
ETURN
END