Generating delay problem ADC

Thread Starter

reyes33d88

Joined Jan 10, 2015
7
Hi guys, I have a problem to convert a signal with the ADC of a DSPic33FJ12GP202, because I need a delay to retain the data and to convert it. My problem is with the Delay10KTCYx (660) instruction, because the compiler marks me wrong, my code is as follows, thanks


Code:
#include <p33FJ12GP202.h>
#include <delay.h>
#define _XTAL_FREQ 16000000
#define fcy 16000000
//#define fcy
   _FOSC ( FCKSM_CSDCMD & IOL1WAY_OFF & OSCIOFNC_OFF & POSCMD_XT );



int TCY;

int main (void);
void retardo(void);
void inicia_adc(void);


int main (void)
{
    TRISB=0X0000;
    inicia_adc();
    AD1CON1bits.ADON=1;        //SE HABILITA EL ADC
    /////////////OSCILADOR DE 16MHz/////////////////////
    ////////TCY=4/16MHz = 1/4us/////////////////////////
while(1)
{
void retardo(void){
//TCY=1/4000000;
Delay10KTCYx(660);//DELAY DE 10X60X1/4MZ=150us
}
}

PORTB=ADC1BUF0; 
}
void inicia_adc(void)
{

///////DEFINIMOS LA ENTRADA (RA0), LOS BITS DEL ADC Y SE CONFIGURA SOLO ADCON 1/////////////
    TRISA=0X0001;    //ENTRADA POR RA0
    AD1PCFGL=0;        //ENTRADAS ANALÓGICAS
    AD1CON1bits.AD12B=1;//ADC DE 12 BITS}
    AD1CON1bits.ADSIDL=0;
    AD1CON1bits.FORM=0;
    AD1CON1bits.ASAM=1;///EL MUESTRO INICIA DESPUÉS DE LA ÚLTIMA CONVERSIÓN
    AD1CON1bits.SSRC=0b111;///TERMINA EL MUESTREO
    AD1CON1bits.SAMP=1;///INICIA EL MUESTREO

////////////CONFIGURACIÓN AD1CON2//////////////
    AD1CON2bits.VCFG=0b00;///VDD Y VSS
    AD1CON2bits.CSCNA=1;///ESCANEA LAS ENTRADAS
    AD1CON2bits.SMPI=0b111;//DETERMINA LA CANTIDAD DE MUESTRAS (EN ESTE CASO 16)7
    AD1CON2bits.BUFM=0;//SIEMPRE SE LLENA EL BUFFER DESDE EL PRINCIPIO
    AD1CON2bits.ALTS=0;//SIEMPRE SE UTILIZA LA ENTRADA DE CANAL, PARA EL MUESTREO A
////////////CONFIGURACIÓN AD1CON3//////////////

    AD1CON3bits.ADRC=1;//SE SELECCIONA EL RELOJ INTERNO
    AD1CON3bits.SAMC=0b11111;//BITS DE AUTO MUESTREO
    AD1CON3bits.ADCS=0b11111;//TIEMPO DE CONVERSIÓN DE BITS ADC
////////////CONFIGURACIÓN AD1CHS0://////////////
    AD1CHS0bits.CH0NB=0;//VREF
    AD1CHS0bits.CH0SB=0b00000;//ENTRADA POSITIVA POR AN0
    AD1CHS0bits.CH0NA=0;//ENTRADA NEGATIVA POR -REF
    AD1CHS0bits.CH0SA=0b00000;//ENTRADA POSITIVA POR AN0
///////////CONFIGURACIÓN DEL REGISTRO BAJO///////////
//    AD1CSSLbits.CSS=1;//SE ESCANEAN LAS ENTRADAS


    IFS0bits.AD1IF=1;
    IEC0bits.AD1IE=1;/////SI SE REALIZA LA CONVERSIÓN HAY INTERRUPCIÓN
    IPC0bits.T1IP=1;//PRIORIDAD DE LA INTERRUPIÓN

    AD1CON1bits.DONE=1;
}
Moderators note: Please use code tags for pieces of code
 
Last edited by a moderator:

JohnInTX

