ADC测量差值输入模式:
#include <atmel_start.h>
#include <util/delay.h>
#include <stdio.h>
#include <avr/io.h>
#include <stdbool.h>
#include <string.h>
volatile uint8_t switchPressInterruptFlag;
volatile uint8_t pitInterruptFlag;
volatile int16_t adcVal;
static void PORT_init(void);
static void VREF0_init(void);
static void ADC0_init(void);
static void ADC0_start(void);
static bool ADC0_conversionDone(void);
static int16_t ADC0_read(void);
static void SYSTEM_init(void);
static void PORT_init(void)
{
/* Disable interrupt and digital input buffer on PD3 */
PORTD.PIN3CTRL &= ~PORT_ISC_gm;
PORTD.PIN3CTRL |= PORT_ISC_INPUT_DISABLE_gc;
/* Disable interrupt and digital input buffer on PD4 */
PORTD.PIN4CTRL &= ~PORT_ISC_gm;
PORTD.PIN4CTRL |= PORT_ISC_INPUT_DISABLE_gc;
/* Disable pull-up resistor */
PORTD.PIN3CTRL &= ~PORT_PULLUPEN_bm;
PORTD.PIN4CTRL &= ~PORT_PULLUPEN_bm;
}
static void VREF0_init(void)
{
VREF.ADC0REF = VREF_REFSEL_2V048_gc; /* Internal 2.048V reference */
}
static void ADC0_init(void)
{
ADC0.CTRLC = ADC_PRESC_DIV4_gc; /* CLK_PER divided by 4 */
ADC0.CTRLA = ADC_ENABLE_bm /* ADC Enable: enabled */
| ADC_RESSEL_12BIT_gc /* 12-bit mode */
| ADC_CONVMODE_bm /* Differential Conversion */
| ADC_FREERUN_bm; /* Enable Free-Run mode */
ADC0.MUXPOS = ADC_MUXPOS_AIN3_gc; /* Select ADC channel AIN3 <-> PD3 */
ADC0.MUXNEG = ADC_MUXNEG_AIN4_gc; /* Select ADC channel AIN4 <-> PD4 */
}
static int16_t ADC0_read(void)
{
/* Clear the interrupt flag by reading the result */
return ADC0.RES;
}
static void ADC0_start(void)
{
/* Start conversion */
ADC0.COMMAND = ADC_STCONV_bm;
}
static bool ADC0_conversionDone(void)
{
/* Check if the conversion is done */
return (ADC0.INTFLAGS & ADC_RESRDY_bm);
}
static void SYSTEM_init(void)
{
PORT_init();
VREF0_init();
ADC0_init();
}
int main(void)
{
/* Initializes MCU, drivers and middleware */
atmel_start_init();
SYSTEM_init();
ADC0_start();
while (1) {
if(ADC0_conversionDone()){
adcVal = ADC0_read();
printf("%d \r\n",adcVal);
_delay_ms(1000);
}
}
}
avr128da48-adc_chazhi_usart-studio.rar
(197.19 KB)
PD3悬空 PD4接地时:
显示变化值
PD3接VDD PD4接地时:
参考电压最高2.048 显示最大值2047
|