gpt4 book ai didi

c - PIC24读取ADC错误

转载 作者:行者123 更新时间:2023-11-30 15:24:53 25 4
gpt4 key购买 nike

我正在尝试让 PIC24 与 ADC 转换器配合使用,使用电位计作为音量旋钮,但为此,我需要读出 ADC,而代码不允许我这样做 build 。请参阅问题底部。接线正确。

MPlab 在 main.c 中没有显示任何错误,但是当我构建项目时,user.c 中出现了一些错误。

我将 PIC24FJ64GB002 连接到板上,并带有一些按钮和 mdog 显示屏。

main.c

#include <p24FJ64GB002.h>
//#include <pic.h>
#include "DogM.h"
#include <stdlib.h>
#include <time.h>
#include <p24fxxxx.h>
//#include <user.c>

_CONFIG1( JTAGEN_OFF & //JTAG port is disabled
GCP_OFF & //GSP Memory Code Protection OFF
GWRP_OFF & //GCC Flash Write Protection OFF
//COE_OFF & //
FWDTEN_OFF & //Watchdog Timer OFF
ICS_PGx1) //debug over PGD1 and PGC1

_CONFIG2( FNOSC_FRCPLL & //Internal FRC with PLL
OSCIOFNC_ON & //RA3 is clk out (fosc/2)
POSCMOD_NONE & //Primary oscillator disabled
I2C1SEL_PRI) //Use default SCL1/SDA1 pins

#define VREG33_DIR TRISAbits.TRISA0
#define VREG33_EN LATAbits.LATA0
#define MODE_LED_DIR TRISAbits.TRISA1
#define MODE_LED LATAbits.LATA1


#pragma code

int main(void)
{
// Set up the hardware of the display
InitApp();
mdog_Init(0x81, 0x19);
init_adc();

clearDisplay();

// Initscreen clears a internal bitmap used
// in drawScreen to send out to the display
initScreen();

// Beware writeString will write directly to the display
// the internal bitmap is not modified.

CLKDIVbits.RCDIV0=0; //clock divider to 0
AD1PCFG = 0xFFFF; // Default all pins to digital
OSCCONbits.SOSCEN=0; //Disables the secondary oscilator

MODE_LED_DIR = 0; //sets the Mode LED pin RA1 as output
MODE_LED = 0; //turns LED off
VREG33_DIR =0; //sets teh VREG pin RA0 as output
VREG33_EN = 1; //turns on the voltage regulator

unsigned long int i,voltage;
//////////////////////////////////////////////////////////////
///////////////ADC config//////////////////////////////////
AD1PCFGbits.PCFG12=0; //configure RB12 as analog
AD1CON1bits.SSRC = 0b111; // SSRC<3:0> = 111 implies internal
// counter ends sampling and starts
// converting.
AD1CON3 = 0x1F02; // Sample time = 31Tad,
// Tad = 2 Tcy
AD1CHS =12; //ADC channel select 12
AD1CON1bits.ADON =1; // turn ADC on
///FOREVER LOOP///////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
while(1)
{
//this just gives us a little delay between measurements
i =0xFFFFF; //sets i to 1048575
while (i--); //delay function

//start a measurement with the ADC
AD1CON1bits.DONE=0; //resets DONE bit
AD1CON1bits.SAMP=1; //start sample

while(AD1CON1bits.DONE==0); //wait for conversion to finish

//get the measurement and use it to control the LED
voltage = ADC1BUF0; //get the voltage measurement
//if (voltage > 0x1D1) MODE_LED = 1; //enable LED if measurement is > 3volts
//else MODE_LED = 0; //disable LED if less than 3volts
writeString(boldFont, 0x0, 0x3, "Hallo");

}
}

用户.c

  /******************************************************************************/
/* Files to Include */
/******************************************************************************/

/* Device header file */
#if defined(__PIC24E__)
#include <p24Exxxx.h>
#elif defined (__PIC24F__)
//#include <p24Fxxxx.h>
#include <p24FJ64GB002.h>
#elif defined(__PIC24H__)
#include <p24Hxxxx.h>
#endif

//#include "user.h" /* variables/params used by user.c */

/******************************************************************************/
/* User Functions */
/******************************************************************************/

/* <Initialize variables in user.h and insert code for user algorithms.> */

/* TODO Initialize User Ports/Peripherals/Project here */
/**
* Initialize the Analog to Digital Converter.
*/
/**/void init_adc(void)
{
TRISAbits.TRISA1 = 0b1; // set pin as input
ANCON0bits.ANSEL1 = 0b1; // set pin as analog
ADCON1bits.VCFG = 0b00; // set v+ reference to Vdd
ADCON1bits.VNCFG = 0b0; // set v- reference to GND
ADCON1bits.CHSN = 0b000;// set negative input to GND
ADCON2bits.ADFM = 0b1; // right justify the output
ADCON2bits.ACQT = 0b110;// 16 TAD
ADCON2bits.ADCS = 0b101;// use Fosc/16 for clock source
ADCON0bits.ADON = 0b1; // turn on the ADC
}
/**
* Preform an analog to digital conversion.
* @param channel The ADC input channel to use.
* @return The value of the conversion.
*/
/* uint16_t adc_convert(uint8_t channel)
{
ADCON0bits.CHS = channel; // select the given channel
ADCON0bits.GO = 0b1; // start the conversion
while(ADCON0bits.DONE); // wait for the conversion to finish
return (ADRESH<<8)|ADRESL; // return the result
} */
void InitApp(void) {
// Setup analog functionality and port direction
AD1PCFGL = 0xFFFF; // Make analog pins digital

// Initialize peripherals
// set up I/O ports
TRISB = 0x0000; // all pins as output
LATB = 0x0; // all set to 0

// CN interrupts
CNEN1 = 0; /* Disable all CN */
CNEN2 = 0;
init_adc();
CNEN1bits.CN2IE = 1;
CNEN1bits.CN3IE = 1;
CNEN2bits.CN29IE = 1;
CNEN2bits.CN30IE = 1;

IPC4bits.CNIP0 = 1;
IPC4bits.CNIP1 = 0;
IPC4bits.CNIP2 = 0;

IFS1bits.CNIF = 0;
IEC1bits.CNIE = 1;
}

构建时记录:

"D:\Program Files (x86)\Microchip\xc16\v1.23\bin\xc16-gcc.exe"   user.c  -o build/default/production/user.o  -c -mcpu=24FJ64GB002  -MMD -MF "build/default/production/user.o.d"        -g -omf=elf -O0 -I"Dogm128x64" -msmart-io=1 -msfr-warn=off
user.c: In function 'init_adc':
user.c:30:5: error: 'ANCON0bits' undeclared (first use in this function)
user.c:30:5: note: each undeclared identifier is reported only once for each function it appears in
user.c:31:5: error: 'ADCON1bits' undeclared (first use in this function)
user.c:34:5: error: 'ADCON2bits' undeclared (first use in this function)
user.c:37:5: error: 'ADCON0bits' undeclared (first use in this function)
make[2]: *** [build/default/production/user.o] Error 255
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
make[2]: Leaving directory `D:/Dropbox/HvA/Embedded/Potmeter'
make[1]: Leaving directory `D:/Dropbox/HvA/Embedded/Potmeter'

BUILD FAILED (exit value 2, total time: 254ms)

最佳答案

我认为@ElderBug是对的。你把所有的名字都混在一起。该错误告诉您该名称不存在。然后检查是否有其他名称,例如 AD1CON0bits
Microchip 为每个外设提供了手册引用。它比普通数据表提供更多提示。
对于您的 MCU,您可以在这里找到它:http://ww1.microchip.com/downloads/en/DeviceDoc/39705b.pdf

关于c - PIC24读取ADC错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28171807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com