Joined Jun 26, 2012
4,787
Welcome to AAC!
Code:
while(1)
{
void retardo(void){
//TCY=1/4000000;
Delay10KTCYx(660);//DELAY DE 10X60X1/4MZ=150us
}
}
I don't think you can declare the function 'redardo' inside a while loop.
Also, Microchip says this about delays on the PIC32:
3.5.7 How Can I Implement a Delay In My Code?
If an accurate delay is required, or if there are other tasks that can be performed during
the delay, then using a timer to generate an interrupt is the best way to proceed.
Microchip does not recommend using a software delay on PIC32 devices as there are
many variables that can affect timing such as the configuration of the L1 cache,
prefetch cache, & Flash wait states. On PIC32 devices, you may choose to poll the core
timer, which increments every two instruction cycles.
I agree.

Anyway, you should not have to use a 'delay' at all. Just start the conversion and poll the 'DONE' flag.

Is this X32? Delay10KTCYx is not in the library (that I have, anyway).

Also, you can put your code inside 'CODE' tags (click the icon that looks like a little american flag) and it will be easier to read.
 
Last edited:

ErnieM

Joined Apr 24, 2011
8,377
My problem is with the Delay10KTCYx (660) instruction, because the compiler marks me wrong
The symptom "the compiler marks me wrong" is meaningless to anyone not seeing the build output. What is the compiler output? If it points to specific line numbers what are those lines (with numbers!) ?

Additionally, in C it is oft the case that one incorrect line will cause a compile error on the very next line.

You cannot define the functions redardo() and inicia_adc() as you do within the main() (or any other) function. Move the code out of main and replace with function calls.

(Aside to JohnInTX: I have seen a Delay10KTCYx() function in some library code Microchip holds for alphanumeric LCD work.)
 

Thread Starter

reyes33d88

Joined Jan 10, 2015
7
Thank you, I have another code, where I made a delay using the timer, buT I have another errors, the compiler says "my_adc1.c: In function 'main':
my_adc1.c:26: error: too many arguments to function 'atraso_ms'
my_adc1.c:88: error: syntax error at end of input", what do you think about my code?, thank you for all



Here is my code

Code:
#include <p33FJ12GP202.h>

#define _XTAL_FREQ 16000000
#define fcy 16000000

   _FOSC ( FCKSM_CSDCMD & IOL1WAY_OFF & OSCIOFNC_OFF & POSCMD_XT );



unsigned int t=0;

int main (void);
void atraso_ms(void);
void inicia_adc(void);


int main (void)
{
    TRISB=0X0000;
    inicia_adc();
    AD1CON1bits.ADON=1;        //SE HABILITA EL ADC
while(1)
{
    AD1CON1bits.SAMP=1;///INICIA EL MUESTREO
    atraso_ms(3);///TIEMPO EN EL QUE SE REALIZA EL MUESTREO
    AD1CON1bits.SAMP=0;///RETIENE EL DATO
while(!AD1CON1bits.DONE); //MIENTRAS SE REALIZE LA CONVERSIÓN, LOS DATOS SE GUARDAN EN EL PUERTO B
PORTB=ADC1BUF0; // Datos se guardan en ADC1BUF0 y se envía al puerto B

}


/////////////CONFIGURACIÓN DEL RETARDO//////////////////////
void atraso_ms(void){
TMR1=0;//INICIAMOS RESETEANDO EL TIMER 1
IPC0bits.T1IP=0b101;//LA INTERRUPCIÓN TIENE UNA PRIORIDAD 5
    /////////////OSCILADOR DE 20MHz///////////////////////////////////////////
    ////////TCY=4/16MHz = 1/4us///////////////////////////////////////////////
    //////PARA UN RETARDO DE 1s SE CARGA A PR1 CON  39062.5///////////////////
    //////////////////////////////////////////////////////////////////////////
    //////FCY=20MHz/2=10MHz, PREESCALAR DE 256////////////////////////////////
    //////POR REGLA DE 3, PARA UN RETARDO DE 1ms, PR1=39.0625/////////////////
    PR1=39.0625;
    IFS0bits.T1IF=0;//LIMNPIA LA BANDERA DEL TIMER
    IEC0bits.T1IE=0;//CUANDO OCURRE UNA INTERRUPCIÓN DESHABILITA EL TIMER
    T1CON=0X8030;//CONFIGURA T1CON
while(IFS0bits.T1IF==0);
}
void inicia_adc(void)
{

///////DEFINIMOS LA ENTRADA (RA0), LOS BITS DEL ADC Y SE CONFIGURA SOLO ADCON 1/////////////
    TRISA=0X0001;    //ENTRADA POR RA0
    AD1PCFGL=0;        //ENTRADAS ANALÓGICAS
    AD1CON1bits.AD12B=1;//ADC DE 12 BITS}
    AD1CON1bits.ADSIDL=0;
    AD1CON1bits.FORM=0;
    AD1CON1bits.ASAM=1;///EL MUESTRO INICIA DESPUÉS DE LA ÚLTIMA CONVERSIÓN
    AD1CON1bits.SSRC=0b111;///TERMINA EL MUESTREO


////////////CONFIGURACIÓN AD1CON2//////////////
    AD1CON2bits.VCFG=0b00;///VDD Y VSS
    AD1CON2bits.CSCNA=1;///ESCANEA LAS ENTRADAS
    AD1CON2bits.SMPI=0b111;//DETERMINA LA CANTIDAD DE MUESTRAS (EN ESTE CASO 16)7
    AD1CON2bits.BUFM=0;//SIEMPRE SE LLENA EL BUFFER DESDE EL PRINCIPIO
    AD1CON2bits.ALTS=0;//SIEMPRE SE UTILIZA LA ENTRADA DE CANAL, PARA EL MUESTREO A
////////////CONFIGURACIÓN AD1CON3//////////////

    AD1CON3bits.ADRC=1;//SE SELECCIONA EL RELOJ INTERNO
    AD1CON3bits.SAMC=0b11111;//BITS DE AUTO MUESTREO
    AD1CON3bits.ADCS=0b11111;//TIEMPO DE CONVERSIÓN DE BITS ADC
////////////CONFIGURACIÓN AD1CHS0://////////////
    AD1CHS0bits.CH0NB=0;//VREF
    AD1CHS0bits.CH0SB=0b00000;//ENTRADA POSITIVA POR AN0
    AD1CHS0bits.CH0NA=0;//ENTRADA NEGATIVA POR -REF
    AD1CHS0bits.CH0SA=0b00000;//ENTRADA POSITIVA POR AN0
///////////CONFIGURACIÓN DEL REGISTRO BAJO///////////
//    AD1CSSLbits.CSS=1;//SE ESCANEAN LAS ENTRADAS


    IFS0bits.AD1IF=1;
    IEC0bits.AD1IE=1;/////SI SE REALIZA LA CONVERSIÓN HAY INTERRUPCIÓN
    IPC0bits.T1IP=1;//PRIORIDAD DE LA INTERRUPIÓN

    AD1CON1bits.DONE=1;
}
Moderators note: Please use code tags for pieces of code
 
Last edited by a moderator:

JohnInTX

Joined Jun 26, 2012
4,787
my_adc1.c:26: error: too many arguments to function 'atraso_ms'
This is because on line 14 you have prototyped void atraso_ms(void); and on line 35 declared the function void atraso_ms(void) but on line 26, you try to pass a value to the function atraso_ms(3);///TIEMPO EN EL QUE SE REALIZA EL MUESTREO. You can't do that. void atraso_ms(void); means no arguments are expected or accepted.

my_adc1.c:88: error: syntax error at end of input",
I think you are missing a closing bracket '}' at the end of 'main'. It is easier to keep track of things if you indent your code blocks and line up the brackets.

Also, please use 'CODE' tags to post your code. It makes it easier to look at:
Code:
#include <p33FJ12GP202.h>

#define _XTAL_FREQ 16000000
#define fcy 16000000

_FOSC ( FCKSM_CSDCMD & IOL1WAY_OFF & OSCIOFNC_OFF & POSCMD_XT );

unsigned int t=0;

int main (void);
void atraso_ms(void);
void inicia_adc(void);


int main (void)
{
    TRISB=0X0000;
    inicia_adc();
    AD1CON1bits.ADON=1; //SE HABILITA EL ADC
    while(1){
        AD1CON1bits.SAMP=1;///INICIA EL MUESTREO
        atraso_ms(3);///TIEMPO EN EL QUE SE REALIZA EL MUESTREO
        AD1CON1bits.SAMP=0;///RETIENE EL DATO
        while(!AD1CON1bits.DONE); //MIENTRAS SE REALIZE LA CONVERSIÓN, LOS DATOS SE GUARDAN EN EL PUERTO B
        PORTB=ADC1BUF0; // Datos se guardan en ADC1BUF0 y se envía al puerto B
    }
}// *** ADDED THIS  ****

/////////////CONFIGURACIÓN DEL RETARDO//////////////////////
void atraso_ms(void){
    TMR1=0;//INICIAMOS RESETEANDO EL TIMER 1
    IPC0bits.T1IP=0b101;//LA INTERRUPCIÓN TIENE UNA PRIORIDAD 5
/////////////OSCILADOR DE 20MHz///////////////////////////////////////////
////////TCY=4/16MHz = 1/4us///////////////////////////////////////////////
//////PARA UN RETARDO DE 1s SE CARGA A PR1 CON 39062.5///////////////////
//////////////////////////////////////////////////////////////////////////
//////FCY=20MHz/2=10MHz, PREESCALAR DE 256////////////////////////////////
//////POR REGLA DE 3, PARA UN RETARDO DE 1ms, PR1=39.0625/////////////////
    PR1=39.0625;
    IFS0bits.T1IF=0;//LIMNPIA LA BANDERA DEL TIMER
    IEC0bits.T1IE=0;//CUANDO OCURRE UNA INTERRUPCIÓN DESHABILITA EL TIMER
    T1CON=0X8030;//CONFIGURA T1CON
    while(IFS0bits.T1IF==0);
}

void inicia_adc(void)
{
///////DEFINIMOS LA ENTRADA (RA0), LOS BITS DEL ADC Y SE CONFIGURA SOLO ADCON 1/////////////
    TRISA=0X0001; //ENTRADA POR RA0
    AD1PCFGL=0; //ENTRADAS ANALÓGICAS
    AD1CON1bits.AD12B=1;//ADC DE 12 BITS}
    AD1CON1bits.ADSIDL=0;
    AD1CON1bits.FORM=0;
    AD1CON1bits.ASAM=1;///EL MUESTRO INICIA DESPUÉS DE LA ÚLTIMA CONVERSIÓN
    AD1CON1bits.SSRC=0b111;///TERMINA EL MUESTREO


    ////////////CONFIGURACIÓN AD1CON2//////////////
    AD1CON2bits.VCFG=0b00;///VDD Y VSS
    AD1CON2bits.CSCNA=1;///ESCANEA LAS ENTRADAS
    AD1CON2bits.SMPI=0b111;//DETERMINA LA CANTIDAD DE MUESTRAS (EN ESTE CASO 16)7
    AD1CON2bits.BUFM=0;//SIEMPRE SE LLENA EL BUFFER DESDE EL PRINCIPIO
    AD1CON2bits.ALTS=0;//SIEMPRE SE UTILIZA LA ENTRADA DE CANAL, PARA EL MUESTREO A
    ////////////CONFIGURACIÓN AD1CON3//////////////

    AD1CON3bits.ADRC=1;//SE SELECCIONA EL RELOJ INTERNO
    AD1CON3bits.SAMC=0b11111;//BITS DE AUTO MUESTREO
    AD1CON3bits.ADCS=0b11111;//TIEMPO DE CONVERSIÓN DE BITS ADC
    ////////////CONFIGURACIÓN AD1CHS0://////////////
    AD1CHS0bits.CH0NB=0;//VREF
    AD1CHS0bits.CH0SB=0b00000;//ENTRADA POSITIVA POR AN0
    AD1CHS0bits.CH0NA=0;//ENTRADA NEGATIVA POR -REF
    AD1CHS0bits.CH0SA=0b00000;//ENTRADA POSITIVA POR AN0
    ///////////CONFIGURACIÓN DEL REGISTRO BAJO///////////
    // AD1CSSLbits.CSS=1;//SE ESCANEAN LAS ENTRADAS


    IFS0bits.AD1IF=1;
    IEC0bits.AD1IE=1;/////SI SE REALIZA LA CONVERSIÓN HAY INTERRUPCIÓN
    IPC0bits.T1IP=1;//PRIORIDAD DE LA INTERRUPIÓN

    AD1CON1bits.DONE=1;
}
 
Last edited:

Thread Starter

reyes33d88

Joined Jan 10, 2015
7
Thank you for your answers and your advices, it is so importan for me. I´ve modified the program, but Know I need to process this signal, I need to get the FFT (Fast Fourier Transform), but I don´t know how to get the signal and process it, because signal converted is outed in the port B. Do you have some idea how to do it?, thank you for all and for you patient with me, I´m new programming microcontrol. I´ll try to use code tags, thank you!
Code:
#include <p33FJ12GP202.h>
_FOSC ( FCKSM_CSDCMD & IOL1WAY_OFF & OSCIOFNC_OFF & POSCMD_XT );
//#define _XTAL_FREQ 16000000
#define fcy 16000000


int main (void);
void atraso_ms();
void inicia_adc(void);

/////////////////INICIO DEL PROGRAMA////////////
int main (void)
{
    TRISB=0X0000;
    inicia_adc();
    AD1CON1bits.ADON=1;        //SE HABILITA EL ADC
while(1)
{
    AD1CON1bits.SAMP=1;///INICIA EL MUESTREO
    atraso_ms(3);///TIEMPO EN EL QUE SE REALIZA EL MUESTREO
    AD1CON1bits.SAMP=0;///RETIENE EL DATO
while(!AD1CON1bits.DONE); //MIENTRAS SE REALIZE LA CONVERSIÓN, LOS DATOS SE GUARDAN EN EL PUERTO B
PORTB=ADC1BUF0; // Datos se guardan en ADC1BUF0 y se envía al puerto B

}
}
void inicia_adc(void)
{
///////DEFINIMOS LA ENTRADA (RA0), LOS BITS DEL ADC Y SE CONFIGURA SOLO ADCON 1/////////////
    TRISA=0X0001;    //ENTRADA POR RA0
    AD1PCFGL=0X0000;        //ENTRADAS ANALÓGICAS
    AD1CON1bits.AD12B=1;//ADC DE 12 BITS}
    //AD1CON1bits.ADSIDL=0;
    //AD1CON1bits.FORM=0;
    AD1CON1bits.ASAM=0;///EL MUESTRO INICIA DESPUÉS DE LA ÚLTIMA CONVERSIÓN
    AD1CON1bits.SSRC=0b111;///TERMINA EL MUESTREO


////////////CONFIGURACIÓN AD1CON2//////////////
    AD1CON2bits.VCFG=0b00;///VDD Y VSS
    AD1CON2bits.CSCNA=0;//MUESTREO SECUENCIAL DESHABILITADO
    AD1CON2bits.SMPI=0b111;//DETERMINA LA CANTIDAD DE MUESTRAS (EN ESTE CASO 16)7
    AD1CON2bits.BUFM=0;//SIEMPRE SE LLENA EL BUFFER DESDE EL PRINCIPIO
    AD1CON2bits.ALTS=0;//SIEMPRE SE UTILIZA LA ENTRADA DE CANAL, PARA EL MUESTREO A
////////////CONFIGURACIÓN AD1CON3//////////////

    AD1CON3bits.ADRC=1;//SE SELECCIONA EL RELOJ INTERNO
    AD1CON3bits.ADRC=21;//TIEMPO DE CONVERSIÓN
    AD1CON3bits.SAMC=0b11111;//BITS DE AUTO MUESTREO
    AD1CON3bits.ADCS=0b11111;//TIEMPO DE CONVERSIÓN DE BITS ADC
////////////CONFIGURACIÓN AD1CHS0://////////////
    AD1CHS0bits.CH0NA=0;//ENTRADA NEGATIVA POR VSS
    AD1CHS0bits.CH0SA=0;//ENTRADA POSITIVA POR AN
}
/////////////CONFIGURACIÓN DEL RETARDO//////////////////////
void atraso_ms(){
T1CON= 0;
TMR1=0;//INICIAMOS RESETEANDO EL TIMER 1
IPC0bits.T1IP=0b101;//LA INTERRUPCIÓN TIENE UNA PRIORIDAD 5
    /////////////OSCILADOR DE 20MHz///////////////////////////////////////////
    ////////TCY=4/16MHz = 1/4us///////////////////////////////////////////////
    //////PARA UN RETARDO DE 1s SE CARGA A PR1 CON  39062.5///////////////////
    //////////////////////////////////////////////////////////////////////////
    //////FCY=20MHz/2=10MHz, PREESCALAR DE 256////////////////////////////////
    //////POR REGLA DE 3, PARA UN RETARDO DE 1ms, PR1=39.0625/////////////////
    PR1=39.0625;
    IFS0bits.T1IF=0;//LIMNPIA LA BANDERA DEL TIMER
    IEC0bits.T1IE=0;//CUANDO OCURRE UNA INTERRUPCIÓN DESHABILITA EL TIMER
    T1CON=0X8030;//CONFIGURA T1CON
while(IFS0bits.T1IF==0);
}
 
Last edited:

Thread Starter

reyes33d88

Joined Jan 10, 2015
7
Thank you for your advices, I´m sending data to PORTB because I wanted to see if my ADC was workin well. I type my comments in Caps because I like to coment my programs in Caps, I like more caps hehehehe
 
Top