Advertisement
xerpi

Arduino 7 seg display w/shift register

Sep 22nd, 2012
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.81 KB | None | 0 0
  1. /* by xerpi */
  2.  
  3. static const byte segment_bits[11] =
  4. {
  5.  
  6. /*
  7. PORTD - digital 0-7
  8. PORTB - digital 8-13
  9. PORTC - analog  0-5
  10.  
  11.       a
  12.      ---
  13.       f | g | b
  14.      ---
  15.       e |   | c
  16.      --- .
  17.       d   DOT
  18.  
  19.       7
  20.      ---
  21.       6 | 5 | 8
  22.      ---
  23.       1 |   | 3
  24.      --- .
  25.       2   4
  26.  
  27. */
  28.     0b11100111, // 0
  29.     0b10000100, // 1
  30.     0b11010011, // 2
  31.     0b11010110, // 3
  32.     0b10110100, // 4
  33.     0b01110110, // 5
  34.     0b01110111, // 6
  35.     0b11000100, // 7
  36.     0b11110111, // 8
  37.     0b11110100, // 9
  38.     0b00001000, // DOT
  39. };
  40.  
  41. /*
  42. 1) DATA -> 0 or 1
  43. 2) CLOCK -> LOW
  44. 3) delay 1us ?¿
  45. 4) CLOCK -> HIGH (Here data enters to the register)
  46. 5) delay 1us ?¿
  47. 6) End
  48. */
  49.  
  50.  
  51. #define _BIT(n) (1<<n)
  52. #define _NOP() asm("nop\n")
  53.  
  54. #define DDRB_OUTPUT(pin) (DDRB |= _BIT(pin))
  55. #define DDRB_INPUT(pin)  (DDRB &= ~_BIT(pin))
  56.  
  57. #define PORTB_HIGH(pin) (PORTB |= _BIT(pin))
  58. #define PORTB_LOW(pin)  (PORTB &= ~_BIT(pin))
  59.  
  60. #define PINB_STATUS(pin) (PINB & _BIT(pin))
  61.  
  62. #define DATA_PIN    0 //Digital pin 8
  63. #define CLK_PIN     1 //Digital pin 9
  64. #define MR_PIN      2 //Digital pin 10 (not necessary)
  65.  
  66. #define RESET_MR() {PORTB_LOW(MR_PIN); _NOP(); PORTB_HIGH(MR_PIN);}
  67. #define SEND_CLK() {PORTB_LOW(CLK_PIN);_NOP();PORTB_HIGH(CLK_PIN);_NOP();}
  68.  
  69. void sendBinary(byte number)
  70. {
  71.     for(int i = 7; i > -1; i--)
  72.     {
  73.         (number & _BIT(i)) ? PORTB_LOW(DATA_PIN) : PORTB_HIGH(DATA_PIN);
  74.         SEND_CLK();
  75.     }
  76. }
  77.  
  78. void sendNumber(int number)
  79. {
  80.     if((number > -1) && (number < 11))
  81.     {
  82.         sendBinary(segment_bits[number]);
  83.     }
  84. }
  85.  
  86. void CLEAR_ALL()
  87. {
  88.     sendBinary(0x0);   
  89. }
  90.  
  91. void setup()
  92. {
  93.     DDRB_OUTPUT(DATA_PIN);
  94.     DDRB_OUTPUT(CLK_PIN);
  95.     DDRB_OUTPUT(MR_PIN);
  96.    
  97.     PORTB_HIGH(DATA_PIN);
  98.     PORTB_HIGH(MR_PIN);
  99. }
  100.  
  101.  
  102.  
  103. void loop()
  104. {
  105.     for(int i = 0; i < 10; i++)
  106.     {
  107.         sendNumber(i);
  108.         delay(350);
  109.        
  110.     }
  111.     for(int i = 10; i > 0; i--)
  112.     {
  113.         sendNumber(i);
  114.         delay(350);
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement