Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.50 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <util/delay.h>
  3. #include <avr/interrupt.h>
  4.  
  5. #define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT))
  6. #define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT))
  7. #define FLIPBIT(ADDRESS,BIT) (ADDRESS ^= (1<<BIT))
  8. #define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT))
  9.  
  10. volatile int counter;
  11.  
  12. void set_digit(int digit) {
  13.     if (digit == 0) {
  14.         PORTB = 0b00001110;
  15.         PORTD = 0b11100000;
  16.     } else if (digit == 1) {
  17.         PORTD = 0b10000000;
  18.         PORTB = 0b00001000;
  19.     } else if (digit == 2) {
  20.         PORTB = 0b00001101;    
  21.         PORTD = 0b01100000;
  22.     } else if (digit == 3) {
  23.         PORTB = 0b00001101;    
  24.         PORTD = 0b11000000;
  25.     } else if (digit == 4) {
  26.         PORTB = 0b00001011;
  27.         PORTD = 0b10000000;
  28.     } else if (digit == 5) {
  29.         PORTB = 0b00000111;    
  30.         PORTD = 0b11000000;
  31.     } else if (digit == 6) {
  32.         PORTB = 0b00000111;    
  33.         PORTD = 0b11100000;
  34.     } else if (digit == 7) {
  35.         PORTB = 0b00001100;    
  36.         PORTD = 0b10000000;
  37.     } else if (digit == 8) {
  38.         PORTB = 0b00001111;    
  39.         PORTD = 0b11100000;
  40.     } else if (digit == 9) {
  41.         PORTB = 0b00001111;    
  42.         PORTD = 0b11000000;
  43.     }
  44. }
  45.  
  46. ISR(INT0_vect) {
  47.     counter = (counter + 1) % 10;
  48.     set_digit(counter);
  49. }
  50.  
  51. int main()
  52. {  
  53.     int i;
  54.     DDRD = PORTD = DDRB = PORTB = 0;
  55.     for (i = 5; i < 8; i++) {
  56.         DDRD |= (1 << i);
  57.     }
  58.     for (i = 0; i < 4; i++) {
  59.         DDRB |= (1 << i);
  60.     }
  61.     PORTD |= 1 << 2;
  62.     counter = 0;
  63.    
  64.     CLEARBIT(EICRA, ISC01);
  65.     SETBIT(EICRA, ISC00);
  66.     sei(); // allow interrupts
  67.     SETBIT(EIMSK, 0);
  68.      
  69.     set_digit(counter);
  70.     while(1) { }
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement