Ch17 Spi Protocol and Max7221 Display Interfacing
Ch17 Spi Protocol and Max7221 Display Interfacing
void SPI_MasterInit(void){
// Set MOSI, SCK and SS as Output Pins
DDRB |= (1<<MOSI) | (1<<SCK) | (1<<SS) ;
DDRB &= ~(1<<MISO); // Set MISO as an Input Pin
// Enable SPI, Master mode, Shift Clock = CLK /16
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
}
Write an AVR program to initialize the SPI for master, mode 0, with CLCK
frequency = Fosc/16, and then transmit 0 to 255 via SPI repeatedly. The
received data should be displayed on Port A and send back the received no.
///////////////// SPI MASTER TRANSMITTER PROGRAM PAGE 2/2 ////////////////
void SPI_SlaveInit(void){
DDRB |= (1<<MISO); // Set MISO as an Output Pin
// Set MOSI, SCK and SS as Input Pins
DDRB &= ~(1<<MOSI) & ~(1<<SCK) & ~(1<<SS) ;
// Enable SPI as a Slave Device
SPCR = (1<<SPE);
}
Write an AVR program to initialize the SPI for slave, mode 0, and Show the
received data on Port A
////////////////////// SPI SLAVE PROGRAM PAGE 2/2 ///////////////////////
unsigned char SPI_SlaveReceive(unsigned char cData){
SPDR = cData; // send cData to master
// Wait for reception complete
while(!(SPSR & (1<<SPIF)));
// Return received data
return SPDR;
}
int main(){
DDRA = 0xFF; // PortA is Output
SPI_SlaveInit(); // configure as SPI Slave
while(1)
// send value of PORTA to Master and
// Show Received data at PORTA
PORTA = SPI_SlaveReceive(PORTA);
return 0;
}
MAX7221 Interfacing and Prog…
7-Segment Display:
In many applications, when you want to
display numbers, 7-segments are the
best choice. These displays are made
of 7 LEDs to show different numbers
plus another LED to display the
decimal point.
MAX7221 Interfacing and Prog…
If you want to connect four 7-segment LEDs
directly to a microcontroller you need 4 x 8 =
32 pins. This is not feasible.
The MAX7221 IC is supports up to eight 7-
segment LEDs. We can connect the MAX7221 to
the AVR chip using SPI protocol and control up
to eight 7-segment LEDs.
The MAX7221 contains an internal decoder that
can be used to convert binary numbers to 7-
segment codes. That means we do not need to
refresh the 7-segment LEDs.
All you need to do is to send a binary number
to the MAX7221, and the chip decodes the
binary data and displays the number.
The device includes analog and digital
brightness control, an 8x8 static RAM that
stores each digit, and a test mode that forces
all LEDs on.
MAX7221 Pins and Connections
The MAX7221 is a 24-pin DIP
chip. It can be directly
connected to the AVR and con-
trol up to eight 7-segment
LEDs. A resistor or a
potentiometer is the only
external component that you
need. Next, we will discuss
the pins of the MAX7221.
execute(0x09,0x03);