2015-01-27 2 views
0

나는 PIC24를 ADC 변환기와 함께 작동 시키려고 노력하고 있는데, 전위차계를 선 아래의 볼륨 손잡이로 사용하려고합니다. 그러나이를 위해 ADC를 읽어야합니다. 나를 지을 수 없어요. 질문 하단을 참조하십시오. 배선이 올바르게 수행되었습니다.PIC24는 ADC 오류를 읽습니다.

MPlab은 main.c에 오류를 표시하지 않지만 프로젝트를 빌드 할 때 user.c의 일부 오류가 발생합니다.

일부 버튼과 mdog 디스플레이가있는 보드에 PIC24FJ64GB002가 부착되어 있습니다.

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"); 

    } 
} 

user.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) 
+1

그리고 * 오류 *가 표시됩니까? 그리고 * 그들이 올 수있는 곳 (코드에서 그 줄을 표시하십시오)? –

+0

@JoachimPileborg 제 코드가 buildlog에 추가되었습니다. – Riekelt

+1

표시되는 오류와 코드가 일치하지 않습니다! 오류는 표시하지 않는 함수'init_adc'에 있습니다. –

답변

0

내가 @ElderBug 잘 생각합니다. 당신은 모든 이름을 혼합합니다. 오류는 이름이 존재하지 않는다고 알려줍니다. 그런 다음 AD1CON0bits과 같은 다른 이름을 확인하십시오.
마이크로 칩은 모든 주변 장치에 대한 마뉴 엘 기준을 가지고 있습니다. 일반 데이터 시트보다 더 많은 팁을 제공합니다.
MCU의 경우 여기에서 확인할 수 있습니다. http://ww1.microchip.com/downloads/en/DeviceDoc/39705b.pdf