SPI Communication β ATmega16 (Slave Mode) π Introduction
This project demonstrates how to use the ATmega16 as an SPI Slave. The slave waits for data from the SPI Master, reads incoming bytes, and controls an LED based on the received character. If the master sends 'a', the LED turns ON. If the master sends 'b', the LED turns OFF.
This gives a clear understanding of SPI receiving logic and slave configuration.
Requirements βοΈ
ATmega16 (Slave MCU)
ATmega16 or any SPI Master device
Atmel Studio 7
Proteus 8 Professional
LED + 330Ξ© resistor
Jumper wires / simulation setup
SPI Pin Connections (Slave Mode) π Pin Function Direction PB5 MOSI Input PB6 MISO Output PB7 SCK Input PB4 SS Input
LED is connected to PC0.
Circuit Layout (Simple Text) ATmega16 (Slave) PB5 β MOSI β Master MOSI PB6 β MISO β Master MISO PB7 β SCK β Master SCK PB4 β SS β Master SS PC0 β LED β Resistor β GND VCC β 5V GND β Ground
Program Code (SPI Slave) π§βπ» #define F_CPU 8000000UL #include <avr/io.h> #include <util/delay.h>
void SPI_Write(unsigned char data) { SPDR = data; while(!(SPSR & (1<<SPIF))); }
unsigned char SPI_Read() { while(!(SPSR & (1<<SPIF))); // Wait until data received return SPDR; }
unsigned char data;
int main(void)
{
DDRB &= ((1<<DDB4)) & ((1<<DDB5)) & ((1<<DDB7)); // SS, MOSI, SCK = Input
DDRB |= (1<<DDB6); // MISO Output
DDRC |= (1<<DDC0); // PC0 Output for LED
PORTC &= ((1<<PORTC0));
SPCR |= (1<<SPE); // Enable SPI in Slave Mode
while (1)
{
data = SPI_Read();
if(data == 'a')
{
PORTC |= (1<<PORTC0); // LED ON
}
if(data == 'b')
{
PORTC &= ~(1<<PORTC0); // LED OFF
}
}
}
How It Works π
Slave waits for SCK clock pulses from the Master.
When the Master sends a byte, it appears in SPDR.
The program checks the received value:
'a' β LED ON
'b' β LED OFF
MISO can be used if master expects a response.
Proteus Simulation π₯οΈ
Place Master and Slave ATmega16
Connect SPI lines (MISO/MOSI/SCK/SS) correctly
Add an LED to PC0 on the Slave
Load master and slave HEX files
Run the simulation
Youβll see the LED toggle based on the masterβs transmitted characters.
Output βοΈ
Master β 'a' β LED ON Master β 'b' β LED OFF