rt1052 读取ad7616程序
时间: 2023-12-04 11:05:01 浏览: 180
以下是使用RT1052读取AD7616的示例程序:
```c
#include "board.h"
#include "fsl_debug_console.h"
#include "fsl_spi.h"
#include "pin_mux.h"
#include "clock_config.h"
#define EXAMPLE_SPI_MASTER SPI4
#define EXAMPLE_SPI_MASTER_IRQ FLEXSPI_IRQn
#define EXAMPLE_SPI_MASTER_CLK_FREQ CLOCK_GetIpFreq(kCLOCK_Flexspi4)
#define EXAMPLE_SPI_SLAVE_SELECT_INDEX AD7616_SPI_CS
#define EXAMPLE_SPI_MASTER_SOURCE_CLOCK kCLOCK_Flexspi4
#define BUFFER_SIZE 8U
/*******************************************************************************
* Variables
******************************************************************************/
spi_master_handle_t g_masterHandle;
spi_transfer_t g_masterXfer;
uint8_t g_masterRxData[BUFFER_SIZE] = {0};
/*******************************************************************************
* Code
******************************************************************************/
int main(void)
{
uint32_t sourceClock = 0U;
uint32_t i = 0U;
BOARD_InitPins();
BOARD_BootClockRUN();
BOARD_InitDebugConsole();
/* attach 12 MHz clock to FLEXCOMM4 (SPI4) */
CLOCK_AttachClk(kFRO12M_to_FLEXCOMM4);
PRINTF("\r\nSPI board to board polling example started!\r\n");
/* Master config */
spi_master_config_t masterConfig;
memset(&masterConfig, 0, sizeof(masterConfig));
masterConfig.baudRate_Bps = 500000U;
masterConfig.enableLoopback = false;
masterConfig.phase = kSPI_ClockPhaseFirstEdge;
masterConfig.polarity = kSPI_ClockPolarityActiveHigh;
masterConfig.direction = kSPI_MsbFirst;
masterConfig.dataMode = kSPI_8BitMode;
masterConfig.txWatermark = kSPI_TxFifoOneHalfEmpty;
masterConfig.rxWatermark = kSPI_RxFifoOneHalfFull;
masterConfig.pinMode = kSPI_PinModeNormal;
masterConfig.outputMode = kSPI_SlaveSelectAutomaticOutput;
masterConfig.inputDelayInNanoSec = 1000U;
masterConfig.transferDelayInNanoSec = 1000U;
masterConfig.csHoldTimeInNanoSec = 1000U;
masterConfig.csSetupTimeInNanoSec = 1000U;
sourceClock = EXAMPLE_SPI_MASTER_CLK_FREQ;
SPI_MasterInit(EXAMPLE_SPI_MASTER, &masterConfig, sourceClock);
NVIC_SetPriority(EXAMPLE_SPI_MASTER_IRQ, 2U);
/* Slave select */
uint8_t slaveSelectValue = 0x01U << EXAMPLE_SPI_SLAVE_SELECT_INDEX;
/* Init transfer structure */
g_masterXfer.txData = NULL;
g_masterXfer.rxData = g_masterRxData;
g_masterXfer.dataSize = BUFFER_SIZE;
g_masterXfer.configFlags = kSPI_FrameAssert;
while (1)
{
/* Start master transfer */
SPI_MasterTransferBlocking(EXAMPLE_SPI_MASTER, &g_masterXfer);
/* Print master received data */
PRINTF("Master received:\r\n");
for (i = 0U; i < BUFFER_SIZE; i++)
{
PRINTF(" 0x%x\r\n", g_masterRxData[i]);
}
/* Wait for a while */
for (i = 0U; i < 10000000U; i++)
{
__asm("NOP");
}
}
}
```
此示例程序使用SPI4作为主设备,AD7616的SPI片选引脚连接到了GPIO_AD_B1_13。在主设备初始化后,使用`SPI_MasterTransferBlocking`函数进行数据传输。在接收到数据后,使用PRINTF打印数据。
阅读全文