Created
November 9, 2011 02:45
-
-
Save caspencer/1350169 to your computer and use it in GitHub Desktop.
attiny85_shift_register - displays digits 0 through 9 on 7-seg LED using 74HC595 for I/O expansion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* attiny85_shift_register - displays digits 0 through 9 on 7-seg LED using 74HC595 for I/O expansion | |
* | |
* [ATtiny85 AVR] ==> [74HC595 shift register] ==> [7-seg led display] | |
*/ | |
#include <avr/io.h> | |
#include <util/delay.h> | |
// serial data-in | |
#define ser PB3 | |
// register clock | |
#define rclk PB2 | |
// serial clock | |
#define srclk PB4 | |
// serial clear (active-low) | |
#define srclr PB1 | |
#define delayTime 250 | |
//========================================================================== | |
// ATtiny85 pin summary other functions | |
// | |
// pin 1 is PortB bit 5 (PB5) (PCINT5/RESET/ADC0/dW) | |
// pin 2 is PortB bit 3 (PB3) (PCINT3/XTAL1/CLKI/OC1B/ADC3) | |
// pin 3 is PortB bit 4 (PB4) (PCINT4/XTAL2/CLKO/OC1B/ADC2) | |
// pin 4 is GND | |
// pin 5 is PortB bit 0 (PB0) (MOSI/DI/SDA/AIN0/OC0A/OC1A/AREF/PCINT0) | |
// pin 6 is PortB bit 1 (PB1) (MISO/DO/AIN1/OC0b/OC1A/PCINT1) | |
// pin 7 is PortB bit 2 (PB2) (SCK/USCK/SCL/ADC1/T0/INT0/PCINT2) | |
// pin 8 is VCC | |
//========================================================================== | |
void outputPattern(uint8_t); | |
uint8_t digitPattern[] = { | |
0x6F, //0b01101111 "0" | |
0x03, //0b00000011 "1" | |
0x76, //0b01110110 "2" | |
0x37, //0b00110111 "3" | |
0x1B, //0b00011011 "4" | |
0x3D, //0b00111101 "5" | |
0x7D, //0b01111101 "6" | |
0x07, //0b00000111 "7" | |
0x7F, //0b01111111 "8" | |
0x3F //0b00111111 "9" | |
}; | |
int main(void) { | |
//DDRB |= ((1 << rclk) | (1 << ser) | (1 << srclk) | (1 << srclr)); | |
DDRB |= ((1 << rclk) | (1 << ser) | (1 << srclk)); | |
PORTB = 0x00; | |
// instead can just connect srclr to AVR reset pin and let it clear the 74HC595 | |
//PORTB |= (1 << srclr); | |
for (;;) { | |
for (int i=0; i<10; i++) { | |
outputPattern(digitPattern[i]); | |
_delay_ms(delayTime); | |
} | |
} | |
return 0; | |
} | |
void outputPattern(uint8_t pattern) { | |
PORTB &= ~(1 << rclk); | |
for (int i = 0; i < 8; i++) { | |
uint8_t dataBit = (((pattern & (1 << i)) >> i)); | |
PORTB &= ~(1 << srclk); | |
if (dataBit) | |
PORTB |= (1 << ser); | |
else | |
PORTB &= ~(1 << ser); | |
PORTB |= (1 << srclk); | |
PORTB &= ~(1 << ser); | |
} | |
PORTB |= (1 << rclk); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment