100% found this document useful (1 vote)
176 views

Lab 7

The document contains C code that initializes various hardware peripherals including SPI, GPIO, and UART to communicate with an ADC and DAC. It defines functions for initializing the hardware, writing data to the DAC and amplifier via SPI, and reading data from the ADC. The main function initializes the hardware, enters a loop that receives commands over UART and performs the corresponding action like setting the DAC voltage or reading the ADC.

Uploaded by

kenerkivah
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
176 views

Lab 7

The document contains C code that initializes various hardware peripherals including SPI, GPIO, and UART to communicate with an ADC and DAC. It defines functions for initializing the hardware, writing data to the DAC and amplifier via SPI, and reading data from the ADC. The main function initializes the hardware, enters a loop that receives commands over UART and performs the corresponding action like setting the DAC voltage or reading the ADC.

Uploaded by

kenerkivah
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

/********************** Incluo de Bibliotecas *******************************/ #include "xparameters.h" /* XPAR parameters */ #include "xspi.h" /* SPI device driver */ #include "xspi_l.

h" #include "xuartlite.h" #include "xgpio.h" /************************** Definio de constantes ***************************/ #define SPI_DEVICE_ID XPAR_SPI_0_DEVICE_ID /* Tamanho do Buffer para gerar valores de teste*/ #define BUFFER_SIZE 4 /***************** Definio de um novo tipo de varivel **********************/ typedef u8 DataBuffer[BUFFER_SIZE]; /***************** Macros (Inline Functions) Definitions *********************/ /************************** Function Prototypes ******************************/ int int int int initSpiPolled(XSpi *SpiInstancePtr, u16 SpiDeviceId); writeDac(XSpi *SpiPtr, XGpio *SsPtr, int data); writeAmp(XSpi *SpiPtr, XGpio *SsPtr, int data); readAdc(XSpi *SpiPtr, XGpio *SsPtr);

