Пример #1
0
int main(int argc, char **argv)
{
  int i, j;
  long tmp=0;
  long tmp_avg=0;
  long tmp_avg2;
  long offset=0;
  float filter_low, filter_high;
  float spread_percent = SPREAD / 100.0 /2.0;
  int b;
  int nsamples=N_SAMPLES;
  long samples[nsamples];

  if (argc == 2) {
   offset = atol(argv[1]);
  }

  setHighPri();
  setup_io();
  setup_gpio();
  reset_converter();

  j=0;

  // get the dirty samples and average them
  for(i=0;i<nsamples;i++) {
  	reset_converter();
  	samples[i] = read_cnt(0, argc);
  	tmp_avg += samples[i];
  }

  tmp_avg = tmp_avg / nsamples;

  tmp_avg2 = 0;
  j=0;

  filter_low =  (float) tmp_avg * (1.0 - spread_percent);
  filter_high = (float) tmp_avg * (1.0 + spread_percent);

//  printf("%d %d\n", (int) filter_low, (int) filter_high);

  for(i=0;i<nsamples;i++) {
	if ((samples[i] < filter_high && samples[i] > filter_low) || 
            (samples[i] > filter_high && samples[i] < filter_low) ) {
		tmp_avg2 += samples[i];
	        j++;
	}
  }

  if (j == 0) {
    printf("No data to consider\n");
    exit(255);

  }
  printf("%d", (tmp_avg2 / j) - offset);

//  printf("average within %f percent: %d from %d samples, original: %d\n", spread_percent*100, (tmp_avg2 / j) - offset, j, tmp_avg - offset);
  unpull_pins();
  restore_io();
}
int main(void)
{ 

  printf ("Traffic light demo\n");

  // Map the I/O sections
  setup_io();

  // Set 12 GPIO pins to output mode
  setup_gpio();

  GPIO_CLR0 = ALL_LEDS;		//Turn all LEDs off

  int i = 1;

  while(i < 4)	//Loop forever
  {
    printf("Traffic lights loop %d\n", i);

	GPIO_SET0 = RED_NORTH | GRN_EAST;	// Turn on North red. Turn on East green
	long_wait(80);						// Wait a bit
	GPIO_CLR0 = ALL_LEDS;				// Turn led off
	
	GPIO_SET0 = RED_NORTH | YEL_EAST;	// Turn on North red. Turn on East yellow 
	long_wait(20);						// Wait a short bit
	GPIO_CLR0 = ALL_LEDS;				// Turn led off
	
	GPIO_SET0 = RED_NORTH | RED_EAST;
	long_wait(20);
	GPIO_CLR0 = ALL_LEDS;

	GPIO_SET0 = YEL_NORTH | RED_EAST;
	long_wait(20);
	GPIO_CLR0 = ALL_LEDS;

	GPIO_SET0 = GRN_NORTH | RED_EAST;
	long_wait(80);
	GPIO_CLR0 = ALL_LEDS;	

	GPIO_SET0 = YEL_NORTH | RED_EAST;
	long_wait(20);
	GPIO_CLR0 = ALL_LEDS;

	GPIO_SET0 = RED_NORTH | RED_EAST;
	long_wait(20);
	GPIO_CLR0 = ALL_LEDS;	
	
	GPIO_SET0 = RED_NORTH | YEL_EAST;
	long_wait(20);
	GPIO_CLR0 = ALL_LEDS;
	
	
  i++;
  } 

  GPIO_CLR0 = ALL_LEDS;	//Turn all LEDs off
  restore_io();
} // main
Пример #3
0
//
// Quick play all patterns
//
int main(void)
{ int p,r,last;

  printf ("These are the connections for the LEDs test:\n");
  printf ("jumpers in every out location (U3-out-B1, U3-out-B2, etc)\n");
  printf ("GP25 in J2 --- B1 in J3\n");
  printf ("GP24 in J2 --- B2 in J3\n");
  printf ("GP23 in J2 --- B3 in J3\n");
  printf ("GP22 in J2 --- B4 in J3\n");
  printf ("GP21 in J2 --- B5 in J3\n");
  printf ("GP18 in J2 --- B6 in J3\n");
  printf ("GP17 in J2 --- B7 in J3\n");
  printf ("GP11 in J2 --- B8 in J3\n");
  printf ("GP10 in J2 --- B9 in J3\n");
  printf ("GP9 in J2 --- B10 in J3\n");
  printf ("GP8 in J2 --- B11 in J3\n");
  printf ("GP7 in J2 --- B12 in J3\n");
  printf ("(If you don't have enough straps and jumpers you can install\n");
  printf ("just a few of them, then run again later with the next batch.)\n");
  printf ("When ready hit enter.\n");
  (void) getchar();

  // Map the I/O sections
  setup_io();

  // Set 12 GPIO pins to output mode
  setup_gpio();

  /* for testing purposes...
  GPIO_SET0 = 0x180;
  (void) getchar();
  GPIO_CLR0 = 0x100;
  (void) getchar();
  */

  for (p=0; p<3; p++)
  {
    // run pattern several times
    start_new_pattern(p);
    for (r=0; r<2; r++)
    { do {
        last = led_step();
        long_wait(3);
      } while (!last);
    } // run the pattern 2 times
  } // loop over patterns

  leds_off();
  restore_io();
} // main
Пример #4
0
int main(void)
{ char key;

   // Map the I/O sections
   setup_io();

   // Set ALL GPIO pins to the required mode
   setup_gpio();

   // Set up PWM module
   setup_pwm();

   // Setup the SPI
   setup_spi();

   // We don't touch the UART for now

   //
   // Here your main program can start
   //
   do {
     printf(" l/L : Walk the LEDS\n");
     printf(" b/B : Show buttons\n");
     printf(" m/M : Control the motor\n");
     printf(" a/A : Read the ADC values\n");
     printf(" c/C : ADC => Motor\n");
     printf("( D : Set  the DAC values\n");
     printf(" q/Q : Quit program\n");
     key = getchar();
     switch (key)
     {
     case 'l':
     case 'L':
         quick_led_demo();
         break;

     case 'b':
     case 'B':
         quick_buttons_demo();
         break;

     case 'm':
     case 'M':
         quick_pwm_demo();
         break;

     case 'a':
     case 'A':
         quick_adc_demo();
         break;

     case 'c':
     case 'C':
         adc_pwm_demo();
         break;

     case 0x0A:
     case 0x0D:
         // ignore CR/LF
         break;

     default:
       printf("???\n");
     }

   } while (key!='q' && key!='Q');

   // make sure everything is off!
   leds_off();
   pwm_off();

   restore_io();

   return 0;
} // main
Пример #5
0
//
//  Do digital to analogue to digital conversion
//
void main(void)
{ int d, dac_val, v, s, i;

  printf ("These are the connections for the digital to analogue to digital test:\n");
  printf ("jumper connecting GP11 to SCLK\n");
  printf ("jumper connecting GP10 to MOSI\n");
  printf ("jumper connecting GP9 to MISO\n");
  printf ("jumper connecting GP8 to CSnA\n");
  printf ("jumper connecting GP7 to CSnB\n");
  printf ("jumper connecting DA1 on J29 to AD0 on J28\n");
  printf ("When ready hit enter.\n");
  (void) getchar();

  // Map the I/O sections
  setup_io();

  // activate SPI bus pins
  setup_gpio();

  // Setup SPI bus
  setup_spi();

  // The value returned by the A to D can jump around quite a bit, so 
  // simply printing out the value isn't very useful. The bar graph
  // is better because this hides the noise in the signal.

  printf ("dig ana\n");
  for (d=0; d <= 256; d+= 32)
  {
    if (d == 256) 
      dac_val = 255 * 16;
    else 
      dac_val = d * 16;
    write_dac(1, dac_val);
    v= read_adc(0);
    // v should be in range 0-1023
    // map to 0-63
    s = v >> 4;
    printf("%3x %04d ", dac_val, v);
    // show horizontal bar
    for (i = 0; i < s; i++)
      putchar('#');
    for (i = 0; i < 64 - s; i++)
      putchar(' ');
    putchar('\n');
    short_wait();
  } // repeated write/read
  for (d=224; d >= 0; d-= 32)
  {
    dac_val = d * 16;
    write_dac(1, dac_val);
    v= read_adc(0);
    // v should be in range 0-1023
    // map to 0-63
    s = v >> 4;
    printf("%3x %04d ", dac_val, v);
    // show horizontal bar
    for (i = 0; i < s; i++)
      putchar('#');
    for (i = 0; i < 64 - s; i++)
      putchar(' ');
    putchar('\n');
    short_wait();
  } // repeated write/read

  printf("\n");
  restore_io();
} // main