Wednesday, June 10, 2015

June 10 - Code that reads in signal from function generator



/* Input is on pin 3.  The program reads 8 bits of data sequentially and stores them as an 8 bit value.
The process of storage is shown using the LEDs.  Each bit is shifted to the left.  This is a program to
understand the bit-bang process and how the Maxim Integrated 11205 ADC works.*/

/* 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 */





u32 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;
 u8 uchPortWriteData=0;
 u8 uchPortReadData=0;
 u32 adcValue=0;


 int nClockCount;
 int uchUseCalibrationMode=0;

  /* 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 PMod - pin 3 is an input, rest are outputs. */
    XGpio_SetDataDirection(&GpioP, PMOD_CHANNEL, 0x04);

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


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

   // XGpio_DiscreteWrite(&Gpio, LED_CHANNEL, 0x00);

    // First, set the clock and data lines low, and the SS# line high
            // GPIO[3:0] = {SCK, CLR#, MOSI, SS#} // default 'off' is 4'b0101 = 0x05
        //delay(10);
        uchPortWriteData = 0;
        XGpio_DiscreteWrite(&GpioP, 1, uchPortWriteData);  // The (1) means write to the only port (#1) on the uBlaze GPIO port
        //delay(10);

        // Wait until RDY# goes high
        //uchPortReadData = XGpio_DiscreteRead(&GpioP,1);
        uchPortReadData=0;
        while((uchPortReadData & DATAREADY_high)==0x00)  // keep looping while low
        {
            uchPortReadData = XGpio_DiscreteRead(&GpioP,1);
        }

        // Now, wait until RDY# goes low
        uchPortReadData = XGpio_DiscreteRead(&GpioP,1);
        while((uchPortReadData & DATAREADY_high)==DATAREADY_high)
        {
            uchPortReadData = XGpio_DiscreteRead(&GpioP,1);
        }
 for(i=15;i>=0;i--)
    {
        // Send clock high
        uchPortWriteData |= CLOCK_on;
        XGpio_DiscreteWrite(&GpioP, 1, uchPortWriteData);

       // delay(10);  // small delay, then read
        uchPortReadData = XGpio_DiscreteRead(&GpioP,1);
        //delay(10);
        // Shift the GPIO read data to the lowest bit, mask off all the other bits, then shift i (15:0)
        // number of bits to set the appropriate bit in the
        if(((uchPortReadData >> 2) & 0x01)==0x01) // the serial bit is a one, set the bit
        {
            if(i==15)
                adcValue = 0x8000;  // extend the sign of the 2s complement number to bits 31..16, and set bit 15 = 1
            else
                adcValue |= (int)(1 << i);  // bit shift
        }
        uchPortWriteData &= ~CLOCK_on;// Clock in data via negative edge
        XGpio_DiscreteWrite(&GpioP, 1, uchPortWriteData);
      //  delay(10);
       // XGpio_DiscreteWrite(&Gpio, LED_CHANNEL, adcValue);


        //printf("The Value  %d \n \r", adcValue);

        //storedValues[i]= adcValue;
    }
  
 // The 11205 device requires 25 clocks total to complete a read.
     // We have already sent (16) clocks, so send 9 more clocks.
     // (If self-calibration mode set then we send 10 more clocks.)
     if(uchUseCalibrationMode==1)
         nClockCount = 9;
     else
         nClockCount=9;
     for(i=nClockCount;i>=0;i--)
     {
         // Send clock high
         uchPortWriteData |= CLOCK_on;
         XGpio_DiscreteWrite(&GpioP, 1, uchPortWriteData);

         //delay(10);  // small delay, then read
         uchPortWriteData &= ~CLOCK_on;// Clock in data via negative edge
         XGpio_DiscreteWrite(&GpioP, 1, uchPortWriteData);
         //delay(10);
     }



      //printf("The ADC Value  %d \n \r", adcValue);

      //XGpio_DiscreteWrite(&Gpio, LED_CHANNEL, 0x01);
     // sleep(1);
     // XGpio_DiscreteWrite(&Gpio, LED_CHANNEL, 0x00);
      //sleep(1);



  return adcValue; /* 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){

     u32 storedValues[100]={};
 int Status;
int j=0;
while(j<100){
 /* Execute the LED output. */
    storedValues[j] = LEDOutputExample();
    //printf("The J Value  %d \n \r", j );
   // printf("The STORED Values  %d \n \r", storedValues[j]);
    j++;
 }
int k;
printf("The ARRAY Values \n");
  for(k=0; k<100; k++)
  {

      printf("%d \n \r", storedValues[k]);
  }


 return 0;
}

No comments:

Post a Comment