Advertisement
AbstractBeliefs

Untitled

Jan 20th, 2012
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.77 KB | None | 0 0
  1. #include <util/delay.h>
  2. #include <avr/io.h>
  3.  
  4. //Pin connected to ST_CP of 74HC595
  5. char latchPin = 8;
  6. //Pin connected to SH_CP of 74HC595
  7. char clockPin = 12;
  8. ////Pin connected to DS of 74HC595
  9. char dataPin = 11;
  10.  
  11. //holders for infromation you're going to pass to shifting function
  12. char dataRED;
  13. char dataGREEN;
  14. char dataArrayRED[11];
  15. char dataArrayGREEN[11];
  16.  
  17. void setup() {
  18.   //set pins to output because they are addressed in the main loop
  19.   DDRD |= _BV(3);
  20.   //Serial.begin(9600);
  21.  
  22.   //Arduino doesn't seem to have a way to write binary straight into the code
  23.   //so these values are in HEX.  Decimal would have been fine, too.
  24.   dataArrayRED[0] = 126; //11111111
  25.   dataArrayRED[1] = 72; //11111110
  26.   dataArrayRED[2] = 61; //11111100
  27.   dataArrayRED[3] = 109; //11111000
  28.   dataArrayRED[4] = 75; //11110000
  29.   dataArrayRED[5] = 103; //11100000
  30.   dataArrayRED[6] = 119; //11000000
  31.   dataArrayRED[7] = 76; //10000000
  32.   dataArrayRED[8] = 127; //00000000
  33.   dataArrayRED[9] = 111; //11100000
  34.   dataArrayRED[10] = 0; //11100000
  35.  
  36.  
  37.   //Arduino doesn't seem to have a way to write binary straight into the code
  38.   //so these values are in HEX.  Decimal would have been fine, too.
  39.   dataArrayGREEN[0] = 126; //11111111
  40.   dataArrayGREEN[1] = 72; //01111111
  41.   dataArrayGREEN[2] = 61; //00111111
  42.   dataArrayGREEN[3] = 109; //00011111
  43.   dataArrayGREEN[4] = 75; //00001111
  44.   dataArrayGREEN[5] = 103; //00000111
  45.   dataArrayGREEN[6] = 119; //00000011
  46.   dataArrayGREEN[7] = 76; //00000001
  47.   dataArrayGREEN[8] = 127; //00000000
  48.   dataArrayGREEN[9] = 111; //00000111
  49.   dataArrayGREEN[10] = 0; //00000111
  50. }
  51.  
  52. void loop() {
  53.     int reading = 0;
  54.     while (reading < someval){
  55.         reading = analogRead(somepin);
  56.     }
  57.    
  58.     for (char j = 30; j >= 0; j--) {
  59.         //load the light sequence you want from array
  60.         char ones = j%10;
  61.         char tens = j/10;
  62.    
  63.         //ground latchPin and hold low for as long as you are transmitting
  64.         digitalWrite(latchPin, 0);
  65.         //move 'em out
  66.         shiftOut(dataArrayGREEN[ones]);  
  67.         shiftOut(dataArrayRED[ones]);
  68.         //return the latch pin high to signal chip that it
  69.         //no longer needs to listen for information
  70.         digitalWrite(latchPin, 1);
  71.         _delay_ms(1000);
  72.     }
  73.    
  74.     // Start tone and flash pins
  75.     while (1){
  76.         tone(10, 14000);
  77.         shiftOut(dataPin, clockPin, dataArrayGREEN[8]);
  78.         shiftOut(dataPin, clockPin, dataArrayRED[8]);
  79.         _delay_ms(500);
  80.         tone(10, 10000);
  81.         shiftOut(dataPin, clockPin, dataArrayGREEN[10]);
  82.         shiftOut(dataPin, clockPin, dataArrayRED[10]);
  83.         _delay_ms(500);
  84. }
  85.  
  86.  
  87.  
  88. // the heart of the program
  89. void shiftOut(char myDataOut) {
  90.   // This shifts 8 bits out MSB first,
  91.   //on the rising edge of the clock,
  92.   //clock idles low
  93.  
  94.   //internal function setup
  95.   char pinState;
  96.   DDRD |= _BV(2);
  97.   DDRD |= _BV(4);
  98.  
  99.   //clear everything out just in case to
  100.   //prepare shift register for bit shifting
  101.   PORTD &= ~(_BV(2));
  102.   PORTD &= ~(_BV(4));
  103.  
  104.   //for each bit in the byte myDataOut?
  105.   //NOTICE THAT WE ARE COUNTING DOWN in our for loop
  106.   //This means that %00000001 or "1" will go through such
  107.   //that it will be pin Q0 that lights.
  108.   for (char i=7; i>=0; i--){
  109.     PORTD &= ~(_BV(4));
  110.  
  111.     //if the value passed to myDataOut and a bitmask result
  112.     // true then... so if we are at i=6 and our value is
  113.     // %11010100 it would the code compares it to %01000000
  114.     // and proceeds to set pinState to 1.
  115.     if ( myDataOut & (1<<i) ) {
  116.       pinState= 1;
  117.     }
  118.     else { 
  119.       pinState= 0;
  120.     }
  121.  
  122.     //Sets the pin to HIGH or LOW depending on pinState
  123.     digitalWrite(myDataPin, pinState);
  124.     //register shifts bits on upstroke of clock pin  
  125.     digitalWrite(myClockPin, 1);
  126.     //zero the data pin after shift to prevent bleed through
  127.     digitalWrite(myDataPin, 0);
  128.   }
  129.  
  130.   //stop shifting
  131.   digitalWrite(myClockPin, 0);
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement