uwezi

20211116_1TE663_lab5

Nov 16th, 2021
1,416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.37 KB | None | 0 0
  1. #define F_CPU 1000000UL
  2. #include <avr/io.h>
  3. #include <util/delay.h>
  4. #include <avr/interrupt.h>
  5.  
  6. uint8_t font[10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
  7. volatile uint8_t framebuffer[4];     // use to buffer contents of display
  8.  
  9. ISR(TIMER0_OVF_vect) // the code for the interrupt service routine
  10. {
  11.   static uint8_t current_digit = 0;
  12.   PORTD = 0xff;                  // all segments off
  13.   current_digit += 1;            // step one digit
  14.   current_digit %= 4;            //    and keep between 0-3
  15.   PORTB = (0b00010000 << current_digit);
  16.   PORTD = framebuffer[current_digit];
  17. }
  18.  
  19. void display(uint16_t value)  // print a 4-digit number
  20. {
  21.   uint16_t A = value;
  22.   uint8_t  i;
  23.   for (i = 0; i < 4; i++)
  24.   {
  25.     framebuffer[3-i] = ~font[A % 10];
  26.     A = A / 10;
  27.   }
  28. }
  29.  
  30. void init(void)
  31. {
  32.   DDRB = 0b11110000;    // upper B-pins as output
  33.   DDRD = 0b11111111;    // all D-pins as output
  34.  
  35.   // setting up TIMER0
  36.   TCCR0A = (0 << COM0A1) | (0 << COM0A0) | (0 << COM0B1) | (0 << COM0B0)
  37.          | (0 << WGM01)  | (0 << WGM00);
  38.   TCCR0B = (0 << WGM02)  | (0 << CS02)   | (1 << CS01)   | (0 << CS00);
  39.   TIMSK0 = (0 << OCIE0B) | (0 << OCIE0A) | (1 << TOIE0);
  40.  
  41.   sei();  // don't forget to enable interrupts
  42. }
  43.  
  44. int main(void)
  45. {
  46.   uint16_t value = 9999;
  47.   init();
  48.   while (1)
  49.   {
  50.     display(value);
  51.     value --;
  52.     _delay_ms(20);
  53.   }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment