Advertisement
Guest User

Untitled

a guest
Apr 6th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.10 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #include<util/delay.h>
  4. #include<stdlib.h>
  5.  
  6. char result[5];
  7.  
  8. void init_ADC()
  9. {
  10. ADMUX |= 1<<REFS0;
  11. ADMUX |= 1<<ADLAR;
  12. ADCSRA |= 1<<ADEN;
  13. ADCSRA |= 1<<ADIE;
  14.  
  15. //ADCSRA |= 1<<ADFR;
  16.  
  17. ADCSRA |= 1<<ADPS2; //prescalar factor of 16
  18. }
  19.  
  20. int ReadADC(uint8_t ch)
  21. {
  22. //Select ADC Channel ch must be 0-7
  23. ch=ch&0b00000111;
  24. ADMUX|=ch;
  25.  
  26. //Start Single conversion
  27. ADCSRA |= (1<<ADSC);
  28.  
  29. //Wait for conversion to complete
  30. while(!(ADCSRA&(1<<ADIF)));
  31.  
  32. ADCSRA|=(1<<ADIF);
  33.  
  34. return(ADC);
  35. }
  36.  
  37. int main()
  38. {
  39. DDRC = 0×00;
  40.  
  41. //sei();    
  42. init_ADC();
  43. int x1,x2,x3,x4;
  44.  
  45. DDRB = 0xFF;
  46. DDRD = 0xFF;
  47. PORTB= 0x00;
  48. _delay_ms(100);
  49. while(1)
  50.  
  51. {
  52.  
  53. x1=ReadADC(2);      //forward   PINC2
  54. x2=ReadADC(3);      //right     PINC3
  55. x3=ReadADC(4);      //left      PINC4
  56. x4=ReadADC(5);      //BACK      PINC5
  57.  
  58. if(x1<512)
  59. {
  60.     PORTB = 0X0F;
  61.     PORTD = 0b00010100;
  62. }
  63. if(x2<512)
  64. {
  65.     PORTB = 0X0F;
  66.     PORTD = 0b00100100;
  67. }
  68. if(x3<512)
  69. {
  70.     PORTB = 0X0F;
  71.     PORTD = 0b00011000;
  72. }
  73. if(x4<512)
  74. {
  75.     PORTB = 0X0F;
  76.     PORTD = 0b00101000;
  77. }
  78. }
  79.  
  80. return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement