You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.6 KiB

5 years ago
  1. /* SevenSegmentLEDdisplay102a.ino
  2. * 2017-02-20
  3. * Mel Lester Jr.
  4. * Simple example of using Shift Register with a
  5. * Single Digit Seven Segment LED Display
  6. */
  7. // Globals
  8. const int dataPin = 4; // blue wire to 74HC595 pin 14
  9. const int latchPin = 7; // green to 74HC595 pin 12
  10. const int clockPin = 8; // yellow to 74HC595 pin 11
  11. /* uncomment one of the following lines that describes your display
  12. * and comment out the line that does not describe your display */
  13. const char common = 'a'; // common anode
  14. //const char common = 'c'; // common cathode
  15. bool decPt = true; // decimal point display flag
  16. void setup() {
  17. // initialize I/O pins
  18. pinMode(dataPin, OUTPUT);
  19. pinMode(latchPin, OUTPUT);
  20. pinMode(clockPin, OUTPUT);
  21. }
  22. void loop() {
  23. decPt = !decPt; // display decimal point every other pass through loop
  24. // generate characters to display for hexidecimal numbers 0 to F
  25. for (int i = 0; i <= 15; i++) {
  26. byte bits = myfnNumToBits(i) ;
  27. if (decPt) {
  28. bits = bits | B00000001; // add decimal point if needed
  29. }
  30. myfnUpdateDisplay(bits); // display alphanumeric digit
  31. delay(500); // pause for 1/2 second
  32. }
  33. }
  34. void myfnUpdateDisplay(byte eightBits) {
  35. if (common == 'a') { // using a common anonde display?
  36. eightBits = eightBits ^ B11111111; // then flip all bits using XOR
  37. }
  38. digitalWrite(latchPin, LOW); // prepare shift register for data
  39. shiftOut(dataPin, clockPin, LSBFIRST, eightBits); // send data
  40. digitalWrite(latchPin, HIGH); // update display
  41. }
  42. byte myfnNumToBits(int someNumber) {
  43. switch (someNumber) {
  44. case 0:
  45. return B11111100;
  46. break;
  47. case 1:
  48. return B01100000;
  49. break;
  50. case 2:
  51. return B11011010;
  52. break;
  53. case 3:
  54. return B11110010;
  55. break;
  56. case 4:
  57. return B01100110;
  58. break;
  59. case 5:
  60. return B10110110;
  61. break;
  62. case 6:
  63. return B10111110;
  64. break;
  65. case 7:
  66. return B11100000;
  67. break;
  68. case 8:
  69. return B11111110;
  70. break;
  71. case 9:
  72. return B11110110;
  73. break;
  74. case 10:
  75. return B11101110; // Hexidecimal A
  76. break;
  77. case 11:
  78. return B00111110; // Hexidecimal B
  79. break;
  80. case 12:
  81. return B10011100; // Hexidecimal C or use for Centigrade
  82. break;
  83. case 13:
  84. return B01111010; // Hexidecimal D
  85. break;
  86. case 14:
  87. return B10011110; // Hexidecimal E
  88. break;
  89. case 15:
  90. return B10001110; // Hexidecimal F or use for Fahrenheit
  91. break;
  92. default:
  93. return B10010010; // Error condition, displays three vertical bars
  94. break;
  95. }
  96. }