Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Help ADC DsPIC33F setup - Syntax error?

Status
Not open for further replies.

bubble_d

Junior Member level 3
Junior Member level 3
Joined
Sep 12, 2012
Messages
25
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,483
Hi everyone,


I am trying to set up ADC on my dspic33f microcontroller (w/ explorer 16 board) in order to read 4 "modules" or 3.7 volt batteries.
I initially want to start with reading a single battery voltage then implementing the other 3.

I used previous code I had for my PIC24F to implement my new code and obviously implementing the new registers to work with dspic33f. Unfortunately, My code does not work. It errors when I include the ADC init and ADC read functions from my .h file.


Here is my Current code:

Code:
#include <stdio.h>
#include <stdlib.h>
#include "p33FJ256GP710a.h"
#include "lcd.h"
#include "ADC.h"

//ADC CHANNELS
// Each channel was selected such that a ground pin is right next to the input
// pin while the pin needed to be a ADC pin
#define B1ADC 3; //AN3 Selected for 'Battery Module' 1
#define B2ADC 0; //AN0 Selected for 'Battery Module' 2
#define B3ADC 1; //AN1 Selected for 'Battery Module' 3
#define B4ADC 8; //AN8 Selected for 'Battery Module' 4

// I will start with A single battery module then once this
// works will I continue with all the modules.

//***************************************************************

// Buttons
#define SW3 PORTDbits.RD6
#define SW6 PORTDbits.RD7
#define SW4 PORTDbits.RD13
//LEDS
#define LED1 LATAbits.LATA0

_FOSC(OSCIOFNC_ON & FCKSM_CSDCMD & POSCMD_NONE);	//Oscillator Configuration
_FOSCSEL(FNOSC_FRC);								//Oscillator Selection
_FWDT(FWDTEN_OFF);									//Turn off WatchDog Timer
_FGS(GCP_OFF);										//Turn off code protect
_FPOR( FPWRT_PWR1 );								//Turn off power up timer

void Update_LCD( void );
const char mytext[] = "hello";
const char mytext1[] = "EDP 2012";
const char mytext2[] = "shitz";
const char mytext3[] = "Holla";
const char mytext4[] = "fuck you";

char result [24];
double test;

int main(void)
{
	
	int SW3Pressed =0;
	int SW6Pressed =0;
	int SW4Pressed =0;
	int voltage =0;
	TRISA = 0x007E;			//Make all PORTs all outputs

	/* Initialize LCD Display */
	Init_LCD();
	/* Welcome message */
	home_clr();
	puts_lcd( (char*) &mytext[0] );
	line_2();
	puts_lcd("BITCHES");
	
	initADC1(B1ADC);
	TRISB = 0x0008; //Sets RB3/AN3 as input since AN3 is being used as analog Input

	while(1)
	{				// delay 200ms
	

		if(SW3Pressed==1){
			home_clr();
			Delay(50);
			puts_lcd(&mytext4);
		}else if (SW6Pressed==1){
			home_clr();
			Delay(50);
			puts_lcd(&mytext2);
		}else if (SW4Pressed==1){
			home_clr();
			Delay(50);
			puts_lcd(&mytext3);
			voltage = readADC1(B1ADC); //Read ADC
}
		if(!SW3){
		
			SW3Pressed =1;
			SW6Pressed =0;
			SW4Pressed =0;
		}else if (!SW6) {
		
			SW3Pressed =0;
			SW6Pressed =1;
			SW4Pressed =0;
		}else if (!SW4) {
		
			SW3Pressed =0;
			SW6Pressed =0;
			SW4Pressed =1;
		}
		
}
}

and here is the ADC.h

Code:
#include "p33FJ256GP710a.h"


// Start with one pin being used to read a single battery
// Next read all four batteries from battery circuit.


// initialize the ADC for single conversion, select Analog input pins
void initADC1( int amask)
{
    AD1PCFGH = amask;      // select analog input pins
    AD1CON1 = 0x04E0;     // data output format-integer, auto convert after end of sampling.12bit Operation mode. No need to include a delay loop to provide time for completion of sampling
    AD1CSSL = 0;          // no scanning required
    AD1CON3 = 0x1FFF;     // Use internal clock, max sample time avail is 31Tad, conversion time Tad = 128*Tcy = 128*(2/Fosc) = 256/8MHz = 32us which is > (required)75ns ); 31 Tad = 31*32us = 992us is the sample time. About 100samples/sec
    //Possibly 1FFFF Check Both
	AD1CON2 = 0;          // use MUXA, AVss and AVdd are used as Vref+/-
    AD1CON1bits.ADON = 1; // turn on the ADC
} //initADC


int readADC1( int ch)
{
    AD1CHS0  = ch;               // select analog input channel
    ADC1BUF0 = 0x0000;   		// Reset Buffer
    AD1CON1bits.SAMP = 1;       // start sampling, automatic conversion will follow at end of sampling   
    while (!AD1CON1bits.DONE);  // wait to complete the conversion. DONE will be set as soon as conversion ends
    return ADC1BUF0;            // read the conversion result
} // readADC


I get syntax errors when I call the ADC functions.

Help Anyone?

[EDIT] I sent in actual values into the ADC functions and it worked... Why doesn't the variables I defined not work?.

[EDIT2]

The exact syntax error is:

Code:
main.c:60: error: syntax error before ';' token
main.c:66: error: syntax error before ';' token
 
Last edited:

Could you please post the exact syntax error messages that are generated during compilation.

Are you receiving errors for both calls of the initADC1() and readADC1()?


BigDog
 

Code:
main.c:60: error: syntax error before ';' token
main.c:66: error: syntax error before ';' token

Yes I am receiving the same error for both functions.
 

Remove the semicolons at the end of the define statements.

This is what you have now:
Code:
#define B1ADC 3; //AN3 Selected for 'Battery Module' 1
#define B2ADC 0; //AN0 Selected for 'Battery Module' 2
#define B3ADC 1; //AN1 Selected for 'Battery Module' 3
#define B4ADC 8; //AN8 Selected for 'Battery Module' 4

Change it to:
Code:
#define B1ADC 3 //AN3 Selected for 'Battery Module' 1
#define B2ADC 0 //AN0 Selected for 'Battery Module' 2
#define B3ADC 1 //AN1 Selected for 'Battery Module' 3
#define B4ADC 8 //AN8 Selected for 'Battery Module' 4

Hope this helps.
Tahmid.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top