ADC dsPIC not working. Please help.

Discussion in 'General Electronics Chat' started by Electron_23, May 25, 2014.

  1. Electron_23

    Thread Starter New Member

    May 6, 2014
    8
    0
    Hello everyone.

    Im trying to use the ADC module of the dsPIC33ep512mu810, reading the potentiometer of the Explorar 16 board, and show the binary conversion in PORT A (LEDs). Up to now I did not have success and I don't know whats missing or wrong. Could someone help please?

    Code ( (Unknown Language)):
    1.  
    2. //DEFINES or PARAMETERS
    3. #define FOSC 8000000 //OSCILLATION FREQUENCY
    4. #define PLL 1 //SETS PLL
    5. #define FCY (FOSC*PLL/2) //CYCLE FREQUENCY
    6.  
    7.  
    8.  
    9. #include <stdio.h>
    10. #include <stdlib.h>
    11. #include <p33ep512mu810.h>
    12.  
    13. //MACROS or CONFIGURATION BITS
    14. _FOSCSEL(FNOSC_PRI & IESO_OFF)
    15. // Primary oscillator as source
    16. // Start up with user-selected oscillator source
    17. _FOSC(FCKSM_CSDCMD & POSCMD_XT & OSCIOFNC_ON)
    18. // Both Clock switching and Fail-safe Clock Monitor are disabled
    19. // XT Crystal Oscillator Mode
    20. // OSC2 is general purpose digital I/O pin
    21. _FWDT(FWDTEN_OFF)
    22. // Watchdog timer enabled/disabled by user software
    23. _FPOR(FPWRT_PWR16 & BOREN_ON)
    24. // Power-on Reset Timer Value and bor enabled
    25. _FGS(GWRP_OFF & GSS_OFF & GSSK_OFF)
    26. // General Segment may be written
    27. // General Segment Code protect is disabled
    28. // General Segment Write Protection and Code Protection is Disabled
    29.  
    30.  
    31. // FUNCTION DECLARATIONS
    32. void adc(void);
    33. void delay_ms(long ms);
    34.  
    35.  
    36. /*
    37.  *
    38.  */
    39. int main() {
    40.     TRISA = 0;
    41.     adc();
    42.  
    43.     //loop to sample and acquire data
    44.     while(1){
    45.         AD1CON1bits.SAMP = 1;   //sampling
    46.         delay_ms(10);            //sample time
    47.         AD1CON1bits.SAMP = 0;   //holding
    48.  
    49.         while(!AD1CON1bits.DONE);   //is the conversion done?
    50.         PORTA = ADC1BUF1;  //data is shown in port A (LEDs)
    51.     }
    52.  
    53.     return (EXIT_SUCCESS);
    54. }
    55.  
    56.  
    57.  
    58. void adc(void){
    59.  
    60.     ANSELA = ANSELB = ANSELC = ANSELD = ANSELE = ANSELG = 0x0000;
    61.     // Set ports A, B, C, D, E, G as I/O digital ports.
    62.     ANSELBbits.ANSB5 = 1;  //makes AN5/RB5 analog input.
    63.  
    64.     AD1CON1bits.AD12B = 1; // Sets 12 bits ADC.
    65.  
    66.     AD1CON1 = 0x0000;
    67.     AD1CON2 = 0x0000;
    68.     AD1CON3 = 0x000F;
    69.     AD1CON4 = 0x0000;
    70.     AD1CHS0 = 0x0005;
    71.     AD1CHS123 = 0x0000;
    72.     AD1CSSH = 0x0000;
    73.     AD1CSSL = 0x0000;
    74.     AD1CON1bits.ADON = 1;  //enables ADC
    75.     delay_ms(10);
    76. }
    77.  
    78.  
    79.  
    80. void delay_ms(long ms){
    81.  
    82.     T1CON = 0;   //reset timer
    83.     TMR1 = 0;    //load timer count register to 0
    84.  
    85.     IPC0bits.T1IP = 1; // interrupt priority count
    86.     PR1 = ((FCY/256)*ms)/1000; //20ms delay. Comes from (((Fosc/4)/prescaler)*20ms)/(1000ms)
    87.     IFS0bits.T1IF = 0;
    88.     IEC0bits.T1IE = 0;
    89.     T1CON = 0X8030; //hexadecimal of 1000000000110000
    90.     // which means start the timer and select 256 prescaler
    91.     while(IFS0bits.T1IF == 0);
    92.  
    93. }
    94.  
    95.  
    Thank you very much in advance.
     
  2. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    I don't use that chip, but in 90% of cases the reason a PIC ADC is not working right is becuase it has not been configured right. :)

    That also includes the port pins configuration, if some other hardware feature has taken over the pin and forced it to be an output or force it to another function.
     
Loading...