2

I have a problem with my code; My aim is to create a code that should take 8000 - 10000 value per second from ADC. Now i wrote a test code to configure the ADC, but according to my calculation ADC Time for Conversion shoul be :

* Tad=Tcy*(adcs+1)=(1/40)*4 = 1.6us
* Tc = 14 * 1.6 = 22.4 us

, but in MPLAB when i use StopWatch i see that 1 conversion take nearly 5 ms.

Configuration :

DSPIC33FJ128MC802
FRC + PLL -> ~ 80 MHz
FCY 40 MHz

Function for ADC Configuration

void adc_init(){

AD1CON1bits.ADON=0;
AD1CON1bits.AD12B = 1;
AD1CON1bits.FORM = 0; 
AD1CON1bits.SSRC = 0; //
AD1CON1bits.ASAM = 0; 
AD1CON2bits.CSCNA = 1;
AD1CON2bits.CHPS = 0; 
AD1CON2bits.SMPI = 0; 
AD1CON3bits.ADRC = 0;
AD1CON3bits.ADCS = 63; 
AD1CSSLbits.CSS3 = 1; 
AD1PCFGL=0xFFFF; 
AD1PCFGLbits.PCFG3 = 0;

IPC3bits.AD1IP = 6; 

AD1CON1bits.ADON = 1; }

Function for Read ADC Value

unsigned int read_adc_value(void){
AD1CON1bits.SAMP = 1; // start 
//while(!AD1CON1bits.SAMP);
__delay_us(10);
AD1CON1bits.SAMP = 0; // stop 
while(!AD1CON1bits.DONE);
return ADCBUF0;}
Furch Radeon
  • 77
  • 1
  • 7
  • Are you using MPLAB simulator or ICD debugger? – GJ. Apr 08 '17 at 21:06
  • MPLAB Simulator. I think that i found the problem: AD1CON3bits.ADRC = 0; AD1CON3bits.ADCS = 4; should be 4 , not 63 , because ADC take only 125 ns. This 125 ns is Tad. And time for conversion for my situation is 14*Tad . while(!AD1CON1bits.DONE); <- 14 * Tad ; – Furch Radeon Apr 09 '17 at 08:31

0 Answers0