Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Oscillator: 2, 3 on uC
- Input 1 -> Pin8 (5 on uC) B2
- Input 2 -> Pin7 (6 on uC) A7
- Taster -> Pin6 (7 on uC) A6
- 7Segments Cathodes -> Pin0-2 (12 - 10 on uC) + Output8 of Shift Register
- 888.8
- 012 O8
- ShiftRegister -> CL: Latch: Pin3(10) Pin4(9) DATA: Pin5(8)
- IO:9 -Cathodes = 5 -Inputs = 2
- */
- const byte dataPin = 3;
- const byte latchPin = 4;
- const byte clockPin = 5;
- const byte segments[] = {
- 252, 144, 122, 218, 150, 206, 238, 152, 254, 222, 2, 108}; //Replace with 7 segment numbers, 255 = '-', 254 = 'C'
- volatile unsigned int overflows = 0;
- unsigned int temp_timer = 0;
- unsigned long time;
- boolean waiting = true;
- void setup()
- {
- DDRA = 0b10111111;
- pinMode(7, OUTPUT); //DECIMAL
- pinMode(6, INPUT); //Lichtschranke2
- //pinMode(8, INPUT); //Lichtschranke1, Interrupt
- byte timer0a = TCCR0A;
- byte timer0b = TCCR0B;
- TCCR0A = 0; //disable timer0 for micros()
- TCCR0B = 0;
- TCCR1A = 0;// set entire TCCR1A register to 0
- TIMSK1 = _BV(TOIE1); //enalbe interrupt
- TCCR1B = 0;// start timer 1
- writeSegment(0, 10); //zeige Strich an
- while(!(PINB & 0b100));//wait for input 1
- TCCR1B = 1;// start timer 1
- PORTA = 0;
- while(!(PINA & 0b1000000));//wait for input 2
- cli();
- temp_timer = TCNT1; //store passed ticks
- TCCR0A = timer0a;//reenable timer0 for micros()
- TCCR0B = timer0b;
- time = (((unsigned long)overflows - 1ul) << 16) | (unsigned long)temp_timer - 4; //calculate time
- unsigned long xspeed = (F_CPU / time); //10-fache geschwindigkeit
- if(xspeed < 10000ul)
- digitalWrite(7, HIGH);
- else
- xspeed = xspeed / 10;
- byte digits[4];
- String s = String(xspeed);
- while(s.length() < 4)
- s = "0" + s;
- byte blinkb = 0;
- byte blinkstate = 0;
- if(xspeed > 9999)
- blinkb = true;
- for(int i = 0 ; i < 4; i++) //ziffern bestimmen
- {
- digits[i] = s[i] - '0';
- }
- if(!blinkb)
- {
- while(true) // ziffern zeigen
- {
- for(byte i = 0; i < 4; i++)
- {
- writeSegment(i, digits[i]);
- delayMicroseconds(1500); //~200 Hz update rate
- }
- }
- }
- else
- {
- while(true) // ziffern zeigen
- {
- for(byte b = 0; b < 30; b++)
- {
- for(byte i = 0; i < 4; i++)
- {
- writeSegment(i, digits[i]);
- delayMicroseconds(1500); //~200 Hz update rate
- }
- }
- blinkstate = !blinkstate;
- digitalWrite(7, blinkstate);
- }
- }
- // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
- }
- ISR(TIM1_OVF_vect)
- {
- overflows++;
- }//end ISR
- void writeSegment(byte segment, byte value)
- {
- byte out = segments[value];
- byte sethigh = false;
- for(byte i = 0; i < 3; i++)
- digitalWrite(i, LOW);
- if(segment == 3)
- bitSet(out, 0); //replace according to wiring
- else
- sethigh = true;
- shiftOut(dataPin, clockPin, MSBFIRST, out);
- digitalWrite(latchPin, HIGH);
- if(sethigh)
- digitalWrite(segment, HIGH);
- digitalWrite(latchPin, LOW);
- }
- void loop()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement