DIYstompboxes.com

DIY Stompboxes => Digital & DSP => Topic started by: MstrKurt on December 04, 2013, 08:40:49 PM

Title: ADC initialization
Post by: MstrKurt 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:

#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?.
Title: Re: ADC initialization
Post by: potul on December 05, 2013, 04:48:53 AM
can you show what's in ADCinit() ?
Title: Re: ADC initialization
Post by: Seljer 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.
Title: Re: ADC initialization
Post by: MstrKurt on December 05, 2013, 08:34:33 AM
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.
Title: Re: ADC initialization
Post by: 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.

Title: Re: ADC initialization
Post by: kingswayguitar 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.
Title: Re: ADC initialization
Post by: potul on December 06, 2013, 03:44:11 AM
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!
Title: Re: ADC initialization
Post by: MstrKurt 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?
Title: Re: ADC initialization
Post by: greaser_au on December 14, 2013, 08:27:19 AM
Does nobody cut assembly language any more?   <sob> <sniffle>   ;D

david
Title: Re: ADC initialization
Post by: slacker on December 14, 2013, 10:26:29 AM
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. 
Title: Re: ADC initialization
Post by: MstrKurt 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?.
Title: Re: ADC initialization
Post by: potul 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.
Title: Re: ADC initialization
Post by: 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?
Title: Re: ADC initialization
Post by: slacker 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 (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.
Title: Re: ADC initialization
Post by: 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.
Title: Re: ADC initialization
Post by: MstrKurt on December 18, 2013, 01:26:10 PM
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?.
Title: Re: ADC initialization
Post by: MstrKurt 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?.
Title: Re: ADC initialization
Post by: greaser_au on December 20, 2013, 06:33:20 AM
these are questions for Messrs. Nyquist and Fourier...  :)

david
Title: Re: ADC initialization
Post by: 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?
Title: Re: ADC initialization
Post by: MstrKurt on December 20, 2013, 01:35:11 PM
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.
Title: Re: ADC initialization
Post by: potul on December 20, 2013, 03:27:47 PM
so, are you using the internal DAC of the dspic?
Title: Re: ADC initialization
Post by: MstrKurt on December 20, 2013, 03:36:51 PM
Quote from: potul on December 20, 2013, 03:27:47 PM
so, are you using the internal DAC of the dspic?

No, I just had a port A pin set as an output. I then just used the code PORTAbits.RA1 = ADC1BUF0;
Title: Re: ADC initialization
Post by: slacker on December 20, 2013, 04:37:16 PM
Ok, so what you're doing is sending the contents of the ADC buffer which is a 12 bit number, 4096 different values, to an output pin which only has two states 1 or 0 (3.3 Volts or 0 Volts). I'm not sure how that is making a signal anything like your input, I haven't used any DSPics so I don't know what happens if you do that, on an 8 bit PICs the output pin would be set to the value of the least significant digit of the ADC buffer, so you'd just get noise. Sounds like what could be  happening is loud signals are giving a 1 and quiet signals a 0, so you get a square wave signal what ever frequency you play, I don't know if that's actually possible though.

What you want to do is use the PWM module as a DAC, with some external filtering this will give you an analogue output.
http://extremeelectronics.co.in/avr-tutorials/introduction-to-pwm-pulse-width-modulation/ (http://extremeelectronics.co.in/avr-tutorials/introduction-to-pwm-pulse-width-modulation/)
https://duckduckgo.com/?q=pic+pwm+tutorial (https://duckduckgo.com/?q=pic+pwm+tutorial)
Title: Re: ADC initialization
Post by: MstrKurt on December 20, 2013, 04:46:32 PM
I will give that a try when I manage to fix my project. I think the chip might have blown as there is continuity between VDD and VSS with the chip out of circuit. Shouldn't those 2 lines be isolated?

Actually ground has continuity to every single pin, is this normal?.
Title: Re: ADC initialization
Post by: MstrKurt on December 20, 2013, 05:36:57 PM
https://www.dropbox.com/s/m8i9onqbw2fxuva/sample.mp3 - Here is a sample of what I'm trying to explain sounds like. (it's meant to sound clean)

It's also lighting up an LED as I play the guitar haha. The higher notes can be heard but faintly, if you would like me to record the higher notes, let me know.

I've now fixed my project :).

I'm still unsure how to code an effect in C. I understand how the effects are made etc and after having done a DSP module last year in uni I'm comfortable enough with the maths involved but I'm unsure how to implement it to code.