News:

SMF for DIYStompboxes.com!

Main Menu

ADC initialization

Started by MstrKurt, December 04, 2013, 08:40:49 PM

Previous topic - Next topic

MstrKurt

Hey guys,

I've attempted to initialize the ADC on a dsPIC33FJ32GP202 by controlling the rate at which a LED flashes (This is used just as a check to ensure the ADC is working).

My attempt is here:

#include "p33fxxxx.h"
#include "dsp.h"
#include "stdio.h"



// Internal FRC Oscillator
_FOSCSEL(FNOSC_FRC);            // FRC Oscillator
_FOSC(FCKSM_CSECMD & OSCIOFNC_ON  & POSCMD_NONE);
                          // Clock Switching is enabled and Fail Safe Clock Monitor is disabled
                          // OSC2 Pin Function: OSC2 is Clock Output
                          // Primary Oscillator Mode: Disabled

_FWDT(FWDTEN_OFF);                            // Watchdog Timer Enabled/disabled by user software
                                                    // (LPRC can be disabled by clearing SWDTEN bit in RCON registe




int main(void)             //Main Program
{
    int result;
    int adcconv;

    TRISA = 0xFD;           // Initialize IO's on PORTA
    ADCinit();
///////////////////////////////////////////////////
    while(1)
    {
     ///////ENTER CODE HERE/////
        adcconv = AD1CON1bits.DONE;
        while(adcconv == 0)
        {
            //Wait here while conversion completes.
        }
        result = ADC1BUF0;

        PORTA = 0x02;
        delay();
        delay();
        if(result > 1024)
        {
          delay();
          delay();
        }
        PORTA = 0x00;
        delay();
        delay();
        if (result > 1024)
        {
          delay();
          delay();
        }


    }

}


Can anyone see where I've gone wrong?.

potul

can you show what's in ADCinit() ?

Seljer

Does the LED blink? or not turn at all? or stay on all the time?

Does the chip have a UART module? for crude debugging I'd reccomend you set up just the transmitter portion of it and hook it up to your computer so you can print messages from the microcontroller(it's usually quicker and easier to set up than hooking up a full LCD). Its often quicker to get the bottom of something if you can actually print out numbers rather than judge a blinking LED.

MstrKurt

#3
Sorry! I thought the ADC part was there.

The code is:

void ADCinit(void)
{
   AD1CON1bits.ADON = 1;
   AD1CON1bits.AD12B = 1;
   AD1CON1bits.FORM = 00;
   AD1CON1bits.SSRC = 111;
   AD1CON2bits.VCFG = 000;
   AD1CON3bits.ADRC = 0;   // 1 = ADC Internal RC Clock, 0 = Clock Derived from System clock
   AD1CON3bits.SAMC = 00000;
   AD1PCFGLbits.PCFG4 = 0;

   

}


I can't hook it up to a PC unfortunately. It needs to be a standalone project as it's for a major project module for uni.

The LED doesn't blink due to this part of the code:

        while(adcconv == 0)
        {
            //Wait here while conversion completes.
        }


if I remove that it blinks, but no control over the rate at which the toggle happens is given. I'm trying to control it using a pot.

Seljer

#4
The issue you're having come from

adcconv = AD1CON1bits.DONE;
       while(adcconv == 0) {}


You're saving the status bit into your own variable, and the exit condition of your while loops is tied to the what you saved into memory at that moment, not whats actually going on with the ADC. To make it wait until the result is ready you'd probably use something like while (!AD1CON1bits.DONE) {}

In your init function I'd move AD1CON1bits.ADON = 1; to the end off all commands there. The datasheet mentions it not being a good idea to change clocks and such while the ADC is running.

And also refer to the datasheet to exactly what kind of scheme you want for sampling and conversion. You set SSRC = 111 for autmatic conversion but it seems you need to run the sampling via SAMP manually unless you set ASAM.


kingswayguitar

sorry to interrupt but how the h#ll do you guys know all this stuff?  i look at those datasheets and feel like I'm trying to learn Martian!  anyway, keep up the good work.

potul

Quote from: Seljer on December 05, 2013, 10:33:53 AM
The issue you're having come from

adcconv = AD1CON1bits.DONE;
       while(adcconv == 0) {}


