AD_Wandler.c


1
//ATMEGA32
2
//Quarz 16MHz
3
//WinAVR
4
5
6
#include <avr/io.h>
7
#include <avr/interrupt.h>
8
9
10
void Port_Initial(void);          //Port als Ausgang initialsieren
11
void adc_init(void);            //Analog-Digital Wandler initialisieren 
12
13
14
int main(void)
15
{
16
17
  typedef unsigned short WORD; 
18
  WORD intADW0;
19
  WORD intADW1;
20
21
22
  Port_Initial();
23
  adc_init();  
24
25
26
  while (1)
27
  {
28
  
29
    ADMUX = 0;                    //Kanal 0
30
    ADCSRA = (1<<ADEN)|(1<<ADSC)|(1<<ADIF);    //Wandlung starten
31
    while (!(ADCSRA & (1<<ADIF)));           //Warten bis die AD-Wandlung 
32
    intADW0 = ADC;                  //AD-Wert lesen
33
    
34
    
35
    ADMUX = 1;                    //Kanal 1
36
    ADCSRA = (1<<ADEN)|(1<<ADSC)|(1<<ADIF);    //Wandlung starten
37
    while (!(ADCSRA & (1<<ADIF)));           //Warten bis die AD-Wandlung 
38
    intADW1 = ADC;                  //AD-Wert lesen
39
    
40
    PORTB = intADW0;
41
    
42
    /*if (intADW0 > intADW1)
43
    {
44
      PORTB = 0xFF;
45
    }
46
    else
47
    {
48
      PORTB = 0x0F;
49
    }*/
50
    
51
    
52
  }
53
}
54
55
56
void Port_Initial(void)  //Ausgabeport wird konfiguriert
57
{
58
  DDRB = 0xff;        //Port B als Ausgang
59
  DDRA = 0x00;      //Port A als Eingang
60
}  
61
62
63
void adc_init(void)      //Analog-Digital Wandler initialisieren 
64
{
65
  ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);        
66
}