;PIC 16F15313 - pinout
;VDD 1 8 VSS
;RA5 2 7 RA0/DAT
;RA4 3 6 RA1/CLK
;RA3/MCLR 4 5 RA2
;Connect Potentiometer wiper to RA5
;Connect Potentiometer outer legs to VDD and VSS
;RA2 output pin ranges from 0Hz to aproximately 31kHz as you turn the potentiometer
;Driving a stepper in 400 pulses per rev mode will turn the stepper at just over 4500 rpm
; 31kHz / 400 = 77.5 revolutions per second ... 77.5 x 60 = 4650 rpm
; Note: Switching the stepper controller to 200 pulses per rev, the stepper topped out at 20kHz before losing sync.
;
; 20kHz / 200 = 100 revolutions per second ... 100 x 60 = 6000 rpm
#include "p16lf15313.inc"
CONFIG1 = _FEXTOSC_OFF & _RSTOSC_HFINT32 & _CLKOUTEN_OFF & _CSWEN_ON & _FCMEN_ON
CONFIG2 = _MCLRE_OFF & _PWRTE_OFF & _LPBOREN_OFF & _BOREN_ON & _BORV_LO & _ZCD_OFF & _PPS1WAY_ON & _STVREN_ON
CONFIG3 = _WDTCPS_WDTCPS_31 & _WDTE_OFF & _WDTCWS_WDTCWS_7 & _WDTCCS_SC
CONFIG4 = _BBSIZE_BB512 & _BBEN_OFF & _SAFEN_OFF & _WRTAPP_OFF & _WRTB_OFF & _WRTC_OFF & _WRTSAF_OFF & _LVP_ON
CONFIG5 = _CP_ON
;*******************************************************************************
; Reset Vector
;*******************************************************************************
RES_VECT CODE 0x0000 ; processor reset vector
GOTO Initialize ; go to beginning of program
;*******************************************************************************
; MAIN PROGRAM
;*******************************************************************************
MAIN_PROG CODE ; let linker place main program
Initialize:
banksel OSCCON1
movlw b'00010000'
; X....... Reserved
; .XXX.... OSC Source
; ....XXXX Divider
movwf OSCCON1
banksel OSCFRQ
movlw b'00000110'
; XXXXX... Reserved
; .....XXX HFINTOSC Frequency Selection
movwf OSCFRQ
banksel TRISA
movlw b'00100000' ; Set RA5 as an INPUT
movwf TRISA
banksel LATA ; Set all OUTPUTS LOW
movlw b'00000000'
movwf LATA
banksel WDTCON1
movlw b'01110000'
; X....... Unemplemented
; .XXX.... WDT Clock Select
; ....X... Unemplemented
; .....XXX WDT Window Select
movwf WDTCON1
banksel RA2PPS ; RA2->NCO1:NCO; Redirect NCO output to RA2
movlw h'1A'
movwf RA2PPS
banksel PMD1
movlw b'00000000'
; X....... NCO1MD: Disable Numerically Control Oscillator bit
; .XXXX... Unimplemented: Read as ‘0’
; .....X.. TMR2MD: Disable Timer TMR2 bit
; ......X. TMR1MD: Disable Timer TMR1 bit
; .......X TMR0MD: Disable Timer TMR0 bit
movwf PMD1
banksel NCO1CON
movlw b'10000000'
; X....... N1EN: NCO1 Enable bit
; .X...... Unimplemented: Read as ‘0’
; ..X..... N1OUT: NCO1 Output bit
; ...X.... N1POL: NCO1 Polarity bit
; ....XXX. Unimplemented: Read as ‘0’
; .......X N1PFM: NCO1 Pulse Frequency Mode bit
movwf NCO1CON
banksel NCO1CLK
movlw b'00000000'
; XXX..... N1PWS<2:0>: NCO1 Output Pulse Width Selectbits(1)
; ...X.... Unimplemented: Read as ‘0’
; ....XXXX N1CKS<3:0>: NCO1 Clock Source Select bits
movwf NCO1CLK
banksel ADCON1
MOVLW b'11110000'
; X....... ADFM: ADC Result Format Select bit
; .XXX.... ADCS<2:0>: ADC Conversion Clock Select bits
; ....XX.. Unimplemented: Read as ‘0’
; ......XX ADPREF<1:0>: ADC Positive Voltage Reference Configuration bits
MOVWF ADCON1
banksel ANSELA
movlw b'00100000'
; XX...... Unimplemented
; ..XX.... RA5 RA4 anaolg or digital select
; ....X... Unimplemented
; .....XXX RA2 RA1 RA0 anaolg or digital select
movwf ANSELA
banksel ADCON0
MOVLW b'00010101'
; XXXXXX.. CHS<5:0>: Analog Channel Select bits
; ......X. GO/DONE: ADC Conversion Status bit
; .......X ADON: ADC Enable bit
MOVWF ADCON0
banksel NCO1INCU
clrf NCO1INCU
banksel NCO1INCH
clrf NCO1INCH
banksel NCO1INCL
clrf NCO1INCL
START:
banksel ADCON0
bsf ADCON0,1 ; Start conversion
CHECK_ADC:
btfsc ADCON0,1 ; Is conversion done?
goto CHECK_ADC ; No, test again
banksel ADRESH
movf ADRESH,W ; Read upper 2 bits
banksel NCO1INCH
movwf NCO1INCH
banksel ADRESL
movf ADRESL,W ; Read lower 8 bits
banksel NCO1INCL
movwf NCO1INCL
GOTO START ; loop forever
END