Advertisement
ivosexa

PERHIDROL 2

Mar 14th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.04 KB | None | 0 0
  1. #define F_CPU 1000000UL
  2. #include <avr/io.h>
  3. #include <avr/interrupt.h>
  4. #include "LCD4b.h"
  5. #include <unit/delay.h>
  6. #include <math.h>
  7.  
  8. int sens[6];
  9.  
  10. int val1, val2, val3, val4, val5;
  11. float tot[5];
  12.  
  13. int bashtotal=0;
  14. int a;
  15. int dc;
  16. int bc;
  17. int limit=150;
  18. int value;
  19. int measure();
  20. float totalvalue;
  21.  
  22.     int main(void)
  23.     {
  24.     int distance;
  25.     DDRD=0xff;
  26.     PORTD=0x00;
  27.    
  28.    
  29.     InitalizeLCD();
  30.    
  31.    
  32.    
  33.  
  34.    
  35.  
  36.         while (1)
  37.         {
  38.            
  39.         measure();
  40.    
  41.            
  42.  
  43.         }  
  44.     }
  45. measure()
  46. {
  47.     for(a=0; a<5; a++)
  48.     {
  49.         ADCSRA |= 1<<ADPS2;
  50.             if(a==0)
  51.             {
  52.             ADMUX = 5;
  53.             }
  54.             if(a==1)
  55.             {
  56.             ADMUX = 4;
  57.             }
  58.             if(a==2)
  59.             {
  60.             ADMUX = 3;
  61.             }
  62.             if(a==3)
  63.             {
  64.             ADMUX = 2;
  65.             }
  66.             if(a==4)
  67.             {
  68.             ADMUX = 1;
  69.             }
  70.         ADCSRA |= 1<<ADIE;
  71.         ADCSRA |= 1<<ADEN;
  72.  
  73.         sei();
  74.  
  75.         ADCSRA |= 1<<ADSC;
  76.     }
  77.    
  78.  
  79.  
  80.  
  81.  
  82. }
  83.  
  84. ISR(ADC_vect)
  85. {
  86. int  theLowADC = ADCL;
  87. int  theTenBitResults = ADCH<<8 | theLowADC;
  88. int distance = theTenBitResults;
  89.  
  90. if(ADMUX == 5)
  91. {
  92. val5=distance;
  93.     if(distance<limit)
  94.     {sens[5]=1;}
  95.     else
  96.     {sens[5]=0;}
  97. }
  98.  
  99. if(ADMUX == 4)
  100. {
  101. val4=distance;
  102.     if(distance<limit)
  103.     {sens[4]=1;}
  104.     else
  105.     {sens[4]=0;}
  106. }
  107.  
  108. if(ADMUX == 3)
  109. {
  110. val3=distance;
  111.     if(distance<limit)
  112.     {sens[3]=1;}
  113.     else
  114.     {sens[3]=0;}
  115. }
  116.  
  117. if(ADMUX == 2)
  118. {
  119. val2=distance;
  120.     if(distance<limit)
  121.     {sens[2]=1;}
  122.     else
  123.     {sens[2]=0;}
  124. }
  125.  
  126. if(ADMUX == 1)
  127. {
  128. val1=distance;
  129.  
  130.     if(distance<limit)
  131.     {sens[1]=1;}
  132.     else
  133.     {sens[1]=0;}
  134. }
  135.  
  136. totalvalue= ((sens[1]*5 + sens[2]*1 + sens[3]*2 + sens[4]*3 + sens[5]*4)/(sens[1]+sens[2]+sens[3]+sens[4]+sens[5]));
  137.  
  138. //switch (a)
  139. //{
  140. //case 0:
  141. //tot[0]=totalvalue;
  142. //break;
  143. ////case 1:
  144. //tot[1]=totalvalue;
  145. //break;
  146. //case 2:
  147. //tot[2]=totalvalue;
  148. //break;
  149. //case 3:
  150. //tot[3]=totalvalue;
  151. //break;
  152. //case 4:
  153. //tot[4]=totalvalue;
  154. //break;
  155. //}
  156.  
  157.     bashtotal=totalvalue;
  158.             if(bashtotal == 1)
  159.             {PORTD=1<<PD0;}
  160.             if(bashtotal == 2)
  161.             {PORTD=1<<PD1;}
  162.             if(bashtotal == 3)
  163.             {PORTD=1<<PD2;}
  164.             if(bashtotal == 4)
  165.             {PORTD=1<<PD3;}
  166.             if(bashtotal == 5)
  167.             {PORTD=1<<PD4;}
  168.  
  169.  
  170. ADCSRA |= 1<<ADSC;
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement