Advertisement
Guest User

Untitled

a guest
Mar 20th, 2016
1,813
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <wiringPi.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5.  
  6.  
  7. // define our pins :
  8.  
  9. #define Set_DIN(high) (high?(digitalWrite(7, HIGH)):(digitalWrite(7, LOW)))
  10. #define Set_CS(high) (high?(digitalWrite(8, HIGH)):(digitalWrite(8, LOW)))
  11. #define Set_CLK(high) (high?(digitalWrite(10, HIGH)):(digitalWrite(10, LOW)))
  12.  
  13. const uint8_t smile[8] = {
  14. 0b00000000,
  15. 0b01100110,
  16. 0b01100110,
  17. 0b00011000,
  18. 0b00011000,
  19. 0b10000001,
  20. 0b01000010,
  21. 0b00111100};
  22.  
  23. const uint8_t sad[8] = {
  24. 0b00000000,
  25. 0b01100110,
  26. 0b01100110,
  27. 0b00011000,
  28. 0b00011000,
  29. 0b00000000,
  30. 0b00111100,
  31. 0b01000010
  32. };
  33.  
  34.  
  35. void spi_send(uint8_t data)
  36. {
  37. uint8_t i;
  38.  
  39. for (i = 0; i < 8; i++, data <<= 1)
  40. {
  41. Set_CLK(0);//CLK_HIGH();
  42. if (data & 0x80)
  43. Set_DIN(1);//DATA_HIGH();
  44. else
  45. Set_DIN(0);//DATA_LOW();
  46. Set_CLK(1);//CLK_HIGH();
  47. }
  48. }
  49.  
  50. void max7219_writec(uint8_t high_byte, uint8_t low_byte)
  51. {
  52. Set_CS(0);//CS_LOW();
  53. spi_send(high_byte);
  54. spi_send(low_byte);
  55. Set_CS(1);//CS_HIGH();
  56. }
  57.  
  58. void max7219_clear(void)
  59. {
  60. uint8_t i;
  61. for (i = 0; i < 8; i++)
  62. {
  63. max7219_writec(i+1, 0);
  64. }
  65. }
  66.  
  67. void max7219_init(void)
  68. {
  69. // Decode mode: none
  70. max7219_writec(0x09, 0);
  71. // Intensity: 3 (0-15)
  72. max7219_writec(0x0A, 1);
  73. // Scan limit: All "digits" (rows) on
  74. max7219_writec(0x0B, 7);
  75. // Shutdown register: Display on
  76. max7219_writec(0x0C, 1);
  77. // Display test: off
  78. max7219_writec(0x0F, 0);
  79. max7219_clear();
  80. }
  81.  
  82. uint8_t display[8];
  83.  
  84. void update_display(void)
  85. {
  86. uint8_t i;
  87.  
  88. for (i = 0; i < 8; i++)
  89. {
  90. max7219_writec(i+1, display[i]);
  91. }
  92. }
  93.  
  94. void image(const uint8_t im[8])
  95. {
  96. uint8_t i;
  97.  
  98. for (i = 0; i < 8; i++)
  99. display[i] = im[i];
  100. }
  101.  
  102. void set_pixel(uint8_t r, uint8_t c, uint8_t value)
  103. {
  104. switch (value)
  105. {
  106. case 0: // Clear bit
  107. display[r] &= (uint8_t) ~(0x80 >> c);
  108. break;
  109. case 1: // Set bit
  110. display[r] |= (0x80 >> c);
  111. break;
  112. default: // XOR bit
  113. display[r] ^= (0x80 >> c);
  114. break;
  115. }
  116. }
  117.  
  118.  
  119.  
  120. int main (void)
  121. {
  122. wiringPiSetup();
  123. pinMode(7, OUTPUT);
  124. pinMode(8, OUTPUT);
  125. pinMode(10, OUTPUT);
  126.  
  127. uint8_t i;
  128.  
  129. max7219_init();
  130.  
  131. while(1)
  132. {
  133. image(sad);
  134. update_display();
  135. usleep(5000);
  136. image(smile);
  137. update_display();
  138. usleep(5000);
  139.  
  140. // Invert display
  141. for (i = 0 ; i < 8*8; i++)
  142. {
  143. set_pixel(i / 8, i % 8, 2);
  144. update_display();
  145. usleep(5000);
  146. }
  147. usleep(5000);
  148. }
  149.  
  150. return 0;
  151. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement