/* **********************************************************************
* ## Ready to build ##
* ATTiny85_7Seg_DHT22
* 7-SegmentDisplay - v1.0
* Uses (2) Common cathode 7-segment display,
* Uses (2) 74HC595 shift register IC
* Uses ATTiny85
*
* Description:
* This sketch illustrates the controlling of two 7-segment displays with
* two 74HC595 shift register. Charlieplexing is not used. Daisychained
* shift registers push 8 bits at a time from one display to the next.
* For a two digit display this is quite easy to code.
* Sketch uses 5236 bytes (63%) of program storage space. Maximum is 8192 bytes.
* Global variables use 80 bytes (15%) of dynamic memory, leaving 432 bytes for local variables. Maximum is 512 bytes.
*
* See spreadsheet file 74HC595_SegmentDisplay.ods
* Define the LED digit patterns, from 0 - 9
* 1 = LED on, 0 = LED off, common cathode, in this order:
* 74HC595 pin Q7,Q6,Q5,Q4,Q3,Q2,Q1,Q0
* Mapping to dp,g,f,e,d,c,b,a of Seven-Segment LED
* ********************************************************* */
#include <TinyDebug.h>
/********** Hardware Definitions **********/
int latchPin = PB0; // connect to the ST_CP of 74HC595 (pin 12,latch pin)
int dataPin = PB1; // connect to the DS of 74HC595 (pin 14, data pin)
int clockPin = PB2; // connect to the SH_CP of 74HC595 (pin 11, clock pin)
byte digits[10] = { B11111100, // = 0
B00110000, // = 1
B11011010, // = 2
B01111010, // = 3
B00110110, // = 4
B01101110, // = 5
B11100110, // = 6
B00111000, // = 7
B11111110, // = 8
B00111110 // = 9
};
byte sevenSegDP = B00000001; // = DP
/*---------- Function Declaration ----------*/
void writeDigits(byte r1, byte r2);
void writeSegment(byte segment);
void writeBlank();
void setup() {
Debug.begin();
// Set latchPin, clockPin, dataPin as output
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void loop() {
//*
// count to 99
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
// First 8 bytes 'j' goes into register 1.
// Second 8 bytes 'i' goes into register 1 and pushes first 8 bytes into register 2.
writeDigits(i, j);
delay(500);
}
}
//*/
/*
writeDigits(5, 2); // write humid to 7-Segments
delay(2000);
writeDigits(8, 0);
delay(2000);
*/
//*
writeSegment(B11111111); // write temp to 7-Segments
delay(2000);
writeSegment(B00000000);
delay(2000);
//*/
Debug.print("7Seg1: "); Debug.println(digits[0], BIN);
Debug.print("7Seg2: "); Debug.println(digits[1], BIN);
} // end of void loop
/*---------- Functions ----------*/
// display a alpha, binary value, or number on the digital segment display
void writeDigits(byte r1, byte r2) {
// r1, r2 = array pointer or binary value, as a byte
digitalWrite(latchPin, LOW); // set latchPin low, before sending data
shiftOut(dataPin, clockPin, MSBFIRST, digits[r2]);
shiftOut(dataPin, clockPin, MSBFIRST, digits[r1]);
digitalWrite(latchPin, HIGH); // set latchPin high, after sending data
}
void writeSegment(byte segment) {
// segment = array pointer or binary value, as a byte
digitalWrite(latchPin, LOW); // set latchPin low, before sending data
shiftOut(dataPin, clockPin, MSBFIRST, segment);
shiftOut(dataPin, clockPin, MSBFIRST, segment);
digitalWrite(latchPin, HIGH); // set latchPin high, after sending data
}
void writeBlank() {
digitalWrite(latchPin, LOW); // set latchPin low, before sending data
shiftOut(dataPin, clockPin, MSBFIRST, B00000000);
shiftOut(dataPin, clockPin, MSBFIRST, B00000000);
digitalWrite(latchPin, HIGH); // set latchPin high, after sending data
}