Advertisement
czaru34

keypadavrc

Dec 10th, 2019
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.90 KB | None | 0 0
  1. //Cezary Zaprawa
  2.  
  3. #include <avr/io.h>
  4. #include <avr/interrupt.h>
  5.  
  6. volatile uint8_t columnPosition = 0;
  7. volatile uint8_t rowPosition = 0;
  8.  
  9. ISR(TIMER1_OVF_vect) {
  10.     DDRA = (1 << columnPosition);
  11.     PORTA = ~(1 << columnPosition);
  12.     if((PINA & 0xf0) != 0xf0) {
  13.         rowPosition = PINA;
  14.         if(!(rowPosition & (1 << 4))) rowPosition = 0;
  15.         else if(!(rowPosition & (1 << 5))) rowPosition = 1;
  16.         else if(!(rowPosition & (1 << 6))) rowPosition = 2;
  17.         else if(!(rowPosition & (1 << 7))) rowPosition = 3;
  18.         PORTD = rowPosition*4 + columnPosition;
  19.     }
  20.     columnPosition++;
  21.     if(columnPosition == 4) columnPosition = 0;
  22. }
  23.  
  24. void init() {
  25.     cli();
  26.     ICR1 = 15625;
  27.     TCCR1A |= (1 << WGM11);
  28.     TCCR1B |= (1 << WGM12) | (1 << WGM13) | (1 << CS11) | (1 << CS10);
  29.     TIMSK |= (1 << TOIE1);
  30.  
  31.     DDRD = 0xFF;
  32.     DDRA = 0x0F;
  33.     PORTA = 0xFF;
  34.     PORTD = 0xFF;
  35.  
  36.     sei();
  37. }
  38.  
  39. int main(void) {
  40.     init();
  41.     while(1) {
  42.         asm("nop");
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement