GHJGHJJHG

Untitled

Dec 2nd, 2020
441
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SPI.h>
  2.  
  3. #define MAX7219_REG_SHUTDOWN 0x0C
  4. #define MAX7219_REG_SHUTDOWN_NORMAL 1
  5. #define MAX7219_REG_SHUTDOWN_SHUTDOWN 0
  6.  
  7. #define MAX7219_REG_DECODE 0x09
  8. #define MAX7219_REG_DECODE_NONE 0
  9.  
  10. #define MAX7219_REG_INTENSITY 0x0A
  11. #define MAX7219_REG_INTENSITY_MAX 0xF
  12.  
  13. #define MAX7219_REG_SCANLIMIT 0x0B
  14.  
  15. #define MAX7219_REG_MODE 0x0F
  16. #define MAX7219_REG_MODE_NORMAL 0
  17. #define MAX7219_REG_MODE_TEST 1
  18.  
  19. #define SS_MAX7219 9
  20. #define SLK 13
  21. #define MOSI 11
  22.  
  23. class MAX7219 {
  24. public:
  25.  
  26.   inline void beginTransaction() {
  27.     pinMode(SS_MAX7219, OUTPUT);
  28.     pinMode(SLK, OUTPUT);
  29.     pinMode(MOSI, OUTPUT);
  30.     digitalWrite(SS_MAX7219, HIGH);
  31.     delay(.1);
  32.     digitalWrite(SS_MAX7219, LOW);
  33.     delay(.1);
  34.     digitalWrite(SS_MAX7219, HIGH);
  35.     SPI.beginTransaction(SPISettings(140000, MSBFIRST, SPI_MODE0));
  36.   }
  37.   MAX7219() {
  38.     beginTransaction();
  39.     setScanLimit(7);
  40.     setIntensity(0x06);
  41.     setDecode(MAX7219_REG_DECODE_NONE);
  42.     setMode(MAX7219_REG_MODE_NORMAL);
  43.     setShutdown(MAX7219_REG_SHUTDOWN_SHUTDOWN);
  44.     setShutdown(MAX7219_REG_SHUTDOWN_NORMAL);
  45.   }
  46.  
  47.   void setScanLimit(uint8_t val) {
  48.     digitalWrite(SS_MAX7219, LOW);
  49.     SPI.transfer(MAX7219_REG_SCANLIMIT);
  50.     SPI.transfer(val);
  51.     digitalWrite(SS_MAX7219, HIGH);
  52.     delay(.1);
  53.   }
  54.  
  55.   void setIntensity(uint8_t val) {
  56.     digitalWrite(SS_MAX7219, LOW);
  57.     SPI.transfer(MAX7219_REG_INTENSITY);
  58.     SPI.transfer(val);
  59.     digitalWrite(SS_MAX7219, HIGH);
  60.     delay(.1);
  61.   }
  62.  
  63.   void setDecode(uint8_t val) {
  64.     digitalWrite(SS_MAX7219, LOW);
  65.     SPI.transfer(MAX7219_REG_DECODE);
  66.     SPI.transfer(val);
  67.     digitalWrite(SS_MAX7219, HIGH);
  68.     delay(.1);
  69.   }
  70.  
  71.   void setShutdown(uint8_t val) {
  72.     digitalWrite(SS_MAX7219, LOW);
  73.     SPI.transfer(MAX7219_REG_SHUTDOWN);
  74.     SPI.transfer(val);
  75.     digitalWrite(SS_MAX7219, HIGH);
  76.     delay(.1);
  77.   }
  78.  
  79.   void setMode(uint8_t val) {
  80.     digitalWrite(SS_MAX7219, LOW);
  81.     SPI.transfer(MAX7219_REG_MODE);
  82.     SPI.transfer(val);
  83.     digitalWrite(SS_MAX7219, HIGH);
  84.     delay(.1);
  85.    
  86.   }
  87.  
  88.   void writeDigit(uint8_t dig, uint8_t val) {
  89.     beginTransaction();
  90.     digitalWrite(SS_MAX7219, HIGH);
  91.     digitalWrite(SS_MAX7219, LOW);
  92.     SPI.transfer(dig+1);
  93.     SPI.transfer(val);
  94. //    SPI.transfer16( ((dig+1) << 8) | val);
  95.     digitalWrite(SS_MAX7219, HIGH);
  96.    
  97.   }
  98. };
  99.  
  100.  
  101. void setup() {
  102. //  Serial.begin(9600);
  103. }
  104.  
  105.  
  106. void loop() {
  107.   MAX7219 m;
  108.  
  109. //  for (uint8_t i= 0; i < 3; ++i)
  110. //  {
  111. //    m.writeDigit(i, 0xff);
  112. //    delay(400);
  113. //    m.writeDigit(i, 0x00);
  114. //    delay(10);
  115. //  }
  116.  
  117.   m.writeDigit(0, 0x5);
  118.   m.writeDigit(1, 0x2);
  119.   m.writeDigit(2, 0x5);
  120.   delay(700);
  121.   m.writeDigit(0, 0x2);
  122.   m.writeDigit(1, 0x7);
  123.   m.writeDigit(2, 0x2);
  124.   delay(700);
  125.   m.writeDigit(0, 0x0);
  126.   m.writeDigit(1, 0x0);
  127.   m.writeDigit(2, 0x0);
  128.   delay(700);
  129.  
  130. }
RAW Paste Data