XADC实现串口循环打印

  1. 打开VIVADO,建一个工程,板卡选择ZC702。

  2. 点击左边栏的IP INTERGRETOR-->Create Block Design。

  1. 右边Diagram-->加号(ADD IP)。


  1. 添加ZYNQ7 Processing System和XADC Wizard两个IP核。
  1. 点击Run Block Automation以及Run Connection Automation,IP核将自己连接好。
  1. 依次点击综合,实现,生成比特流。

  2. File-->Export Hardware和File-->Launch SDK。

  3. 在打开的SDK下,新建一个Application Project。Project name:Hello,BSP:Create New-->next-->选择Hello WorldàFinish。



  1. 板卡连接电脑,查询串口号,打开串口接收工具,设置如下(注意不要勾选RTS/CTS)。
  1. SDK下Xilinx Tools-->Program FPGA。

  2. Project Explorer-->右键HELLO文件夹-->Build Project。

  3. Project Explorer-->右键HELLO文件夹-->RUNàLaunch on hardware。

  4. 如果串口工具打印出了“HELLO WORLD”则说明板卡连接没有问题。

  5. 按照步骤8重新创建一个Application Project名字为XADC。

  6. ~\Xilinx\SDK\2017.2\data\embeddedsw\XilinxProcessorIPLib\drivers\sysmon_v7_3\examples下找到PL端XADC的驱动例程xsysmon_polled_printf_example.c将其复制到你的路径下,我的为C:\FPGA_wkp\DuYu\project_1\project_1.sdk\xadc\src。

  7. 这是如果一切正常就能够打印出一次板卡传感器读到的信息,并在串口打印。

  8. 寻找一个计时器能够在串口每秒钟打印出一次传感器信息,找到了一个叫XSCU的TIMER查找相应的例程,在xsysmon_polled_printf_example.c中加入相应的定时器完成每秒钟打印一次的操作。

/***************************** Include Files ********************************/

#include "xsysmon.h"
#include "xparameters.h"
#include "xstatus.h"
#include "stdio.h"
#include "xil_printf.h"
/******************************MY CODE***************************************/
#include "xscutimer.h"

/************************** Constant Definitions ****************************/

/*
 * The following constants map to the XPAR parameters created in the
 * xparameters.h file. They are defined here such that a user can easily
 * change all the needed parameters in one place.
 */
#define SYSMON_DEVICE_ID    XPAR_SYSMON_0_DEVICE_ID


/**************************** Type Definitions ******************************/


/***************** Macros (Inline Functions) Definitions ********************/

#define printf xil_printf /* Small foot-print printf function */

/************************** Function Prototypes *****************************/

static int SysMonPolledPrintfExample(u16 SysMonDeviceId);
static int SysMonFractionToInt(float FloatNum);

/************************** Variable Definitions ****************************/

static XSysMon SysMonInst;      /* System Monitor driver instance */

/****************************************************************************/
/**
*
* Main function that invokes the polled example in this file.
*
* @param    None.
*
* @return
*       - XST_SUCCESS if the example has completed successfully.
*       - XST_FAILURE if the example has failed.
*
* @note     None.
*
*****************************************************************************/
int main(void)
{
        int Status;
        /************************** Function Prototypes *****************************/
        int Status1;
        int timer_value;
        int counter = 0;
        XScuTimer my_Timer;
        XScuTimer_Config *Timer_Config;
        Timer_Config = XScuTimer_LookupConfig(XPAR_PS7_SCUTIMER_0_DEVICE_ID);
        Status1 = XScuTimer_CfgInitialize(&my_Timer,Timer_Config,Timer_Config->BaseAddr);
        XScuTimer_LoadTimer(&my_Timer,XPAR_PS7_CORTEXA9_0_CPU_CLK_FREQ_HZ/2);
        XScuTimer_Start(&my_Timer);

        /*
         * Run the SysMonitor polled example, specify the Device ID that is
         * generated in xparameters.h.
         */
        while(1)
        {
            timer_value = XScuTimer_GetCounterValue(&my_Timer);
            if (timer_value==0)
            {
                Status = SysMonPolledPrintfExample(SYSMON_DEVICE_ID);
                xil_printf("Sysmon polled printf Example success %d\r\n",counter++);
                XScuTimer_RestartTimer(&my_Timer);
            }
        }
        return 0;
}



