Skip to content

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.

Notifications You must be signed in to change notification settings

asathiskumar98-byte/SPI-Communication-ATmega16-Slave-Mode

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

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

About

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.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages