ADC不工作PIC32MX795F512L MPLAB X IDE v5.20

问题描述

我正在尝试从两个电流传感器和两个热敏电阻获取ADC读数。但是ADC缓冲区始终返回零。代码有问题吗?

// PIC32MX795F512L Configuration Bit Settings
// 'C' source line config statements
// DEVCFG3

#pragma config USERID = 0xFFFF // Enter Hexadecimal value (Enter Hexadecimal value)
#pragma config FSRSSEL = PRIORITY_7 // SRS Select (SRS Priority 7)
#pragma config FMIIEN = ON // Ethernet RMII/MII Enable (MII Enabled)
#pragma config FETHIO = ON // Ethernet I/O Pin Select (Default Ethernet I/O)
#pragma config FCANIO = ON // CAN I/O Pin Select (Default CAN I/O)
#pragma config FUSBIdio = ON // USB USID Selection (Controlled by the USB Module)
#pragma config FVBUSONIO = ON // USB VBUS ON Selection (Controlled by USB Module)

// DEVCFG2
#pragma config FPLLIDIV = DIV_2 // PLL Input Divider (2x Divider)
#pragma config FPLLMUL = MUL_20 // PLL Multiplier (20x Multiplier)
#pragma config UPLLIDIV = DIV_12 // USB PLL Input Divider (12x Divider)
#pragma config UPLLEN = OFF // USB PLL Enable (disabled and Bypassed)
#pragma config FPLLODIV = DIV_4 // System PLL Output Clock Divider (PLL Divide by 4)

// DEVCFG1
#pragma config FNOSC = FRCPLL // Oscillator Selection Bits (Fast RC Osc with PLL)
#pragma config FSOSCEN = OFF // Secondary Oscillator Enable (disabled)
#pragma config IESO = OFF // Internal/External Switch Over (disabled)
#pragma config POSCMOD = OFF // Primary Oscillator Configuration (Primary osc disabled)
#pragma config OSCIOFNC = OFF // CLKO Output Signal Active on the OSCO Pin (disabled)
#pragma config FPBDIV = DIV_1 // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/1)
#pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection (Clock Switch disable,FSCM disabled)
#pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT disabled (SWDTEN Bit Controls))

// DEVCFG0
#pragma config DEBUG = OFF // Background Debugger Enable (Debugger is disabled)
#pragma config ICESEL = ICS_PGx1 // ICE/ICD Comm Channel Select (ICE EMUC1/EMUD1 pins shared with PGC1/PGD1)
#pragma config PWP = OFF // Program Flash Write Protect (disable)
#pragma config BWP = OFF // Boot Flash Write Protect bit (Protection disabled)
#pragma config CP = OFF // Code Protect (Protection disabled)
// #pragma config statements should precede project file includes.

// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<plib.h>

void initADC(void);
int ReadADC(int ch);
void delay_ms (WORD delay);

int main()
{
initADC();
unsigned int channel0;// conversion result as read from result buffer
unsigned int channel1;// conversion result as read from result buffer
unsigned int channel2;// conversion result as read from result buffer
unsigned int channel3;// conversion result as read from result buffer

while(1)
{
channel0=ReadADC(0);
delay_ms(0.5);
channel0=ReadADC(1);
delay_ms(0.5);
channel0=ReadADC(2);
delay_ms(0.5);
channel0=ReadADC(3);
delay_ms(0.5);
}

void initADC(void)
{
AD1PCFGbits.PCFG0 = 0; // Analog input in Analog mode
AD1PCFGbits.PCFG1 = 0;
AD1PCFGbits.PCFG2=0;
AD1PCFGbits.PCFG3=0;
AD1PCFGbits.PCFG4=0;
TRISBbits.TRISB6 = 1; // Pin set as input
TRISBbits.TRISB7 = 1;
TRISBbits.TRISB2=1;
TRISBbits.TRISB3=1;
TRISBbits.TRISB4=1;
AD1CHSbits.CH0NA = 0; // Channel 0 negative input is VR-
AD1CHSbits.CH0SA = 0; // Channel 0 positive input is AN0
AD1CON1bits.FORM = 0; // Integer 16-bit output
AD1CON1bits.SSRC = 0b111; // Internal counter ends sampling and starts conversion
AD1CSSL = 0; // No scanning required
AD1CON2bits.VCFG = 0; // Internal voltage references
AD1CON2bits.CSCNA = 0; // Do not scan inputs
AD1CON2bits.BUFM = 0; // Buffer configured as one 16-word buffer
AD1CON2bits.ALTS = 0; // Always use MUX A input multiplexer settings
AD1CON3bits.ADRC = 0; // Clock derived from PBclock
AD1CON3bits.ADCS = 0b00111111; // TAD
AD1CON3bits.SAMC = 0b11111; // 31 TAD auto-sample time
AD1CON1bits.ON = 1; // A/D converter module is operating
}

int ReadADC(int ch)
{
AD1CHSbits.CH0SA = ch; // Select input channel
AD1CON1bits.SAMP = 1; // Start sampling
while(!AD1CON1bits.DONE); // Wait for conversion to complete
return ADC1BUF0; // Read conversion result
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)