In this example we connect a MAX7219 seven segment display to our ATmega16, the connection was as follows
VCC → 5V
GND → GND
DIN → PB5
CS → PB0
CLK → PB7
Schematic
Code
[c]
sbit Chip_Select at PORTB0_bit;
sbit Chip_Select_Direction at DDB0_bit;
char i;
void max7219_init1() {
Chip_Select = 0; // Select MAX7219
SPI1_Write(0x09); // BCD mode for digit decoding
SPI1_Write(0xFF);
Chip_Select = 1; // Deselect MAX7219
Chip_Select = 0; // Select MAX7219
SPI1_Write(0x0A);
SPI1_Write(0x0F); // Segment luminosity intensity
Chip_Select = 1; // Deselect MAX7219
Chip_Select = 0; // Select MAX7219
SPI1_Write(0x0B); // Set scan-limit
SPI1_Write(0x07); // Display all 8 digits
Chip_Select = 1; // Deselect MAX7219
Chip_Select = 0; // Select MAX7219
SPI1_Write(0x0C); // Set Shutdown register
SPI1_Write(0x01); // Normal operation
Chip_Select = 1; // Deselect MAX7219
Chip_Select = 0; // Select MAX7219
SPI1_Write(0xFF); // No test
SPI1_Write(0x00);
Chip_Select = 1; // Deselect MAX7219
}
void main() {
Chip_Select_Direction = 1; // Set PB0 pin as output
SPI1_Init(); // Initialize SPI module
max7219_init1(); // Initialize MAX7219
for (i = 1; i<=8; i++) {
Chip_Select = 0; // Select MAX7219
SPI1_Write(i); // Send i to MAX7219 (digit place)
SPI1_Write(8-i); // Send 8-i to MAX7219 (digit value)
Chip_Select = 1; // Deselect MAX7219
}
// The result “01234567” is written on the 7-Seg displays
}
[/c]
Links
MAX7219 CWG 8-Digital Tube Display Module Control Module