[SOLVED] Code check for sequential sampling

Status
Not open for further replies.

P.Copper

Member level 5
Joined
Mar 18, 2013
Messages
82
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
1,807
Hi all,

can any one check the below code if it will work for sequential sampling. feedback will highly be appreciated

Code:
#include <p30F4013.h>#include <libpic30.h>
#include <adc12.h>
#include <dsp.h>




//****** Device configuration register macros for building the hex file ******//
_FOSC(CSW_FSCM_OFF & FRC);
_FWDT(WDT_OFF);                         /* Watchdog timer disabled */
_FBORPOR(PBOR_OFF & MCLR_EN);   /*Brown-out reset disabled, MCLR reset enabled*/
_FGS(CODE_PROT_OFF);






 unsigned int ADC_voltage();
 unsigned int ADC_current();
 int Voltage_process();
 int Current_process();
 int Conversion();
 double V1_rms,I1_rms;


 void PORT_INITIAL (void);




int main()
{
    long int V;
    long int I;


    V = ADC_voltage(0);
    I = ADC_current(0);


    PORT_INITIAL ();
    ADC_voltage();
    ADC_current();
            //----configure Oscillator-----------//
    OSCCONbits.COSC0 =1;        //internal fast oscillator
    OSCCONbits.COSC1 =0;
    
}
//***************************PORT INITIALISATION******************************//
  void PORT_INITIAL (void)
  {
    LATB = 0xFFFF;
    ADPCFG = 0xFFCF;        // RB4 & RB5 pins are analog
    TRISB = 0x00C0;         //ONLY RB4,5 pins are inputs


  }
//***************************CONVERTING VOLTAGE*******************************//
unsigned int ADC_voltage()
{
    ADCHSbits.CH0SA = 0x0004; // Connect RB4/AN4 as CH0 input for MUXA
    ADCSSL = 0;
    ADCON1 = 0x00E0;        // SSRC bit = 111 implies internal
                            // buffer results are integer FORM<1,0>=00


    ADCON2 = 0x0001;       // Select AVdd and AVss as reference voltage
                           // Interrupts at the completion of conversion for each sample/convert sequence
                            // alternates between MUXA and MUXB


    ADCON3 = 0x0113;        // Sample time = 1Tad, Tad = 333.33 ns @ 30 MIPS
                            // which will give 1 / (15 * 333.33 ns) = 200 ksps


    ADCON1bits.ADON = 1;    // turn ADC ON


while (1) // repeat continuously
  {
    
    IFS0bits.ADIF = 0;         // clear ADC interrupt flag
    ADCON1bits.ASAM = 1;       // auto start sampling for 31Tad then go to conversion
    
  while (!IFS0bits.ADIF);       // conversion done?
       ADCON1bits.ASAM = 0;        // yes then stop sample/convert
        return  ADCBUF4;            // yes then get ADC voltage value from buffer4
   }
}
//***************************CONVERTING CURRENT*******************************//
unsigned int ADC_current()
{
    ADCHSbits.CH0SB = 0x0005; // Connect RB5/AN5 as CH0 input for MUXB
    ADCSSL = 0;
    ADCON1 = 0x00E0;         // SSRC bit = 111 implies internal
                             // buffer results are integer FORM<1,0>=00


    ADCON2 = 0x0001;        // Select AVdd and AVss as reference voltage
                            // Interrupts at the completion of conversion for each sample/convert sequence
                            // alternates between MUXA and MUXB


    ADCON3 = 0x0113;        // Sample time = 1Tad, Tad = 333.33 ns @ 30 MIPS
                            // which will give 1 / (15 * 333.33 ns) = 200 ksps


    ADCON1bits.ADON = 1;    // turn ADC ON


while (1) // repeat continuously
  {
    IFS0bits.ADIF = 0;                // clear ADC interrupt flag
    ADCON1bits.ASAM = 1;              // auto start sampling for 31Tad then go to conversion
        while (!IFS0bits.ADIF);       // conversion done?
           ADCON1bits.ASAM = 0;        // yes then stop sample/convert
           return ADCBUF5;        // yes then get ADC current value from buffer5
   }
}
 

I think there is mistake in your code - you should modify the code for following things -

1) you are using while(1) in ADC_Current() and ADC_Voltage() function it will not come out of the loop ADC_Current() as while(1) loop is always true.... I think you should use while(1) in the main loop
2) Give all the configuration data like OSCCON bit setting before it goes to function call becasue it will initialize your system at the time of booting....

good luck
 
Last edited:


can you please post a code snippet of what you're saying i should do in the main... as im not sure if you mean i should do this
Code:
int main()
{
    long int V;
    long int I;
 while(1)
    V = ADC_voltage(0);
    I = ADC_current(0);
            //----configure Oscillator-----------//
    OSCCONbits.COSC0 =1;        //internal fast oscillator
    OSCCONbits.COSC1 =0;
            //-----function  call---------------//
    PORT_INITIAL ();            
    ADC_voltage();
    ADC_current();

    
}
 

