list p=12f675 ; list directive to define processor
#include <p12f675.inc> ; processor specific variable definitions
errorlevel -302 ; suppress message 302 from list file
__CONFIG _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT
; '__CONFIG' directive is used to embed configuration word within .asm file.
; The lables following the directive are located in the respective .inc file.
; See data sheet for additional information on configuration word settings.
;***** VARIABLE DEFINITIONS
Word equ 0x30
;**********************************************************************
ORG 0x000 ; processor reset vector
goto main ; go to beginning of program
ORG 0x004 ; interrupt vector location
; isr code can go here or be located as a call subroutine elsewhere
; these first 4 instructions are not required if the internal oscillator is not used
main
call 0x3FF ; retrieve factory calibration value
bsf STATUS,RP0 ; set file register bank to 1
movwf OSCCAL ; update register with factory cal value
bcf STATUS,RP0 ; set file register bank to 0
;*******************************************************************************************************
;************************************** Begining of application code *********************************
;*******************************************************************************************************
portsetup
; ensure Bank "0" is previously selected
clrf GPIO ; clear GPIO
; the default chip configuration upon start up, or reset, is analogue input on GP0, GP1, GP2 and GP4
; to alter this configuration, next 2 instructions are necessary, customize per your application.
movlw 03h ; move literal 03h working register
movwf ANSEL ; move working register to file ANSEL, setting GP2 as digital input and GP1 and GP0 as analogue input.
movlw 07h ; move literal to working register
movwf CMCON ; turn comparators off as they are not used here
bsf STATUS, RP0 ; switch to Bank "1" to set up ports
movlw 0Bh ; move literal to working register, 0Bh (001011b) (1 = input, 0 = output)
movwf TRISIO ; move working register value to TRISIO setting GP3, GP1 and GP0, as inputs, and GP2, GP4 and GP5, as digital outputs.
bcf STATUS, RP0 ; return to Bank "0"
start
nop ; small delay to settle micro
nop
nop
nop
nop
btfss GPIO,3 ; check to see if PTO is engaged.(high) if set then skip next instruction
goto start ; if not set, loop back to start
call Init_ADC0
call Read_ADC1
nop
nop
nop
call result
nop
nop
nop ; small delay to settle micro
btfss GPIO,3 ; check to see if PTO is engaged.(high) if set then skip next instruction
goto start ; if not set, loop back to start
call Init_ADC1
nop
nop
nop
call Read_ADC2
nop
nop
nop
call result
goto start
Init_ADC0 ;set for AN0
; Set ADCON0
movlw b'10000001'
movwf ADCON0 ; bit 7 is set meaning results will be right justified, and bit 0 is also set, meaning A/D module is operating. no other bit are set so Channel 00 for AN0 will be read.
; Set ADCON1
BANKSEL ANSEL
movlw b'10100001'
movwf ANSEL ; bit 7 is set, but is unimplemented. bit 0 is set, meaning GP0(AN0) is assigned as an analogue input.
BANKSEL ADCON0 ; switch to bank 0.
return
Init_ADC1 ; set for AN1
; Set ADCON0
movlw b'10000101'
movwf ADCON0 ; bit 7 is set meaning results will be right justified, and bit 0 is also set, meaning A/D module is operating. bit 2 is set, meaning Channel 01 for AN1 is ready for reading.
; Set ADCON1
BANKSEL ANSEL
movlw b'10100101'
movwf ANSEL ; bit 7 is set, but is unimplemented. bit 0 and 2 are set, meaning GP0(AN0) and GP2(AN1), are assigned as an analogue input.
BANKSEL ADCON0
return
Read_ADC1 ; read CHS0 ( channel 0)
bsf ADCON0, GO_DONE ; initiate conversion
btfsc ADCON0, GO_DONE
goto $-1 ; wait for ADC to finish
movf ADRESH,W ; move results from ADRESH file to W
movwf Word ; move value from W to file named Word
BANKSEL ADRESL ; switch to bank 1
movf ADRESL,W ; move results from ADRESL file to W
movwf Word+1 ; move value from W to file named Word and add 1
return
Read_ADC2 ; read CHS1 (channel 1)
bsf ADCON0, GO_DONE ; initiate conversion
btfsc ADCON0, GO_DONE
goto $-1 ; wait for ADC to finish
movf ADRESH,W ; move results from ADRESH file to W
movwf Word ; move value from W to file named Word
BANKSEL ADRESL ; switch to bank 1
movf ADRESL,W ; move results from ADRESL file to W
movwf Word+1 ; move value from W to file named Word and add 1
return
result
movlw low(0x015B) ; load W with value of low 0x15
subwf Word,W ; subtract value in file Word, with value in W
movlw high(0x015B) ; load W with value of 0x15
btfss STATUS,C ; check to see if bit "C", in Status is set.
movlw high(0x015B)+1 ; load W with value high 0x15 and add 1
subwf Word+1,W ; subtract value in file Word, with value in W
btfss STATUS,C ; carry clear if word is less than 0x15B
call LEDoff
movlw low(0x0266)
subwf Word,W
movlw high(0x0266)
btfss STATUS,C
movlw high(0x0266)+1
subwf Word+1,W
btfsc STATUS,C ; carry set if word is greater or equal to 0x266
call LEDon
goto start
; turn LED off here
LEDon
bsf GPIO,5 ; set bit 5 of GPIO register to 1 (high)
return
LEDoff
bcf GPIO,5 ; set bit 5 of GPIO register to 0 (low)
return
; initialize eeprom locations
ORG 0x2100
DE 0x00, 0x01, 0x02, 0x03
END ; directive 'end of program'