• 在github上下载Digilent提供的IP核,在路径\ip\Pmods下找到PmodAD1
    Digilent提供的Pmod AD1驱动程序-编程知识网
  • 点进去\drivers\PmodAD1中examples是main主程序
/******************************************************************************/
/*                                                                            */
/* main.c -- PmodAD1 Example Project                                          */
/*                                                                            */
/******************************************************************************/
/* Author: Arthur Brown                                                       */
/* Copyright 2017, Digilent Inc.                                              */
/******************************************************************************/
/* Module Description:                                                        */
/*                                                                            */
/* This file contains code for running a demonstration of the PmodAD1 when    */
/* used with the PmodAD1 IP core. This demo initializes the PmodAD1 IP core   */
/* and then polls its sample register, printing the analog voltage last       */
/* sampled by each of the AD1's two channels over UART.                       */
/*                                                                            */
/* Messages printed by this demo can be received by using a serial terminal   */
/* configured with the appropriate Baud rate. 115200 for Zynq systems, and    */
/* whatever the AXI UARTLITE IP is configured with for MicroBlaze systems,    */
/* typically 9600 or 115200 Baud.                                             */
/*                                                                            */
/******************************************************************************/
/* Revision History:                                                          */
/*                                                                            */
/*    08/15/2017(ArtVVB):   Created                                           */
/*    02/10/2018(atangzwj): Validated for Vivado 2017.4                       */
/*                                                                            */
/******************************************************************************/#include <stdio.h>
#include "PmodAD1.h"
#include "sleep.h"
#include "xil_cache.h"
#include "xil_io.h"
#include "xil_types.h"
#include "xparameters.h"PmodAD1 myDevice;
const float ReferenceVoltage = 3.3;void DemoInitialize();
void DemoRun();
void DemoCleanup();
void EnableCaches();
void DisableCaches();int main() {DemoInitialize();DemoRun();DemoCleanup();return 0;
}void DemoInitialize() {EnableCaches();AD1_begin(&myDevice, XPAR_PMODAD1_0_AXI_LITE_SAMPLE_BASEADDR);// Wait for AD1 to finish powering onusleep(1); // 1 us (minimum)
}void DemoRun() {AD1_RawData RawData;AD1_PhysicalData PhysicalData;while (1) {AD1_GetSample(&myDevice, &RawData); // Capture raw samples// Convert raw samples into floats scaled to 0 - VDDAD1_RawToPhysical(ReferenceVoltage, RawData, &PhysicalData);printf("Input Data 1: %.02f;   ", PhysicalData[0]);printf("Input Data 2: %.02f\r\n", PhysicalData[1]);// Do this 10x per secondusleep(100000);}
}void DemoCleanup() {DisableCaches();
}void EnableCaches() {
#ifdef __MICROBLAZE__
#ifdef XPAR_MICROBLAZE_USE_ICACHEXil_ICacheEnable();
#endif
#ifdef XPAR_MICROBLAZE_USE_DCACHEXil_DCacheEnable();
#endif
#endif
}void DisableCaches() {
#ifdef __MICROBLAZE__
#ifdef XPAR_MICROBLAZE_USE_DCACHEXil_DCacheDisable();
#endif
#ifdef XPAR_MICROBLAZE_USE_ICACHEXil_ICacheDisable();
#endif
#endif
}
  • src里是PmodAD1.c和PmodAD1.h,可以对此进行魔改
/******************************************************************************/
/*                                                                            */
/* PmodAD1.c -- PmodAD1 Driver Source                                         */
/*                                                                            */
/******************************************************************************/
/* Author: Arthur Brown                                                       */
/* Copyright 2017, Digilent Inc.                                              */
/******************************************************************************/
/* Module Description:                                                        */
/*                                                                            */
/* This file contains source code for the PmodAD1 driver                      */
/*                                                                            */
/******************************************************************************/
/* Revision History:                                                          */
/*                                                                            */
/*    08/15/2017(ArtVVB):   Created                                           */
/*    02/10/2018(atangzwj): Validated for Vivado 2017.4                       */
/*                                                                            */
/******************************************************************************//***************************** Include Files *******************************/#include "PmodAD1.h"/************************** Function Definitions ***************************//* ------------------------------------------------------------ */
/*** void AD1_begin(PmodAD1 *InstancePtr, u32 BaseAddress)
**
**   Parameters:
**      InstancePtr: A PmodAD1 object to start
**      BaseAddress: The base address of the PmodAD1 AXI_LITE_SAMPLE interface
**
**   Description:
**      Initialize the PmodAD1 device - note that the AD1 IP is free-running,
**      and this function just prepares the driver for use.
*/
void AD1_begin(PmodAD1 *InstancePtr, u32 BaseAddress) {InstancePtr->BaseAddress = BaseAddress;
}/* ------------------------------------------------------------ */
/*** void AD1_GetSample(PmodAD1 *InstancePtr, AD1_RawData *RawDataPtr)
**
**   Parameters:
**      InstancePtr: A PmodAD1 object to start
**      RawDataPtr:  Pointer to an array of raw data to return values in
**
**   Return:
**      *RawDataPtr: an array of unsigned 16 bit integers to store the ADC data
**         Channel 1's 12 bit data is stored in the RawData array at index 0
**         Channel 2's 12 bit data is stored in the RawData array at index 1
**
**   Description:
**      This function captures the most recently read sample from the PmodAD1 IP
**      core.
*/
void AD1_GetSample(PmodAD1 *InstancePtr, AD1_RawData *RawDataPtr) {u32 data;data = Xil_In32(InstancePtr->BaseAddress);(*RawDataPtr)[0] = data & AD1_DATA_MASK;(*RawDataPtr)[1] = (data >> 16) & AD1_DATA_MASK;
}/* ------------------------------------------------------------ */
/*** void AD1_RawToPhysical(float ReferenceVoltage, AD1_RawData RawData,
**         AD1_PhysicalData *PhysicalDataPtr)
**
**   Parameters:
**      ReferenceVoltage: Floating point representation of the voltage available
**                        to the AD1's VCC pin.
**      RawData:          Array of raw 12 bit sample data received from the
**                        PmodAD1
**      PhysicalDataPtr:  Pointer to an array of floats to return values in
**
**   Return:
**      *PhysicalDataPtr: an array of floats to store the converted data in.
**
**   Description:
**      This function converts an AD1 sample to a human readable value.
*/
void AD1_RawToPhysical(float ReferenceVoltage, AD1_RawData RawData,AD1_PhysicalData *PhysicalDataPtr) {float conversionFactor = ReferenceVoltage / ((1 << AD1_NUM_BITS) - 1);(*PhysicalDataPtr)[0] = ((float) RawData[0]) * conversionFactor;(*PhysicalDataPtr)[1] = ((float) RawData[1]) * conversionFactor;
}
/******************************************************************************/
/*                                                                            */
/* PmodAD1.h -- PmodAD1 Driver Definitions                                    */
/*                                                                            */
/******************************************************************************/
/* Author: Arthur Brown                                                       */
/* Copyright 2017, Digilent Inc.                                              */
/******************************************************************************/
/* Module Description:                                                        */
/*                                                                            */
/* This file contains definitions for the PmodAD1 driver                      */
/*                                                                            */
/******************************************************************************/
/* Revision History:                                                          */
/*                                                                            */
/*    08/15/2017(ArtVVB):   Created                                           */
/*    02/10/2018(atangzwj): Validated for Vivado 2017.4                       */
/*                                                                            */
/******************************************************************************/#ifndef PMODAD1_H
#define PMODAD1_H/****************** Include Files ********************/#include "xil_io.h"
#include "xil_types.h"/* ------------------------------------------------------------ */
/*                  Definitions                                 */
/* ------------------------------------------------------------ */#define AD1_NUM_BITS  12
#define AD1_DATA_MASK 0xFFFtypedef struct PmodAD1 {u32 BaseAddress;
} PmodAD1;typedef u16 AD1_RawData[2];
typedef float AD1_PhysicalData[2];/* ------------------------------------------------------------ */
/*                  Procedure Declarations                      */
/* ------------------------------------------------------------ */void AD1_begin(PmodAD1 *InstancePtr, u32 SPI_Address);
void AD1_GetSample(PmodAD1 *InstancePtr, AD1_RawData *RawDataPtr);
void AD1_RawToPhysical(float ReferenceVoltage, AD1_RawData RawData,AD1_PhysicalData *PhysicalDataPtr);#endif // PMODAD1_H