Ok..... you made it wrong .... Please note that I am not checking here the chip specific resgistors.... that you have to take care of it...... kindly, please also note that you are using ADC in interrupt mode so..... I am not sure about the correctness of that code as I never used this chip in PIC.....So based on ADC config and interrupt resistors you need to clear the ADC conversion interrupt flag.... here is code that is modified from what you are posted ....


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <p30F4013.h>#include <libpic30.h>
#include <adc12.h>
#include <dsp.h>
 
//****** Device configuration register macros for building the hex file ******//
_FOSC(CSW_FSCM_OFF & FRC);
_FWDT(WDT_OFF);                         /* Watchdog timer disabled */
_FBORPOR(PBOR_OFF & MCLR_EN);   /*Brown-out reset disabled, MCLR reset enabled*/
_FGS(CODE_PROT_OFF);
 
 unsigned int ADC_voltage();
 unsigned int ADC_current();
 int Voltage_process();
 int Current_process();
 int Conversion();
 double V1_rms,I1_rms;
 
 void PORT_INITIAL (void);
 
int main()
{
    //----configure Oscillator-----------//
    OSCCONbits.COSC0 =1;        //internal fast oscillator
    OSCCONbits.COSC1 =0;
   
    long int V;
    long int I;
 
    PORT_INITIAL ();
    while(1)
    {
        V = ADC_voltage();
        I = ADC_current();
     }
 
   
    //ADC_voltage();
   // ADC_current();
             
}
//***************************PORT INITIALISATION******************************//
  void PORT_INITIAL (void)
  {
    LATB = 0xFFFF;
    ADPCFG = 0xFFCF;        // RB4 & RB5 pins are analog
    TRISB = 0x00C0;         //ONLY RB4,5 pins are inputs
 
 
  }
//***************************CONVERTING VOLTAGE*******************************//
unsigned int ADC_voltage()
{
    ADCHSbits.CH0SA = 0x0004; // Connect RB4/AN4 as CH0 input for MUXA
    ADCSSL = 0;
    ADCON1 = 0x00E0;        // SSRC bit = 111 implies internal
                            // buffer results are integer FORM<1,0>=00
 
 
    ADCON2 = 0x0001;       // Select AVdd and AVss as reference voltage
                           // Interrupts at the completion of conversion for each sample/convert sequence
                            // alternates between MUXA and MUXB
 
 
    ADCON3 = 0x0113;        // Sample time = 1Tad, Tad = 333.33 ns @ 30 MIPS
                            // which will give 1 / (15 * 333.33 ns) = 200 ksps
 
 
    ADCON1bits.ADON = 1;    // turn ADC ON
 
 
//while (1) // repeat continuously
//  {
    
    IFS0bits.ADIF = 0;         // clear ADC interrupt flag
    ADCON1bits.ASAM = 1;       // auto start sampling for 31Tad then go to conversion
    
  while (!IFS0bits.ADIF);       // conversion done?
     {
       ADCON1bits.ASAM = 0;        // yes then stop sample/convert
      
     }   
     return  ADCBUF4;            // yes then get ADC voltage value from buffer4
  // }
}
//***************************CONVERTING CURRENT*******************************//
unsigned int ADC_current()
{
    ADCHSbits.CH0SB = 0x0005; // Connect RB5/AN5 as CH0 input for MUXB
    ADCSSL = 0;
    ADCON1 = 0x00E0;         // SSRC bit = 111 implies internal
                             // buffer results are integer FORM<1,0>=00
 
 
    ADCON2 = 0x0001;        // Select AVdd and AVss as reference voltage
                            // Interrupts at the completion of conversion for each sample/convert sequence
                            // alternates between MUXA and MUXB
 
 
    ADCON3 = 0x0113;        // Sample time = 1Tad, Tad = 333.33 ns @ 30 MIPS
                            // which will give 1 / (15 * 333.33 ns) = 200 ksps
 
 
    ADCON1bits.ADON = 1;    // turn ADC ON
 
 
//while (1) // repeat continuously
 // {
    IFS0bits.ADIF = 0;                // clear ADC interrupt flag
    ADCON1bits.ASAM = 1;              // auto start sampling for 31Tad then go to conversion
     while (!IFS0bits.ADIF);       // conversion done?
      {     
      ADCON1bits.ASAM = 0;        // yes then stop sample/convert
      }  
      return ADCBUF5;        // yes then get ADC current value from buffer5
  // }
}




I hope this works..... I mean try on board one thing is missing here printing the values of V and I .....I think you need that also in your logic without that you will not came to know it is working or not

Good Luck
 
Last edited:
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…