263 lines
6.9 KiB
C++
263 lines
6.9 KiB
C++
|
//**************************************************************//
|
||
|
// Name : shiftOutCode, Predefined Dual Array Style //
|
||
|
// Author : Carlyn Maw, Tom Igoe //
|
||
|
// Date : 25 Oct, 2006 //
|
||
|
// Version : 1.0 //
|
||
|
// Notes : Code for using a 74HC595 Shift Register //
|
||
|
// : to count from 0 to 255 //
|
||
|
//****************************************************************
|
||
|
|
||
|
//Pin connected to ST_CP of 74HC595
|
||
|
#include "WProgram.h"
|
||
|
void setup();
|
||
|
void loop();
|
||
|
void clockOutput();
|
||
|
void shiftOut(int myDataPin, int myClockPin, byte myDataOut);
|
||
|
void blinkAll_4Bytes(int n, int d);
|
||
|
int latchPin = 8;
|
||
|
//Pin connected to SH_CP of 74HC595
|
||
|
int clockPin = 12;
|
||
|
////Pin connected to DS of 74HC595
|
||
|
int dataPin = 11;
|
||
|
|
||
|
int second=0, minute=0, hour=0, weekday=1;
|
||
|
|
||
|
//holders for infromation you're going to pass to shifting function
|
||
|
byte data;
|
||
|
byte dataArrayseg1[10];
|
||
|
byte dataArrayseg2[10];
|
||
|
byte dataArrayseg3[10];
|
||
|
byte dataArrayseg4[10];
|
||
|
byte puntos[10];
|
||
|
|
||
|
void setup() {
|
||
|
//set pins to output because they are addressed in the main loop
|
||
|
pinMode(latchPin, OUTPUT);
|
||
|
Serial.begin(9600);
|
||
|
|
||
|
//Arduino doesn't seem to have a way to write binary straight into the code
|
||
|
//so these values are in HEX. Decimal would have been fine, too.
|
||
|
dataArrayseg1[0] = B01101111;
|
||
|
dataArrayseg1[1] = B00000011;
|
||
|
dataArrayseg1[2] = B01011101;
|
||
|
dataArrayseg1[3] = B01010111;
|
||
|
dataArrayseg1[4] = B00110011;
|
||
|
dataArrayseg1[5] = B01110110;
|
||
|
dataArrayseg1[6] = B01111110;
|
||
|
dataArrayseg1[7] = B01000011;
|
||
|
dataArrayseg1[8] = B01111111;
|
||
|
dataArrayseg1[9] = B01110111;
|
||
|
|
||
|
//Arduino doesn't seem to have a way to write binary straight into the code
|
||
|
//so these values are in HEX. Decimal would have been fine, too.
|
||
|
|
||
|
puntos[0] = B00000000;
|
||
|
puntos[1] = B00000001;
|
||
|
puntos[2] = B00000000;
|
||
|
puntos[3] = B00000001;
|
||
|
puntos[4] = B00000000;
|
||
|
puntos[5] = B00000001;
|
||
|
puntos[6] = B00000000;
|
||
|
puntos[7] = B00000001;
|
||
|
puntos[8] = B00000000;
|
||
|
puntos[9] = B00000001;
|
||
|
|
||
|
|
||
|
dataArrayseg2[0] = B11011110;
|
||
|
dataArrayseg2[1] = B00000110;
|
||
|
dataArrayseg2[2] = B10111010;
|
||
|
dataArrayseg2[3] = B10101110;
|
||
|
dataArrayseg2[4] = B01100110;
|
||
|
dataArrayseg2[5] = B11101100;
|
||
|
dataArrayseg2[6] = B11111100;
|
||
|
dataArrayseg2[7] = B10000110;
|
||
|
dataArrayseg2[8] = B11111110;
|
||
|
dataArrayseg2[9] = B11101110;
|
||
|
|
||
|
dataArrayseg3[0] = B01111011;
|
||
|
dataArrayseg3[1] = B01100000;
|
||
|
dataArrayseg3[2] = B01011101;
|
||
|
dataArrayseg3[3] = B01110101;
|
||
|
dataArrayseg3[4] = B01100110;
|
||
|
dataArrayseg3[5] = B00110111;
|
||
|
dataArrayseg3[6] = B00111111;
|
||
|
dataArrayseg3[7] = B01100001;
|
||
|
dataArrayseg3[8] = B01111111;
|
||
|
dataArrayseg3[9] = B01110111;
|
||
|
|
||
|
dataArrayseg4[0] = B01111011;
|
||
|
dataArrayseg4[1] = B01100000;
|
||
|
dataArrayseg4[2] = B01011101;
|
||
|
dataArrayseg4[3] = B01110101;
|
||
|
dataArrayseg4[4] = B01100110;
|
||
|
dataArrayseg4[5] = B00110111;
|
||
|
dataArrayseg4[6] = B00111111;
|
||
|
dataArrayseg4[7] = B01100001;
|
||
|
dataArrayseg4[8] = B01111111;
|
||
|
dataArrayseg4[9] = B01110111;
|
||
|
|
||
|
|
||
|
|
||
|
//function that blinks all the LEDs
|
||
|
//gets passed the number of blinks and the pause time
|
||
|
blinkAll_4Bytes(2,500);
|
||
|
}
|
||
|
|
||
|
void loop() {
|
||
|
|
||
|
static unsigned long lastTick = 0; // set up a local variable to hold the last time we moved forward one second
|
||
|
|
||
|
// move forward one second every 1000 milliseconds
|
||
|
if (millis() - lastTick >= 1000) {
|
||
|
lastTick = millis();
|
||
|
clockOutput();
|
||
|
second++;
|
||
|
}
|
||
|
|
||
|
// move forward one minute every 60 seconds
|
||
|
if (second > 59) {
|
||
|
minute++;
|
||
|
second = 0; // reset seconds to zero
|
||
|
}
|
||
|
|
||
|
// move forward one hour every 60 minutes
|
||
|
if (minute > 59) {
|
||
|
hour++;
|
||
|
minute = 0; // reset minutes to zero
|
||
|
}
|
||
|
|
||
|
// move forward one weekday every 24 hours
|
||
|
if (hour > 23) {
|
||
|
weekday++;
|
||
|
hour = 0; // reset hours to zero
|
||
|
}
|
||
|
|
||
|
// reset weekdays on Saturday
|
||
|
if (weekday > 7) {
|
||
|
weekday = 1;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
for (int j = 0; j < 10; j++) {
|
||
|
digitalWrite(latchPin, 0);
|
||
|
shiftOut(dataPin, clockPin, dataArrayseg1[j]);
|
||
|
data=dataArrayseg2[j]|puntos[j];
|
||
|
shiftOut(dataPin, clockPin, data);
|
||
|
shiftOut(dataPin, clockPin, dataArrayseg3[j]);
|
||
|
shiftOut(dataPin, clockPin, dataArrayseg4[j]);
|
||
|
digitalWrite(latchPin, 1);
|
||
|
delay(1000);
|
||
|
}
|
||
|
*/
|
||
|
|
||
|
}
|
||
|
|
||
|
void clockOutput()
|
||
|
{
|
||
|
int sec_unit, sec_dec;
|
||
|
int min_unit, min_dec;
|
||
|
sec_unit = second%10;
|
||
|
sec_dec = second/10;
|
||
|
min_unit = minute%10;
|
||
|
min_dec = minute/10;
|
||
|
|
||
|
digitalWrite(latchPin, 0);
|
||
|
shiftOut(dataPin, clockPin, dataArrayseg1[min_dec]);
|
||
|
//data=dataArrayseg2[j]|puntos[j];
|
||
|
shiftOut(dataPin, clockPin, dataArrayseg2[min_unit]);
|
||
|
shiftOut(dataPin, clockPin, dataArrayseg3[sec_dec]);
|
||
|
shiftOut(dataPin, clockPin, dataArrayseg4[sec_unit]);
|
||
|
digitalWrite(latchPin, 1);
|
||
|
|
||
|
}
|
||
|
|
||
|
// the heart of the program
|
||
|
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
|
||
|
// This shifts 8 bits out MSB first,
|
||
|
//on the rising edge of the clock,
|
||
|
//clock idles low
|
||
|
|
||
|
//internal function setup
|
||
|
int i=0;
|
||
|
int pinState;
|
||
|
pinMode(myClockPin, OUTPUT);
|
||
|
pinMode(myDataPin, OUTPUT);
|
||
|
|
||
|
//clear everything out just in case to
|
||
|
//prepare shift register for bit shifting
|
||
|
digitalWrite(myDataPin, 0);
|
||
|
digitalWrite(myClockPin, 0);
|
||
|
|
||
|
//for each bit in the byte myDataOut?
|
||
|
//NOTICE THAT WE ARE COUNTING DOWN in our for loop
|
||
|
//This means that %00000001 or "1" will go through such
|
||
|
//that it will be pin Q0 that lights.
|
||
|
for (i=7; i>=0; i--) {
|
||
|
digitalWrite(myClockPin, 0);
|
||
|
|
||
|
//if the value passed to myDataOut and a bitmask result
|
||
|
// true then... so if we are at i=6 and our value is
|
||
|
// %11010100 it would the code compares it to %01000000
|
||
|
// and proceeds to set pinState to 1.
|
||
|
if ( myDataOut & (1<<i) ) {
|
||
|
pinState= 1;
|
||
|
}
|
||
|
else {
|
||
|
pinState= 0;
|
||
|
}
|
||
|
|
||
|
//Sets the pin to HIGH or LOW depending on pinState
|
||
|
digitalWrite(myDataPin, pinState);
|
||
|
//register shifts bits on upstroke of clock pin
|
||
|
digitalWrite(myClockPin, 1);
|
||
|
//zero the data pin after shift to prevent bleed through
|
||
|
digitalWrite(myDataPin, 0);
|
||
|
}
|
||
|
|
||
|
//stop shifting
|
||
|
digitalWrite(myClockPin, 0);
|
||
|
}
|
||
|
|
||
|
|
||
|
//blinks the whole register based on the number of times you want to
|
||
|
//blink "n" and the pause between them "d"
|
||
|
//starts with a moment of darkness to make sure the first blink
|
||
|
//has its full visual effect.
|
||
|
void blinkAll_4Bytes(int n, int d) {
|
||
|
digitalWrite(latchPin, 0);
|
||
|
shiftOut(dataPin, clockPin, 0);
|
||
|
shiftOut(dataPin, clockPin, 0);
|
||
|
digitalWrite(latchPin, 1);
|
||
|
delay(200);
|
||
|
for (int x = 0; x < n; x++) {
|
||
|
digitalWrite(latchPin, 0);
|
||
|
shiftOut(dataPin, clockPin, 255);
|
||
|
shiftOut(dataPin, clockPin, 255);
|
||
|
shiftOut(dataPin, clockPin, 255);
|
||
|
shiftOut(dataPin, clockPin, 255);
|
||
|
digitalWrite(latchPin, 1);
|
||
|
delay(d);
|
||
|
digitalWrite(latchPin, 0);
|
||
|
shiftOut(dataPin, clockPin, 0);
|
||
|
shiftOut(dataPin, clockPin, 0);
|
||
|
shiftOut(dataPin, clockPin, 0);
|
||
|
shiftOut(dataPin, clockPin, 0);
|
||
|
digitalWrite(latchPin, 1);
|
||
|
delay(d);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
int main(void)
|
||
|
{
|
||
|
init();
|
||
|
|
||
|
setup();
|
||
|
|
||
|
for (;;)
|
||
|
loop();
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|