;free sample code, copy and paste processor 16F684 include "p16f684.inc" ; Configuration settings __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF
R1 EQU 0x20 ; outer loop counter R2 EQU 0x21 ; inner loop counter C2 EQU 0X23 ; specify file to store an 8 bit value ; Define connections #define SH_LD PORTC, 0 ; RC0 connected to SH_LD (pin 1) of 74HC165, config as output #define CLK PORTC, 1 ; RC1 connected to CLK (pin 2) of 74HC165, config as output #define SER_OUT_CHECK PORTC, 2 ; RC2 connected to SER_OUT (pin 3) of 74HC165, config as input #define PUSH_BUTTON_CHECK PORTC, 3 ; RC3 connected to external push button controlled by user, config as input
; Main program start org 0x0000 goto Initialize ; Main program Initialize: ; Initialize ports
banksel CMCON0 ; Select bank containing CMCON register movlw 0x07 ; Load 0x07 into WREG (binary 00000111) movwf CMCON0 ; Move WREG into CMCON to turn off comparators
bsf STATUS, RP0 ; Switch to Bank 1 clrf ANSEL ; Set all pins to digital (for PORTC) movlw b'00001100' ; Load W with the value 00001100 (binary) movwf TRISC ; Set PORTC pin 2 and pin 3 as input and others as output bcf STATUS, RP0 ; Switch to Bank 0
CLRF C2
MOVLW b'00000011' ; Pin 1 and 2 (Shift/Load and clock ) on 74HC165, triggered from High to LOW, so set high MOVWF PORTC ; this sets Shift/Load and clock both high
Check_Button btfss PUSH_BUTTON_CHECK ; Check if the button (PORTC pin 3) is pressed goto Check_Button CALL DELAY
SHIFT_LOAD ; might label as "Loop" BCF SH_LD ;PORTC, 0 CALL DELAY BSF SH_LD ; the preceding three lines load 8 bits of data into 74HC165
BTFSC SER_OUT_CHECK ; check the logic level of the SER_OUT pin of 74HC165 CALL IF_MSB_HIGH BTFSS SER_OUT_CHECK CALL IF_MSB_LOW CALL BIT_TESTER
IF_MSB_HIGH ; the need for this relies upon a technical function of the 74HC165 MOVLW b'10000000' ; the first bit of data (MSB) doesn't require being clocked in MOVWF C2 RETLW 0
IF_MSB_LOW MOVLW b'00000000' ; the first bit of data (MSB) doesn't require being clocked in MOVWF C2 RETLW 0
BIT_TESTER CALL CLOCK_ROUTINE ; this advances to the next bit BTFSC SER_OUT_CHECK ; check the logic level of the SER_OUT pin of 74HC165 BSF C2, 6 BTFSS SER_OUT_CHECK BCF C2, 6 CALL DELAY
;free sample code, copy and paste
processor 16F684
include "p16f684.inc"
; Configuration settings
__CONFIG _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF
R1 EQU 0x20 ; outer loop counter
R2 EQU 0x21 ; inner loop counter
C2 EQU 0X23 ; specify file to store an 8 bit value
; Define connections
#define SH_LD PORTC, 0 ; RC0 connected to SH_LD (pin 1) of 74HC165, config as output
#define CLK PORTC, 1 ; RC1 connected to CLK (pin 2) of 74HC165, config as output
#define SER_OUT_CHECK PORTC, 2 ; RC2 connected to SER_OUT (pin 3) of 74HC165, config as input
#define PUSH_BUTTON_CHECK PORTC, 3 ; RC3 connected to external push button controlled by user, config as input
; Main program start
org 0x0000
goto Initialize
; Main program
Initialize:
; Initialize ports
banksel CMCON0 ; Select bank containing CMCON register
movlw 0x07 ; Load 0x07 into WREG (binary 00000111)
movwf CMCON0 ; Move WREG into CMCON to turn off comparators
bsf STATUS, RP0 ; Switch to Bank 1
clrf ANSEL ; Set all pins to digital (for PORTC)
movlw b'00001100' ; Load W with the value 00001100 (binary)
movwf TRISC ; Set PORTC pin 2 and pin 3 as input and others as output
bcf STATUS, RP0 ; Switch to Bank 0
CLRF C2
MOVLW b'00000011' ; Pin 1 and 2 (Shift/Load and clock ) on 74HC165, triggered from High to LOW, so set high
MOVWF PORTC ; this sets Shift/Load and clock both high
Check_Button
btfss PUSH_BUTTON_CHECK ; Check if the button (PORTC pin 3) is pressed
goto Check_Button
CALL DELAY
SHIFT_LOAD ; might label as "Loop"
BCF SH_LD ;PORTC, 0
CALL DELAY
BSF SH_LD ; the preceding three lines load 8 bits of data into 74HC165
BTFSC SER_OUT_CHECK ; check the logic level of the SER_OUT pin of 74HC165
CALL IF_MSB_HIGH
BTFSS SER_OUT_CHECK
CALL IF_MSB_LOW
CALL BIT_TESTER
IF_MSB_HIGH ; the need for this relies upon a technical function of the 74HC165
MOVLW b'10000000' ; the first bit of data (MSB) doesn't require being clocked in
MOVWF C2
RETLW 0
IF_MSB_LOW
MOVLW b'00000000' ; the first bit of data (MSB) doesn't require being clocked in
MOVWF C2
RETLW 0
BIT_TESTER
CALL CLOCK_ROUTINE ; this advances to the next bit
BTFSC SER_OUT_CHECK ; check the logic level of the SER_OUT pin of 74HC165
BSF C2, 6
BTFSS SER_OUT_CHECK
BCF C2, 6
CALL DELAY
CALL CLOCK_ROUTINE ; bit 5
BTFSC SER_OUT_CHECK
BSF C2, 5
BTFSS SER_OUT_CHECK
BCF C2, 5
CALL DELAY
CALL CLOCK_ROUTINE ; bit 4
BTFSC SER_OUT_CHECK
BSF C2, 4
BTFSS SER_OUT_CHECK
BCF C2, 4
CALL DELAY
CALL CLOCK_ROUTINE ; bit 3
BTFSC SER_OUT_CHECK
BSF C2, 3
BTFSS SER_OUT_CHECK
BCF C2, 3
CALL DELAY
CALL CLOCK_ROUTINE ; bit 2
BTFSC SER_OUT_CHECK
BSF C2, 2
BTFSS SER_OUT_CHECK
BCF C2, 2
CALL DELAY
CALL CLOCK_ROUTINE ; bit 1
BTFSC SER_OUT_CHECK
BSF C2, 1
BTFSS SER_OUT_CHECK
BCF C2, 1
CALL DELAY
CALL CLOCK_ROUTINE ; bit 0
BTFSC SER_OUT_CHECK
BSF C2, 0
BTFSS SER_OUT_CHECK
BCF C2, 0
CALL DELAY
goto Check_Button
CLOCK_ROUTINE ; indiviually clock-in subsequent 7 bits
BCF CLK
CALL DELAY
BSF CLK
RETLW 0
DELAY MOVLW 0xFF ; adjusted delay time
MOVWF R1
AGAIN MOVLW 0xFF
MOVWF R2
HERE NOP
NOP
DECFSZ R2, F
GOTO HERE
DECFSZ R1, F
GOTO AGAIN
RETLW 0
END ; Needed to end the program.