PIC16F684 Part 016 - Controlling Two Shift Registers

Поділитися
Вставка
  • Опубліковано 11 вер 2024

КОМЕНТАРІ • 1

  • @2bit661
    @2bit661  Місяць тому

    ;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
    C1 EQU 0X22 ; 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

    #define SER_IN PORTA, 0 ; RA0 connected to 74HC595 SER_IN
    #define L_Clock PORTA, 1 ; RA1 connected to 74HC595 L_Clock
    #define Clock PORTA, 2 ; RA2 connected to 74HC595 Clock

    ; 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

    bsf STATUS, RP0 ; Switch to Bank 1
    clrf ANSEL ; Set all pins to digital
    clrf TRISA ; Set all pins of PORTA as output
    bcf STATUS, RP0 ; Switch to Bank 0

    CLRF C1
    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

    MOVF C2, W ; this takes the data from the first register and writes to the second
    MOVWF C1

    BITTESTER
    BTFSC C1, 0
    BSF SER_IN ; PORTA, 0 (RA0) controls the serial input on the shift register
    CALL DELAY
    CALL SHIFT_CODE

    BTFSC C1, 1
    BSF SER_IN
    CALL DELAY
    CALL SHIFT_CODE

    BTFSC C1, 2
    BSF SER_IN
    CALL DELAY
    CALL SHIFT_CODE
    BTFSC C1, 3
    BSF SER_IN
    CALL DELAY
    CALL SHIFT_CODE

    BTFSC C1, 4
    BSF SER_IN
    CALL DELAY
    CALL SHIFT_CODE

    BTFSC C1, 5
    BSF SER_IN
    CALL DELAY
    CALL SHIFT_CODE

    BTFSC C1, 6
    BSF SER_IN
    CALL DELAY
    CALL SHIFT_CODE

    BTFSC C1, 7
    BSF SER_IN
    CALL DELAY
    CALL SHIFT_CODE

    BSF L_Clock ; L_CLOCK (PORTA, 1) sets or latches previously-sent 8 bits
    CALL DELAY
    BCF L_Clock ; L_CLOCK cleared back to low
    CALL DELAY

    GOTO Check_Button

    SHIFT_CODE ; shift register subroutine
    BSF Clock ; Clock pin (PORTA, 2), clocks in whatever serial input (PORTA, 0) is doing
    CALL DELAY
    BCF Clock
    CALL DELAY
    BCF SER_IN
    ;CALL DELAY
    RETLW 0

    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.