You're saving the status bit into your own variable, and the exit condition of your while loops is tied to the what you saved into memory at that moment, not whats actually going on with the ADC. To make it wait until the result is ready you'd probably use something like while (!AD1CON1bits.DONE) {}

In your init function I'd move AD1CON1bits.ADON = 1; to the end off all commands there. The datasheet mentions it not being a good idea to change clocks and such while the ADC is running.

And also refer to the datasheet to exactly what kind of scheme you want for sampling and conversion. You set SSRC = 111 for autmatic conversion but it seems you need to run the sampling via SAMP manually unless you set ASAM.



good catch!

MstrKurt

Thanks for the help guys. I have it working now :)

Can anyone confirm if a dsPIC33FJ32GP202 is powerful enough to build a multi effects guitar pedal?

greaser_au

Does nobody cut assembly language any more?   <sob> <sniffle>   ;D

david

slacker

Quote from: MstrKurt on December 14, 2013, 08:25:06 AM
Can anyone confirm if a dsPIC33FJ32GP202 is powerful enough to build a multi effects guitar pedal?

It looks powerful enough to sample audio, do stuff to it and then output it, so you can use it to make effects. Whether it's powerful enough to do what you want probably depends what sort of effects you have in mind, for example you may need to add external memory if you want to do delay based effects. 

MstrKurt

#10
Some delay based effects are needed, yes.

Delay itself, Chorus, Phaser and Wah

Can I also ask, will I need an external DAC on the dspic33fj32gp202 for a distortion effect?.

potul

Chorus, phaser and wah shouldn't be an issue. Delay it's not going to fly without external memory... maybe some "slapback" type of echo will work, but nothing longer.

MstrKurt

Ok thanks for the input Potul.

Would you know about creating a Distortion? will I need an external DAC? or is it just better to buy a dsPIC33FJ128GP802?

slacker

You don't need an external DAC you can use the PWM outputs as a DAC like they do here http://www.diystompboxes.com/smfforum/index.php?topic=103795.0. I would stick with the chip you have and make some simple effects, then if you decide you need better audio quality or whatever move on from there.

potul

Quote from: MstrKurt on December 17, 2013, 10:43:33 AM
Ok thanks for the input Potul.

Would you know about creating a Distortion? will I need an external DAC? or is it just better to buy a dsPIC33FJ128GP802?

The issue with distortion is really to find an algorithm to generate some musical distortion. Simple digital distortion (like clipping), is usually very harsh. But you can do it without any external DAC.

MstrKurt

Quote from: potul on December 17, 2013, 04:08:15 PM
Quote from: MstrKurt on December 17, 2013, 10:43:33 AM
Ok thanks for the input Potul.

Would you know about creating a Distortion? will I need an external DAC? or is it just better to buy a dsPIC33FJ128GP802?

The issue with distortion is really to find an algorithm to generate some musical distortion. Simple digital distortion (like clipping), is usually very harsh. But you can do it without any external DAC.


Simple digital distortion will be a fine starting point for the project I'm doing. Is there somewhere I can learn to create that clipping on the ADC?.

MstrKurt

#16
I've hooked up the digital based output to the amp and I can hear what I'm playing but it's very noisy, sounds like I've made distortion just from the noise haha.

I've found that it could be because my analog and digital share the same ground could be the problem? A diff amp surely wouldn't work if this is the case?.

Also, the higher strings have got to be plucked fairly hard to be heard at the amp (non inverting op amp to be the savior here?). And could somebody please explain why I can hear the signal being played when it's coming out of a digital output?, isn't that a 2 case scenario of either high or low? in the dsPIC's case 3.3V or 0V?, I thought a DAC would have been needed to get the different levels of the input waves?.

greaser_au

these are questions for Messrs. Nyquist and Fourier...  :)

david

slacker

We need more info on what you're actually doing to answer your question. Are you using PWM or just some how trying to send the sampled signal to a normal digital output?

MstrKurt

Quote from: slacker on December 20, 2013, 12:29:57 PM
We need more info on what you're actually doing to answer your question. Are you using PWM or just some how trying to send the sampled signal to a normal digital output?

I'm just sending an analog signal in (from my guitar) to an ADC. Then setting the output to equal what's being read at the input.