Thursday, May 28, 2015

May 28 - Code for turning the voltage from pmod on and off

We modified the tutorial one code to not only blink the LEDs but to turn the voltage from the pins of PMOD JA1 on and off. The following code is what we used:


/* LED_test.c
 *
 *  Created on:  13 June 2013
 *      Author:  Ross Elliot
 *     Version:  1.1
 */

/********************************************************************************************
* VERSION HISTORY
********************************************************************************************
* v1.1 - 27 January 2014
*  GPIO_DEVICE_ID definition updated to reflect new naming conventions in Vivado 2013.3
*  onwards.
*
* v1.0 - 13 June 2013
*  First version created.
*******************************************************************************************/


/********************************************************************************************
 * This file contains an example of using the GPIO driver to provide communication between
 * the Zynq Processing System (PS) and the AXI GPIO block implemented in the Zynq Programmable
 * Logic (PL). The AXI GPIO is connected to the LEDs on the ZedBoard.
 *
 * The provided code demonstrates how to use the GPIO driver to write to the memory mapped AXI
 * GPIO block, which in turn controls the LEDs.
 ********************************************************************************************/


/* Include Files */
#include "xparameters.h"
#include "xgpio.h"
#include "xstatus.h"
#include "xil_printf.h"


/* Definitions */
#define GPIO_DEVICE_ID  XPAR_AXI_GPIO_0_DEVICE_ID /* GPIO device that LEDs are connected to */
#define LED 0xC3         /* Initial LED value - XX0000XX */
#define PMOD 0xC3         /* Initial LED value - XX0000XX */
#define LED_DELAY 10000000       /* Software delay length */
#define LED_CHANNEL 1        /* GPIO port for LEDs */
#define PMOD_CHANNEL 1        /* GPIO port for PMODs */
#define printf xil_printf       /* smaller, optimised printf */
#define PMOD_JA1_DEVICE_ID  XPAR_AXI_GPIO_1_DEVICE_ID


XGpio Gpio, GpioP;           /* GPIO Device driver instance */


int LEDOutputExample(void)
{


 volatile int Delay;
 int Status;
 int led = LED; /* Hold current LED value. Initialise to LED definition */
 int pmod = PMOD;


  /* GPIO driver initialisation */
  Status = XGpio_Initialize(&Gpio, GPIO_DEVICE_ID);
  if (Status != XST_SUCCESS) {
   return XST_FAILURE;
  }
  Status = XGpio_Initialize(&GpioP, PMOD_JA1_DEVICE_ID);
   if (Status != XST_SUCCESS) {
    return XST_FAILURE;
  }


  /*Set the direction for the LEDs to output. */
  XGpio_SetDataDirection(&Gpio, LED_CHANNEL, 0x00);


  /*Set the direction for the LEDs to output. */
    XGpio_SetDataDirection(&GpioP, PMOD_CHANNEL, 0x00);


  /* Loop forever blinking the LED. */
   while (1) {
    /* Write output to the LEDs. */
    XGpio_DiscreteWrite(&Gpio, LED_CHANNEL, led);


    /* Flip LEDs. */
    led = ~led;


    /* Write output to the LEDs. */
    XGpio_DiscreteWrite(&GpioP, PMOD_CHANNEL, pmod);


    /* Flip LEDs. */
    pmod = ~pmod;


    /* Wait a small amount of time so that the LED blinking is visible. */
    for (Delay = 0; Delay < LED_DELAY; Delay++);
   }


  return XST_SUCCESS; /* Should be unreachable */
}


/* Main function. */
int main(void){


 int Status;


 /* Execute the LED output. */
 Status = LEDOutputExample();
 if (Status != XST_SUCCESS) {
  xil_printf("GPIO output to the LEDs failed!\r\n");
 }


 return 0;
}


This code is different from the code posted in the blog on May 27th because we changed these two lines:
#define PMOD 0xC3         /* Initial LED value - XX0000XX */
#define PMOD_CHANNEL 1        /* GPIO port for PMODs */
The following link explains why we have to use channel 1 even though channel one is already defined for the LEDs. It states that "The hardware must be built for dual channels if this function is used with any channel other than 1. If it is not, this function will assert."
http://stackoverflow.com/questions/16946270/xgpio-setdatadirection-xilinx-c-developer









No comments:

Post a Comment