#include <avr/io.h>
#include <util/delay.h>
#define wait1 350
// Binary values of each digit for 7-segment display
uint8_t seven_seg_digits[10] = { 0xFC, // = 0
0x60, // = 1
0xDA, // = 2
0xF2, // = 3
0x66, // = 4
0xB6, // = 5
0xBE, // = 6
0xE0, // = 7
0xFE, // = 8
0xE6 // = 9
};
// Binary values of each individual segment
uint8_t single_seg_digits[8] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
// Define pins connected to 74HC595
#define latchPin 3 // ST_CP of 74HC595, latch pin
#define clockPin 4 // SH_CP of 74HC595, clock pin
#define dataPin 2 // DS of 74HC595, serial data
// Setup function to initialize pins as output
void setup()
{
// Set latchPin, clockPin, dataPin as output
DDRD |= (1 << latchPin) | (1 << clockPin) | (1 << dataPin);
}
// Display each individual segment on the display
void singleSegWrite(uint8_t digit)
{
// Set latchPin to low voltage before sending
PORTD &= ~(1 << latchPin);
// Send the bit pattern to the shift register
shiftOut(dataPin, clockPin, LSBFIRST, single_seg_digits[digit]);
// Set latchPin to high voltage after sending data
PORTD |= (1 << latchPin);
}
// Display a number on the digital segment display
void sevenSegWrite(uint8_t digit)
{
// Set latchPin to low voltage before sending
PORTD &= ~(1 << latchPin);
// Send the bit pattern to the shift register
shiftOut(dataPin, clockPin, LSBFIRST, seven_seg_digits[digit]);
// Set latchPin to high voltage after sending data
PORTD |= (1 << latchPin);
}
int main(void)
{
// Initialize ports
setup();
while (1)
{
// Count from hexadecimal 0 - 9
for (uint8_t digit = 0; digit < 10; ++digit)
{
_delay_ms(wait1); // Delay before displaying next digit
sevenSegWrite(digit); // Display the digit
}
}
return 0;
}