Friday, June 5, 2015

Code to read from pin 3 and write to serial and LED

/*Pin 3 is connected to a pull-down resistor and a button.  When the button is pressed, the pin reads high and outputs the value 4 to the terminal (gtkterm).  It also turns on an LED. */

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

/* Definitions */
#define GPIO_DEVICE_ID  XPAR_AXI_GPIO_0_DEVICE_ID /* GPIO device that LEDs are connected to */
#define LED 0xFC          /* Initial LED value - XX0000XX */
#define PMOD 0xFC         /* Initial LED value - XX0000XX */
#define LED_DELAY 100000000       /* 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
#define ABOUT_ONE_SECOND 74067512      //!< approx 1 second delay when used as argument with function delay(numberCyclesToDelay)
// Update this if uBlaze/Zynq CPU core frequency is changed, or if the external memory timing changes.
// Although emprirically tested to 1.0000003 seconds, it is not meant to be used for precise timing purposes

/* Definitions */
#define CLOCK_on 0x08    //high for the clock
#define DATAREADY_high 0x04   //high for data ready pin 3


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;
 int WritetoLEDs;
 int i;
 u32 x;

  /* 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;
  }
   delay(ABOUT_ONE_SECOND*5);

  /*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, 0x04);

    WritetoLEDs =0x01;
    for(i =1;i<8;i++){
        XGpio_DiscreteWrite(&Gpio, LED_CHANNEL, WritetoLEDs);
        delay(ABOUT_ONE_SECOND/2);
        WritetoLEDs=(int)(1<<i);
        XGpio_DiscreteWrite(&Gpio, LED_CHANNEL, WritetoLEDs);
        delay(ABOUT_ONE_SECOND/2);
    }


    printf("Ready to receive.\n \r");
    delay(10);
    XGpio_DiscreteWrite(&Gpio, LED_CHANNEL, 0x00);


   while (1) {
       XGpio_DiscreteWrite(&Gpio, LED_CHANNEL, 0x00);
       delay(10);
       XGpio_DiscreteWrite(&GpioP, PMOD_CHANNEL, 0x00);
    x = XGpio_DiscreteRead(&GpioP,PMOD_CHANNEL);
    delay(10);
        printf("The voltage on pin 3 is %d \n \r",x);
        delay(10);
        while(x==0x04){
            XGpio_DiscreteWrite(&Gpio, LED_CHANNEL, 0x01);
            delay(10);
            XGpio_DiscreteWrite(&GpioP, PMOD_CHANNEL, 0x08);
            delay(10);
            x = XGpio_DiscreteRead(&GpioP,PMOD_CHANNEL);
        }
    /* 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 */
}



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 = LEDOutputExample();
 if (Status != XST_SUCCESS) {
  xil_printf("GPIO output to the LEDs failed!\r\n");
 }


 return 0;
}




No comments:

Post a Comment