// Define CPU Frequency
#define F_CPU 8000000UL
// Some Includes
#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 ( 0x0C , 0x01 ) ; // normal mode
SPI_SEND ( 0x0A , 0x0f ) ; // set intensity 0x00 - 0x0F
SPI_SEND ( 0x0B , 0x07 ) ; // scan digits 0,1
SPI_SEND ( 0x09 , 0x00 ) ; // no decoding
}
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 TurnOnAnimation (void){
for ( int z = 0; z <= 1; z++ ){
int a = 1;
for ( int x = 0; x <= 8; x++){
for ( int y = 0; y <= 7; y++){
_delay_ms(5);
SPI_SEND(y,a);
}
a = a * 2;
}
}
}
/*
ROW 1
A
###########
# #
ROW 6 - F # 7 # B - ROW 2
# G #
###########
# #
ROW 5 - E # # C - ROW 3
# #
###########
D
ROW 4
*/
// DigValues stores 8 byte for the display information
unsigned char DigValues[] = {0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000};
// Display function
void Display(int dig, int number)
{
// Array with information about which bit has to be set to display the correct number
// numbers[0] -> Displays 0
// numbers[4] -> Displays 4
// ...
char numbers[] = {0b011111110,0b00001100,0b10110110,0b10011110,0b11001100,0b11011010,0b11111010,0b00001110,0b01111110,0b11111110,0b11011110};
// Dig positioning byte
char bPosition = 0x00000001;
// If [param]dig position is greater than 1
if ( dig > 1){
// shift bPosition left to the correct digit ([param]dig -1 )
bPosition = bPosition<<dig-1;
}
// iterate through the numbers array
for(int x = 0; x < 8; x++){
// check if bit is high at the current location of our selected number
if ( numbers[number] & (1 << x) )
{
// if bitX of numbers is high just binary OR bPosition to the current DigValues value
DigValues[x] |= bPosition;
}else{
// if bitX of numbers is low, invert bPosition and binary AND it with current DigValues value to set bit low
DigValues[x] = (0b11111111 ^ bPosition) & DigValues[x] ;
}
// Send your information to max7219
SPI_SEND(x,DigValues[x]);
}
}
void main (void)
{
/* Initialize SPI */
spi_init();
/* Initialize MAX7219 */
max7219_Init();
/* Run a tiny animation */
TurnOnAnimation();
/* Write data to display */
Display(1,8);
Display(1,1);
Display(2,2);
Display(3,3);
Display(4,4);
Display(5,5);
Display(6,6);
Display(7,7);
Display(8,5);
Display(8,7);
while(1)
{
}
}