Advertisement
95Tomasz

Joystick Elo

Jan 16th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.63 KB | None | 0 0
  1. /*
  2.  * DawidTomaszewski3.c
  3.  *
  4.  * Created: 2018-01-10 08:55:34
  5.  *  Author: C245
  6.  */
  7.  
  8.  
  9. #include <avr/io.h>
  10. #include <util/delay.h>
  11. #include <avr/interrupt.h>
  12.  
  13. #define d1 0
  14. #define d2 1
  15. #define d3 2
  16. #define d4 3
  17. #define d5 4
  18. #define d6 5
  19. #define d7 6
  20. #define d8 7
  21.  
  22. int i = -1;
  23. int a = 0;
  24. int b = 0;
  25.  
  26.  
  27. void setup(){
  28.     DDRD |= (1<<d1);
  29.     DDRD |= (1<<d2);
  30.     DDRD |= (1<<d3);
  31.     DDRD |= (1<<d4);
  32.     DDRD |= (1<<d5);
  33.     DDRD |= (1<<d6);
  34.     DDRD |= (1<<d7);
  35.     DDRD |= (1<<d8);
  36.    
  37.     PORTD |=(1<<d1);
  38.     PORTD |=(1<<d2);
  39.     PORTD |=(1<<d3);
  40.     PORTD |=(1<<d4);
  41.     PORTD |=(1<<d5);
  42.     PORTD |=(1<<d6);
  43.     PORTD |=(1<<d7);
  44.     PORTD |=(1<<d8);
  45.    
  46.    
  47.     DDRC &=~(1<<0);
  48.    
  49.     ADMUX |= (1<<REFS0);
  50.     ADCSRA |=(1<<ADEN) | (1<<ADSC) | (1<<ADPS2) | (1<<ADPS0) | (1<<ADIE);
  51.    
  52.    
  53.    
  54. }
  55.  
  56.  
  57.  
  58. void loop(){
  59. int AC = ADC;
  60. if(AC < 600 && AC > 400) a = 0;
  61. else if (AC > 1000) a = 1;
  62. else if(AC < 40) a = 2;
  63. //_delay_ms(20);
  64. switch(a){
  65.     case 0:
  66.     b=0;
  67.     break;
  68.     case 1:
  69.     if (b==0)
  70.     {
  71.         i++;
  72.         PORTD &= ~(1<<i);
  73.         b=1;
  74.     }
  75.     else
  76.     {
  77.         b=1;
  78.     }
  79.     break;+++-+
  80.     case 2:
  81.     if (b==0)
  82.     {
  83.         i--;
  84.         PORTD|=  (1<<i);
  85.         b=2;
  86.     }
  87.     else
  88.     {
  89.         b=2;
  90.     }
  91.     break;
  92. }
  93. }
  94.  
  95. int main(void)
  96. {
  97.     setup();
  98.     while (1) loop();
  99.     return 0;
  100. }
  101.  
  102. ISR(ADC_vect){
  103.    
  104.     int AC = ADC;
  105.     if(AC < 600 && AC > 400) a = 0;
  106.     else if (AC > 1000) a = 1;
  107.     else if(AC < 40) a = 2;
  108.     _delay_ms(20);
  109.     switch(a){
  110.         case 0:
  111.             b=0;
  112.             break;
  113.         case 1:
  114.             if (b==0)
  115.             {
  116.                 i++;
  117.                 PORTD &= ~(1<<i);
  118.                 b=1;
  119.             }
  120.             else
  121.             {
  122.                 b=1;
  123.             }
  124.             break;
  125.         case 2:
  126.             if (b==0)
  127.             {
  128.                 i--;
  129.                 PORTD|=  (1<<i);
  130.                 b=2;
  131.             }
  132.             else
  133.             {
  134.                 b=2;
  135.             }
  136.             break;
  137.     }
  138.    
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement