Hallo,
ich ein Fehler in dem Code. Wenn Spannung am ADC fehlt runter, LEDs zeigen das. Aber wenn es steigt, LEDs sind immer aus. Ich nutze CodeVisionAVR und arbeite mit dem Xmega128-A1 Xplained board.
Code:
#include <xmega128a1.h> //Xmega-A1-Xplained development board
#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 > 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
}//while(1)
}//main
Lesezeichen