Advertisement
elektronek

Miroslav Škoda - MAX7219 - čísla

Mar 15th, 2021 (edited)
962
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.86 KB | None | 0 0
  1. /*
  2.  * M328P-matrix-7219-text.c
  3.  *
  4.  * Created: 13. 12. 2019 22:17:17
  5.  * Author : martin
  6.  */
  7.  
  8. #define F_CPU 16000000ul
  9. #include <avr/io.h>
  10. #include <util/delay.h>
  11.  
  12. #define NUMMAX  4   // sem dej po4et MAXu za sebou
  13. #define CLK_HIGH()  PORTB |= (1<<PB5)
  14. #define CLK_LOW()   PORTB &= ~(1<<PB5)
  15. #define CS_HIGH()   PORTB |= (1<<PB2)
  16. #define CS_LOW()    PORTB &= ~(1<<PB2)
  17. #define DATA_HIGH() PORTB |= (1<<PB3)
  18. #define DATA_LOW()  PORTB &= ~(1<<PB3)
  19. #define GPIO_INIT() DDRB |= (1<<PB5) | (1<<PB3) | (1<<PB2)
  20.  
  21. void spi_send(uint8_t data)
  22. {
  23.   uint8_t i;
  24.   for (i = 0; i < 8; i++, data <<= 1)
  25.   {
  26.     CLK_LOW();
  27.     if (data & 0x80)
  28.     DATA_HIGH();
  29.     else
  30.     DATA_LOW();
  31.     CLK_HIGH();
  32.   }
  33.  
  34. }
  35.  
  36. void max7219_writec(uint8_t high_byte, uint8_t low_byte)
  37. {
  38.   CS_LOW();
  39.   spi_send(high_byte);
  40.   spi_send(low_byte);
  41.   CS_HIGH();
  42. }
  43.  
  44. void max7219_init(void)
  45. {
  46.   GPIO_INIT();
  47.   for (uint8_t a=1; a<=NUMMAX; a++)
  48.   {
  49.     // Decode mode: none
  50.     max7219_writec(0x09, 0xff);
  51.     // Intensity: 3 (0-15)
  52.     max7219_writec(0x0A, 5);
  53.     // Scan limit: All "digits" (rows) on
  54.     max7219_writec(0x0B, 7);
  55.     //Shutdown register: Display on
  56.     max7219_writec(0x0C, 1);
  57.     // Display test: off
  58.     max7219_writec(0x0F, 0);
  59.   }
  60. }
  61.  
  62.  
  63. int main(void)
  64. {
  65.   max7219_init();
  66.   // ALL OFF
  67.   for (uint8_t b=1; b<=8; b++)
  68.     for (uint8_t a=1; a<=NUMMAX; a++)
  69.       max7219_writec(b, 0);
  70.      
  71.   while (1)
  72.   {
  73.     // Vypis na mista 1-8
  74.     for (uint8_t b=1; b<=8; b++)
  75.     {
  76.       for (uint8_t a=0; a<=9; a++)
  77.       {
  78.         for (uint8_t d=1; d<=NUMMAX; d++)
  79.           max7219_writec(9-b, b); // 9-b vypisujeme zleva
  80.         _delay_ms(100);
  81.       }
  82.       _delay_ms(100);
  83.     }
  84.     // smazani
  85.     for (uint8_t b=1; b<=8; b++)
  86.     {
  87.       for (uint8_t a=1; a<=NUMMAX; a++)
  88.         max7219_writec(b, 0);
  89.     }
  90.     _delay_ms(1000);
  91.   }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement