Estoy trabajando en dsPIC30F2020 para leer dos canales analógicos de forma secuencial en AN0 y AN1. Pero todo lo que obtuve es solo el valor de AN1, así que, ¿qué hay de malo en mis códigos a continuación? Muchas gracias por la ayuda
#define FCY 8000000UL // FCY = FOSC / 2
#include <xc.h>
#include <stdio.h>
#include <libpic30.h>
#include <p30F2020.h>
#include "config.h"
int main(void)
{
int channel1Result;
int channel3Result;
/* Set up the ADC Module */
ADCONbits.ADSIDL = 0; /* Operate in Idle Mode */
ADCONbits.FORM = 0; /* Output in Integer Format */
ADCONbits.EIE = 1; /* Enable Early Interrupt */
ADCONbits.ORDER = 0; /* Even channel first */
ADCONbits.SEQSAMP = 1; /* Sequential Sampling Enabled */
ADCONbits.ADCS = 5; /* Clock Divider is set up for Fadc/14 */
ADPCFG = 0; // all ANx analog input
ADSTAT = 0; /* Clear the ADSTAT register */
ADCPC0bits.TRGSRC0 = 1; /* Use SW trigger */
ADCPC0bits.IRQEN0 = 1; /* Enable the interrupt */
ADCPC1bits.TRGSRC2 = 1;
ADCPC1bits.IRQEN2 = 1;
ADCONbits.ADON = 1; /* Start the ADC module */
/* Set up the Interrupts */
IFS0bits.ADIF = 0; /* Clear AD Interrupt Flag */
IFS2bits.ADCP2IF = 0;
IPC2bits.ADIP = 4; /* Set ADC Interrupt Priority */
IEC0bits.ADIE = 1; /* Enable the ADC Interrupt */
IEC1bits.AC1IE = 1;
ADCPC0bits.SWTRG0 = 1; /* Trigger the Conversion Pair 0 */
ADCPC1bits.SWTRG2 = 1;
int a1;
int a2;
// enable UART communication
U1BRG = 12; // baudrate
U1MODEbits.UARTEN = 1; // enable UART
PORTA = 0xffff;
TRISAbits.TRISA9 = 0;
while (1)
{
PORTAbits.RA9 = 1;
while(ADCPC0bits.PEND0); /* Wait for the 2nd conversion to
complete */
channel1Result = ADCBUF1; /* Read the result of the second
conversion */
while(ADCPC1bits.PEND2);
channel3Result = ADCBUF3;
a1 = channel1Result;
a2 = channel3Result;
ADCPC0bits.SWTRG0 = 1; /* Trigger another conversion */
ADCPC1bits.SWTRG2 = 1;
printf("data 1 : ");
printf("%d", a1);
printf("\t data 2 : ");
printf("%d", a2);
printf("\n");
__delay_ms(500);
}
}
void __attribute__ ((interrupt, no_auto_psv)) _ADCInterrupt(void)
{
/* AD Conversion complete early interrupt handler */
int channel0Result;
int channel2Result;
IFS0bits.ADIF = 0; /* Clear ADC Interrupt Flag */
IFS2bits.ADCP1IF = 0;
channel0Result = ADCBUF0; /* Get the conversion result */
channel2Result = ADCBUF2;
ADSTATbits.P0RDY = 0; /* Clear the ADSTAT bits */
ADSTATbits.P2RDY = 0;
}