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 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.
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.
ATMega644 and MCP23S17 SPI Programming
The write operations contain three data bytes, slave write address, register address and data. For example to make GPIOA 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
The read operation also contains three data bytes, slave read address, register address, any data and read operation.
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.