Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

TwoDigitsDisplay.cpp

a guest Jun 5th, 2016 21 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. #include "TwoDigitsDisplay.h"
  2.  
  3. TwoDigitsDisplay::TwoDigitsDisplay(
  4.     uint8_t pin_address_0,
  5.     uint8_t pin_address_1,
  6.     uint8_t pin_address_2,
  7.     uint8_t pin_write_0,
  8.     uint8_t pin_write_1,
  9.     uint8_t pin_data
  10. ) : pin_address_0(pin_address_0),
  11.     pin_address_1(pin_address_1),
  12.     pin_address_2(pin_address_2),
  13.     pin_write_0(pin_write_0),
  14.     pin_write_1(pin_write_1),
  15.     pin_data(pin_data)
  16. {
  17.     pinMode(pin_address_0, OUTPUT);
  18.     pinMode(pin_address_1, OUTPUT);
  19.     pinMode(pin_address_2, OUTPUT);
  20.     pinMode(pin_write_0, OUTPUT);
  21.     pinMode(pin_write_1, OUTPUT);
  22.     pinMode(pin_data, OUTPUT);
  23. }
  24.  
  25. uint8_t TwoDigitsDisplay::bcdToSevenSegmentBits(uint8_t bcd) // Input from 0 to 9, output binary 7 segment data (bits 0 to 6)
  26. {
  27.     switch (bcd) {
  28.         case 0: return B00111111;
  29.         case 1: return B00001100;
  30.         case 2: return B01011011;
  31.         case 3: return B01011110;
  32.         case 4: return B01101100;
  33.         case 5: return B01110110;
  34.         case 6: return B01110111;
  35.         case 7: return B00011100;
  36.         case 8: return B01111111;
  37.         case 9: return B01111110;
  38.         default: return 0;
  39.     }
  40. }
  41.  
  42. void TwoDigitsDisplay::setBit(uint8_t digit, uint8_t address, bool bit)
  43. {
  44.     digitalWrite(pin_address_0, (address & B00000001) ? HIGH : LOW);
  45.     digitalWrite(pin_address_1, (address & B00000010) ? HIGH : LOW);
  46.     digitalWrite(pin_address_2, (address & B00000100) ? HIGH : LOW);
  47.  
  48.     digitalWrite(pin_data, bit ? LOW : HIGH);
  49.  
  50.     switch (digit) {
  51.         case 0:
  52.         delayMicroseconds(5);
  53.         digitalWrite(pin_write_0, LOW); // Write disable off
  54.         delayMicroseconds(5);
  55.         digitalWrite(pin_write_0, HIGH); // Write disable on
  56.         delayMicroseconds(5);
  57.         break;
  58.  
  59.         case 1:
  60.         delayMicroseconds(5);
  61.         digitalWrite(pin_write_1, LOW); // Write disable off
  62.         delayMicroseconds(5);
  63.         digitalWrite(pin_write_1, HIGH); // Write disable on
  64.         delayMicroseconds(5);
  65.         break;
  66.     }
  67. }
  68.  
  69. void TwoDigitsDisplay::setValue(uint8_t value)
  70. {
  71.     if (value > 99)
  72.         value = 99;
  73.     else if (value < 0)
  74.         value = 0;
  75.    
  76.     uint8_t dataDigitLeft  = bcdToSevenSegmentBits(value / 10);
  77.     uint8_t dataDigitRight = bcdToSevenSegmentBits(value % 10);
  78.  
  79.     for (uint8_t address = 0; address <= 7; address++) {
  80.         setBit(0, address, (dataDigitRight & (1 << address)) ? true : false);
  81.         setBit(1, address, (dataDigitLeft  & (1 << address)) ? true : false);
  82.     }
  83. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top