This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Hello,
I want to digitize an analog signal, manipulate (curve) it, and then convert it back to analog domain for measurement purpose.
My friend and I are using MSP432P401R MCU with its ADC, and along with TI DAC8564. I need 16-bit DAC resolution so MUST use this external DAC8564. (Other MCUs with embedded DACs only provide 12-bit resolution). The ADC's function can be enabled now, but we don't know how to convert the ADC's 16-bit data to a suitable SPI format to feed DAC8564. Does anyone know how to write the code ?
We found an example code for MSP430F449 to feed a DAC8564 but couldn't work if we just apply this code to our MSP432P401R directly.
Does anyone know how to fix it ? (The code is attached, please see "dac.c")
Thank you very much !
/****************************************************************/ /* FILENAME: main.c */ /* DESCRIPTION: This program uses the MSP430F449 to write 256 */ /* samples to the DAC8564/5. */ /* The samples are generated by sine/cosine functions */ /* */ /* Author: Jojo Parguian */ /* Precision Analog Applications, Dallas, TX */ /* CREATED 06/18/08(C) BY TEXAS INSTRUMENTS INCORPORATED. */ /* VERSION: 01 */ /****************************************************************/ #include <msp430x44x.h> #include <math.h> /**************** Function prototypes ***********************************/ /* */ void init_sys(void); /* MSP430 Initialisation routine */ void setupClock(int); void delay(int); /* Software delay */ void dac_convert(int, int); /* Do the dac conversion */ void InitTables(int); void int_ref_disable(int, int, int); /* Disable the Internal Reference */ void int_ref_enable(void); /* Enable the Internal Reference */ void int_ref_ON (void); /* Enable the Internal Reference */ /* all the time */ /************************************************************************/ /* */ /* HPA449 variable declarations */ /* */ /************************************************************************/ #define DAC_CS 0x20 //p3.5 #define LDAC 0x40 //p3.6 #define Resonator (0) #define disable_ref (0) #define enable_ref (0) /************************************************************************/ /* */ /* main() variable declarations */ /* */ /************************************************************************/ #define BLOCK_SZ 256 /* size of data buffer */ #define TABLE_SIZE 256 int sinetable[TABLE_SIZE], costable[TABLE_SIZE], byte0, byte1, byte2; int i, y; /******************************************************************************\ * Function: InitTables() * Description: Initializes Sine and Cosine Values \******************************************************************************/ void main(void) { int val0, val1, val2; int dac_A, dac_B, dac_C, dac_D; setupClock(Resonator); init_sys(); // Initialise the MSP430 InitTables(1); // Number defines the cycles per period if (disable_ref) { val0 = 0x00; val1 = 0x20; val2 = 0x01; int_ref_disable(val0, val1, val2); } else if (enable_ref) { int_ref_enable(); } else { int_ref_ON(); } do { dac_A = 0x0; dac_B = 0x2; dac_C = 0x4; dac_D = 0x26; for(i=0; i<BLOCK_SZ; i++) { dac_convert(dac_A, sinetable[i]); // Send sine values to the DAC A = sinetable[i] dac_convert(dac_B, sinetable[i]); // Send sine values to the DAC B = sinetable[i] dac_convert(dac_C, sinetable[i]); // Send sine values to the DAC C = sinetable[i] dac_convert(dac_D, sinetable[i]); // Send sine values to the DAC D = sinetable[i] } } while (1); // Endless Loop } /*end of main*/ /************************************************************/ /* Prototype - init_table */ /* */ /* Description */ /* This prototype initialises the waveform table */ /************************************************************/ void InitTables(int cycles) { int y; for (y = 0; y < TABLE_SIZE; y++) { sinetable[y] = (unsigned int)((sin(2 * 3.141593659 * cycles * y / TABLE_SIZE) + 1.0) * 32767); costable[y] = (unsigned int)((cos(2 * 3.141593659 * cycles * y / TABLE_SIZE) + 1.0) * 32767); } } /************************************************************/ /* Prototype - init_sys */ /* */ /* Description */ /* This prototype initialises the MSP430F449 */ /************************************************************/ void init_sys(void) { void setupports(void); /* Local function prototype */ void setupSPI(void); /* Local function prototype */ setupports(); setupSPI(); // _EINT(); // Enable interrupts } /************************************************************/ /* Prototype - setupClock */ /* */ /* Description */ /* This prototype sets-up the XT2 oscillator and tests */ /* that it has settled before moving on */ /************************************************************/ void setupClock (int RES) { switch (RES) { case 0: { WDTCTL = WDTPW + WDTHOLD; /* D=2, N=121, no mod */ SCFQCTL=121; // SCFI0 = 0100 0100 SCFI0=0x44; // DCOPLUS=1, XTS_FLL=0 // XCAPXPF=00 FLL_CTL0=0x80; // SMCLK=0, XT2OFF=1, SELMX=00, SELS=0, FLL_DIVX=10 // FLL_CTL1= 0010 0010 FLL_CTL1=0x22; } break; case 1: { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer FLL_CTL0&=XT2OFF; // Switch on the XT2 osc. FLL_CTL1|=SELM_XT2+SELS; // Select XT2 osc. for // Test the osc. flag bit do { IFG1 &= ~OFIFG; // Clear the OFIFG bit } while (OFIFG&IFG1); // } break; } } /************************************************************/ /* Prototype - setupports */ /* Description */ /* This prototype sets-up the GPIO ports as appropriate */ /************************************************************/ void setupports (void) { // SPI port for DAC P3SEL = BIT3 + BIT2 + BIT1; // Bits 3, 2 & 1 are assigned as SPI specific pins P3DIR = DAC_CS+LDAC; // Set the DAC_CS bit as an output P3OUT = DAC_CS+LDAC; // De-assert DAC_CS for the DAC - HIGH P4SEL = BIT5 + BIT3 + BIT2; //SetupInterrupts P1IFG |= 0x00; P1IES |= 0x40; P1IE |= 0x40; } /************************************************************/ /* Prototype - setupSPI */ /* */ /* Description */ /* This prototype sets-up the P3 for communication via SPI */ /************************************************************/ void setupSPI (void) /************************************************************/ /* System definitions */ /************************************************************/ #define SPI0en 0x40 #define SPI1en 0x10 { ME1 |= USPIE0; // Module Enable - SPI1 U0CTL &= ~SWRST; // Make sure the RESET bit is off U0CTL |= CHAR + SYNC + MM; // USART0 module operation // CHAR = 1 => 8-bit data // SYNC = 1 => SPI selected // MM = 1 => master mode, U0TCTL |= CKPH + CKPL + SSEL0 + SSEL1 + STC; // USART0 Tranmit control register U0BR0 = 0x02; // Divide SMCLK by 2 => transfer clock U0BR1 = 0x00; // U0MCTL = 0x00; // Modulation control - not used. Ensure // all these bits are reset } /************************************************************/ /* Prototype - delay */ /* Description */ /* This prototype gives a delay of around 1 second */ /************************************************************/ void delay(int time) { unsigned int i; for (i = 0; i < time; i++); } /************************************************************/ /* Prototype - int_ref_disable */ /* Description */ /* This prototype disables the DAC internal reference */ /************************************************************/ void int_ref_disable (val0, val1, val2) { byte0 = val0; byte1 = val1; byte2 = val2; P3OUT &= ~(DAC_CS); // Set DAC /CS low while ((IFG1 & UTXIFG0) == 0); U0TXBUF = byte2 ; // Send clocks to the DAC, this shifts CMD byte // Wait until all 8 bits have been received. while ((IFG1 & UTXIFG0) == 0); U0TXBUF = byte1; // Send clocks to the DAC, this shifts MSB byte // Wait until all 8 bits have been received. while ((IFG1 & UTXIFG0) == 0); U0TXBUF = byte0; // Send clocks to the DAC, this shifts LSB byte // Wait until all 8 bits have been received. delay(3); // Wait until all 8 bits have been received. P3OUT |= (DAC_CS); // Set DAC /CS high } /************************************************************/ /* Prototype - int_ref_enable */ /* Description */ /* This prototype enables the DAC internal reference */ /************************************************************/ void int_ref_enable (void) { byte0 = 0x00; byte1 = 0x00; byte2 = 0x01; P3OUT &= ~(DAC_CS); // Set DAC /CS low for 1st write cycle while ((IFG1 & UTXIFG0) == 0); U0TXBUF = byte2 ; // Send clocks to the DAC, this shifts CMD byte // Wait until all 8 bits have been received. while ((IFG1 & UTXIFG0) == 0); U0TXBUF = byte1; // Send clocks to the DAC, this shifts MSB byte // Wait until all 8 bits have been received. while ((IFG1 & UTXIFG0) == 0); U0TXBUF = byte0; // Send clocks to the DAC, this shifts LSB byte // Wait until all 8 bits have been received. delay(3); // Wait until all 8 bits have been received. P3OUT |= (DAC_CS); // Set DAC /CS high } /*******************************************************************/ /* Prototype - int_ref_ON */ /* Description */ /* This prototype enables the DAC internal reference all the time */ /*******************************************************************/ void int_ref_ON (void) { byte0 = 0x00; byte1 = 0x10; byte2 = 0x01; P3OUT &= ~(DAC_CS); // Set DAC /CS low for 2nd write cycle while ((IFG1 & UTXIFG0) == 0); U0TXBUF = byte2 ; // Send clocks to the DAC, this shifts CMD byte // Wait until all 8 bits have been received. while ((IFG1 & UTXIFG0) == 0); U0TXBUF = byte1; // Send clocks to the DAC, this shifts MSB byte // Wait until all 8 bits have been received. while ((IFG1 & UTXIFG0) == 0); U0TXBUF = byte0; // Send clocks to the DAC, this shifts LSB byte // Wait until all 8 bits have been received. delay(3); // Wait until all 8 bits have been received. P3OUT |= (DAC_CS); // Set DAC /CS high } /************************************************************/ /* Prototype - convert */ /* Description */ /* This prototype does the DAC conversion */ /************************************************************/ void dac_convert (dac_Ch, value) { byte0 = value >>8; byte1 = value >>0; byte2 = dac_Ch; P3OUT &= ~(DAC_CS); // Set DAC /CS low while ((IFG1 & UTXIFG0) == 0); U0TXBUF = byte2 ; // Send clocks to the DAC, this shifts CMD byte // Wait until all 8 bits have been received. while ((IFG1 & UTXIFG0) == 0); U0TXBUF = byte0; // Send clocks to the DAC, this shifts MSB byte // Wait until all 8 bits have been received. while ((IFG1 & UTXIFG0) == 0); U0TXBUF = byte1 ; // Send clocks to the DAC, this shifts LSB byte // Wait until all 8 bits have been received. delay(1); // Wait until all 8 bits have been received. P3OUT |= (DAC_CS); // Set DAC /CS high }
>We found an example code for MSP430F449 to feed a DAC8564 but couldn't work if we just apply this code to our MSP432P401R directly.
You would want to get msp432 examples regarding SPI to familiarize with SPI of particular chip, read documentation of both chips and write code or port code from mentioned ADC example msp430 code to msp432 _yourself_.
GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_PJ, GPIO_PIN0 | GPIO_PIN1, GPIO_PRIMARY_MODULE_FUNCTION); CS_setExternalClockSourceFrequency(32768, 0); CS_initClockSignal(CS_ACLK, CS_LFXTCLK_SELECT, CS_CLOCK_DIVIDER_1); CS_startLFXT(CS_LFXT_DRIVE0); /* Selecting P1.1 P1.2 and P1.3 in SPI mode */ GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1, GPIO_PIN1 | GPIO_PIN2 | GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION); /* Configuring SPI in 3wire master mode */ SPI_initMaster(EUSCI_A0_MODULE, &spiMasterConfig); /* Enable SPI module */ SPI_enableModule(EUSCI_A0_MODULE); /* Transmitting data to slave */ UCA0TXBUF = 0b00000000;//byte 2 to TX buffer DB23-16 while (!(MAP_SPI_getInterruptStatus(EUSCI_A0_MODULE,EUSCI_A_SPI_TRANSMIT_INTERRUPT))); //Make sure buffer is ready before loading the next byte UCA0TXBUF = 0b11111110; //byte0;//byte 0 DB 15 14 13 12 11 10 9 8 while (!(MAP_SPI_getInterruptStatus(EUSCI_A0_MODULE,EUSCI_A_SPI_TRANSMIT_INTERRUPT))); UCA0TXBUF = 0b11111111; //byte1;//byte DB0-13 7-6-5-4-3-2-1-0 while((UCA0STATW&BIT0));//wait for the the busy signal to clear
**Attention** This is a public forum