Aim:
The Serial Peripheral Interface Bus (SPI) interface is used for
communication between multiple devices over short distances, and at high
speed.
Typically there is a single "master" device, which initiates
communications and supplies the clock which controls the data transfer
rate. There can be one or more slaves. For more than one slave, each one
has its own "slave select" signal, described later.
The Serial Peripheral Interface Bus (SPI) interface is used for
communication between multiple devices over short distances, and at high
speed.
Typically there is a single "master" device, which initiates
communications and supplies the clock which controls the data transfer
rate. There can be one or more slaves. For more than one slave, each one
has its own "slave select" signal, described later.
Description:
SPI signals
In a full-blown SPI system you will have four signal lines:
- Master Out, Slave In (MOSI) - which is the data going from the master to the slave
- Master In, Slave Out (MISO) - which is the data going from the slave to the master
- Serial Clock (SCK) - when this toggles both the master and the slave sample the next bit
- Slave Select (SS) - this tells a particular slave to go "active"
When multiple slaves are connected to the MISO signal they are expected
to tri-state (keep at high impedance) that MISO line until they are
selected by Slave Select being asserted. Normally Slave Select (SS) goes
low to assert it. That is, it is active low. Once a particular slave is
selected it should configure the MISO line as an output so it can send
data to the master.
This image shows the way that data is exchanged as one byte is sent:
Note that three signals are outputs from the master (MOSI, SCK, SS) and one is an input (MISO).
In a full-blown SPI system you will have four signal lines:
- Master Out, Slave In (MOSI) - which is the data going from the master to the slave
- Master In, Slave Out (MISO) - which is the data going from the slave to the master
- Serial Clock (SCK) - when this toggles both the master and the slave sample the next bit
- Slave Select (SS) - this tells a particular slave to go "active"
When multiple slaves are connected to the MISO signal they are expected
to tri-state (keep at high impedance) that MISO line until they are
selected by Slave Select being asserted. Normally Slave Select (SS) goes
low to assert it. That is, it is active low. Once a particular slave is
selected it should configure the MISO line as an output so it can send
data to the master.
This image shows the way that data is exchanged as one byte is sent:
Note that three signals are outputs from the master (MOSI, SCK, SS) and one is an input (MISO).
Wiring for output-only SPI
The above code (which sends only) might be used to drive an output
serial shift register. These are output-only devices, so we don't need
to worry about any incoming data. In their case the SS pin might be
called the "store" or "latch" pin.
Examples of this are the 74HC595 serial shift register, and various LED
strips, just to mention a couple. For example, this 64 pixel LED display
driven by a MAX7219 chip:
In this case you can see that the board maker has used slightly different signal names:
- DIN (Data In) is MOSI (Master Out, Slave In)
- CS (Chip Select) is SS (Slave Select)
- CLK (Clock) is SCK (Serial Clock)
Most boards will follow a similar pattern. Sometimes DIN is just DI (Data In).
Here is another example, this time a 7-segment LED display board (also based on the MAX7219 chip):
The above code (which sends only) might be used to drive an output
serial shift register. These are output-only devices, so we don't need
to worry about any incoming data. In their case the SS pin might be
called the "store" or "latch" pin.
Examples of this are the 74HC595 serial shift register, and various LED
strips, just to mention a couple. For example, this 64 pixel LED display
driven by a MAX7219 chip:
In this case you can see that the board maker has used slightly different signal names:
- DIN (Data In) is MOSI (Master Out, Slave In)
- CS (Chip Select) is SS (Slave Select)
- CLK (Clock) is SCK (Serial Clock)
Most boards will follow a similar pattern. Sometimes DIN is just DI (Data In).
Here is another example, this time a 7-segment LED display board (also based on the MAX7219 chip):
Block Diagram
Schematic
Code
// *******************************************************
// Project: Interfacing SPI-MAX72xx using AVR (Atmega-8)
// Author: Hack Projects India
// Module description: Operate DAC
// *******************************************************
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
void SPI_SEND(unsigned char addr, char data) {
// CS Load
PORTB &= (~(1<<PB2));
// Transmit Data (Address)
SPDR = addr;
// Wait until completed
while(!(SPSR&(1<<SPIF)));
// Transmit Data (Information)
SPDR = data;
// Wait until complete
while(!(SPSR&(1<<SPIF)));
// CS Load
PORTB |= (1 << PB2);
}
void max7219_Init(void)
{
/* STEUERBITS */
SPI_SEND(0x0a,0x0f);
SPI_SEND(0x09,0xff);
SPI_SEND(0x0b,0x07);
SPI_SEND(0x0c,0x01);
}
void spi_init (void)
{
// Definition for output ports to Max7219.
// PORTB2, PORTB3, PORTB5 -> Ausgänge
DDRB = (1 << PB3) | (1 << PB5) | (1 << PB2);
// Define SPI for MAX7219
// SPE=1 - Turn SPI on
// MSTR=1 - Controller is Master
// SPR0=1 -Frequency to osc/16
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
}
void main (void)
{
/* Initialize SPI */
spi_init();
/* Initialize MAX7219 */
max7219_Init();
SPI_SEND(0x01,0x08);
SPI_SEND(0x02,0x07);
SPI_SEND(0x03,0x06);
SPI_SEND(0x04,0x05);
SPI_SEND(0x05,0x04);
SPI_SEND(0x06,0x03);
SPI_SEND(0x07,0x02);
SPI_SEND(0x08,0x01);
while(1)
{
}
}
Downloads:
The code was compiled in Keil uvision4 and simulation was made in Proteus v7.7.
To download code and proteus simulation click here.
To download code and proteus simulation click here.
Further Reading suggestions:
You may also like,
- Interfacing keypad with AVR
- nterfacing DAC with AVR
- Interfacing with UART of AVR controller
- Interfacing SPI communication with AVR
- AVR Displaying Custom Characters on LCD
- AVR Graphical LCD
- RTC interfacing using I2C in AVR
- Interfacing ultrasonic sensor with AVR
- Interfacing GPS Modu with AVR
- Interfacing GSM Module with AVR
- Interfacing PWM in AVR
- Interfacing ADC with AVR
- Scrolling string on LCD using AVR
No comments:
Post a Comment