P.Copper
Member level 5
- Joined
- Mar 18, 2013
- Messages
- 82
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,286
- 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
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
}
}