Pongdanai823
New Member
I want to get the signal from adc then send the same signal out by dac(use SPI-->MCP4922)
the problem is I can get the signal from analog to digital but the signal doesn't come out
NOTE:adc and dac work properly when they separate
pl help me
code->>
#include <p30f4011.h>
#include <adc10.h>
#include <spi.h>
#include<math.h>
/* Setup Configuration For ET-dsPIC30F4011 */
_FOSC(CSW_FSCM_OFF & XT_PLL16);
_FWDT(WDT_OFF);
_FBORPOR(PBOR_OFF & PWRT_64 & MCLR_EN);
_FGS(CODE_PROT_OFF);
void adc_init()
{
ADCON1bits.ADON=0; //turn off ADC
ADPCFG =0XFFFC; // Enable analog port AN0 AN1
// Channal CH0
ADCHSbits.CH0SA=0;
ADCHSbits.CH0SA=1;
ADCHSbits.CH0NA=0;
IFS0bits.ADIF = 0; // Disable interrupt
// scan port AN0,AN1
ADCSSLbits.CSSL0=1;
ADCSSLbits.CSSL1=1;
// ADCON3
ADCON3bits.SAMC=1010; // 10 TAD
ADCON3bits.ADRC=1; // clock internal RC
ADCON3bits.ADCS=11001; // 13 TCY
/*----------------------------------------*/
// ADCON2
ADCON2bits.VCFG=0; // Voltage Reference AVDD ,AVSS
ADCON2bits.CSCNA=1; // input scan
ADCON2bits.BUFM=0; // disable alternate buffer
ADCON2bits.ALTS=0; // mux a only
ADCON2bits.CHPS=0; // converts CH0
ADCON2bits.SMPI=1111; // 16th sample/convert sequence******
/*-----------------------------------------*/
// ADCON1
// ADCON1bits.ADON=1; // turn on ADC
ADCON1bits.ADSIDL=0; // Continue module operation in Idle mode
ADCON1bits.FORM=0; // unsigned integer
ADCON1bits.SSRC=0; // Clearing SAMP
ADCON1bits.SIMSAM=1; // Simultaneous
ADCON1bits.ASAM=1; // Auto set
}
void init_spi(void)
{
CloseSPI1(); // Close SPI Before New Config
// Initial SPI Interrupt Control
ConfigIntSPI1(SPI_INT_DIS & // Disable SPI Interrupt
SPI_INT_PRI_7); // Set Priority Interrupt = 7
//****************************************************
// ET-BASE dsPIC30F4011 Hardware Board
// XTAL = 7.3728MHz
// Fosc = 7.3728MHz x 16 = 117.9648 MHz
// Fcy = Fosc/4
// = 117.9648 / 4 = 29.4912 MHz
// Tcy = 1/29.4912 MHz
// = 33.90842 nS
//****************************************************
// Open SPI Function (460.8KHz Baud)
SPI1CONbits.FRMEN = 0 ; // Disable SPI Frame
SPI1CONbits.SPIFSD = 0 ; // SPI = Output (Master)
SPI1CONbits.DISSDO = 0 ; // SDO = Output Data to SDI of MCP4922
SPI1CONbits.MODE16 = 1 ; // SPI = Word
SPI1CONbits.SMP = 1 ; // Sampling Data on Half of Data Clock
SPI1CONbits.CKE = 1 ; // Change Data on Falling Edge Clock
SPI1CONbits.SSEN = 0 ; // Disable SS# Pin Function
SPI1CONbits.CKP = 0 ; // Rising Edge Clock Active
SPI1CONbits.MSTEN = 1 ; // SPI Function = Master
SPI1CONbits.SPRE = 4 ; // Secondary Prescale = 4:1
SPI1CONbits.PPRE = 1 ; // Primary Prescale = 16.1
SPI1STATbits.SPIEN = 1 ; // Enable SPI Function
SPI1STATbits.SPISIDL = 1 ; // Disable SPI in IDLE Mode
SPI1STATbits.SPIROV = 1 ; // Clear Overflow Flag
}
void delay_ms(unsigned int ms)
{
unsigned int i;
for(;ms>0;ms--)
for(i=0;i<182;i++)
Nop();
}
int main(void)
{
unsigned int a,b,aout,j;
//TRISE=0;
//PORTE=0;
adc_init();
ADCON1bits.ADON=1;
TRISEbits.TRISE3 = 0;
TRISEbits.TRISE0 = 0; // Config RE0 = Output(LDAC#)
TRISEbits.TRISE1 = 0; // Config RE1 = Output(SHDN#)
TRISEbits.TRISE2 = 0; // Config RE2 = Output(CS#)
init_spi(); // Initial SPI Function
LATEbits.LATE1 = 1; // Active MCP4922(SHDN# = 1)
while(1)
{
//analog to digital
ADCON1bits.SAMP=1; //start sampling
while(!ADCON1bits.SAMP); //wait end sampling
ConvertADC10(); //Convert ADC
a=ReadADC10(0); //Keep value for ADCValue
b=ReadADC10(1);
if(a>b)LATEbits.LATE3=1;
else LATEbits.LATE3=0;
//digital to analog
LATEbits.LATE2 = 0; // Enable SPI Chips Select
aout = a*4;
aout |= 0x3000; // Set config command as BUF=1,CA=1,SHDN=1
WriteSPI1(aout); // Send Data to MCP4922
while(SPI1STATbits.SPITBF); // Wait SPI Send Complete
LATEbits.LATE2 = 1; // Disable SPI Chips Select
LATEbits.LATE0 = 0; // Latch DAC Output(LDAC# = 0)
LATEbits.LATE0 = 1; // LDAC# = 1
}
}
the problem is I can get the signal from analog to digital but the signal doesn't come out
NOTE:adc and dac work properly when they separate
pl help me
code->>
#include <p30f4011.h>
#include <adc10.h>
#include <spi.h>
#include<math.h>
/* Setup Configuration For ET-dsPIC30F4011 */
_FOSC(CSW_FSCM_OFF & XT_PLL16);
_FWDT(WDT_OFF);
_FBORPOR(PBOR_OFF & PWRT_64 & MCLR_EN);
_FGS(CODE_PROT_OFF);
void adc_init()
{
ADCON1bits.ADON=0; //turn off ADC
ADPCFG =0XFFFC; // Enable analog port AN0 AN1
// Channal CH0
ADCHSbits.CH0SA=0;
ADCHSbits.CH0SA=1;
ADCHSbits.CH0NA=0;
IFS0bits.ADIF = 0; // Disable interrupt
// scan port AN0,AN1
ADCSSLbits.CSSL0=1;
ADCSSLbits.CSSL1=1;
// ADCON3
ADCON3bits.SAMC=1010; // 10 TAD
ADCON3bits.ADRC=1; // clock internal RC
ADCON3bits.ADCS=11001; // 13 TCY
/*----------------------------------------*/
// ADCON2
ADCON2bits.VCFG=0; // Voltage Reference AVDD ,AVSS
ADCON2bits.CSCNA=1; // input scan
ADCON2bits.BUFM=0; // disable alternate buffer
ADCON2bits.ALTS=0; // mux a only
ADCON2bits.CHPS=0; // converts CH0
ADCON2bits.SMPI=1111; // 16th sample/convert sequence******
/*-----------------------------------------*/
// ADCON1
// ADCON1bits.ADON=1; // turn on ADC
ADCON1bits.ADSIDL=0; // Continue module operation in Idle mode
ADCON1bits.FORM=0; // unsigned integer
ADCON1bits.SSRC=0; // Clearing SAMP
ADCON1bits.SIMSAM=1; // Simultaneous
ADCON1bits.ASAM=1; // Auto set
}
void init_spi(void)
{
CloseSPI1(); // Close SPI Before New Config
// Initial SPI Interrupt Control
ConfigIntSPI1(SPI_INT_DIS & // Disable SPI Interrupt
SPI_INT_PRI_7); // Set Priority Interrupt = 7
//****************************************************
// ET-BASE dsPIC30F4011 Hardware Board
// XTAL = 7.3728MHz
// Fosc = 7.3728MHz x 16 = 117.9648 MHz
// Fcy = Fosc/4
// = 117.9648 / 4 = 29.4912 MHz
// Tcy = 1/29.4912 MHz
// = 33.90842 nS
//****************************************************
// Open SPI Function (460.8KHz Baud)
SPI1CONbits.FRMEN = 0 ; // Disable SPI Frame
SPI1CONbits.SPIFSD = 0 ; // SPI = Output (Master)
SPI1CONbits.DISSDO = 0 ; // SDO = Output Data to SDI of MCP4922
SPI1CONbits.MODE16 = 1 ; // SPI = Word
SPI1CONbits.SMP = 1 ; // Sampling Data on Half of Data Clock
SPI1CONbits.CKE = 1 ; // Change Data on Falling Edge Clock
SPI1CONbits.SSEN = 0 ; // Disable SS# Pin Function
SPI1CONbits.CKP = 0 ; // Rising Edge Clock Active
SPI1CONbits.MSTEN = 1 ; // SPI Function = Master
SPI1CONbits.SPRE = 4 ; // Secondary Prescale = 4:1
SPI1CONbits.PPRE = 1 ; // Primary Prescale = 16.1
SPI1STATbits.SPIEN = 1 ; // Enable SPI Function
SPI1STATbits.SPISIDL = 1 ; // Disable SPI in IDLE Mode
SPI1STATbits.SPIROV = 1 ; // Clear Overflow Flag
}
void delay_ms(unsigned int ms)
{
unsigned int i;
for(;ms>0;ms--)
for(i=0;i<182;i++)
Nop();
}
int main(void)
{
unsigned int a,b,aout,j;
//TRISE=0;
//PORTE=0;
adc_init();
ADCON1bits.ADON=1;
TRISEbits.TRISE3 = 0;
TRISEbits.TRISE0 = 0; // Config RE0 = Output(LDAC#)
TRISEbits.TRISE1 = 0; // Config RE1 = Output(SHDN#)
TRISEbits.TRISE2 = 0; // Config RE2 = Output(CS#)
init_spi(); // Initial SPI Function
LATEbits.LATE1 = 1; // Active MCP4922(SHDN# = 1)
while(1)
{
//analog to digital
ADCON1bits.SAMP=1; //start sampling
while(!ADCON1bits.SAMP); //wait end sampling
ConvertADC10(); //Convert ADC
a=ReadADC10(0); //Keep value for ADCValue
b=ReadADC10(1);
if(a>b)LATEbits.LATE3=1;
else LATEbits.LATE3=0;
//digital to analog
LATEbits.LATE2 = 0; // Enable SPI Chips Select
aout = a*4;
aout |= 0x3000; // Set config command as BUF=1,CA=1,SHDN=1
WriteSPI1(aout); // Send Data to MCP4922
while(SPI1STATbits.SPITBF); // Wait SPI Send Complete
LATEbits.LATE2 = 1; // Disable SPI Chips Select
LATEbits.LATE0 = 0; // Latch DAC Output(LDAC# = 0)
LATEbits.LATE0 = 1; // LDAC# = 1
}
}