Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1.  
  2. // GLOBAL VARIABLES
  3. unsigned long beg_time; // the time right before the current number was initially displayed
  4. int curr_num = 0; // the current number being displayed, start at 0
  5. // loop through the 10 numbers in the array. Each contains the 5 columns that are used for the persistance of vision of
  6. // the particular number
  7. byte numbers[][5] = {{0x3E, 0x51, 0x49, 0x45, 0x3E},{0x00, 0x42, 0x7F, 0x40, 0x00},{0x42, 0x61, 0x51, 0x49, 0x46},
  8. {0x21, 0x41, 0x45, 0x4B, 0x31},{0x18, 0x14, 0x12, 0x7F, 0x10},{0x27, 0x45, 0x45, 0x45, 0x39},
  9. {0x3C, 0x4A, 0x49, 0x49, 0x30},{0x01, 0x71, 0x09, 0x05, 0x03},{0x36, 0x49, 0x49, 0x49, 0x36},
  10. {0x06, 0x49, 0x49, 0x29, 0x1E}}; // an array of arrays containing each number's 5 LED column byte sequences
  11.  
  12. void setup()
  13. {
  14. // clock is PORTB0, latch is PORTB1, data is PORTB2
  15. DDRB = DDRB | B00000111; // set latch, clock and data to outputs
  16. beg_time = millis(); // set the beginning time for the first 0
  17. }
  18.  
  19. // Prof. Rajit said this way of doing it was ok
  20. void loop()
  21. {
  22. // if the current number has been displayed for more than 1 second, change
  23. if (millis() - beg_time > 1000) {
  24. beg_time = millis(); // the beginning time of the new number
  25. // if the number is 9, then change it back to 0, otherwise increment
  26. if (curr_num >= 9) {
  27. curr_num = 0;
  28. } else {
  29. curr_num++;
  30. }
  31. }
  32.  
  33. // loop through the five columns of the 5x7 display
  34. for (int j=0; j < 5; j++) {
  35. // the baseline bits to set all LEDs in the column off
  36. // NOTE: this is LSB first, so leds[0] is the first pin and so on. The last 4 don't matter,
  37. // since we don't use the last 4 output pins of the second shift register
  38. int leds[16] = {1,0,1,0,0,0,1,1,0,1,0,0,0,0,0,0};
  39.  
  40. // our custom function that takes in the desired column as a byte and the array of
  41. // output pins on the shift registers and changes the array to appropriately set pins
  42. // on the display. Ex. if pin 1 is low and pin 2 is high, the third row, first column
  43. // LED is lit up (Understanding of the data sheet)
  44. convert(j, numbers[curr_num][j], leds);
  45.  
  46. PORTB = PORTB & B11111101; // sets latch LOW
  47.  
  48. // set all the output pins of the shift register
  49. for (int i=15; i>=0; i--) {
  50. PORTB = PORTB & B11111010; // sets clk and data to LOW
  51. PORTB = PORTB | leds[i] << 2; // sets data to leds[i]
  52. PORTB = PORTB | B00000001; // sets digital pin 8 to HIGH
  53. }
  54.  
  55. PORTB = PORTB | B00000010; // sets digital pin 9 to HIGH
  56. }
  57. }
  58.  
  59. void convert(int col, byte values, int *def) {
  60. // check if any LED was high
  61. bool saw_high = false;
  62. // for the 2nd column, the actual pin is 3, so adjust these values
  63. // based on the LED matrix's data sheet
  64. int real_col = 0;
  65. if (col == 0) {
  66. real_col = 0;
  67. } else if (col == 1) {
  68. real_col = 2;
  69. } else if (col == 2) {
  70. real_col = 9;
  71. } else if (col == 3) {
  72. real_col = 6;
  73. } else if (col == 4) {
  74. real_col = 7;
  75. }
  76. // loop through the first 7 values of the byte storing the column
  77. // LEDs (the 8th is useless, since there are only 7 values to represent).
  78. // If they are high, set the appropriate shift register output pin to
  79. // high
  80. for (int i=0; i<7; i++) {
  81. if (((values >> i) & 0x01) == 1) {
  82. if (i == 0) {
  83. def[11] = 1;
  84. } else if (i == 1) {
  85. def[10] = 1;
  86. } else if (i == 2) {
  87. def[1] = 1;
  88. } else if (i == 3) {
  89. def[8] = 1;
  90. } else if (i == 4) {
  91. def[3] = 1;
  92. } else if (i == 5) {
  93. def[4] = 1;
  94. } else if (i == 6) {
  95. def[5] = 1;
  96. }
  97. saw_high = true;
  98. }
  99. }
  100. // set the column to ground (so the circuit allows current to flow) if
  101. // any LED was seen to be on
  102. if (saw_high) {
  103. def[real_col] = 0;
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement