【转载】zedboard中PL_GPIO控制(8个sw、8个leds)

【原文链接】:http://blog.chinaaet.com/yyiyy/p/39024#

PL端GPIO控制,利用zed上的8个开关去控制8个led灯的亮灭,利用btn8和btn9两个PS按键控制Demo的开始和结束,并通过LD9显示程序是否开始。
VIVADO工程如图1所示,用两个AXI_GPIO IP核分别连接LED和SW,工程建立全部使用自动连接。
硬件设计图如下:
【转载】zedboard中PL_GPIO控制(8个sw、8个leds)_第1张图片

自动生成的约束文件:
【转载】zedboard中PL_GPIO控制(8个sw、8个leds)_第2张图片

SDK创建完毕后:
连接串口和JTAG,运行程序,点击btn8开始Demo,LD9灯两,尝试拨动开关,看看led灯的反应,点击btn9结束Demo,LD9灯灭。

程序:

#include 
#include "platform.h"
#include "xil_types.h"
#include "xgpio.h"
#include "xparameters.h"
#include "xgpiops.h"
#include "xil_io.h"


extern char inbyte(void);//调用inbyte接收一个字节输入函数
extern XGpioPs_Config XGpioPs_ConfigTable[XPAR_XGPIOPS_NUM_INSTANCES];
int main()
 {
   static XGpioPs psGpioInstancePtr;
   XGpioPs_Config*GpioConfigPtr;
   static XGpio GPIOInstance_Ptr0;
   static XGpio GPIOInstance_Ptr1;
   int xStatus0,xStatus1,xStatus,start,end;
   u32 Readstatus;
   int iPinNumberld9 = 7;  /*Ld9 is connected to MIO pin 7*/
   int iPinNumberbtn8 = 50;/*Btn8 is connected to MIO pin 50*/
   int iPinNumberbtn9 = 51;/*Btn9 is connected to MIO pin 51*/
   init_platform();
   print("##### Application Starts #####\n\r");
   print("\r\n");
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   //Step-1 :AXI GPIO Initialization
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   xStatus0 = XGpio_Initialize(&GPIOInstance_Ptr0,XPAR_AXI_GPIO_0_DEVICE_ID);
   if(XST_SUCCESS != xStatus0)
     print("GPIO INIT FAILED\n\r");
   xStatus1 = XGpio_Initialize(&GPIOInstance_Ptr1,XPAR_AXI_GPIO_1_DEVICE_ID);
   if(XST_SUCCESS != xStatus1)
     print("GPIO INIT FAILED\n\r");
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   //Step-2 :AXI GPIO Set the Direction
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   XGpio_SetDataDirection(&GPIOInstance_Ptr0, 1,0x00);
   XGpio_SetDataDirection(&GPIOInstance_Ptr1, 1,0xff);
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   //Step-3 :PS GPIO Intialization
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   GpioConfigPtr = XGpioPs_LookupConfig(XPAR_PS7_GPIO_0_DEVICE_ID);
   if(GpioConfigPtr == NULL)
     return XST_FAILURE;
   xStatus = XGpioPs_CfgInitialize(&psGpioInstancePtr,
       GpioConfigPtr,
       GpioConfigPtr->BaseAddr);
   if(XST_SUCCESS != xStatus)
     print(" PS GPIO INIT FAILED \n\r");
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   //Step-4 :PS GPIO pin setting to Output
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   XGpioPs_SetDirectionPin(&psGpioInstancePtr, iPinNumberld9,1);
   XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, iPinNumberld9,1);
   XGpioPs_SetDirectionPin(&psGpioInstancePtr, iPinNumberbtn8,0);
   XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, iPinNumberbtn8,1);
   XGpioPs_SetDirectionPin(&psGpioInstancePtr, iPinNumberbtn9,0);
   XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, iPinNumberbtn9,1);
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   //Step-5 :press btn8 to start and btn9 to end
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   while (1){
  start=XGpioPs_ReadPin(&psGpioInstancePtr, iPinNumberbtn8);
  if(start){
  XGpioPs_WritePin(&psGpioInstancePtr,iPinNumberld9,1);
  print("###################### Demo Starts ########################\r\n");
  break;
      }
  end=XGpioPs_ReadPin(&psGpioInstancePtr, iPinNumberbtn9);
  if(end){
  XGpioPs_WritePin(&psGpioInstancePtr,iPinNumberld9,0);
  XGpio_DiscreteWrite(&GPIOInstance_Ptr0,1,0x00);break;
    }
       }
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   //Step-6 :connect switch(8bit) to led(8bit)
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   while(1){
      Readstatus = XGpio_DiscreteRead(&GPIOInstance_Ptr1, 1) ;
      XGpio_DiscreteWrite(&GPIOInstance_Ptr0,1,Readstatus);
      end=XGpioPs_ReadPin(&psGpioInstancePtr, iPinNumberbtn9);
      if(end){
          XGpioPs_WritePin(&psGpioInstancePtr,iPinNumberld9,0);
          XGpio_DiscreteWrite(&GPIOInstance_Ptr0,1,0x00);break;
      }
   }
   print("\r\n");
   print("***********\r\n");
   print("BYE \r\n");
   print("***********\r\n");
   cleanup_platform();
   return 0;
 }

你可能感兴趣的:(ZedBoard)