/****************************************************************************/
/**
*
* This function runs a test on the System Monitor/ADC device using the
* driver APIs.
* This function does the following tasks:
*   - Initiate the System Monitor device driver instance
*   - Run self-test on the device
*   - Setup the sequence registers to continuously monitor on-chip
*   temperature, VCCINT and VCCAUX
*   - Setup configuration registers to start the sequence
*   - Read the latest on-chip temperature, VCCINT and VCCAUX
*
* @param    SysMonDeviceId is the XPAR_<SYSMON_ADC_instance>_DEVICE_ID value
*       from xparameters.h.
*
* @return
*       - XST_SUCCESS if the example has completed successfully.
*       - XST_FAILURE if the example has failed.
*
* @note     None
*
****************************************************************************/
int SysMonPolledPrintfExample(u16 SysMonDeviceId)
{
    int Status;
    XSysMon_Config *ConfigPtr;
    u32 TempRawData;
    u32 VccAuxRawData;
    u32 VccIntRawData;
    float TempData;
    float VccAuxData;
    float VccIntData;
    float MaxData;
    float MinData;
    XSysMon *SysMonInstPtr = &SysMonInst;

    printf("\r\nEntering the SysMon Polled Example. \r\n");

    /*
     * Initialize the SysMon driver.
     */
    ConfigPtr = XSysMon_LookupConfig(SysMonDeviceId);
    if (ConfigPtr == NULL) {
        return XST_FAILURE;
    }
    XSysMon_CfgInitialize(SysMonInstPtr, ConfigPtr,
                ConfigPtr->BaseAddress);

    /*
     * Self Test the System Monitor/ADC device
     */
    Status = XSysMon_SelfTest(SysMonInstPtr);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }

    /*
     * Disable the Channel Sequencer before configuring the Sequence
     * registers.
     */
    XSysMon_SetSequencerMode(SysMonInstPtr, XSM_SEQ_MODE_SAFE);


    /*
     * Disable all the alarms in the Configuration Register 1.
     */
    XSysMon_SetAlarmEnables(SysMonInstPtr, 0x0);


    /*
     * Setup the Averaging to be done for the channels in the
     * Configuration 0 register as 16 samples:
     */
    XSysMon_SetAvg(SysMonInstPtr, XSM_AVG_16_SAMPLES);

    /*
     * Setup the Sequence register for 1st Auxiliary channel
     * Setting is:
     *  - Add acquisition time by 6 ADCCLK cycles.
     *  - Bipolar Mode
     *
     * Setup the Sequence register for 16th Auxiliary channel
     * Setting is:
     *  - Add acquisition time by 6 ADCCLK cycles.
     *  - Unipolar Mode
     */
    Status = XSysMon_SetSeqInputMode(SysMonInstPtr, XSM_SEQ_CH_AUX00);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }

    Status = XSysMon_SetSeqAcqTime(SysMonInstPtr, XSM_SEQ_CH_AUX15 |
                        XSM_SEQ_CH_AUX00);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }


    /*
     * Enable the averaging on the following channels in the Sequencer
     * registers:
     *  - On-chip Temperature, VCCINT/VCCAUX  supply sensors
     *  - 1st/16th Auxiliary Channels
      * - Calibration Channel
     */
    Status =  XSysMon_SetSeqAvgEnables(SysMonInstPtr, XSM_SEQ_CH_TEMP |
                        XSM_SEQ_CH_VCCINT |
                        XSM_SEQ_CH_VCCAUX |
                        XSM_SEQ_CH_AUX00 |
                        XSM_SEQ_CH_AUX15 |
                        XSM_SEQ_CH_CALIB);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }

    /*
     * Enable the following channels in the Sequencer registers:
     *  - On-chip Temperature, VCCINT/VCCAUX supply sensors
     *  - 1st/16th Auxiliary Channel
     *  - Calibration Channel
     */
    Status =  XSysMon_SetSeqChEnables(SysMonInstPtr, XSM_SEQ_CH_TEMP |
                        XSM_SEQ_CH_VCCINT |
                        XSM_SEQ_CH_VCCAUX |
                        XSM_SEQ_CH_AUX00 |
                        XSM_SEQ_CH_AUX15 |
                        XSM_SEQ_CH_CALIB);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }


    /*
     * Set the ADCCLK frequency equal to 1/32 of System clock for the System
     * Monitor/ADC in the Configuration Register 2.
     */
    XSysMon_SetAdcClkDivisor(SysMonInstPtr, 32);


    /*
     * Set the Calibration enables.
     */
    XSysMon_SetCalibEnables(SysMonInstPtr,
                XSM_CFR1_CAL_PS_GAIN_OFFSET_MASK |
                XSM_CFR1_CAL_ADC_GAIN_OFFSET_MASK);

    /*
     * Enable the Channel Sequencer in continuous sequencer cycling mode.
     */
    XSysMon_SetSequencerMode(SysMonInstPtr, XSM_SEQ_MODE_CONTINPASS);

    /*
     * Wait till the End of Sequence occurs
     */
    XSysMon_GetStatus(SysMonInstPtr); /* Clear the old status */
    while ((XSysMon_GetStatus(SysMonInstPtr) & XSM_SR_EOS_MASK) !=
            XSM_SR_EOS_MASK);

    /*
     * Read the on-chip Temperature Data (Current/Maximum/Minimum)
     * from the ADC data registers.
     */
    TempRawData = XSysMon_GetAdcData(SysMonInstPtr, XSM_CH_TEMP);
    TempData = XSysMon_RawToTemperature(TempRawData);
    printf("\r\nThe Current Temperature is %0d.%03d Centigrades.\r\n",
                (int)(TempData), SysMonFractionToInt(TempData));


    TempRawData = XSysMon_GetMinMaxMeasurement(SysMonInstPtr, XSM_MAX_TEMP);
    MaxData = XSysMon_RawToTemperature(TempRawData);
    printf("The Maximum Temperature is %0d.%03d Centigrades. \r\n",
                (int)(MaxData), SysMonFractionToInt(MaxData));

    TempRawData = XSysMon_GetMinMaxMeasurement(SysMonInstPtr, XSM_MIN_TEMP);
    MinData = XSysMon_RawToTemperature(TempRawData);
    printf("The Minimum Temperature is %0d.%03d Centigrades. \r\n",
                (int)(MinData), SysMonFractionToInt(MinData));

    /*
     * Read the VccInt Votage Data (Current/Maximum/Minimum) from the
     * ADC data registers.
     */
    VccIntRawData = XSysMon_GetAdcData(SysMonInstPtr, XSM_CH_VCCINT);
    VccIntData = XSysMon_RawToVoltage(VccIntRawData);
    printf("\r\nThe Current VCCINT is %0d.%03d Volts. \r\n",
            (int)(VccIntData), SysMonFractionToInt(VccIntData));

    VccIntRawData = XSysMon_GetMinMaxMeasurement(SysMonInstPtr,
                            XSM_MAX_VCCINT);
    MaxData = XSysMon_RawToVoltage(VccIntRawData);
    printf("The Maximum VCCINT is %0d.%03d Volts. \r\n",
            (int)(MaxData), SysMonFractionToInt(MaxData));

    VccIntRawData = XSysMon_GetMinMaxMeasurement(SysMonInstPtr,
                            XSM_MIN_VCCINT);
    MinData = XSysMon_RawToVoltage(VccIntRawData);
    printf("The Minimum VCCINT is %0d.%03d Volts. \r\n",
            (int)(MinData), SysMonFractionToInt(MinData));

    /*
     * Read the VccAux Votage Data (Current/Maximum/Minimum) from the
     * ADC data registers.
     */
    VccAuxRawData = XSysMon_GetAdcData(SysMonInstPtr, XSM_CH_VCCAUX);
    VccAuxData = XSysMon_RawToVoltage(VccAuxRawData);
    printf("\r\nThe Current VCCAUX is %0d.%03d Volts. \r\n",
            (int)(VccAuxData), SysMonFractionToInt(VccAuxData));

    VccAuxRawData = XSysMon_GetMinMaxMeasurement(SysMonInstPtr,
                            XSM_MAX_VCCAUX);
    MaxData = XSysMon_RawToVoltage(VccAuxRawData);
    printf("The Maximum VCCAUX is %0d.%03d Volts. \r\n",
                (int)(MaxData), SysMonFractionToInt(MaxData));


    VccAuxRawData = XSysMon_GetMinMaxMeasurement(SysMonInstPtr,
                            XSM_MIN_VCCAUX);
    MinData = XSysMon_RawToVoltage(VccAuxRawData);
    printf("The Minimum VCCAUX is %0d.%03d Volts. \r\n\r\n",
                (int)(MinData), SysMonFractionToInt(MinData));

    printf("Exiting the SysMon Polled Example. \r\n");

    return XST_SUCCESS;
}


/****************************************************************************/
/**
*
* This function converts the fraction part of the given floating point number
* (after the decimal point)to an integer.
*
* @param    FloatNum is the floating point number.
*
* @return   Integer number to a precision of 3 digits.
*
* @note
* This function is used in the printing of floating point data to a STDIO device
* using the xil_printf function. The xil_printf is a very small foot-print
* printf function and does not support the printing of floating point numbers.
*
*****************************************************************************/
int SysMonFractionToInt(float FloatNum)
{
    float Temp;

    Temp = FloatNum;
    if (FloatNum < 0) {
        Temp = -(FloatNum);
    }

    return( ((int)((Temp -(float)((int)Temp)) * (1000.0f))));
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,456评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,370评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,337评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,583评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,596评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,572评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,936评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,595评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,850评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,601评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,685评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,371评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,951评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,934评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,167评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,636评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,411评论 2 342

推荐阅读更多精彩内容