Advertisement
martin2250

Untitled

Nov 17th, 2013
844
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1. byte blinks[10] = {
  2.   0b0001, 0b0000, 0b0010, 0b0000, 0b0000, 0b0000, 0b0000, 0b0000, 0b0100, 0b1000};
  3. byte delays[10] = {
  4.   0b0000, 0b0000, 0b0000, 0b0000, 0b0000, 0b0000, 0b0000, 0b0000, 0b0100, 0b1000};
  5.  
  6. unsigned int revolution;
  7. unsigned int digittime;
  8. unsigned int delayy;
  9. unsigned int blinkdur;
  10. byte digit;
  11.  
  12. void setup()
  13. {
  14.   Serial.begin(9600);
  15.  
  16.   shift();
  17.  
  18.   cli();
  19.  
  20.   TCCR1A = 0;  
  21.   TCCR1B = 0;  
  22.   TCCR1C = 0;  
  23.   TCNT1 = 0;
  24.  
  25.  
  26.  
  27.   TCCR1B = 0b11;  //64 divisor;
  28.   TIMSK1 |= (1 << OCIE1A);
  29.  
  30.   sei();
  31.  
  32.   DDRD |= 0xF0;    //pins 4-7 as output;
  33.  
  34.   attachInterrupt(0, rev, RISING);  //Pin 2
  35. }
  36.  
  37. void loop()
  38. {
  39.   if(Serial.available() >= 4)
  40.   {
  41.     for(byte x = 0; x < 10; x++)
  42.     {
  43.       blinks[x] = 0;
  44.       delays[x] = 0;
  45.     }
  46.     byte digits[4];
  47.     Serial.readBytes((char*)digits, 4);
  48.     for(byte x = 0; x < 4; x++)
  49.       digits[x] -= '0';
  50.  
  51.     for(byte r = 0; r < 4; r++)
  52.     {
  53.       if(digits[r] <= (6 + r))
  54.       {
  55.         byte pos = (6 + r) - digits[r];
  56.         blinks[pos] |= _BV(3 - r);
  57.       }
  58.       else
  59.       {
  60.         byte pos = (16 + r) - digits[r];
  61.         blinks[pos] |= _BV(3 - r);
  62.         delays[pos] |= _BV(3 - r);
  63.       }
  64.  
  65.  
  66.     }
  67.  
  68. /*
  69.     if(digits[0] <= 6)
  70.     {
  71.       byte pos = 6 - digits[0];
  72.       blinks[pos] |= 0b1000;
  73.     }
  74.     else
  75.     {
  76.       byte pos = 16 - digits[0];
  77.       blinks[pos] |= 0b1000;
  78.       delays[pos] |= 0b1000;
  79.     }
  80.  
  81.     if(digits[1] <= 7)
  82.     {
  83.       byte pos = 7 - digits[1];
  84.       blinks[pos] |= 0b100;
  85.     }
  86.     else
  87.     {
  88.       byte pos = 17 - digits[1];
  89.       blinks[pos] |= 0b100;
  90.       delays[pos] |= 0b100;
  91.     }
  92. */
  93.  
  94.  
  95.  
  96.  
  97.  
  98.     shift();
  99.   }
  100.  
  101. }
  102.  
  103. void shift()
  104. {
  105.   for(byte x = 0; x < 10; x++)
  106.     blinks[x] = blinks[x] << 4;
  107.   for(byte x = 0; x < 10; x++)  
  108.     delays[x] = delays[x] << 4;
  109. }
  110.  
  111. void rev()
  112. {
  113.   TCCR1B = 0;                    //stop timer
  114.   revolution = TCNT1;            //note time for revolution
  115.   TCNT1 = 1;                     //reset Timer
  116.   digittime = revolution / 10 + (revolution / 700);   //calculate time per digit  
  117.   OCR1A = digittime;             //set compare match to next digit
  118.  
  119.  
  120.  
  121.   delayy = digittime * 2;        //microsecondsdelay until 2nd pass
  122.   blinkdur = delayy / 10;        //calulate blink duration
  123.   delayy = delayy - blinkdur;    //calculate delay between 2 digits
  124.   digit = 1;                     //reset digit counter
  125.  
  126.   delayMicroseconds(delayy);
  127.  
  128.   byte x = blinks[0] & (~delays[0]);
  129.   PORTD ^= x;
  130.   delayMicroseconds(blinkdur);
  131.   PORTD ^= x;
  132.  
  133.   TCCR1B = 0b11;                 //restart timer
  134. }
  135.  
  136. ISR(TIMER1_COMPA_vect, ISR_NOBLOCK)
  137. {
  138.   byte x = blinks[digit] & (~delays[digit]);
  139.   PORTD ^= x;
  140.   delayMicroseconds(blinkdur);
  141.   PORTD ^= x;
  142.  
  143.  
  144.   delayMicroseconds(delayy);
  145.  
  146.  
  147.   x = blinks[digit] & delays[digit];
  148.   PORTD ^= x;
  149.   delayMicroseconds(blinkdur);
  150.   PORTD ^= x;
  151.  
  152.   TCCR1B = 0;
  153.   OCR1A += digittime;
  154.   TCCR1B = 0b11;
  155.   digit++;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement