Monday, June 6, 2016

Own LED Program

We made our own hardware and software design in order to have the LEDs light up one by one consecutively. We used the 1st Tutorial as a guide to build the hardware with the processing system and an LED GPIO, and then modified the code to change the way the LEDs lit up.


/********************************************************************************************
*This software file will demonstrate the use of a GPIO (General Purpose Input Output) and the
*connection to a Zynq Processing System in order to carry out some function.
*The relatively simple software application that we have created was designed to control the
*LEDs on the ZedBoard. When working, the LEDs will light up one by one consecutively from one
*end to the other.
*******************************************************************************************/


/* 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 0x01                                    /* Initial LED value - X0000000 */
#define LED_DELAY 10000000                            /* Software delay length */
#define LED_CHANNEL 1                                /* GPIO port for LEDs */
#define printf xil_printf                            /* smaller, optimised printf */

XGpio Gpio;                                            /* GPIO Device driver instance */

int EmilyLEDProject(void)
{

    //volatile int delay;
    int Status;
    int led = LED; /* Hold current LED value. Initialize to LED definition */

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

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


        led = 0x01;
        int i;
        for (i=1;i<8;i++){
            XGpio_DiscreteWrite(&Gpio,LED_CHANNEL,led);
            delay(LED_DELAY);
            led = (int)(1<<i);
            XGpio_DiscreteWrite(&Gpio,LED_CHANNEL,led);
            delay(LED_DELAY);
        }

        return XST_SUCCESS; /* Should be unreachable */
}

void delay(int nStopValue)
/**
* \brief       Loop for nStopValue iterations to provide a delay.
* \par         Details
*              It is commonly used with the constant 'ABOUT_ONE_SECOND' defined in maximPMOD.h for
*              setting approximate delays
*
* \param[in]   nStopValue    - number of iterations to loop
*
* \retval      None
*/
{
    int i=0;
    int a=0;

    for(i=0;i<nStopValue;i++)
    {
        a=i;
    }
}
/* Main function. */
int main(void){

    int Status;

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

    return 0;
}





/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Most of the code was the same as the 1st Tutorial, but we needed to add the delay function from a previous post in order to properly delay the time after each LED lit up. The delay code we used was taken from Mike's previous blog post, and looked like the following:

void delay(int nStopValue)
/**
* \brief       Loop for nStopValue iterations to provide a delay.
* \par         Details
*              It is commonly used with the constant 'ABOUT_ONE_SECOND' defined in maximPMOD.h for
*              setting approximate delays
*
* \param[in]   nStopValue    - number of iterations to loop
*
* \retval      None
*/
{
    int i=0;
    int a=0;

    for(i=0;i<nStopValue;i++)
    {
        a=i;
    }

This allowed for a pause of about one second after one LED lit up to the time the next one did.

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

After this code was successfully working, we decided to modify it to make the LEDs light up 1-8 and then back down 8-1 continuously without stopping.
In order to do this we needed to add another 'for' loop right after the first one telling the LEDs to blink consecutively in reverse order from 8 down to 1. The 'for' loop we made looked like the following:

int j;
        for (j=8;j>-2;j--){
                    XGpio_DiscreteWrite(&Gpio,LED_CHANNEL,led);
                    delay(LED_DELAY);
                    led = (int)(1<<j);
                    //XGpio_DiscreteWrite(&Gpio,LED_CHANNEL,led);
                    delay(LED_DELAY);
                }
}

We named the variable 'j' and made it decrement by 1 starting at LED 8 and going down to -2. We had trouble getting it all the way down to the last LED and it seemed that it was reading the value for the first LED to be -2.


In order to make this non-stop we needed to put both the 2 'for' loops in a 'while' loop, which successfully made it run up the LEDs and back down without stopping.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 






No comments:

Post a Comment