/************************** Variable Definitions *****************************/ /* * The instances to support the device drivers are global such that the * are initialized to zero each time the program runs. */ static XSpi Spi; /* The instance of the SPI device */ static XGpio led; static XGpio SPI_SS; static XGpio DAC_CLEAR; static XUartLite UartLite; /* * The following variables are used to read and write to the Spi device, they * are global to avoid having large buffers on the stack. */ u8 ReadBuffer[BUFFER_SIZE]; u8 WriteBuffer[BUFFER_SIZE]; void delay (int N) { int i; for(i=0;i<N;i++); } int main(void) { int Status, n, k, i; u8 Count; u8 RecvBuffer[16]; //INICIALIZAO LED XGpio_Initialize(&led, XPAR_LEDS_8BIT_DEVICE_ID); XGpio_SetDataDirection(&led, 1, 0x0);

//INICIALIZAO CHIP SELECT XGpio_Initialize(&SPI_SS, XPAR_SPI_SS_DEVICE_ID); XGpio_SetDataDirection(&SPI_SS, 1, 0x0); //INICIALIZAO DAC_CLEAR XGpio_Initialize(&DAC_CLEAR, XPAR_DAC_CLEAR_DEVICE_ID); XGpio_SetDataDirection(&DAC_CLEAR, 1, 0x0); //INICIALIZAO RS232 XUartLite_Initialize (&UartLite, XPAR_RS232_DTE_DEVICE_ID); //INICIALIZAO SPI Status = initSpiPolled(&Spi, SPI_DEVICE_ID); xil_printf("\n\rInicio do programa\n\r"); // Apaga todos os Leds XGpio_DiscreteWrite(&led, 1, 0); // Reseta DAC XGpio_DiscreteWrite(&DAC_CLEAR, 1, 0); // Tira DAC DO RESET XGpio_DiscreteWrite(&DAC_CLEAR, 1, 1); // Desabilita todos os Chips XGpio_DiscreteWrite(&SPI_SS, 1, 58); // Valores que desabilitam. No funciona com qualquer valor // 58: Desabilita todos 58 // 26 : SPI_SS_pin<0>: = 0 (SPI_SS_B deve ficar em 1)26 // 42 : SPI_SS_pin<1>: = 0 (AMP_CS deve ficar em 1)42 // 50 : SPI_SS_pin<2>: = 0 (SF_CE0 deve ficar em 1)50 // 62 : SPI_SS_pin<3>: = 0 (FPGA_INIT_B deve ficar em 0)62 // 56 : SPI_SS_pin<4>: = 0 (DAC_CS deve ficar em 1)56 // 59 : SPI_SS_pin<5>: = 0 (AD_CONV deve ficar em 0)59 /* * Transmit the data. */ while (1) for(k=0;k<9999;k++) if (k==0) { n=XUartLite_Recv (&UartLite, RecvBuffer, 1); if ( n!=0 ) { i=RecvBuffer[0]-48; XGpio_DiscreteWrite(&led, 1, i); switch (i) { case 0: writeDac(&Spi,&SPI_SS, 0); break; case 1: writeDac(&Spi,&SPI_SS, 1240); break; case 2:

writeDac(&Spi,&SPI_SS, 2481); break; case 3: writeDac(&Spi,&SPI_SS, 3722); break; case 4: writeDac(&Spi,&SPI_SS, 4095); break; case 5: writeAmp(&Spi,&SPI_SS,0x22); break; case 6: readAdc(&Spi,&SPI_SS); break; default: writeDac(&Spi,&SPI_SS, 4095); break; } /* * Compare the data received with the da ta that was transmitted. */ xil_printf("\n\rTX\n\r"); for (Count = 0; Count < BUFFER_SIZE; Cou nt++) xil_printf("%x, ",WriteBuffer[Co unt]); xil_printf("\n\rRX\n\r"); for (Count = 0; Count < BUFFER_SIZE; Cou nt++) xil_printf("%x, ",ReadBuffer[Cou nt]); xil_printf("\n\n \r Digite a tensao:"); } } return XST_SUCCESS; } /*****************************************************************************/ int writeAmp(XSpi *SpiPtr, XGpio *SsPtr, int data) { int Status; WriteBuffer[0]=data; // Seleciona AMP XGpio_DiscreteWrite(SsPtr, 1, 42); // Transfere dados XSpi_Transfer(SpiPtr, WriteBuffer, ReadBuffer,1); if (Status != XST_SUCCESS)

{ xil_printf("XSpi_Transfer: XST_FAILURE: Code: %d\n\r", S tatus); return XST_FAILURE; } // Coloca barramento SPI em estado de espera XGpio_DiscreteWrite(SsPtr, 1, 58); return XST_SUCCESS; } /*****************************************************************************/ int readAdc(XSpi *SpiPtr, XGpio *SsPtr) { int Status; // Seleciona ADC XGpio_DiscreteWrite(SsPtr, 1, 59); // Coloca barramento SPI em estado de espera XGpio_DiscreteWrite(SsPtr, 1, 58); // Transfere dados Status=XSpi_Transfer(SpiPtr, WriteBuffer, ReadBuffer, 4); if (Status != XST_SUCCESS) { xil_printf("XSpi_Transfer: XST_FAILURE: Code: %d\n\r", S tatus); return XST_FAILURE; } return XST_SUCCESS; } /*****************************************************************************/ int writeDac(XSpi *SpiPtr, XGpio *SsPtr, int data) { int Status, dontcare4=0, dontcare8=0, address=15,command=3, dataOut=0; // Calcula valor da tenso dataOut=0; dataOut =dontcare8<<24; dataOut =command<<20; dataOut =address<<16; dataOut =data<<4; dataOut =dontcare4; WriteBuffer[3]= WriteBuffer[2]= WriteBuffer[1]= WriteBuffer[0]= (dataOut&0x000000ff); (dataOut&0x0000ff00)>>8; (dataOut&0x00ff0000)>>16; (dataOut&0xff000000)>>24;

// Seleciona DAC XGpio_DiscreteWrite(SsPtr, 1, 56); // Transfere dados XSpi_Transfer(SpiPtr, WriteBuffer, ReadBuffer, BUFFER_SIZE); if (Status != XST_SUCCESS) { xil_printf("XSpi_Transfer: XST_FAILURE: Code: %d\n\r", S tatus); return XST_FAILURE; }

// Coloca barramento SPI em estado de espera XGpio_DiscreteWrite(SsPtr, 1, 58); return XST_SUCCESS; } /******************************************************************************/ int initSpiPolled(XSpi *SpiPtr, u16 SpiDeviceId) { int Status; XSpi_Config *ConfigPtr; /* Pointer to Configuration data */ /* * Initialize the SPI driver so that it is ready to use. */ ConfigPtr = XSpi_LookupConfig(SpiDeviceId); if (ConfigPtr == NULL) { xil_printf("\n\rXSpi_LookupConfig: XST_DEVICE_NOT_FOUND\n\r"); return XST_DEVICE_NOT_FOUND; } Status = XSpi_CfgInitialize(SpiPtr, ConfigPtr, ConfigPtr->BaseAddress); if (Status != XST_SUCCESS) { xil_printf("\n\rXSpi_CfgInitialize: XST_FAILURE\n\r"); return XST_FAILURE; } /* * Perform a self-test to ensure that the hardware was built correctly. */ Status = XSpi_SelfTest(SpiPtr); if (Status != XST_SUCCESS) { xil_printf("\n\rXSpi_SelfTest: XST_FAILURE\n\r"); return XST_FAILURE; } /* * Set the Spi device as a master and in loopback mode. */ Status = XSpi_SetOptions(SpiPtr, XSP_MASTER_OPTION XSP_MANUAL_SSELECT_ OPTION); if (Status != XST_SUCCESS) { xil_printf("\n\rXSpi_SetOptions: XST_FAILURE\n\r"); return XST_FAILURE; } Status = XSpi_SetSlaveSelect(SpiPtr, 1);// No usado if (Status != XST_SUCCESS) { xil_printf("XSpi_SetSlaveSelect: XST_FAILURE\n\r"); return XST_FAILURE; } /* * Start the SPI driver so that the device is enabled. */ Status=XSpi_Start(SpiPtr);

if (Status != XST_SUCCESS) { xil_printf("XSpi_Start: XST_FAILURE\n\r"); return XST_FAILURE; } /* * Disable Global interrupt to use polled mode operation */ Status=XSpi_IntrGlobalDisable(SpiPtr); if (Status != XST_SUCCESS) { xil_printf("XSpi_IntrGlobalDisable: XST_FAILURE\n\r"); return XST_FAILURE; } return XST_SUCCESS; }

Arquivo .ucf -> NET Dac_Clear_GPIO_IO_O_pin<1> LOC = p7 SLEW = SLOW DRIVE = 6;

IOSTANDARD = LVCMOS33

Em DAC Clear(System assembly View), clicar duas vezes e configurar o canal 1 com GPIO Data Channel Width para 2. No lab7.c // Apaga todos os Leds XGpio_DiscreteWrite(&led, 1, 0); // Reseta DAC XGpio_DiscreteWrite(&DAC_CLEAR, 1, 0); // Tira DAC DO RESET e Amplificador XGpio_DiscreteWrite(&DAC_CLEAR, 1, 2);

You might also like