Advertisement
milanmetal

[Zybo] Xilinx Hello World in C

Mar 12th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.92 KB | None | 0 0
  1. /*****************************************************
  2. Getting Started Guide for Zybo
  3.  
  4. This demo displays the status of the switches on the
  5. LEDs and prints a message to the serial communication
  6. when a button is pressed.
  7.  
  8. Terminal Settings:
  9.    -Baud: 115200
  10.    -Data bits: 8
  11.    -Parity: no
  12.    -Stop bits: 1
  13.  
  14. 1/6/14: Created by MarshallW
  15. ****************************************************/
  16.  
  17. #include <stdio.h>
  18. #include "platform.h"
  19. #include <xgpio.h>
  20. #include "xparameters.h"
  21. #include "sleep.h"
  22.  
  23. int main()
  24. {
  25.    XGpio input, output;
  26.    int button_data = 0;
  27.    int switch_data = 0;
  28.  
  29.    XGpio_Initialize(&input, XPAR_AXI_GPIO_0_DEVICE_ID); //initialize input XGpio variable
  30.    XGpio_Initialize(&output, XPAR_AXI_GPIO_1_DEVICE_ID);    //initialize output XGpio variable
  31.  
  32.    XGpio_SetDataDirection(&input, 1, 0xF);          //set first channel tristate buffer to input
  33.    XGpio_SetDataDirection(&input, 2, 0xF);          //set second channel tristate buffer to input
  34.  
  35.    XGpio_SetDataDirection(&output, 1, 0x0);     //set first channel tristate buffer to output
  36.  
  37.    init_platform();
  38.  
  39.    while(1){
  40.       switch_data = XGpio_DiscreteRead(&input, 2);  //get switch data
  41.  
  42.       XGpio_DiscreteWrite(&output, 1, switch_data); //write switch data to the LEDs
  43.  
  44.       button_data = XGpio_DiscreteRead(&input, 1);  //get button data
  45.  
  46.       //print message dependent on whether one or more buttons are pressed
  47.       if(button_data == 0b0000){} //do nothing
  48.  
  49.       else if(button_data == 0b0001)
  50.          xil_printf("button 0 pressed\n\r");
  51.  
  52.       else if(button_data == 0b0010)
  53.          xil_printf("button 1 pressed\n\r");
  54.  
  55.       else if(button_data == 0b0100)
  56.          xil_printf("button 2 pressed\n\r");
  57.  
  58.       else if(button_data == 0b1000)
  59.          xil_printf("button 3 pressed\n\r");
  60.  
  61.       else
  62.          xil_printf("multiple buttons pressed\n\r");
  63.  
  64.       usleep(200000);           //delay
  65.  
  66.    }
  67.    cleanup_platform();
  68.    return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement