?? task2.c
字號:
/*
* -= Task 2 =-
*
* In addition to normal single ended conversions the ADC also has a compare function.
* When this is used, the ADC can be set up to do continues conversions, but it will
* not signal a result (i.e. set the channel抯 interrupt flag) before the result is
* above or below a selectable threshold value. This feature is very useful for
* applications where you only wish to check that the input signal is not higher or
* lower than a specific voltage. Without the compare function, it would be natural
* to check the result in software for every conversion. With the compare function in
* the XMEGA DAC, the application will signal an interrupt or event when the threshold
* is reached. This reduces the CPU load, and keeps the resource use to a minimum.
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include "../adc_driver.h"
#define LEDPORT PORTD
volatile uint8_t offset;
volatile uint16_t ADC_result;
ISR(ADCA_CH0_vect)
{
// Getting the 8MSB of the result
ADC_result = ADCA.CH0.RES;
// Ignore negative results
if(ADC_result > 0x07FF)
{
ADC_result = 0;
}
ADC_result = (ADC_result >> 4);
}
int main( void )
{
/* Move stored calibration values to ADC A. */
ADC_CalibrationValues_Set(&ADCA);
/* Get offset value for ADC A. */
offset = ADC_Offset_Get(&ADCA);
// Set as output, the 8 MSB of the ADC result is output here
LEDPORT.DIR = 0xFF;
// Turn off the LEDs as Default
LEDPORT.OUT = 0xFF;
// Set up ADC A to have signed conversion mode and a adjusted 12 bit resolution
ADC_ConvMode_and_Resolution_Config(&ADCA, true, ADC_RESOLUTION_12BIT_gc);
// Sample rate is CPUFREQ/8. Allow time for storing data
ADC_Prescaler_Config(&ADCA, ADC_PRESCALER_DIV8_gc);
// Set reference voltage on ADC A to be VCC-0.6 V
ADC_Reference_Config(&ADCA, ADC_REFSEL_VCC_gc);
// Setup channel 0, 1, 2 and 3 to have single ended input
ADC_Ch_InputMode_and_Gain_Config(&ADCA.CH0,
ADC_CH_INPUTMODE_SINGLEENDED_gc,
ADC_CH_GAIN_1X_gc);
// Set input to the channels in ADC A to be PIN 1
ADC_Ch_InputMux_Config( &ADCA.CH0,
ADC_CH_MUXPOS_PIN1_gc,
0);
// The ADC Compare (CMP) register must be set with the threshold value when
// compare mode is used. Add code that sets the CMP register to the half of
// the ADCA resolution
ADCA.CMP = (0x800 / 2);
// Wait until common mode voltage is stable. Default clk is 2MHz and
// therefore below the maximum frequency to use this function.
ADC_Wait_8MHz(&ADCA);
// Setup Interrupt Mode Above Compare Value (ADCA.CMP)
ADC_Ch_Interrupts_Config( &ADCA.CH0,
// INSERT CODE
ADC_CH_INTMODE_ABOVE_gc,
ADC_CH_INTLVL_MED_gc);
// Enable the Medium Level interrupts needed in the PMIC
// ADD CODE HERE
PMIC.CTRL |= PMIC_MEDLVLEN_bm;
// Enable free running mode
ADC_FreeRunning_Enable(&ADCA);
// Enable ADC A
ADC_Enable(&ADCA);
// Enable Global interrupts
sei();
while(1)
{
// Invert the output since a pin high is LED off
LEDPORT.OUT = ~ADC_result;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -