Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 25, 2024, 11:28:06 23:28


Login with username, password and session length


Pages: [1]
Print
Author Topic: [SOLVED]dsPIC33f-adc Simulation Problem  (Read 4950 times)
0 Members and 1 Guest are viewing this topic.
biomed12
Junior Member
**
Offline Offline

Posts: 94

Thank You
-Given: 67
-Receive: 5


« on: October 25, 2016, 09:01:24 21:01 »

Hello dear members,

I am trying to implement an adc simulation using proteus-labview. The program logic is that, dsPIC converts adcData as unsigned 16-bit integer and send it with 115200 baud. Input signal is sinusoidal with 1 hz. When I programmed PIC16f, 18f series, I was trying to simulation before realizing circuit physically. But in this dsPIC simulation, some adc values jumps randomly. So I could not solve the problem for hours. The situation did not change in either manual or auto sampling-conversion modes. I have almost read all of related adc documents of microchip and I coded it bit by bit. Here is my setupADC function:

Code:

void ADC_Init(void)
{
    //12bit conversion
    AD1CON1bits.AD12B = 1;
        
    //voltage references
    //000: AVdd,AVss
    //001: +VREF,AVss
    //011: +VREF,-Vref
    AD1CON2bits.VCFG0 = 0;
    AD1CON2bits.VCFG1 = 0;
    AD1CON2bits.VCFG1 = 0;
    
    //select conversion clock freq
    //111111: 64Tcy, for 40MHZ Oscillator
    //for int osc, dont touch these bits
    
    AD1CON3bits.ADCS0 = 1;
    AD1CON3bits.ADCS1 = 1;
    AD1CON3bits.ADCS2 = 1;
    AD1CON3bits.ADCS3 = 1;
    AD1CON3bits.ADCS4 = 1;
    AD1CON3bits.ADCS5 = 1;
    AD1CON3bits.ADCS6 = 0;
    AD1CON3bits.ADCS7 = 0;
    
    //AN0 analog
    AD1PCFGLbits.PCFG0 = 0;
    TRISAbits.TRISA0 = 1;
    
    
    //positive input an0 for ch0
    AD1CHS0bits.CH0SA0 = 0;
    AD1CHS0bits.CH0SA1 = 0;
    AD1CHS0bits.CH0SA2 = 0;
    AD1CHS0bits.CH0SA3 = 0;
    AD1CHS0bits.CH0SA4 = 0;
    
    AD1CHS0bits.CH0SB0 = 0;
    AD1CHS0bits.CH0SB1 = 0;
    AD1CHS0bits.CH0SB2 = 0;
    AD1CHS0bits.CH0SB3 = 0;
    AD1CHS0bits.CH0SB4 = 0;
    
    //just ch0 will do conversion
    //dont touch this for 12bit conversion
    //12 bit conversion just can be done with ch0
    //AD1CON2<9:8>
    
    //
    AD1CON2bits.CHPS = 0;
    
    
    //7. step
    //dont touch this for 12bit conversion
    
    //select manuel or auto sampling
    //000 0 manual sampling manual conversion
    //111 1 auto
    AD1CON1bits.SSRC0 = 1;
    AD1CON1bits.SSRC1 = 1;
    AD1CON1bits.SSRC2 = 1;
    
    AD1CON1bits.ASAM = 1;
    
    //sample time setting for auto sample
    //this is not important for manual sampling
    AD1CON3bits.SAMC0 = 1;
    AD1CON3bits.SAMC1 = 0;
    AD1CON3bits.SAMC2 = 0;
    AD1CON3bits.SAMC3 = 1;
    AD1CON3bits.SAMC4 = 1;
    
    
    //clock source select bit
    //internal RC
    //internal rc for dsPIC33fj32MC204 is 4MHZ
    AD1CON3bits.ADRC = 0;
    
    //select result format
    //For 12-bit operation:
    //10 = unsigned Fractional (DOUT = dddd dddd dddd 0000)
    //00 = unsigned Integer (DOUT = 0000 dddd dddd dddd)
    //11 = signed fractional
    //01 = signed integer
    AD1CON1bits.FORM0 = 0;
    AD1CON1bits.FORM1 = 0;
    
    
    //sample number per interrupt
    //1111 = 16 sample for an interrupt
    //0000 = 1 sample for an interrupt
    AD1CON2bits.SMPI0 = 0;
    AD1CON2bits.SMPI1 = 0;
    AD1CON2bits.SMPI2 = 0;
    AD1CON2bits.SMPI3 = 0;
    
    
    //configure interrupt
    //no nested interrupt
    INTCON1bits.NSTDIS = 0;
    //clear interrupt flag
    IFS0bits.AD1IF = 0;
    //interrupt is priority 7
    IPC3bits.AD1IP0 = 1;
    IPC3bits.AD1IP1 = 1;
    IPC3bits.AD1IP2 = 1;

    //ADC1 Conversion Complete Interrupt Enable bit
    IEC0bits.AD1IE = 0;

    
    //adc module enable
    AD1CON1bits.ADON = 1;
}


this is the task which runs in while(1) loop in main function:


Code:
        
AD1CON1bits.SAMP = 1; // Start sampling
        for(i=0;i<5000;i++)// Wait for sampling time (a little time)
        {
            Nop();
        }
        AD1CON1bits.SAMP = 0; // Start the conversion
        while (!AD1CON1bits.DONE); // Wait for the conversion to complete
        sendData.sendData = ADC1BUF0; // Read the conversion result, this is unsigned int union type
        
        U1TXREG = sendData.bytes[0];
        while(!U1STAbits.TRMT);
                        
        U1TXREG = sendData.bytes[1];
        while(!U1STAbits.TRMT);
                            
        U1TXREG = '\n';
        while(!U1STAbits.TRMT);



Also I attached an image that shows the situation.

Note: I know proteus is not real but it worked all the time with 16f and 18f series.

EDİT:I solved the problem, dear members. If someone have same issue, that person should make false the "termination char" parameter on the open visa variable at the labview side.

Thanks.
« Last Edit: October 26, 2016, 06:01:23 18:01 by biomed12 » Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC