; Example of reading the dipswitch ; ; The Raven board interfaces to the dip switch (SW_DIP08) through a 74LS165 8-bit ; Parallel-in/Serial-out Shift Register. This device has only two control inputs from the ; PIC and one output. The inputs are: ; Clock-In RC3 ; Load/Shift Y14 (the demultiplexor) ; ; And the output is the bits to RB3, MSB first. ; ; Once the 74LS165 is loaded, it waits for pulses on the clock input pin and then outputs ; one bit at a time. ; When Y14 is brought low and then released, it initiates the load operation that latches the ; current state of the dip switch into the 74LS165. RC3 is used to create this clock pulse by ; setting it to 1 and then zero under program control. There is a possible lag between setting ; the load and the bits being available, so if early bits are missed, a short delay MAY be ; necessary - probably on the order of a couple of microseconds (I haven't seen this happen). LIST P=16F877, R=DEC INCLUDE "C:\MPLAB\P16F877.inc" ; Definitions ; __CONFIG _CP_OFF & _DEBUG_OFF & _WRT_ENABLE_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _PWRTE_ON & _WDT_OFF & _HS_OSC ; Define start of vars ScratchPad EQU 0x20 ; Variables Dippy EQU ScratchPad+0 ; Value from dip switch Temp EQU ScratchPad+1 Counter EQU ScratchPad+2 ; Start of the Program ORG 3 goto Start ; This code reads the dip switch and stores the bit pattern in Dippy and then outputs ; to the led_bar. ; ORG 0x50 Start ; Set up RC3 to be output, RB3 to be input, A4-A0 to be output. bsf STATUS, RP0 **** TRISA *** TRISB,3 *** TRISC,3 **** PORTD ; to use the led_bar for output bcf STATUS, RP0 ; Set up the clocking. RC3 is the clock output pin for the SPI interface and SSPCON is the ; register that controls the SPI. Since that might interfere with the use of RC3 set SSPCON to ; all zeros. Also, clear the clock bit on port C which sets the outgoing signal to low. Low ; causes the dip switch value to be loaded and then we can read the bits. **** SSPCON *** PORTC,3 ; Now, we want to enable the 74LS165 and then disable it to create a low-going pulse. movlw 0x1E movwf PORTA movlw 0x10 movwf PORTA ; Now read each bit, shifting it into Dippy ***** ***** ***** ***** loop1 call ReadDipBit decfsz Counter,F goto loop1 ; Put the value into the led_bar comf ***** call DispLedBar call Forever ReadDipBit ; Rotate Dippy to make room for the next bit. Clear bit zero and then test PORTB, bit 3 to ; see its state. If zero, we be done. If not, set bit 0 of Dippy rlf *** bcf *** btfsc *** bsf **** ; toggle the clock to generate a pulse that reads the next bit. Setting the bit drives ; it low and clearing sets it high, giving a square wave, low-going pulse that causes the ; next bit to be shifted out. bsf PORTC,3 bcf PORTC,3 return DispLedBar ; Enable the led bar and put the value in W there. ; You can do this one return ; Wait forever Forever waitforever nop goto waitforever return END