Facebook
From fff, 5 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 186
  1. /*
  2.  * zaj3.c
  3.  *
  4.  * Created: 29.10.2018 15:07:38
  5.  * Author : student
  6.  */
  7.  
  8. #include <avr/io.h>
  9. #include <inttypes.h>
  10. #include <avr/interrupt.h>
  11. #include <avr/sleep.h>
  12.  
  13. volatile float res;
  14. volatile float val;
  15.  
  16.  
  17. volatile int x1=0;
  18. volatile int x2=0;
  19.  
  20. ISR(INT0_vect)
  21. {
  22.         x1=1;
  23.         x2=0;
  24. }
  25.  
  26. ISR(INT1_vect)
  27. {
  28.         x2=1;
  29.         x1=0;
  30. }
  31.  
  32. int main(void)
  33. {
  34.  
  35.         DDRB=0xFF;
  36.         GICR = _BV(INT0) | _BV(INT1);
  37.         MCUCR = _BV(ISC11) | _BV(ISC01);
  38.  
  39.         ADCSRA = _BV(ADEN);
  40.         //ADMUX = _BV(MUX0);
  41.  
  42.         sei();
  43.  
  44.         PORTB = _BV(0) | _BV(1) | _BV(2) | _BV(3) | _BV(4) | _BV(5);
  45.  
  46.     while (1)
  47.     {
  48.                 sleep_mode();
  49.                
  50.                 if(x1==1) // INT0
  51.                 {
  52.                         ADCSRA |= _BV(ADSC) | _BV(ADPS1) | _BV(ADPS0); //preskaler 8
  53.                
  54.             loop_until_bit_is_clear( ADCSRA,ADSC);
  55.                                 uint16_t wynik= ADCW;
  56.  
  57.                     val = (wynik /1024.0) * 5.20;
  58.  
  59.                         res=(int)val;
  60.  
  61.  
  62.                 }
  63.  
  64.                 if(x2==1) // INT1
  65.                  {
  66.                         //PORTB = _BV(0) | _BV(2) | _BV(3) | _BV(5) |_BV(6);
  67.                         res=(val-(int)val)*10;
  68.                         res=(int) (res);
  69.  
  70.                  }
  71.  
  72.                
  73.  
  74. //              while( ADCSRA & _BV(ADSC) );
  75.  
  76. //              uint16_t wynik= ADCW;
  77.  
  78. //              val = wynik * 1024 / 5.25;
  79.  
  80.         //      PORTB = _BV(0) | _BV(1) | _BV(2) | _BV(3) | _BV(4) | _BV(5) |_BV(6);
  81.        
  82.                 if(res == 0)
  83.                         PORTB = _BV(0) | _BV(1) | _BV(2) | _BV(3) | _BV(4) | _BV(5) |_BV(6);
  84.                 if(res==1)
  85.                         PORTB =_BV(1) | _BV(2);
  86.                 if(res == 2)
  87.                         PORTB = _BV(0) | _BV(1) | _BV(3) | _BV(4) | _BV(6);
  88.                 if(res==3)
  89.                         PORTB = _BV(0) | _BV(1) | _BV(2) | _BV(3) | _BV(6);
  90.                 if(res==4)
  91.                         PORTB = _BV(2) | _BV(5) |_BV(6);
  92.                 if(res==5)
  93.                         PORTB = _BV(0) | _BV(2) | _BV(3) | _BV(5) |_BV(6);
  94.                 if(res==6)
  95.                         PORTB = _BV(0) | _BV(2) | _BV(3) | _BV(4) | _BV(5) |_BV(6) ;
  96.                 if(res==7)
  97.                         PORTB = _BV(0) | _BV(1) | _BV(2);
  98.                 if(res==8)
  99.                         PORTB = _BV(0) | _BV(1) | _BV(2) | _BV(3) | _BV(4) | _BV(5) |_BV(6);
  100.                 if(res ==9)
  101.                         PORTB = _BV(0) | _BV(1) | _BV(2) | _BV(3) | _BV(5) |_BV(6);
  102.                        
  103.                        
  104.         }
  105.         return 0;
  106. }
  107.  
  108.