dspic30f code for reading two analog values

Discussion in 'Embedded Systems and Microcontrollers' started by roymor, Feb 16, 2016.

  1. roymor

    Thread Starter New Member

    Nov 3, 2012
    2
    0
    hello,
    i am trying to write a program for dspic30f4012.
    I have a requirement to call a adcvalue() function
    within an interrupt routine of timer1. This adcvalue()
    function needs to switch on the ADC. Load the digital
    values of analog signals at AN0 and AN1 to variables
    defined as data1, data2. And then again turn down
    the ADC. This cycle repeats each time within the timer1 ISR.

    Can anyone tell me how to do it.(need to know adc setting and adcvalue function)
    I wrote the following code but it doesnt work.
    Code (Text):
    1.  //ADC initialisation
    2. void AD_set()  // ***** A to D (A/D) Settings
    3. {
    4.   ADPCFG = 0x0038; // Configure pins AN(3-5)/RB(3-5) into Digital
    5.   // I/O mode AN(0-2) pins are in Analog mode
    6. //  ADCON1bits.ADSIDL= 0;
    7. //  ADCON1bits.FORM= 0;
    8. //  ADCON1bits.SSRC= 7;
    9. //  ADCON1bits.SAMP= 1;
    10. ADCON1= 0x00E0;
    11.   ADCHS = 0x0000;  //
    12.   ADCSSL = 0x0003;
    13. //  ADCON3bits.SAMC= 8;
    14. //  ADCON3bits.ADRC= 0;
    15. //  ADCON3bits.ADCS= 1;
    16.  ADCON3= 0x0F00;
    17. //  ADCON2bits.VCFG= 0;
    18. //  ADCON2bits.CSCNA= 1;
    19. //  ADCON2bits.SMPI= 1;
    20. //  ADCON2bits.BUFM= 0;
    21. //  ADCON2bits.ALTS= 0;
    22. ADCON2= 0x0404;
    23.   //ADCON2 = 0x0004; // Interrupt after every 2 samples
    24. }
    My main prog is as follows

    Code (Text):
    1.  // main-prog.c
    2. #include<p30f4012.h>
    3. #include "settings-prog.h"
    4. _FOSC(CSW_FSCM_OFF & XT); // To use the external crystal
    5. _FWDT(WDT_OFF);   // To disable the watchdog timer
    6. void send_data(int);  
    7. int AD_value();     // Declare the AD value reading function
    8. int AD_value1();
    9. float R, P;
    10. void main()
    11. {    
    12.   uart_set();  // Initialise UART settings
    13.   AD_set();  // Initialise ADC settings
    14.   timer1_set(T); // Initialise Timer-1 settings & start timer
    15.   TRISEbits.TRISE8 = 0; // RE8 is configured as output
    16.   // Continue until stop the power
    17.   for(;;);
    18. }// End of main()
    19.      
    20. // Interrupt service routine (ISR) for interrupt from Timer1
    21.  
    22. void __attribute__((interrupt, no_auto_psv)) _T1Interrupt (void)
    23. {
    24.   IFS0bits.T1IF = 0; // Clear timer 1 interrupt flag  
    25.   uart_tx(9);
    26.   P= AD_value1();
    27.   send_data(P);
    28.   uart_tx(9);  // Space between 2 pieces of data.
    29.   R= AD_value();
    30.   send_data(R);
    31. } // End of ISR of Timer 1
    32.  
    33. int AD_value()
    34. {
    35. int ADCValue;
    36. ADCON1bits.ASAM= 1;
    37. //IEC0bits.ADIE= 0;
    38. IFS0bits.ADIF= 0;
    39. ADCON1bits.ADON= 1;
    40. while (!IFS0bits.ADIF);
    41. ADCON1bits.ASAM=0;
    42. ADCValue= ADCBUF0;
    43. return(ADCValue);
    44. }// End of AD_value()
    45.  
    46. int AD_value1()
    47. {
    48. int ADCValue1;
    49. ADCON1bits.ASAM= 1;
    50. //IEC0bits.ADIE= 0;
    51. IFS0bits.ADIF= 0;
    52. ADCON1bits.ADON= 1;
    53. while (!IFS0bits.ADIF);
    54. ADCON1bits.ASAM=0;
    55. ADCValue1= ADCBUF1;
    56. return(ADCValue1);
    57. }// End of AD_value()
    58. void send_data(int s_data)
    59. {
    60.   int s;
    61.   if(s_data < 0)
    62.   {
    63.   // Send the negative sign (ASCII is 45)
    64.   uart_tx(45);
    65.   s_data = -1*s_data;
    66.   }
    67.   // Digit with the position value of 100
    68.   s = s_data/100;
    69.   uart_tx(s+48);
    70.  
    71.   // Digit with the position value of 10
    72.   s_data = s_data - (s *100);
    73.   s = s_data/10;
    74.   uart_tx(s+48);
    75.  
    76.   // Digit with the position value of 1
    77.   s_data = s_data - (s *10);
    78.   uart_tx(s_data+4);
    79. }// End of send_data()
     
Loading...