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?.
can you show what's in ADCinit() ?
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.
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.
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.
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.
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!
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?
Does nobody cut assembly language any more? <sob> <sniffle> ;D
david
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.
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?.
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.
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?
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.
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.
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?.
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?.
these are questions for Messrs. Nyquist and Fourier... :)
david
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?
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.
so, are you using the internal DAC of the dspic?
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;
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)
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?.
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.