Advertisement
martin2250

Untitled

Nov 5th, 2013
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. /*
  2.   Oscillator: 2, 3 on uC
  3.  Input 1 -> Pin8 (5 on uC)    B2
  4.  Input 2 -> Pin7  (6 on uC)   A7
  5.  Taster  ->  Pin6  (7 on uC)  A6
  6.  
  7.  7Segments Cathodes -> Pin0-2  (12 - 10 on uC)  + Output8 of Shift Register
  8.  
  9.  888.8
  10.  012 O8  
  11.  
  12.  ShiftRegister -> CL: Latch: Pin3(10) Pin4(9)  DATA: Pin5(8)
  13.  
  14.  IO:9   -Cathodes = 5 -Inputs = 2
  15.  
  16.  
  17.  */
  18. const byte dataPin = 3;
  19. const byte latchPin = 4;
  20. const byte clockPin = 5;
  21.  
  22. const byte segments[] = {
  23.   252, 144, 122, 218, 150, 206, 238, 152, 254, 222, 2, 108};  //Replace with 7 segment numbers, 255 = '-', 254 = 'C'
  24.  
  25.  
  26. volatile unsigned int overflows = 0;
  27. unsigned int temp_timer = 0;
  28. unsigned long time;
  29. boolean waiting = true;
  30.  
  31.  
  32. void setup()
  33. {
  34.   DDRA = 0b10111111;
  35.   pinMode(7, OUTPUT);    //DECIMAL
  36.   pinMode(6, INPUT);    //Lichtschranke2
  37.   //pinMode(8, INPUT);    //Lichtschranke1, Interrupt
  38.    
  39.  
  40.   byte timer0a = TCCR0A;
  41.   byte timer0b = TCCR0B;
  42.  
  43.   TCCR0A = 0; //disable timer0 for micros()
  44.   TCCR0B = 0;
  45.  
  46.  
  47.  
  48.  
  49.   TCCR1A = 0;// set entire TCCR1A register to 0
  50.   TIMSK1 = _BV(TOIE1);  //enalbe interrupt
  51.   TCCR1B = 0;// start timer 1
  52.  
  53.   writeSegment(0, 10);    //zeige Strich an
  54.  
  55.  
  56.   while(!(PINB & 0b100));//wait for input 1
  57.   TCCR1B = 1;// start timer 1
  58.  
  59.   PORTA = 0;
  60.  
  61.  
  62.  
  63.  
  64.  
  65.   while(!(PINA & 0b1000000));//wait for input 2
  66.  
  67.   cli();
  68.   temp_timer = TCNT1;  //store passed ticks
  69.  
  70.  
  71.  
  72.   TCCR0A = timer0a;//reenable timer0 for micros()
  73.   TCCR0B = timer0b;
  74.  
  75.  
  76.   time = (((unsigned long)overflows - 1ul) << 16) | (unsigned long)temp_timer - 4;      //calculate time
  77.  
  78.  
  79.   unsigned long xspeed = (F_CPU / time);    //10-fache geschwindigkeit
  80.  
  81.   if(xspeed < 10000ul)
  82.     digitalWrite(7, HIGH);
  83.   else
  84.     xspeed = xspeed / 10;
  85.  
  86.  
  87.   byte digits[4];
  88.  
  89.   String s = String(xspeed);
  90.   while(s.length() < 4)
  91.     s = "0" + s;
  92.  
  93.   byte blinkb = 0;
  94.   byte blinkstate = 0;
  95.  
  96.   if(xspeed > 9999)
  97.     blinkb = true;
  98.  
  99.   for(int i = 0 ; i < 4; i++)                    //ziffern bestimmen
  100.   {
  101.     digits[i] = s[i] - '0';
  102.   }  
  103.  
  104.  
  105.   if(!blinkb)
  106.   {
  107.     while(true)                      // ziffern zeigen
  108.     {
  109.       for(byte i = 0; i < 4; i++)
  110.       {
  111.         writeSegment(i, digits[i]);
  112.         delayMicroseconds(1500);                              //~200 Hz update rate
  113.       }
  114.     }
  115.   }
  116.   else
  117.   {
  118.     while(true)                      // ziffern zeigen
  119.     {
  120.       for(byte b = 0; b < 30; b++)
  121.       {
  122.         for(byte i = 0; i < 4; i++)
  123.         {
  124.           writeSegment(i, digits[i]);
  125.           delayMicroseconds(1500);                              //~200 Hz update rate
  126.         }
  127.       }
  128.       blinkstate = !blinkstate;
  129.       digitalWrite(7, blinkstate);
  130.  
  131.     }
  132.   }
  133.  
  134.  
  135.  
  136.   // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  137. }
  138.  
  139.  
  140. ISR(TIM1_OVF_vect)
  141. {
  142.   overflows++;
  143. }//end ISR
  144.  
  145.  
  146.  
  147. void writeSegment(byte segment, byte value)
  148. {
  149.   byte out = segments[value];
  150.   byte sethigh = false;
  151.  
  152.   for(byte i = 0; i < 3; i++)
  153.     digitalWrite(i, LOW);
  154.  
  155.   if(segment == 3)
  156.     bitSet(out, 0);            //replace according to wiring
  157.   else
  158.     sethigh = true;
  159.  
  160.   shiftOut(dataPin, clockPin, MSBFIRST, out);  
  161.  
  162.   digitalWrite(latchPin, HIGH);
  163.   if(sethigh)
  164.     digitalWrite(segment, HIGH);
  165.   digitalWrite(latchPin, LOW);
  166. }
  167.  
  168. void loop()
  169. {
  170.  
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement