I'm trying to get the PIC24 to work with an ADC converter, to use a potentiometer as a volume knob down the line, but for that, I need to read out the ADC, where the code doesn't allow me to build. See bottom of the question. Wiring is done correctly.
MPlab shows no errors in main.c, but when I'm building the project, some errors in user.c come up.
I have the PIC24FJ64GB002 attached on a board with some buttons and an mdog display.
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;
}
Log when building:
"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)