; *******************************************************************************
; comparator-16F1827.asm
;
; NOTES
; RA3 input = Junction of resistive divider 10k in series with 5k trimpot.
; free end of 10k to +V, connect wiper to one end of 5k trimpot
; and connect to GND, other end to of trimpot to 10k.
; Set trimpot
; with wiper to GND end.
;
; Reference voltage for Comparator is Fixed Voltage Reference set to 4.096V.
;
; LED with 470 ohm series resistor conected between RA1 and GND.
;
; Turning trimpot up will trigger interrrupt routine and turn on LED.
; Power off to reset.
;********************************************************************************
;
; Target Controller PIC16F1827
; __________
; SPARE------RA2 |1 18| RA1---LED
; COMP-NEG---RA3 |2 17| RA0---SPARE
; SPARE------RA4 |3 16| RA7---SPARE
; SPARE------RA5 |4 15| RA6---SPARE
; Ground-----Vss |5 14| VDD---+5 V
; SPARE------RB0 |6 13| RB7---SPARE
; SPARE------RB1 |7 12| RB6---SPARE
; SPARE------RB2 |8 11| RB5---SPARE
; SPARE------RB3 |9 10| RB4---SPARE
; ----------
;
; *******************************************************************************
; * Device type and options *
; *******************************************************************************
;
processor 16F1827
radix dec
errorlevel -207 ; Skip found label after column 1
errorlevel -302 ; Skip out of bank nuisance messages
errorlevel -303 ; Skip program word too large. Truncated to core size
;
; *******************************************************************************
; * Configuration fuse information for 16F1827: *
; *******************************************************************************
;
include <P16F1827.INC>
__CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
__CONFIG _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _BORV_LO & _LVP_OFF
;
; *******************************************************************************
;
; 16F1827 Oscillator setup
; OSCCON - Oscillator control reg.
; ---------------------------------
; SPLLEN b7 enable PLL x4
; 1 = enabled 0 = disabled
; IRCF | b6-3 frequency selection
; 1111 = 16MHz HF
; 1110 = 8 or 32MHz HF
; 1101 = 4MHz HF
; 1100 = 2MHz HF
; 1011 = 1MHz HF
; 1010 = 500kHz HF
; 1001 = 250kHz HF
; 1000 = 125kHz HF
; 0111 = 500kHz MF (default)
; 0110 = 250kHz MF
; 0101 = 125kHz MF
; 0100 = 62.5kHz MF
; 0011 = 31.25kHz HF
; 0010 = 31.25kHz MF
; 000x = 31.25kHz LF
; Reserved B2 reserved, 0
; SCS B1 0-: 1x = int. OSC.
; 01 = Timer1 oscillator
; 00 = determined by FOSC <2:0> in Configuration
; POR default 00111-00 500 kHz (POR = Power On Reset)
OSCCONVAL EQU b'01101000' ; 4MhZ CLOCK
;
; *******************************************************************************
; * Allocate variables in general purpose register space *
; *******************************************************************************
;
CBLOCK 0x20 ; Start Data Block
temp
ENDC ; End of Data Block
;
; *******************************************************************************
; * Macro's *
; *******************************************************************************
;
LED_on macro
bsf PORTA,1
endm
LED_off macro
bcf PORTA,1
endm
;
; *******************************************************************************
; * Purpose: This is the start of the program. *
; *******************************************************************************
;
;
ORG 0x0000
goto start ; Jump to main program
ORG 0x0004 ; interrupt routine data save
bcf INTCON,GIE ; Clear Global Interrupt Enable bit
BANKSEL PIR2
bcf PIR2,C2IF ; Clear comparator flag
movlb 0
clrf PORTA
clrf PORTB
LED_on
goto $
retfie ; Enable general interrupts and return
;
;--------------------------------------------------------------------------------
;
start
clrf INTCON ; clear INTCON
clrf PORTA
clrf PORTB
; Set PIC oscillator frequency
banksel OSCCON ; Select OSCCON
movlw OSCCONVAL ; Oscillator frequency
movwf OSCCON ; Loads the wanted value
; Configures I/O analog/digital function
Banksel ANSELA
movlw b'00001000' ; RA3 analog all others digital
movwf ANSELA
clrf ANSELB ; PORTB all digital
; Disable all wakeup pull-ups
Banksel WPUA
clrf WPUA
clrf WPUB
banksel OPTION_REG
movlw b'10000000' ; Pull-ups disabled
;
movwf OPTION_REG ;
;
movlw b'00111100' ; PORTA (RA2:5 inputs RA0:1 & 6:7 outputs)
movwf TRISA ;
movlw b'10001111' ; PORTB 0:3 & 7 inputs 4:6 outputs
movwf TRISB ; NOTE: Pull-up via 10k resistor all unused pins
;
BANKSEL FVRCON
movlw b'10001100' ; FVR on, Comparator ref 4.096V
movwf FVRCON
;
movlb 0
movlw 40 ; Delay for FVR to stabilise
movwf temp
decfsz temp
goto $-1
BANKSEL CM1CON0
clrf CM1CON0
clrf CM1CON1
movlw b'10100011'
movwf CM2CON1 ; Positive interrupt on
; Negative interrupt off
; C2VP connects to FVR
; C2VP connects to FVR
; Unimplemented
; Unimplemented
; C2VN connects to C12IN3- pin - RA3
; C2VN connects to C12IN3- pin - RA3
movlw b'10000110'
movwf CM2CON0 ; Comparator enabled
; Comparator Output bit polarity
; Comparator Output internal
; Comparator output is not inverted
; Unimplemented
; Comparator operates in normal power, higher speed mode
; Comparator hysteresis enabled
; Comparator output to Timer1 and I/O pin is asynchronous
BANKSEL PIE2
bsf PIE2,C2IE ; Turn on Comparator 2 interrupt
bsf INTCON,PEIE ; Turn on Periperal interrupts
movlb 0
main
bsf INTCON,GIE ; Turn on Global interrupts
nop
goto $-1 ; Endless loop as comparator is independent of
; anything happening here.
;
END
;--------------------------------------------------------------------------------