Beispiel #1
0
Datei: gyro.c Projekt: rubda/KMM
//Returnerar en vinkel tills vi har roterat klart till en vinkel vi fått in	
double rotate_to(int angle)
{
	activate_adc();
	uint16_t rate;
	double ACHIEVED_ANGLE = 0;
	int TIME = 0;
	int OFFSET = 0;
	int LEFT_ANGLE = (angle - OFFSET);
	int RIGHT_ANGLE = (angle + OFFSET);
		
	if (angle > 0){
		while(LEFT_ANGLE > ACHIEVED_ANGLE && TIME++ < 1000)
		{		
			start_conversion();
			rate = get_angular_rate();
			ACHIEVED_ANGLE += adc_to_angular_rate(rate)/100;
			_delay_ms(10);
		}
	}else if (angle < 0){
		while(ACHIEVED_ANGLE > RIGHT_ANGLE && TIME++ < 1000)
		{
			start_conversion();
			rate = get_angular_rate();
			ACHIEVED_ANGLE += adc_to_angular_rate(rate)/100;
			_delay_ms(10);
		}
	}
	deactivate_adc();
	return ACHIEVED_ANGLE;
}
void main(){

	WDTCTL = WDTPW + WDTHOLD;       // Stop watchdog timer
	BCSCTL1 = CALBC1_8MHZ;			// 8Mhz calibration for clock
  	DCOCTL  = CALDCO_8MHZ;

  	adc_val = 0;   // most recent result is stored in photo.whole_int
  	updates=0; 		//update counter for debugger
  	last_updates=0; // initialize last_update to 0 like updates value
  	data_send = 0;
  	counter = COUNTER_VAL;
  	low = 29;
  	med = 46;
  	high = 53;
  	light_range = high - low;
  	light_to_pot_factor = (double)light_range / 128.0;
  	state = high_state;
  	beat_count = 0;
  	reached_high = 0;	// set to false
  	reached_low = 0;	// set to false

  	init_spi();
  	init_wdt();
  	init_adc();
  	init_timer();

  	start_conversion();		// do a conversion so that updates > last_updates
  	TACCTL0 |= OUTMOD_4; // turn on speaker
 	_bis_SR_register(GIE+LPM0_bits);

}
Beispiel #3
0
// Sample the specified channel.  This is a blocking call
uint16_t sample_channel( uint8_t channel )
{
    uint8_t interrupt_settings;
    uint16_t data;

    // Store interrupt settings
    interrupt_settings = ADCSRA;

    // Make sure interrupts are disabled
    ADCSRA &= ~(1 << ADIE);

    start_conversion(channel);

    // Wait for conversion to complete
    while( (ADCSRA & (1 << ADIF)) == 0 );
    data = get_conversion_value();

    // Clear the interrupt flag
    ADCSRA |= (1 << ADIF);

    // Restore interrupt settings
    ADCSRA = interrupt_settings;

    // Done!  Return the converted value
    return data;
}
Beispiel #4
0
// Initialize the ADC
void init_adc()
{
    g_ADC_state = STATE_ADC_VOUT1;
    g_channel = VOUT_CHANNEL;
    set_mux_address(VOUT1_MUX_CHANNEL);

    g_data.vin_sample_count = 0;
    g_data.vout1_sample_count = 0;
    g_data.vout2_sample_count = 0;
    g_data.vout3_sample_count = 0;

    g_data.iout1_sample_count = 0;
    g_data.iout2_sample_count = 0;
    g_data.iout3_sample_count = 0;

    // Disable the ADC
    ADCSRA = 0;

    // Set the prescaler for the ADC clock
//	ADCSRA |= (5 << ADPS0);		// 8e6 / 64 = 125e3
    ADCSRA |= (5 << ADPS0);		// 10e6 / 64 = 156e3

    // Enable the ADC interrupt
    ADCSRA |= (1 << ADIE);

    // Disable digital input buffers on select ADC input pins (channels 0, 1, and 2 for current measurement)
    DIDR0 = 7;		// That's 0b0000111 to disable channels 0, 1, and 2

    // Enable the ADC (doesn't start a conversion.  That needs to happen later.)
    ADCSRA |= (1 << ADEN);

    start_conversion(g_channel);
}
Beispiel #5
0
/*---------------------------------------------------------------------------*/
static
PT_THREAD(temperature_thread(struct pt *pt))
{
    PT_BEGIN(pt);

    init_adc(5);
    disable_digital_buffer(5);

    while(1)
    {
        cli();
        
        temperature_counter = 0;
        
        sei();

        PT_WAIT_UNTIL(pt,temperature_counter > 750);

        temp_result = 0;
        temp_sampl_cnt = 0;
     
        for(temp_sampl_cnt = 0; temp_sampl_cnt < 64; temp_sampl_cnt++)
        {    
            start_conversion();
        
            PT_WAIT_UNTIL(pt,!is_adc_busy());

            temp_result += read_adc();
        }        

        dbg(PSTR("> ADC result: %u\r\n"),temp_result >> 6);
    }

    PT_END(pt);
}
Beispiel #6
0
/* Perform conversion using the kernel method. */
void
kernel_conversion (struct config *config, char **cmdline, int cmdline_source)
{
  const char *p;

  /* Pre-conversion command. */
  p = get_cmdline_key (cmdline, "p2v.pre");
  if (p)
    run_command ("p2v.pre", p);

  /* Connect to and interrogate virt-v2v on the conversion server. */
  p = get_cmdline_key (cmdline, "p2v.skip_test_connection");
  if (!p) {
    wait_network_online (config);
    if (test_connection (config) == -1) {
      const char *err = get_ssh_error ();

      error (EXIT_FAILURE, 0,
             "error opening control connection to %s:%d: %s",
             config->remote.server, config->remote.port, err);
    }
  }

  /* Some disks must have been specified for conversion. */
  if (config->disks == NULL || guestfs_int_count_strings (config->disks) == 0)
    error (EXIT_FAILURE, 0,
           "no non-removable disks were discovered on this machine.\n"
           "virt-p2v looked in /sys/block and in p2v.disks on the kernel command line.\n"
           "This is a fatal error and virt-p2v cannot continue.");

  /* Perform the conversion in text mode. */
  if (start_conversion (config, notify_ui_callback) == -1) {
    const char *err = get_conversion_error ();

    fprintf (stderr, "%s: error during conversion: %s\n",
             getprogname (), err);

    p = get_cmdline_key (cmdline, "p2v.fail");
    if (p)
      run_command ("p2v.fail", p);

    exit (EXIT_FAILURE);
  }

  ansi_green (stdout);
  printf ("Conversion finished successfully.");
  ansi_restore (stdout);
  putchar ('\n');

  p = get_cmdline_key (cmdline, "p2v.post");
  if (!p) {
    if (geteuid () == 0 && cmdline_source == CMDLINE_SOURCE_PROC_CMDLINE)
      p = "poweroff";
  }
  if (p)
    run_command ("p2v.post", p);
}
Beispiel #7
0
Datei: gyro.c Projekt: rubda/KMM
void start_conversion()
{
	uint16_t REG = 0;
	
	//Steg 2 conversion
	ss_low();
	send_spi(START_CONVERSION);
	REG = get_spi(0xFF);
	ss_high();
	if (REG & (1 << 15)) start_conversion();
	REG = 0;
}
interrupt void WDT_interval_handler(){
	if (updates > last_updates){		// if there has been a conversion updates will be greater than last_updates
		/*P1OUT &= ~SPI_SS;				// bring select line low to signal start of SPI communication
		UCB0TXBUF=0;					// init sending current byte of adc int value
		while (!(IFG2 & UCB0TXIFG)); // wait for TX buffer ready
		data_send = getValueForPot();	// get value between 0 and 128 based on adc value
		UCB0TXBUF=data_send;			// send data
		while (!(IFG2 & UCB0TXIFG)); // wait for TX buffer ready
		while (UCB0STAT & UCBUSY); // wait for the tx to complete
		P1OUT |= SPI_SS;*/			// bring select line back high to signal end of SPI communication

		switch (state) {
			case low_state:
				if (adc_val > med && reached_low) {
					state = high_state;
					beat_count++;
					WDT_per_quarter = quarter_count; //this value stored will be the new quarter note "multiplier"
					quarter_count = 0;
					reached_high = 0;
				}
				break;
			case high_state:
				if (adc_val < med && reached_high) {
					state = low_state;
					reached_low = 0;
				}
				break;
		}

		if (adc_val <= low) {
			reached_low = 1;
		}
		if (adc_val >= high) {
			reached_high = 1;
		}

		last_updates = updates;	// update last_updates variable
		start_conversion();		// start a new adc conversion
	}

	if (--counter==0){          // decrement the counter and act only if it has reached 0
		counter = COUNTER_VAL;
		TACCTL0 |= OUTMOD_4;
	}
	quarter_count++;
}
Beispiel #9
0
Datei: c.c Projekt: dileepk/adc
void main()
{
unsigned int a=0,i;
while(1)
{
for(i=0;i<1000;i++);
init();
start_conversion();
while(check_conversion());
a=ADC10MEM;
if(a>407)
{
ADC10CTL0&=~(1<<1);
ADC10AE0=0;
P1DIR=1;
P1OUT=1;}
else 
P1OUT=0;
}
}
Beispiel #10
0
main()
{
	P1DIR = BIT6;
	P1OUT = BIT6;
	init_adc();
	
	while(1)
	{
		start_conversion();
		
		while( converting() )
			;
			
		if( ADC10MEM > 380 )
			P1OUT = 0;
		else
			P1OUT = BIT6;
		delay();
		
	}
}
 void main() {
         int i=0;
         init_adc();
  
         P1DIR=0x41;
         P1OUT=0x00;    
         while(1) {
                 start_conversion();              
                 while( is_converting() );
                
                 if(ADC10MEM >=0 && ADC10MEM <= 255.75)
                         P1OUT=0x00;
                 else if (ADC10MEM>=256.75 && ADC10MEM <=511.5)
                         P1OUT=0x01;
                 else if(ADC10MEM >=512.5 && ADC10MEM <=767.25)
                         P1OUT=0x40;
                 else
                         P1OUT=0x41;
                                  
                 for(i=0;i<32000;i++);  
         }
 }
