Author Topic: ADC initialization  (Read 7120 times)

0 Members and 1 Guest are viewing this topic.

MstrKurt

ADC initialization
« on: December 04, 2013, 08:40:49 PM »
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:
Code: [Select]
#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

Re: ADC initialization
« Reply #1 on: December 05, 2013, 04:48:53 AM »
can you show what's in ADCinit() ?

Seljer

Re: ADC initialization
« Reply #2 on: December 05, 2013, 07:24:56 AM »
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

Re: ADC initialization
« Reply #3 on: December 05, 2013, 08:34:33 AM »
Sorry! I thought the ADC part was there.

The code is:
Code: [Select]
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:
Code: [Select]
        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.
« Last Edit: December 05, 2013, 08:36:31 AM by MstrKurt »

Seljer

Re: ADC initialization
« Reply #4 on: December 05, 2013, 10:33:53 AM »
The issue you're having come from

Code: [Select]
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.

« Last Edit: December 05, 2013, 10:36:05 AM by Seljer »

kingswayguitar

Re: ADC initialization
« Reply #5 on: December 05, 2013, 09:25:01 PM »
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

Re: ADC initialization
« Reply #6 on: December 06, 2013, 03:44:11 AM »
The issue you're having come from

Code: [Select]
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

Re: ADC initialization
« Reply #7 on: December 14, 2013, 08:25:06 AM »
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

Re: ADC initialization
« Reply #8 on: December 14, 2013, 08:27:19 AM »
Does nobody cut assembly language any more?   <sob> <sniffle>   ;D

david

slacker

Re: ADC initialization
« Reply #9 on: December 14, 2013, 10:26:29 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

Re: ADC initialization
« Reply #10 on: December 15, 2013, 06:04:19 AM »
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?.
« Last Edit: December 15, 2013, 08:08:54 AM by MstrKurt »

potul

Re: ADC initialization
« Reply #11 on: December 16, 2013, 05:58:26 PM »
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

Re: ADC initialization
« Reply #12 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?

slacker

Re: ADC initialization
« Reply #13 on: December 17, 2013, 01:06:37 PM »
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

Re: ADC initialization
« Reply #14 on: December 17, 2013, 04:08:15 PM »
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

Re: ADC initialization
« Reply #15 on: December 18, 2013, 01:26:10 PM »
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

Re: ADC initialization
« Reply #16 on: December 19, 2013, 07:06:41 PM »
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?.
« Last Edit: December 19, 2013, 09:13:39 PM by MstrKurt »

greaser_au

Re: ADC initialization
« Reply #17 on: December 20, 2013, 06:33:20 AM »
these are questions for Messrs. Nyquist and Fourier...  :)

david

slacker

Re: ADC initialization
« Reply #18 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?

MstrKurt

Re: ADC initialization
« Reply #19 on: December 20, 2013, 01:35:11 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.