Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. // Define CPU Frequency
  2. #define F_CPU 8000000UL
  3.  
  4. // Some Includes
  5. #include <avr/io.h>
  6. #include <util/delay.h>
  7.  
  8. void SPI_SEND(unsigned char addr, char data) {
  9.     // CS Load
  10.     PORTB &= ~(1 << PB2);
  11.  
  12.     // Transmit Data (Address)
  13.     SPDR = addr;
  14.  
  15.     // Wait until completed
  16.     while(!(SPSR & (1<<SPIF)));
  17.  
  18.     // Transmit Data (Information)
  19.     SPDR = data;
  20.  
  21.     // Wait until complete
  22.     while(!(SPSR & (1<<SPIF)));
  23.  
  24.     // CS Load
  25.     PORTB |= (1 << PB2);
  26. }
  27.  
  28. void max7219_Init(void)
  29. {
  30.     /* STEUERBITS */
  31.     SPI_SEND ( 0x0C , 0x01 ) ;   // normal mode
  32.     SPI_SEND ( 0x0A , 0x0f ) ;   // set intensity 0x00 - 0x0F
  33.     SPI_SEND ( 0x0B , 0x07 ) ;   // scan digits 0,1
  34.     SPI_SEND ( 0x09 , 0x00 ) ;   // no decoding
  35. }
  36.  
  37. void spi_init (void)
  38. {
  39.     // Definition for output ports to Max7219.
  40.     // PORTB2, PORTB3, PORTB5 -> Ausgänge
  41.     DDRB |= (1 << PB3) | (1 << PB5) | (1 << PB2);
  42.  
  43.     // Define SPI for MAX7219
  44.     // SPE=1 - Turn SPI on
  45.     // MSTR=1 - Controller is Master
  46.     // SPR0=1 -Frequency to osc/16
  47.     SPCR |= ((1 << SPE) | (1 << MSTR)) | (1<<SPR0);
  48. }
  49.  
  50. void TurnOnAnimation (void){
  51.         for ( int z = 0; z <= 1; z++ ){
  52.             int a = 1;
  53.             for ( int x = 0; x <= 8; x++){
  54.                 for ( int y = 0; y <= 7; y++){
  55.                     _delay_ms(5);
  56.                     SPI_SEND(y,a);
  57.                 }
  58.                 a = a * 2;
  59.             }
  60.         }      
  61. }
  62.     /* 
  63.  
  64.              ROW 1
  65.  
  66.                A
  67.           ###########
  68.           #         #
  69. ROW 6 - F #    7    # B - ROW 2
  70.           #    G    #
  71.           ###########
  72.           #         #
  73. ROW 5 - E #         # C - ROW 3
  74.           #         #
  75.           ###########
  76.                D
  77.              ROW 4
  78.  
  79.     */ 
  80. // DigValues stores 8 byte for the display information
  81. unsigned char DigValues[] = {0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000};
  82.  
  83. // Display function
  84. void Display(int dig, int number)
  85. {
  86.     // Array with information about which bit has to be set to display the correct number
  87.     // numbers[0] -> Displays 0
  88.     // numbers[4] -> Displays 4
  89.     // ...
  90.     char numbers[] = {0b011111110,0b00001100,0b10110110,0b10011110,0b11001100,0b11011010,0b11111010,0b00001110,0b01111110,0b11111110,0b11011110};
  91.  
  92.     // Dig positioning byte
  93.     char bPosition = 0x00000001;
  94.  
  95.     // If [param]dig position is greater than 1
  96.     if ( dig > 1){
  97.         // shift bPosition left to the correct digit ([param]dig -1 )
  98.         bPosition = bPosition<<dig-1;
  99.     }
  100.  
  101.     // iterate through the numbers array
  102.     for(int x = 0; x < 8; x++){
  103.         // check if bit is high at the current location of our selected number
  104.         if ( numbers[number] & (1 << x) )
  105.         {
  106.             // if bitX of numbers is high just binary OR bPosition to the current DigValues value
  107.             DigValues[x] |= bPosition;
  108.         }else{
  109.             // if bitX of numbers is low, invert bPosition and binary AND it with current DigValues value to set bit low
  110.             DigValues[x] = (0b11111111 ^ bPosition) & DigValues[x] ;
  111.         }
  112.         // Send your information to max7219
  113.         SPI_SEND(x,DigValues[x]);
  114.     }
  115. }
  116.  
  117. void main (void)
  118. {
  119.  
  120.     /* Initialize SPI */
  121.     spi_init();
  122.  
  123.     /* Initialize MAX7219 */
  124.     max7219_Init();
  125.  
  126.     /* Run a tiny animation */
  127.     TurnOnAnimation();
  128.  
  129.     /* Write data to display */
  130.     Display(1,8);
  131.     Display(1,1);
  132.     Display(2,2);
  133.     Display(3,3);
  134.     Display(4,4);
  135.     Display(5,5);
  136.     Display(6,6);
  137.     Display(7,7);
  138.     Display(8,5);
  139.     Display(8,7);
  140.  
  141.     while(1)
  142.     {
  143.  
  144.     }
  145. }