Beispiel #12
0
void main()
{
	P1DIR = LED1|LED2;
	P1OUT = 0x0;
	adc_init();
	while(1)
	{
		start_conversion();
		if(!converting())
		{
			if(ADC10MEM>767)		/***Check whether the voltage is in between 2.7 and 3.6V*****/
				P1OUT = LED1|LED2;
			else if(ADC10MEM>511)   /****checking whether the voltage level is in between 1.8 and 2.7******/
				P1OUT = LED1;
			else if(ADC10MEM>255)  /***checking whether the voltage is in between 0.9 & 1.8 **/
				P1OUT = LED2;
			else					/****checking whether the voltage is in between 0 and 0.9 *****/
				P1OUT = 0x00;
		}
		delay(30000);
	}
			
}	
Beispiel #13
0
int main(void)
{
	sei();

	initialise_ADC();
	start_conversion();
	
	DDRB |= (1 << PB0) | (1 << PB1);						// PWM control pin, as output
	
	// This block are using Timer/Counter0 to drive FAN and the LED @32.5KHz
	// Uncomment this block if you wanna drive PWM with timer 0
	//TCCR0A |= (1<<COM0A1) | (1<<COM0B1) | (1<<WGM00) | (1<<WGM01);	// fast PWM mode + clear OC0A & CO0B on compare match
	//TIMSK |= (1<<TOIE0);								// Timer/Counter0 Overflow Interrupt Enable
	//OCR0A = 125;										// should later be comment out to let interrupt handle this problem.
	//OCR0B = 125;
	//TCCR0B |= (1<<CS00);								// no prescaler
	
	OCR1C = 127;											// setting the PWM speed. In this case, it is working @500KHz
	TCCR1 |= (1 << PWM1A) | (1 << COM1A0) | (1 << CS10);	// PWM mode + clear OC1A output line + CK/2. COM1A0 decide if PB0 get inverted signal!! Page:87
	TIMSK |= (1 << TOIE1);									//P.92 Timer/Counter Overflow Interrupt Enable
	
	while(1){}
}
Beispiel #14
0
void
kernel_configuration (struct config *config, char **cmdline, int cmdline_source)
{
  const char *p;

  p = get_cmdline_key (cmdline, "p2v.pre");
  if (p)
    run_command (config->verbose, "p2v.pre", p);

  p = get_cmdline_key (cmdline, "p2v.server");
  assert (p); /* checked by caller */
  free (config->server);
  config->server = strdup (p);

  p = get_cmdline_key (cmdline, "p2v.port");
  if (p) {
    if (sscanf (p, "%d", &config->port) != 1) {
      fprintf (stderr, "%s: cannot parse p2v.port from kernel command line",
               guestfs_int_program_name);
      exit (EXIT_FAILURE);
    }
  }

  p = get_cmdline_key (cmdline, "p2v.username");
  if (p) {
    free (config->username);
    config->username = strdup (p);
  }

  p = get_cmdline_key (cmdline, "p2v.password");
  if (p) {
    free (config->password);
    config->password = strdup (p);
  }

  p = get_cmdline_key (cmdline, "p2v.identity");
  if (p) {
    free (config->identity_url);
    config->identity_url = strdup (p);
    config->identity_file_needs_update = 1;
  }

  p = get_cmdline_key (cmdline, "p2v.sudo");
  if (p)
    config->sudo = 1;

  /* We should now be able to connect and interrogate virt-v2v
   * on the conversion server.
   */
  p = get_cmdline_key (cmdline, "p2v.skip_test_connection");
  if (!p) {
    wait_network_online (config);
    if (test_connection (config) == -1) {
      const char *err = get_ssh_error ();

      fprintf (stderr, "%s: error opening control connection to %s:%d: %s\n",
               guestfs_int_program_name, config->server, config->port, err);
      exit (EXIT_FAILURE);
    }
  }

  p = get_cmdline_key (cmdline, "p2v.name");
  if (p) {
    free (config->guestname);
    config->guestname = strdup (p);
  }

  p = get_cmdline_key (cmdline, "p2v.vcpus");
  if (p) {
    if (sscanf (p, "%d", &config->vcpus) != 1) {
      fprintf (stderr, "%s: cannot parse p2v.vcpus from kernel command line\n",
               guestfs_int_program_name);
      exit (EXIT_FAILURE);
    }
  }

  p = get_cmdline_key (cmdline, "p2v.memory");
  if (p) {
    char mem_code;

    if (sscanf (p, "%" SCNu64 "%c", &config->memory, &mem_code) != 2) {
      fprintf (stderr, "%s: cannot parse p2v.memory from kernel command line\n",
               guestfs_int_program_name);
      exit (EXIT_FAILURE);
    }
    config->memory *= 1024;
    if (mem_code == 'M' || mem_code == 'm'
        || mem_code == 'G' || mem_code == 'g')
      config->memory *= 1024;
    if (mem_code == 'G' || mem_code == 'g')
      config->memory *= 1024;
    if (mem_code != 'M' && mem_code != 'm'
        && mem_code != 'G' && mem_code != 'g') {
      fprintf (stderr, "%s: p2v.memory on kernel command line must be followed by 'G' or 'M'\n",
               guestfs_int_program_name);
      exit (EXIT_FAILURE);
    }
  }

  p = get_cmdline_key (cmdline, "p2v.disks");
  if (p) {
    CLEANUP_FREE char *t;

    t = strdup (p);
    guestfs_int_free_string_list (config->disks);
    config->disks = guestfs_int_split_string (',', t);
  }

  p = get_cmdline_key (cmdline, "p2v.removable");
  if (p) {
    CLEANUP_FREE char *t;

    t = strdup (p);
    guestfs_int_free_string_list (config->removable);
    config->removable = guestfs_int_split_string (',', t);
  }

  p = get_cmdline_key (cmdline, "p2v.interfaces");
  if (p) {
    CLEANUP_FREE char *t;

    t = strdup (p);
    guestfs_int_free_string_list (config->interfaces);
    config->interfaces = guestfs_int_split_string (',', t);
  }

  p = get_cmdline_key (cmdline, "p2v.network");
  if (p) {
    CLEANUP_FREE char *t;

    t = strdup (p);
    guestfs_int_free_string_list (config->network_map);
    config->network_map = guestfs_int_split_string (',', t);
  }

  p = get_cmdline_key (cmdline, "p2v.o");
  if (p) {
    free (config->output);
    config->output = strdup (p);
  }

  p = get_cmdline_key (cmdline, "p2v.oa");
  if (p) {
    if (STREQ (p, "sparse"))
      config->output_allocation = OUTPUT_ALLOCATION_SPARSE;
    else if (STREQ (p, "preallocated"))
      config->output_allocation = OUTPUT_ALLOCATION_PREALLOCATED;
    else
      fprintf (stderr, "%s: warning: don't know what p2v.oa=%s means\n",
               guestfs_int_program_name, p);
  }

  p = get_cmdline_key (cmdline, "p2v.oc");
  if (p) {
    free (config->output_connection);
    config->output_connection = strdup (p);
  }

  p = get_cmdline_key (cmdline, "p2v.of");
  if (p) {
    free (config->output_format);
    config->output_format = strdup (p);
  }

  p = get_cmdline_key (cmdline, "p2v.os");
  if (p) {
    free (config->output_storage);
    config->output_storage = strdup (p);
  }

  /* Undocumented command line tool used for testing command line parsing. */
  p = get_cmdline_key (cmdline, "p2v.dump_config_and_exit");
  if (p) {
    print_config (config, stdout);
    exit (EXIT_SUCCESS);
  }

  /* Some disks must have been specified for conversion. */
  if (config->disks == NULL || guestfs_int_count_strings (config->disks) == 0) {
    fprintf (stderr, "%s: error: no non-removable disks were discovered on this machine.\n",
             guestfs_int_program_name);
    fprintf (stderr, "virt-p2v looked in /sys/block and in p2v.disks on the kernel command line.\n");
    fprintf (stderr, "This is a fatal error and virt-p2v cannot continue.\n");
    exit (EXIT_FAILURE);
  }

  /* Perform the conversion in text mode. */
  if (start_conversion (config, notify_ui_callback) == -1) {
    const char *err = get_conversion_error ();

    fprintf (stderr, "%s: error during conversion: %s\n",
             guestfs_int_program_name, err);

    p = get_cmdline_key (cmdline, "p2v.fail");
    if (p)
      run_command (config->verbose, "p2v.fail", p);

    exit (EXIT_FAILURE);
  }

  p = get_cmdline_key (cmdline, "p2v.post");
  if (!p) {
    if (geteuid () == 0 && cmdline_source == CMDLINE_SOURCE_PROC_CMDLINE)
      p = "poweroff";
  }
  if (p)
    run_command (config->verbose, "p2v.post", p);
}