Estoy utilizando msp-exp432p4111
devalopment board y trato de usar DAC161S997
con él. El objetivo principal es crear un 4-20mA
según el valor de un sensor anolog. Pero primero estoy tratando de controlar la salida de DAC. El protocolo de comunicación es SPI
. He encontrado un código de ejemplo para mi DAC pero es para un chip msp430, así que lo he cambiado para mi chip. Según mi código de depurador funciona bien, me refiero a que el envío de datos con spi está compilado, pero no sucede nada en la salida (2mA siempre). Aquí está mi código y conexiones:
Conexiones
Hoja de datos
void main(void)
{
WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD; // Stop watchdog timer
USCIB0_setupMasterSPI();
__enable_irq();
// Configure the DAC161S997
Setup_DAC161 (DAC161_STD_ERR_CONFIG_MASKED, 3370, 21800);
DAC_Error_Processing = 0;
dacValue_mA = 4;
dacValue = dacValue_mA * 2730;
DAC161_Set_Out_Value_uA (dacValue);
while(1)
{
// In case we extend the time between samples in the future, we will still need to keep the DAC
// from timing out by sending a NOP command every time the loop is processed.
DAC161_Nop();
// Did the DAC161 indicate an error?
if (DAC_Error_Processing)
{
// Perform DAC ERROR Processing Here
DAC_Error_Processing = 0;
dacStatus = DAC161_Read_Status(); // Read Status to clear error
}
}
}
const eUSCI_SPI_MasterConfig spiMasterConfig =
{
EUSCI_B_SPI_CLOCKSOURCE_SMCLK, // SMCLK Clock Source
3000000, // SMCLK = DCO = 3MHZ
500000, // SPICLK = 500khz
EUSCI_B_SPI_MSB_FIRST, // MSB First
EUSCI_B_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT, // Phase
EUSCI_B_SPI_CLOCKPOLARITY_INACTIVITY_HIGH, // High polarity
EUSCI_B_SPI_3PIN // 3Wire SPI Mode
};
void USCIB0_setupMasterSPI(void)
{
GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1, GPIO_PIN5 | GPIO_PIN6 | GPIO_PIN7, GPIO_PRIMARY_MODULE_FUNCTION);
GPIO_setAsOutputPin(GPIO_PORT_P4, GPIO_PIN0);
SPI_initMaster(EUSCI_B0_BASE, &spiMasterConfig);
}
void USCIB0_SPI_write (unsigned char *outData, unsigned char *inData, unsigned char length)
{
unsigned char i;
// CS Active Low
P4->OUT ^= BIT0;
for(i=0;i<100;i++); // Pause a bit
for (i=0; i<length; i++)
{
inData[i] = USCIB0_SPI_writeByte (outData[i]);
}
while (!(EUSCI_B0->IFG & EUSCI_B_IFG_TXIFG)); // USCI_B0 TX buffer ready?
for(i=0;i<100;i++); // Pause a bit
P4->OUT |= BIT0;
}
unsigned char USCIB0_SPI_writeByte (unsigned char data)
{
while (!(SPI_getInterruptStatus(EUSCI_B0_BASE,EUSCI_B_SPI_TRANSMIT_INTERRUPT)));
SPI_clearInterruptFlag(EUSCI_B0_BASE, SPI_getEnabledInterruptStatus(EUSCI_B0_BASE));
SPI_transmitData(EUSCI_B0_BASE, data);
return SPI_receiveData(EUSCI_B0_BASE);
}
void DAC161_Set_Out_Value (unsigned short value)
{
DAC161_Write_Regs (&value, DAC161_DACCODE_REG, 2);
}
void DAC161_Nop (void)
{
unsigned short deadCode = 0xdead;
DAC161_Write_Regs (&deadCode, DAC161_NOP_REG, 2);
}
void Setup_DAC161 (unsigned short errConfig, unsigned short errLow_uA, unsigned short errHigh_uA)
{
unsigned short errValue;
DAC161_Write_Regs (&errConfig, DAC161_ERR_CONFIG_REG, 2);
errValue = DAC161_CONVERT_ERR_VALUE(errLow_uA) & 0x7f00;;
DAC161_Write_Regs (&errValue, DAC161_ERR_LOW_REG, 2);
errValue = DAC161_CONVERT_ERR_VALUE(errHigh_uA) & 0xff00;;
if (errValue < 0x80)
errValue = 0x80;
DAC161_Write_Regs (&errValue, DAC161_ERR_HIGH_REG, 2);
}