Advertisement
Guest User

Untitled

a guest
May 28th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.92 KB | None | 0 0
  1. /*
  2. ** Lab 15-1 - Interrupts - Stopwatch
  3. **
  4. ** IO Board connections
  5. **   Seven segment display A to DP connected to AVR port A
  6. **   Seven segment display CC connected to AVR port C, pin 0
  7. **   Button B0 connected to AVR port D, pin 2 - this is the start/stop button
  8. **   Button B1 connected to AVR port D, pin 3 - this is the reset button
  9. */
  10.  
  11. #include <avr/io.h>
  12. #include <avr/interrupt.h>
  13.  
  14. /* The following macros can be used to check for buttons being pushed.
  15. ** STARTSTOP will be 1 if the port D pin 2 button is down, 0 otherwise.
  16. ** RESET will be 1 if the port D pin 3 button is down, 0 otherwise.
  17. */
  18. #define STARTSTOP ((PIND & (1<<2)) >> 2)
  19. #define RESET ((PIND & (1<<3)) >> 3)
  20.  
  21. /* Control variables */
  22.  
  23. /* stopwatch_timing - 1 if the stopwatch is running, 0 if not.
  24. ** Stopwatch is initially stopped.
  25. */
  26. volatile uint8_t stopwatch_timing = 0;
  27.  
  28. /* digits_displayed - 1 if digits are displayed on the seven
  29. ** segment display, 0 if not. No digits displayed initially.
  30. */
  31. volatile uint8_t digits_displayed = 0;
  32.  
  33. /* Time value - we count hundredths of seconds,
  34. ** i.e. increment the count every 10ms.
  35. */
  36. volatile uint16_t count = 0;
  37.  
  38. /* Seven segment display digit being displayed.
  39. ** 0 = right digit; 1 = left digit.
  40. */
  41. volatile uint8_t seven_seg_cc = 0;
  42.  
  43. /* Seven segment display segment values for 0 to 9 */
  44. uint8_t seven_seg_data[10] = {63,6,91,79,102,109,125,7,127,111};
  45.  
  46. int main() {
  47.     /* Make all bits of port A and the least significant
  48.     ** bit of port C be output bits.
  49.     */
  50.     DDRA = 0xFF;
  51.     DDRC = 0x01;
  52.  
  53.     /* Set up timer/counter 1 so that we get an
  54.     ** interrupt 100 times per second, i.e. every
  55.     ** 10 milliseconds.
  56.     */
  57.     OCR1A = 9999; /* Clock divided by 8 - count for 10000 cycles */
  58.     TCCR1A = 0; /* CTC mode */
  59.     TCCR1B = (1<<WGM12)|(1<<CS11); /* Divide clock by 8 */
  60.  
  61.     /* Enable interrupt on timer on output compare match
  62.     */
  63.     TIMSK1 = (1<<OCIE1A);
  64.  
  65.     /* Ensure interrupt flag is cleared */
  66.     TIFR1 = (1<<OCF1A);
  67.  
  68.     /* Turn on global interrupts */
  69.     sei();
  70.  
  71.     /* Implement controller here - RESET functionality is provided */
  72.     /* See button macro definitions above */
  73.     while(1) {
  74.         if(RESET) {
  75.             /* Reset button has been pushed, wait for it to be released */
  76.             while(RESET) {
  77.                 /* Do nothing - start/stop button will be ignored */
  78.             }
  79.             /* RESET button has been released - now reset the stopwatch */
  80.             stopwatch_timing = 0;
  81.             digits_displayed = 0;
  82.             count = 0;
  83.         }
  84.         /* Deal with start/stop button */
  85.         if(STARTSTOP) {
  86.             /* Start/stop button has been pushed. Toggle whether the
  87.             ** stopwatch is running or not. Make sure digits are displayed.
  88.             */
  89.             stopwatch_timing ^= 1;
  90.             digits_displayed = 1;
  91.             while(STARTSTOP && !RESET) {
  92.                 /* Do nothing while the start-stop button is held down
  93.                 ** and the reset button is not
  94.                 */
  95.             }
  96.         }
  97.     }
  98. }
  99.  
  100. /* This interrupt handler will get called every 10ms.
  101. ** We do two things - update out stopwatch count, and
  102. ** output to the other seven segment display digit.
  103. ** We display seconds on the left digit;
  104. ** tenths of seconds on the right digit.
  105. */
  106. ISR(TIMER1_COMPA_vect) {
  107.     /* If the stopwatch is running then increment time.
  108.     ** If we've reached 1000, then wrap this around to 0.
  109.     */
  110.     if(stopwatch_timing) {
  111.         count++;
  112.         if(count == 1000) {
  113.             count = 0;
  114.         }
  115.     }
  116.    
  117.     /* Change which digit will be displayed. If last time was
  118.     ** left, now display right. If last time was right, now
  119.     ** display left.
  120.     */
  121.     seven_seg_cc = 1 ^ seven_seg_cc;
  122.    
  123.     if(digits_displayed) {
  124.         /* Display a digit */
  125.         if(seven_seg_cc == 0) {
  126.             /* Display rightmost digit - tenths of seconds */
  127.             PORTA = seven_seg_data[(count/10)%10];
  128.         } else {
  129.             /* Display leftmost digit - seconds + decimal point */
  130.             PORTA = seven_seg_data[(count/100)%10] | 0x80;
  131.         }
  132.         /* Output the digit selection (CC) bit */
  133.         PORTC = seven_seg_cc;  
  134.     } else {
  135.         /* No digits displayed -  display is blank */
  136.         PORTA = 0;
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement