Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- byte blinks[10] = {
- 0b0001, 0b0000, 0b0010, 0b0000, 0b0000, 0b0000, 0b0000, 0b0000, 0b0100, 0b1000};
- byte delays[10] = {
- 0b0000, 0b0000, 0b0000, 0b0000, 0b0000, 0b0000, 0b0000, 0b0000, 0b0100, 0b1000};
- unsigned int revolution;
- unsigned int digittime;
- unsigned int delayy;
- unsigned int blinkdur;
- byte digit;
- void setup()
- {
- Serial.begin(9600);
- shift();
- cli();
- TCCR1A = 0;
- TCCR1B = 0;
- TCCR1C = 0;
- TCNT1 = 0;
- TCCR1B = 0b11; //64 divisor;
- TIMSK1 |= (1 << OCIE1A);
- sei();
- DDRD |= 0xF0; //pins 4-7 as output;
- attachInterrupt(0, rev, RISING); //Pin 2
- }
- void loop()
- {
- if(Serial.available() >= 4)
- {
- for(byte x = 0; x < 10; x++)
- {
- blinks[x] = 0;
- delays[x] = 0;
- }
- byte digits[4];
- Serial.readBytes((char*)digits, 4);
- for(byte x = 0; x < 4; x++)
- digits[x] -= '0';
- for(byte r = 0; r < 4; r++)
- {
- if(digits[r] <= (6 + r))
- {
- byte pos = (6 + r) - digits[r];
- blinks[pos] |= _BV(3 - r);
- }
- else
- {
- byte pos = (16 + r) - digits[r];
- blinks[pos] |= _BV(3 - r);
- delays[pos] |= _BV(3 - r);
- }
- }
- /*
- if(digits[0] <= 6)
- {
- byte pos = 6 - digits[0];
- blinks[pos] |= 0b1000;
- }
- else
- {
- byte pos = 16 - digits[0];
- blinks[pos] |= 0b1000;
- delays[pos] |= 0b1000;
- }
- if(digits[1] <= 7)
- {
- byte pos = 7 - digits[1];
- blinks[pos] |= 0b100;
- }
- else
- {
- byte pos = 17 - digits[1];
- blinks[pos] |= 0b100;
- delays[pos] |= 0b100;
- }
- */
- shift();
- }
- }
- void shift()
- {
- for(byte x = 0; x < 10; x++)
- blinks[x] = blinks[x] << 4;
- for(byte x = 0; x < 10; x++)
- delays[x] = delays[x] << 4;
- }
- void rev()
- {
- TCCR1B = 0; //stop timer
- revolution = TCNT1; //note time for revolution
- TCNT1 = 1; //reset Timer
- digittime = revolution / 10 + (revolution / 700); //calculate time per digit
- OCR1A = digittime; //set compare match to next digit
- delayy = digittime * 2; //microsecondsdelay until 2nd pass
- blinkdur = delayy / 10; //calulate blink duration
- delayy = delayy - blinkdur; //calculate delay between 2 digits
- digit = 1; //reset digit counter
- delayMicroseconds(delayy);
- byte x = blinks[0] & (~delays[0]);
- PORTD ^= x;
- delayMicroseconds(blinkdur);
- PORTD ^= x;
- TCCR1B = 0b11; //restart timer
- }
- ISR(TIMER1_COMPA_vect, ISR_NOBLOCK)
- {
- byte x = blinks[digit] & (~delays[digit]);
- PORTD ^= x;
- delayMicroseconds(blinkdur);
- PORTD ^= x;
- delayMicroseconds(delayy);
- x = blinks[digit] & delays[digit];
- PORTD ^= x;
- delayMicroseconds(blinkdur);
- PORTD ^= x;
- TCCR1B = 0;
- OCR1A += digittime;
- TCCR1B = 0b11;
- digit++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement