Advertisement
xerpi

Arduino SegDisp Lib v1.0 by xerpi

Sep 16th, 2012
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. /* ------------------------------------------------ SegDisp.h ------------------------------------------------ */
  2.  
  3. /* SegDisp by xerpi */
  4.  
  5.  
  6. #ifndef _SEGDISP_H_
  7. #define _SEGDISP_H_
  8.  
  9. #include <avr/io.h>
  10.  
  11. #ifndef byte
  12.     typedef unsigned char byte;
  13. #endif
  14.  
  15. static const byte segment_bits[11] =
  16. {
  17.  
  18. /*
  19. PORTD - digital 0-7
  20. PORTB - digital 8-13
  21. PORTC - analog  0-5
  22.  
  23.       a
  24.      ---
  25.       f | g | b
  26.      ---
  27.       e |   | c
  28.      --- .
  29.       d   DOT
  30.  
  31.       7
  32.      ---
  33.       6 | 5 | 8
  34.      ---
  35.       1 |   | 3
  36.      --- .
  37.       2   4
  38.  
  39. */
  40.     0b11100111, // 0
  41.     0b10000100, // 1
  42.     0b11010011, // 2
  43.     0b11010110, // 3
  44.     0b10110100, // 4
  45.     0b01110110, // 5
  46.     0b01110111, // 6
  47.     0b11000100, // 7
  48.     0b11110111, // 8
  49.     0b11110100, // 9
  50.     0b00001000, // DOT
  51. };
  52.  
  53. #define DOT 10
  54.  
  55. typedef enum{
  56.     COMMON_CATHODE =    1,
  57.     COMMON_ANODE   =    0
  58. }COMMON_PIN;
  59.  
  60. class SegDisp
  61. {
  62. public:
  63.  
  64.     SegDisp(void);
  65.     SegDisp(int _initial_pin, COMMON_PIN _common_pin);
  66.  
  67.     inline void initialPin(int _initial_pin);
  68.     inline void commonPin(COMMON_PIN _common_pin);
  69.  
  70.     void showBinary(byte number);
  71.     void show(int number, bool show_dot = false);
  72.     inline void show(int _initial_pin, int number, COMMON_PIN _common_pin, bool show_dot = false);
  73.  
  74. private:
  75.  
  76.     int initial_pin;
  77.     COMMON_PIN common_pin;
  78. };
  79.  
  80. #endif
  81.  
  82.  
  83. /* ------------------------------------------------ SegDisp.cpp ------------------------------------------------ */
  84.  
  85.  
  86. /* SegDisp by xerpi */
  87.  
  88. #include "SegDisp.h"
  89.  
  90. SegDisp::SegDisp(void)
  91. {
  92.     initialPin(0);
  93.     commonPin(COMMON_CATHODE);
  94. };
  95.  
  96.  
  97. SegDisp::SegDisp(int _initial_pin, COMMON_PIN _common_pin)
  98. {
  99.     initialPin(_initial_pin);
  100.     commonPin(_common_pin);
  101. }
  102.  
  103.  
  104. void SegDisp::initialPin(int _initial_pin)
  105. {
  106.     initial_pin = _initial_pin;
  107.     if(initial_pin > 0)
  108.     {
  109.         DDRD = 0xFF<<initial_pin;
  110.         DDRB = (0xFF<<initial_pin) ^ 0xFF;
  111.     }
  112.     else
  113.     {
  114.         DDRD = 0xFF;
  115.     }
  116. }
  117.  
  118.  
  119. void SegDisp::commonPin(COMMON_PIN _common_pin)
  120. {
  121.     common_pin = _common_pin;
  122. }
  123.  
  124. void SegDisp::showBinary(byte number)
  125. {
  126.     if(common_pin == COMMON_ANODE)
  127.     {
  128.         number ^= 0xFF;
  129.     }
  130.  
  131.     if(initial_pin > 0)
  132.     {
  133.         PORTD = number<<initial_pin;
  134.         PORTB = (number>>(8 - initial_pin));
  135.     }
  136.     else
  137.     {
  138.         PORTD = number;
  139.     }
  140. }
  141.  
  142.  
  143. void SegDisp::show(int number, bool show_dot)
  144. {
  145.     if(number < 0 || number > 10)
  146.     {
  147.         showBinary(0x0);
  148.     }
  149.     else
  150.     {
  151.         showBinary(show_dot ? segment_bits[number] | 0b1000 : segment_bits[number]);
  152.     }
  153. }
  154.  
  155.  
  156. void SegDisp::show(int _initial_pin, int number, COMMON_PIN _common_pin, bool show_dot)
  157. {
  158.     initialPin(_initial_pin);
  159.     commonPin(_common_pin);
  160.     show(number, show_dot);
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement