Advertisement
czaru34

segmentówka2

Jan 7th, 2020
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.99 KB | None | 0 0
  1.  
  2. //Cezary Zaprawa Prosze wyswietlic sekwencje znakow na wyswietlaczu, tak, aby kazdy znak byl w oddzielnym segmencie
  3. //Po kliknieciu sekwencja na wyswietlaczu powinien
  4. #include <avr/io.h>
  5. #include <avr/interrupt.h>
  6. #include <avr/pgmspace.h>
  7.  
  8. #define BUTTON_MASK (1<<PD2)
  9. #define BUTTON_PIN  PIND
  10. #define BUTTON_PORT PORTD
  11.  
  12. volatile uint8_t button_down;
  13.  
  14.  
  15. const char codeArray[16] PROGMEM =
  16. {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e};
  17.  
  18. char array[16] =
  19. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
  20.  
  21. volatile uint8_t readByte = 0;
  22. volatile uint8_t displayCounter = 0;
  23. volatile uint8_t arrayCounter = 0;
  24. volatile uint8_t index = 0;
  25.  
  26. ISR(TIMER1_OVF_vect) {
  27.     PORTB = ~(1 << displayCounter);
  28.     readByte = pgm_read_byte(&codeArray[array[arrayCounter + index]]);
  29.     PORTA = readByte;
  30.     displayCounter++;
  31.    
  32.     if (displayCounter == 4)
  33.         displayCounter = 0;
  34.        
  35.     if(arrayCounter == index+3)
  36.         arrayCounter = index;
  37.     else
  38.         arrayCounter++;
  39. }
  40.  
  41. ISR(TIMER0_COMP_vect) {
  42.   static uint8_t count = 0;
  43.   static uint8_t button_state = 0;
  44.   uint8_t current_state = (~BUTTON_PIN & BUTTON_MASK) != 0;
  45.   if (current_state != button_state) {
  46.     count++;
  47.    
  48.     if (count >= 4) {
  49.       button_state = current_state;
  50.      
  51.       if (current_state != 0)
  52.         button_down = 1;
  53.        
  54.       count = 0;
  55.     }
  56.   } else {
  57.     count = 0;
  58.   }
  59. }
  60.  
  61.  
  62. ISR(INT0_vect) {
  63.     if(button_down) {
  64.         button_down = 0;
  65.         if (index < 6) index++;
  66.         displayCounter = 0;
  67.     }
  68. }
  69.  
  70. void init(){
  71.     cli();
  72.    
  73.     //Timer1
  74.     ICR1 = 150;
  75.     TCCR1A |= (1 << WGM11);
  76.     TCCR1B |= (1 << WGM12) | (1 << WGM13) | (1 << CS10);
  77.     TIMSK |= (1 << TOIE1);
  78.    
  79.    
  80.     //Timer0
  81.     OCR0 = 250;
  82.     TIMSK |= (1 << OCIE0);
  83.     TCCR0 |= (1 << WGM01) | (1 << CS01);
  84.    
  85.        
  86.     //Int0
  87.     MCUCR |= (1 << ISC01);
  88.     GICR |= (1 << INT0);
  89.    
  90.     DDRA = 0xFF;
  91.     PORTA = 0x00;
  92.  
  93.     DDRB = 0b00001111;
  94.     sei();
  95. }
  96.  
  97. int main(void) {
  98.     init();
  99.     BUTTON_PORT |= BUTTON_MASK;
  100.     while(1) {
  101.         asm("nop");
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement