请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
器件型号:ADC128S102EVM 主题中讨论的其他器件: MSP430FR5969
我在 调试模式下启动 MSP430FR5969 Launchpad 使用外部源在其中一个模拟输入(通常为缓冲输入3)上设置电压(<1.5V)。 通过 SPI 总线的读取电压正确。 我让它循环几次、然后更改同一输入上的输入电压(<1.5V) 、我仍然获得旧的电压读数。 或 在 SPI 位1446上获得固定值。 我正在使用 Launchpad FR5969和插入的 ADC128S102EVM。 我无法使 ADC EVM 作为通过跳线电缆连接的单独实体工作。 以下是我的代码(我知道我在 ISR 中做了很多工作-我的代码的最终版本将不会):
#include "driverlib.h"
#define ADCVREF 2895 // ADC Vref - measured as 2.895V on the EVM
#define Port_4_nADC_CS (0x04) // P4.2. !CS for ADC128S102
volatile static int index = 0;
volatile unsigned short spiRxDataArr[8] = {0,0,0,0,0,0,0,0};
volatile unsigned char spiTxDataArr[] = {0x00, 0x08, 0x10, 0x18, 0x20,0x28, 0x30, 0x38, 0x00};
volatile static int msbLsbTX = 1;
volatile static int msbLsbRX = 1;
volatile static int firstFlag = 1;
volatile unsigned short adcmVoltage[8] = {0,0,0,0,0,0,0,0};
void main(void)
{
//Stop watchdog timer
WDT_A_hold(WDT_A_BASE);
/*
* Select Port 1
* Set Pin 0 as output
*/
GPIO_setAsOutputPin(
GPIO_PORT_P1,
GPIO_PIN0
);
/*
* Select Port 1
* Set Pin 0 to output Low.
*/
GPIO_setOutputLowOnPin(
GPIO_PORT_P1,
GPIO_PIN0
);
//Set DCO frequency to 16 MHz DCO setting
CS_setDCOFreq(CS_DCORSEL_1,CS_DCOFSEL_4);
//Select DCO as the clock source for SMCLK with no frequency divider
CS_initClockSignal(CS_SMCLK,CS_DCOCLK_SELECT,CS_CLOCK_DIVIDER_1);
/*
* Select Port 2
* Set Pin 2 to input Secondary Module Function, (UCB0CLK).
*/
GPIO_setAsPeripheralModuleFunctionInputPin(
GPIO_PORT_P2,
GPIO_PIN2,
GPIO_SECONDARY_MODULE_FUNCTION
);
/*
* Select Port 1
* Set Pin 6, 7 to input Secondary Module Function, (UCB0TXD/UCB0SIMO, UCB0RXD/UCB0SOMI).
*/
GPIO_setAsPeripheralModuleFunctionInputPin(
GPIO_PORT_P1,
GPIO_PIN6 + GPIO_PIN7,
GPIO_SECONDARY_MODULE_FUNCTION
);
/*
* Disable the GPIO power-on default high-impedance mode to activate
* previously configured port settings
*/
PMM_unlockLPM5();
P4DIR |= 0x04; // Configure P4.2 (/ADC_CS) as an output.
//Initialize Master
EUSCI_B_SPI_initMasterParam param1 = {0};
param1.selectClockSource = EUSCI_B_SPI_CLOCKSOURCE_SMCLK;
param1.clockSourceFrequency = CS_getSMCLK();
param1.desiredSpiClock = 8000000; // 8 MHz clock (min requirement for adc128s102)
param1.msbFirst = EUSCI_B_SPI_MSB_FIRST;
param1.clockPhase = EUSCI_B_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT;
param1.clockPolarity = EUSCI_B_SPI_CLOCKPOLARITY_INACTIVITY_HIGH;
param1.spiMode = EUSCI_B_SPI_3PIN;
EUSCI_B_SPI_initMaster(EUSCI_B0_BASE, ¶m1);
//Enable SPI module
EUSCI_B_SPI_enable(EUSCI_B0_BASE);
EUSCI_B_SPI_clearInterrupt(EUSCI_B0_BASE, EUSCI_B_SPI_RECEIVE_INTERRUPT);
// Enable USCI_B0 RX interrupt
EUSCI_B_SPI_enableInterrupt(EUSCI_B0_BASE, EUSCI_B_SPI_RECEIVE_INTERRUPT);
//Wait for slave to initialize
__delay_cycles(100);
P4OUT |= Port_4_nADC_CS; // /ADC_CS frames the SPI transmission. Initialize here.
__delay_cycles(2000);
P4OUT &= ~Port_4_nADC_CS;
//USCI_B0 TX buffer ready?
while (!EUSCI_B_SPI_getInterruptStatus(EUSCI_B0_BASE, EUSCI_B_SPI_TRANSMIT_INTERRUPT)) ;
//Transmit Data to slave (MSB) - Get the process started
EUSCI_B_SPI_transmitData(EUSCI_B0_BASE, spiTxDataArr[index]);
msbLsbTX = 0;
__bis_SR_register(LPM0_bits + GIE); // CPU off, enable interrupts
__no_operation(); // Remain in LPM0 */
}
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=USCI_B0_VECTOR
__interrupt
#elif defined(__GNUC__)
__attribute__((interrupt(USCI_B0_VECTOR)))
#endif
void USCI_B0_ISR (void)
{
volatile unsigned char temp = 0x00; // need to get rid of this - easy to see data in debug
switch (__even_in_range(UCB0IV, USCI_SPI_UCTXIFG))
{
// first 4 bits are zeros, then 12 bit data
case USCI_SPI_UCRXIFG: // UCRXIFG
//USCI_B0 TX buffer ready?
while (!EUSCI_B_SPI_getInterruptStatus(EUSCI_B0_BASE, EUSCI_B_SPI_TRANSMIT_INTERRUPT));
if (firstFlag){ // skip collecting the first 16 bits - garbage
if(!msbLsbRX){
firstFlag = 0;
msbLsbRX = 1;
index++;
}
else
msbLsbRX = 0;
}
else{
if(msbLsbRX){//MSB
temp = EUSCI_B_SPI_receiveData(EUSCI_B0_BASE);
spiRxDataArr[index-1] = spiRxDataArr[index-1] | temp << 8;
msbLsbRX = 0;
}
else{//LSB
temp = EUSCI_B_SPI_receiveData(EUSCI_B0_BASE);
spiRxDataArr[index-1] = spiRxDataArr[index-1] | temp;
// SPI bits * Vref (2895 mV)/4096
adcmVoltage[index-1] = (unsigned short) (((unsigned long)spiRxDataArr[index-1] * ADCVREF)>>12); // divide by 4096
msbLsbRX = 1;
index++;
if (index > 8)
index = 1;
}
}
//Send next value
if(!msbLsbTX){ //LSB
EUSCI_B_SPI_transmitData(EUSCI_B0_BASE,0x00);
msbLsbTX = 1;
P4OUT |= Port_4_nADC_CS;
}
else{ //MSB
P4OUT &= ~Port_4_nADC_CS;
EUSCI_B_SPI_transmitData(EUSCI_B0_BASE,spiTxDataArr[index]);
msbLsbTX = 0;
}
break;
default:
break;
}
}