Zitat von
Che Guevara
Wieso schreibst du in der Mehrzahl? Schreibfehler?
weil z.B. wenn Result liegt zw. 2000 und 3000 soll es LED0 , LED1 und LED2 leuchten.
Code:
while(1) // cycle
{
ADCA.CH0.CTRL |= ADC_CH_START_bm; // start conversion on channel 0
while(!ADCA.CH0.INTFLAGS);
Result = ADCA.CH0RES;
if (Result > 1000) //
{
LED.OUTSET = (1 << 0); // output E0 High.
} //if
if (Result > 2000) //
{
LED.OUTSET = (1 << 1); // output E1 High.
} //if
if (Result > 3000) //
{
LED.OUTSET = (1 << 2); // output E2 High.
} //if
if (Result > 4000) //
{
LED.OUTSET = (1 << 3); // output E3 High.
} //if
LED.OUTCLR = 0b00001111;
}//while(1)
}//main
Wenn ich aber anderes code verwende, dann bleibt nur ein aktuelles LED dunkler.
Code:
while(1) // cycle
{
ADCA.CH0.CTRL |= ADC_CH_START_bm; // start conversion on channel 0
while(!ADCA.CH0.INTFLAGS);
Result = ADCA.CH0RES;
if (Result >= 0 && Result < 1000) //
{
LED.OUTSET = (1 << 0); // output E0 High.
} //if
if (Result >= 2000 && Result < 3000) //
{
LED.OUTSET = (1 << 1); // output E1 High.
} //if
if (Result >= 2000 && Result < 3000) //
{
LED.OUTSET = (1 << 2); // output E2 High.
} //if
if (Result >= 3000 && Result < 4096) //
{
LED.OUTSET = (1 << 3); // output E3 High.
} //if
LED.OUTCLR = 0b00001111;
}//while(1)
}//main
- - - Aktualisiert - - -
@robin, @Che Guevara
Danke euch beiden! Ihr habt mir wirklich geholfen! So sieht es jetzt aus:
Code:
//I/O Registersdefinition:
#include <xmega128a1.h> //Xmega-A1-Xplained development board
#include <delay.h>
#define LED PORTE// LED as PORT E.
volatile int Result;
void main()
{
LED.DIR = 0b00001111; //E0 - E3 - outputs.
PORTA.DIR = 0; // configure PORTA as input
ADCA.CTRLA |= 0x1; // enable adc
ADCA.CTRLB = ADC_RESOLUTION_12BIT_gc; // 12 bit conversion
ADCA.REFCTRL = ADC_REFSEL_VCC_gc | 0x02; // internal 1V bandgap reference
ADCA.PRESCALER = ADC_PRESCALER_DIV8_gc; // peripheral clk/8 (2MHz/16=250kHz)
ADCA.CH0.CTRL = ADC_CH_INPUTMODE_SINGLEENDED_gc;// single ended
ADCA.CH0.MUXCTRL = ADC_CH_MUXPOS_PIN0_gc; // PORTA:0
while(1) // cycle
{
ADCA.CH0.CTRL |= ADC_CH_START_bm; // start conversion on channel 0
while(!ADCA.CH0.INTFLAGS);
Result = ADCA.CH0RES;
if (Result >= 0 && Result < 1000) //
{
LED.OUTCLR = (1 << 0); // output E0 High.
} //if
if (Result >= 1000 && Result < 2000) //
{
LED.OUTCLR = (1 << 1); // output E1 High.
} //if
if (Result >= 2000 && Result < 3000) //
{
LED.OUTCLR = (1 << 2); // output E2 High.
} //if
if (Result >= 3000 && Result < 4096) //
{
LED.OUTCLR = (1 << 3); // output E3 High.
} //if
delay_ms(50);
LED.OUTSET = 0b00001111;
}//while(1)
}//main
Lesezeichen