Overview
The SN74HC165 is an 8-bit parallel-load shift registers chip. It uses the SPI interface to shift data out from the register. This chip commonly use in keys reading in video system, printer/scanner, microwave oven, etc.
Simulating Program in Proteus |
This chip is very easy to use since it's a standard logic IC. It does not require any configuration like the MCP23S17 does.
Pin Configuration and Functions |
It has a Dual In Line Package (DIP) easing the electronics hobbyist prototyping. I don't have a physical chip here because it is hard to find at local store. But it has a model in Proteus VSM allowing us to simulate the behavior of this chip.
Logic Diagram |
The SN74HC165 contain standard logic gates and flip flop as shown in the diagram above. It can be cascaded to make a larger registers more than 8-bit inputs.
Load Shift Sequence |
To use the chip the microprocessor must activate the LOAD (LD) pin from high to low transition first. It will latch the 9-bit input data into the register. Then the 8 clock cycle input via CLK (clock) pin will shift the data out at SO (serial out) pin.
The CLK INH (clock inhabit) must be pulled low to enable this chip.
PIC16F887 SPI Programming
We can use the build-in SPI module of PIC16F887 to read data from this chip. The SPI module has the following pins,
- nCS - Chip Select (RC2 or other pins)
- SCK - Serial Clock (RC3)
- SDI - Serial Data In (RC4)
- SDO - Serial Data Out (RC5)
I use the nCS pin to load data into the SN74C165 first before the data shifts out. In this example, the PIC16F887 read data from the SN74HC165. The 8-bit data will show on a 16x2 character LCD in decimal, hexadecimal and binary value.
/* * File: main.c * Author: Admin * * Created on February 12, 2024, 7:19 PM */ #include <xc.h> #include <stdio.h> #include "config.h" #include "LCD4Bits.h" #include "spi.h" #define _XTAL_FREQ 8000000UL void putch(char data){ lcdData(data); } void dec_to_bin(uint8_t data){ for(int8_t i=7;i>=0;i--){ if((data&(1<<i))==0) putch('0'); else putch('1'); } } void main(void) { uint8_t rc_data; OSCCONbits.IRCF=7; lcdInit(); spi_init(); lcdCommand(0x0C); nCS=1; while(1){ nCS=0; __delay_us(10); nCS=1; rc_data=spi_receive(); lcdXY(1,1); printf("DEC:%3d",rc_data); printf(" HEX:0x%02X",rc_data); lcdXY(1,2); printf("BIN: "); dec_to_bin(rc_data); __delay_ms(100); } return; }
I putted the SPI driver in one package.
The spi.h header file:
/* * File: spi.h * Author: Admin * * Created on February 9, 2024, 9:13 AM */ #include <xc.h> #define WRITE_ADDR 0x40 #define READ_ADDR 0x41 #define nCS RC2 void spi_init(void); void spi_send(uint8_t data); uint8_t spi_receive(void);
The spi.c source file:
#include "spi.h" void spi_init(void){ /*SPI Mode Clock Low To High*/ SSPCONbits.CKP=0; SSPSTATbits.CKE=1; SSPSTATbits.SMP=0; /*SPI Master Mode Clock = Fosc/64*/ SSPCONbits.SSPM=2; /*Turn On The Module*/ SSPCONbits.SSPEN=1; SSPSTATbits.BF=1; PORTC=0; TRISC=0; /*SPI SDI Pin Input*/ TRISC4=1; } void spi_send(uint8_t data){ SSPSTATbits.BF==1; SSPBUF=data; while(SSPSTATbits.BF==0); SSPSTATbits.BF==1; } uint8_t spi_receive(void){ uint8_t data; spi_send(0x00); data=SSPBUF; return data; }
Click here to download this example.
Simulating Program in Proteus |
We can use the PIC16F84A with SPI bit-banging to read the data from this chip. A larger AVR chip, the ATMega32 could use a software SPI to interface to this chip.
You can see the software SPI version of this example.
No comments:
Post a Comment