Skip to content

Instantly share code, notes, and snippets.

@theapi
Created January 27, 2014 19:30
Show Gist options
  • Save theapi/8655672 to your computer and use it in GitHub Desktop.
Save theapi/8655672 to your computer and use it in GitHub Desktop.
Attiny85 avr-c shift register 74HC595
//#define F_CPU 16000000L
#include <avr/io.h>
#include <util/delay.h>
#define PIN_DATA PB0 // DS
#define PIN_CLOCK PB1 // SH_CP
#define PIN_LATCH PB2 // ST_CP
// From Android.h
#define LSBFIRST 0
#define MSBFIRST 1
/********************************************************************************
Function Prototypes
********************************************************************************/
void countTime();
void shiftOut(uint8_t bitOrder, uint8_t val);
void cylon();
/********************************************************************************
Global Variables
********************************************************************************/
char cylonDirection; // the current direction the cylon sweep is moving.
char shift; // the data to be shifted
/********************************************************************************
Main
********************************************************************************/
int
main (void)
{
DDRB = 0xFF; // set all to output
PORTB = 0; // all off
while(1) {
//countTime();
cylon();
}
}
/********************************************************************************
Functions
********************************************************************************/
void shiftOut(uint8_t bitOrder, uint8_t val)
{
uint8_t i;
uint8_t bit;
for (i = 0; i < 8; i++) {
if (bitOrder == LSBFIRST) {
//digitalWrite(dataPin, !!(val & (1 << i)));
bit = i;
} else {
//digitalWrite(dataPin, !!(val & (1 << (7 - i))));
bit = 7 - i;
}
if (val & (1 << bit)) {
PORTB |= (1 << PIN_DATA); // HIGH
} else {
PORTB &= ~(1 << PIN_DATA); // LOW
}
//digitalWrite(clockPin, HIGH);
PORTB |= (1 << PIN_CLOCK); // HIGH
//digitalWrite(clockPin, LOW);
PORTB &= ~(1 << PIN_CLOCK); // LOW
}
}
void countTime()
{
for (int8_t x = 0; x <= 255; x++) {
// take the latchPin low so
// the LEDs don't change while you're sending in bits:
//digitalWrite(latchPin, LOW);
PORTB &= ~(1 << PIN_LATCH); // LOW
// shift out the bits:
shiftOut(LSBFIRST, x);
//take the latch pin high so the LEDs will light up:
//digitalWrite(latchPin, HIGH);
PORTB |= (1 << PIN_LATCH); // HIGH
_delay_ms(1000);
}
}
void cylon()
{
//if (waited(delay)) {
if (shift == 0) {
// Create something to shift
shift = 1;
} else if (cylonDirection == 0) {
// Shift the on bit to the left
shift = shift << 1;
} else {
// Shift the on bit to the right
shift = shift >> 1;
}
// When the highest bit is lit, reverse the direction.
// highest bit is 10000000 / 128 / 0x80
// which can be checked using the bitwiae AND operator
// (could do PORTD == 128, but learning bit manipulation here)
// Compare the current byte to a (byte) mask for the highest bit.
// eg; 11111111 & 10000000 == true
if (shift & (1 << 7)) {
// reverse the direction for next time
cylonDirection = 1;
} else if (shift == 0x01) {
// set the direction forward
cylonDirection = 0;
}
// Latch LOW
PORTB &= ~(1 << PIN_LATCH);
// shift out the bits:
shiftOut(LSBFIRST, shift);
//take the latch pin high
PORTB |= (1 << PIN_LATCH);
_delay_ms(80);
//}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment