Learn To Write Code For 8051, Arduino, AVR, dsPIC, PIC, STM32 ARM Microcontroller, etc.
Coding Embedded Controller With C/C++.
Printed Circuit Board (PCB) Project For Electronics Hobbyists.
The SNx4HC165 devices are 8-bit parallel-load shift registers that, when clocked, shift the data toward a serial (QH) output. Parallel-in access to each stage is provided by eight individual direct data (A-H) inputs that are enabled by a low level at the shift/load (SH/LD) input. The SNx4HC165 devices also feature a clock-inhibit (CLK INH) function and a complementary serial (QH) output. Clocking is accomplished by a low-to-high transition of the clock (CLK) input while SH/LD is held high and CLK INH is held low. The functions of CLK and CLK INH are interchangeable. Because a low CLK and a low-to-high transition of CLK INH also accomplish clocking, CLK INH must be changed to the high level only while CLK is high. Parallel loading is inhibited when SH/LD is held high. While SH/LD is low, the parallel inputs to the register are enabled independently of the levels of the CLK, CLK INH, or serial (SER) inputs.
This input expansion chip can be used in Programmable Logic Controllers, Appliances, Video Display Systems, Keyboards etc. This old digital IC is very easy to use with a typical 8-bit MCU since it has a small footprint and its Dual In-Line Package version. It is widely available at very low cost.
A DIP-16 Sample
It has DIP-16 footprint and various SMD packages.
Pin Configuration and Functions
It made from some digital logic gates and flip-flops.
Logic Diagram Positive Logic
To load parallel input data (H:A) take a look at the timing diagram below.
Load Shift Sequence
Typically the SI (Serial Data In) is ignored or wires it GND. The Clock Inhabit (CLK INH) must wired to GND. At the transition from logic low to logic high of SH/LD pin allows parallel data loading and serial data shifting out via Serial Data Out (SO) pin.
In this example the ATMega644P read parallel input data of an SN74HC165N and display it on PORTD.
Source Code "main.c":
/*
* 12-spi_sn74hc165.c
*
* Created: 2/16/2026 11:12:37 AM
* Author : Admin
*/
#include<avr/io.h>
#include<util/delay.h>
#define F_CPU 16000000UL
#define DDR_SPI DDRB
#define PRT_SPI PORTB
#define DD_SS 4
#define DD_MOSI 5
#define DD_MISO 6
#define DD_SCK 7
voidSPI_MasterInit(void)
{
/* Set MOSI and SCK output, all others input */
DDR_SPI = (1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_SS);
/* Enable SPI, Master, set clock rate fck/16 */
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
}
voidSPI_MasterTransmit(char cData)
{
/* Start transmission */
SPDR = cData;
/* Wait for transmission complete */
while(!(SPSR & (1<<SPIF)))
;
}
voidSPI_SlaveInit(void)
{
/* Set MISO output, all others input */
DDR_SPI = (1<<DD_MISO);
/* Enable SPI */
SPCR = (1<<SPE);
}
charSPI_SlaveReceive(void)
{
/* Wait for reception complete */
while(!(SPSR & (1<<SPIF)))
;
/* Return Data Register */
return SPDR;
}
intmain(void)
{
/* Replace with your application code */
SPI_MasterInit();
//PRT_SPI|=(1<<DD_SS);
DDRD=0xFF;
_delay_ms(1000);
while (1)
{
PRT_SPI&=~(1<<DD_SS);
PRT_SPI|=(1<<DD_SS);
SPI_MasterTransmit(0x00);
PORTD=SPI_SlaveReceive();
_delay_ms(100);
}
}
I don't have this chip because local stores don't stock this part. So I can test it only in simulator.
SPI Transfer and Receive One Byte Of Data Waveform
We can connect multiple SN74HC165N chips using a daisy-chain configuration. Here I scan two input ports of two SN74HC165N. So the master MCU needs to shift 16 clock cycles (two bytes of data).
The MCP23S17 is an SPI 16-bit (two ports) bi-directional GPIO expansion chip. Its TWI version is the MCP23017. It's rich of functions, data direction, control, distinct input and output registers, programmable weak pull up resistors, interrupt capabilities etc. Its configuration setting is very similar to most of PIC micro-controller since it is produced by Microchip Technology.
MCP23S17 I/O Ports Programming
It contains two 8-bit bi-directional port, GPA and GPB. Both of them are programmable with additional interrupt features. Its two interrupt flag pins correspond to each ports.
MCP23S17-E/SP
The SPI version of this chip is very similar to the I2C version.
Package Types
Its SPI interface is bi-directional since it's a general purpose I/O chip. Its four SPI pins are Chip Select(CS), Serial Clock(SCK), Serial In(SI) and Serial Out(SO). Its three optional address pins are A2:A0. We can connect them to GND to set the SPI slave write address to 0x40 and 0x41 for read address.
Functional Block Diagram
The programmer must configure the following registers to get a proper operation.
Register Addressed
By default we don't set the IOCON register the default bank is BANK0 ranges from 0x00 to 0x15. For example the IODIRB locates at 0x01 and its output register OLATB locates at 0x15.
SPI ADDRESSING REGISTERS
The idle state of CS pin is high. To complete a write or read operation the master MCU has to send at least 3 write operations.
ATMega644 and MCP23S17 SPI Programming
The write operations contain three data bytes, slave write address, register address and data. For example to make GPA as output we must do the following step:
Set CS pin low
Send its slave address 0x40 (A2:A0=0)
Send IODIRA address 0x00
Send data direction 0x00 that is output direction
Clear CS pin
Here the output ports GPA and GPB are output. They blink LED(s) every half second.
And then we enable the weak pull up resistors of GPB:
Set CS pin low
Send its slave address 0x40 (A2:A0=0)
Send GPPUB address 0x0D
Send 0xFF to turn on all its pull up resistors
Clear CS pin
The read operation also contains three data bytes, slave read address, register address, any data and read operation. For instance GPB is already an input port. So the MCU needs to read its input data:
Set CS pin low
Send its slave address 0x41 (A2:A0=0)
Send GPIOB address 0x13
Send one byte of data (eg:0x00) for shift data out from MCP23S17 SO pin
Perform the SPI read operation
Clear CS pin
This example GPB of MCP23S17 is an input port with weak pull resistors enabled. The master MCU read data from this port and send it back to GPA output port.
The Serial Peripheral Interface (SPI) is a high speed serial communication that commonly use three wires, serial clock, serial data and enable (master transmit only). Most of modern micro-controller has this serial communication interface. It is useful for board to board or to device communication due to wiring complexity compare to parallel port data transmission.
SN74HC595N LED Driving
SPI is popular among graphical display, Flash memory, I/O expansion chip, SRAM, EEPROM, real time clock chip etc. Its transmission and reception speed could reach up to several Mbits/s. These are some example of SPI devices:
DS3234 Real Time Clock
Nokia 5510 graphical LCD module
MCP23S17 I/O Expansion Chip
SN74HC595N Serial In Parallel Out Shift Registers
ILI9341/ST7785 240x320 TFT Display
ST7735 128x160 TFT Display
W25Q64JVSSIQ Flash Memory Chip
SD MMC Card etc.
W25Q64JVSSIQ
ILI9341/ST7785 240x320 TFT Display
Some earlier MCU doesn't have an SPI module inside for instance the AT89S52 or PIC16F84A. However the programmer could emulate a software SPI by using the bit-banging technique. This method is popular but it yield a lower speed data transmission and reception.
Typically a system operation contains a master MCU that transmit and receive data from its connected SPI slave devices.
Single master to single slave: basic SPI wiring
The pin names of the SPI module in the AVR micro-controller are different from other MCU such as PIC. These pins are:
Abbr.
Name
Description
SS
Slave Select
Active-lowchip selectsignal from master to enable communication with a specific slave device
The master MCU must select any connected SPI slave device via its slave select (SS) pin. One slave device has a unique SS pin. So multiple slave devices have many SS pins.
A typical hardware setup using two shift registers to form an inter-chip circular buffer
The SS pin is active low or active high upon the specific device.
Its clock polarity is commonly positive (low to high). Somes device use a negative (high to low) clock polarity.
However its clock polarity and phase are configured by user software that requires an understanding of technical detail of an MCU (Dedicated SPI Module).
The master MCU can communicates with many different SPI slave devices (Multidrop SPI bus) on a single bus using additional SS pins.
Multidrop SPI bus
If multiple SPI slave devices with the same type connect to a single SPI bus we can use the Daisy chain configuration. For instance the SN74HC595N or SN74HC164 serial in parallel out shift registers.
Daisy chain configuration
It is used for expanding a number of output ports for instance driving a dot matrix display.
Currently there are a lot of newer method of SPI data transmission that is precise and high speed.
ATMega644P Serial Peripheral Interface
The SPI module of the ATMega644P has the following features:
• Full-duplex, Three-wire Synchronous Data Transfer • Master or Slave Operation • LSB First or MSB First Data Transfer •Seven Programmable Bit Rates • End of Transmission Interrupt Flag • Write Collision Flag Protection • Wake-up from Idle Mode • Double Speed (CK/2) Master SPI Mode
Its operation mode and speed are configured in user program via its relevant special function registers.
SPI Block Diagram
Its clock generator is divided form the MCU clock up to CK/2. Data is shifted in and out using its internal 8-bit shift register at each clock cycle of the SPI clock generator. It also generate interrupt flag whenever the shift registers is empty.
SPI Master-slave Interconnection
These are its relevant registers:
SPCR – SPI Control Register
SPCR – SPI Control Register
SPSR – SPI Status Register
SPSR – SPI Status Register
SPDR – SPI Data Register
SPDR – SPI Data Register
The ATMega644P may operate in master transmit only, slave receive only or even full duplex (synchronous data transfer) depending on these registers. For more detail please see the device datasheet.
ATMega644P SPI Interfacing and Programming
We can configure the SPI module of the ATMega644P to operates in a specific mode depends on the worked application.
SPI Master Transmit Mode
An SPI master transmits only is common. There are many SPI slave device that only need data reception such as the SN74HC595N or 74HC164 shift register chips.
SN74HC595N and SN74HC164N Shift Registers
These chips is very popular due to its availability, low cost, ease of control etc. The SN74HC595N is commonly found in output expanding application such as relays driving, LED driving especially the dot matrix display driver.
SN74HC595N Pin Diagram
In this example the ATMega644P operate in master mode to transmit data to the SN74HC595N shifter register chip. The output port of this chip can connects to LED, 7-Segment displays or even a character LCD.
There are sample code of using the SPI transmit in device data sheet in both Assembly and C program.
Sample Code
LED Driving
I have a DIY single chip SN74HC595N LED module that is very easy to make. However we can install them together on a single breadboard.
An MCU operates in master or slave depends on software configuration. In this example one ATMega644P operates in master transmit only mode while another one operates in slave receive only.
Code examples show how to initialize the SPI as a Slave and how to perform a simple reception.
For master MCU the Slave Select (SS or PB4) pin (output mode) goes low whenever it start sending data out. For slave MCU whenever this pin (input mode) goes low data starts shift into its shift register at each clock cycle of SPI data transmission. However this pin is select-able by user software during transmission.
I have only one ATMega644 chip so I just simulate it using a simulator.
Here the master MCU read the ADC input with a variable voltage, convert it into voltage data before sending to its slave MCU. Thee slave MCU keep listening to the master receiving the voltage data and it will present on two 7-Segment displays.
Schematic
The contains three data bytes, slave adress (0xC1), decimal voltage value and fraction point.
The ATMega644P able to operate in full-duplex mode (three-wire synchronous data transfer). It means that during data transmission it also has data reception at the same time.
SPI Master-slave Interconnection
For master MCU data is shifted out via MOSI pin and shifted in via MISO pin during transmission. For slave MCU data is shifted in via MOSI pin and shifted out via MISO pin during reception. The Slave Select (SS) pin is ignore here because it's controlled by SPI hardware.
Schematic and Program Simulation
In this example a master ATMega644P send PORTD input data to its slave ATMega644P device. At slave side received data will show on PORTC and it send back PORTD input data to its master MCU. The master MCU show received data from slave device on PORTC.
Simulate Program Wave Form
Master MCU Source Code "main.c":
/*
* master.c
*
* Created: 2/15/2026 4:38:04 PM
* Author : Admin
*/
#include<avr/io.h>
#include<util/delay.h>
#define F_CPU 16000000UL
#define DDR_SPI DDRB
#define PRT_SPI PORTB
#define DD_MOSI 5
#define DD_MISO 6
#define DD_SCK 7
#define DD_SS 4
voidSPI_MasterInit(void)
{
/* Set MOSI and SCK output, all others input */
DDR_SPI = (1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_SS);
/* Enable SPI, Master, set clock rate fck/16 */
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
}
voidSPI_MasterTransmit(char cData)
{
/* Start transmission */
SPDR = cData;
/* Wait for transmission complete */
while(!(SPSR & (1<<SPIF)))
;
}
voidSPI_SlaveInit(void)
{
/* Set MISO output, all others input */
DDR_SPI = (1<<DD_MISO);
/* Enable SPI */
SPCR = (1<<SPE);
}
charSPI_SlaveReceive(void)
{
/* Wait for reception complete */
while(!(SPSR & (1<<SPIF)))
;
/* Return Data Register */
return SPDR;
}
intmain(void)
{
/* Replace with your application code */
SPI_MasterInit();
DDRD=0x00;
DDRC=0xFF;
PORTD=0xFF;
while (1)
{
//Send data bytes
//PRT_SPI&=~(1<<DD_SS);
SPI_MasterTransmit(PIND);
PORTC=SPI_SlaveReceive();
//PRT_SPI|=(1<<DD_SS);
_delay_ms(100);
}
}
Slave MCU Source Code "main.c":
/*
* slave.c
*
* Created: 2/15/2026 4:39:00 PM
* Author : Admin
*/
#include<avr/io.h>
#include<util/delay.h>
#define F_CPU 16000000UL
#define DDR_SPI DDRB
#define PRT_SPI PORTB
#define DD_MOSI 5
#define DD_MISO 6
#define DD_SCK 7
#define DD_SS 4
voidSPI_MasterInit(void)
{
/* Set MOSI and SCK output, all others input */
DDR_SPI = (1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_SS);
/* Enable SPI, Master, set clock rate fck/16 */
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
}
voidSPI_MasterTransmit(char cData)
{
/* Start transmission */
SPDR = cData;
/* Wait for transmission complete */
while(!(SPSR & (1<<SPIF)))
;
}
voidSPI_SlaveInit(void)
{
/* Set MISO output, all others input */
DDR_SPI = (1<<DD_MISO);
/* Enable SPI */
SPCR = (1<<SPE);
}
charSPI_SlaveReceive(void)
{
/* Wait for reception complete */
while(!(SPSR & (1<<SPIF)))
;
/* Return Data Register */
return SPDR;
}
intmain(void)
{
/* Replace with your application code */
SPI_SlaveInit();
DDRD=0x00;
DDRC=0xFF;
PIND=0xFF;
while (1)
{
PORTC=SPI_SlaveReceive();
SPI_MasterTransmit(PIND);
_delay_ms(10);
}
}
At this time I only one AVR device so I can not test it in physical hardware.