Operácie

AVR A/D example.c: Rozdiel medzi revíziami

Zo stránky SensorWiki

Balogh (diskusia | príspevky)
Bez shrnutí editace
Balogh (diskusia | príspevky)
Bez shrnutí editace
Riadok 2: Riadok 2:
#include <avr/io.h>
#include <avr/io.h>


/* Analog/Digital Wandler initialisieren */
void adc_init(void);                                 // A/D converter initialization
void adc_init(void);
 


int main(void)
int main(void)
{
{
  unsigned int buffer;


unsigned int buffer;
  adc_init();                                         // Init A/D converter


/* A/D - Wandler initialisieren */
  while (1)
adc_init();
  {
    ADCSRA |= (1<<ADSC);                              // Start A/D conversion
    while ( !(ADCSRA & (1<<ADIF)) /* Do nothing */ ;  // Wait until AD not finished
    buffer = (ADCH<<8) | ADCL;                       // Then, read result
  }


while (1)
}
{
 
/* Wandlung starten */
ADCSRA |= (1<<ADSC);
 
/* Warten bis die AD-Wandlung abgeschloßen ist */
while ( !(ADCSRA & (1<<ADIF)) )
;
 
/* AD-Wert auslesen */
buffer = (ADCH<<8) | ADCL;
 
}


}


/* Analog/Digital Wandler initialisieren */
void adc_init(void)                                 // A/D converter initialization
void adc_init(void)
{
{
 
  ADMUX = 0;                                         // Ref: external, Channel 0 (ADC0)
/* externe Referenzspannung und AD-Wandlerkanal 0 ( ADC0 ) auswählen */
  ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1);     // A/D ON, prescaler = 64,
ADMUX = 0;
  SFIOR = 0;                                     // Specialfunctions OFF
 
/* AD-Wandler einschalten und Prescaler = 64 einstellen ( enstpricht 115 khz Wandlertakt ) */
ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1);
 
/* Spezialfunktionen ausschalten */
SFIOR = 0;
}
}
</source>
</source>

Verzia z 23:04, 30. november 2008

#include <avr/io.h>

void adc_init(void);                                 // A/D converter initialization

int main(void)
{
  unsigned int buffer;

  adc_init();                                         // Init A/D converter

  while (1)
  {
    ADCSRA |= (1<<ADSC);                              // Start A/D conversion
    while ( !(ADCSRA & (1<<ADIF)) /* Do nothing */ ;  // Wait until AD not finished
    buffer = (ADCH<<8) | ADCL;                        // Then, read result
  }

}


void adc_init(void)                                  // A/D converter initialization
{
  ADMUX = 0;                                         // Ref: external, Channel 0 (ADC0)
  ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1);  	     // A/D ON, prescaler = 64, 
  SFIOR = 0;	                                     // Specialfunctions OFF
}