Ejemplo n.º 1
0
int
main (void)
{
  // This isn't what we're testing exactly, but we need to know if its
  // working or not to interpret other results.
  term_io_init ();
  PFP ("\n");
  PFP ("\n");
  PFP ("term_io_init() worked.\n");
  PFP ("\n");

  // FIXME: audit item: I like to put const after type as Dan Saks advises
  uint8_t const aip = 0;   // Analog Input Pin (from 0 for ADC0 to 5 for ADC5)

  adc_init (ADC_REFERENCE_AVCC);
  PFP ("Finished adc_init().\n");
  adc_pin_init (aip);
  // This register bit test is hardwired to match the chosen aip value.
  // The initialization should have done this, but we can't tell just by
  // observing that the ADC reads voltages correctly, so we check here.
  if ( ! (DIDR0 & _BV (ADC0D)) ) {
    PFP (
        "failure: Digital input disable bit ADC0D of register DIDR0 not "
        "set\n" );
    assert (0);
  }
  PFP ("Finished adc_pin_init().\n");

  PFP ("\n");

  // Configure pin A1 (aka PC1) as an output, starting out low.
  PORTC &= ~(_BV (PORTC1));
  //FIXXME: could be a no-op, which recent avr libc have a macro for
  loop_until_bit_is_clear (PORTC, PORTC1);
  DDRC |= _BV (DDC1);

  while ( 1 )
  {
    float const supply_voltage = 5.0;

    uint16_t raw = adc_read_raw (aip);
    float tap_voltage = adc_read_voltage (aip, supply_voltage);

    PFP ("ADC input voltage: %f (%d raw)\r\n", tap_voltage, raw);

    toggle_pc1 ();

    float const mspr = 500.0;   // Milliseconds Per Reading (and LED toggle)
    _delay_ms (mspr);
  }
  return 0;
}
int main(int argc, char **argv){
	setvbuf (stdout, NULL, _IONBF, 0); // needed to print to the command line

	if (adc_open() != 1){ // open the ADC spi channel
			exit(1); // if the SPI bus fails to open exit the program
		}

	while (1){
		clearscreen();
		printf("Pin 1: %G \n", adc_read_voltage(1, 0)); // read the voltage from channel 1 in single ended mode
		printf("Pin 2: %G \n", adc_read_voltage(2, 0)); // read the voltage from channel 2 in single ended mode
		printf("Pin 3: %G \n", adc_read_voltage(3, 0)); // read the voltage from channel 3 in single ended mode
		printf("Pin 4: %G \n", adc_read_voltage(4, 0)); // read the voltage from channel 4 in single ended mode
		printf("Pin 5: %G \n", adc_read_voltage(5, 0)); // read the voltage from channel 5 in single ended mode
		printf("Pin 6: %G \n", adc_read_voltage(6, 0)); // read the voltage from channel 6 in single ended mode
		printf("Pin 7: %G \n", adc_read_voltage(7, 0)); // read the voltage from channel 7 in single ended mode
		printf("Pin 8: %G \n", adc_read_voltage(8, 0)); // read the voltage from channel 8 in single ended mode

		usleep(200000); // sleep 0.2 seconds

	}

	return (0);
}
Ejemplo n.º 3
0
static void handle_twi_command(void)
{
    uint8_t command;

    // Get the command from the receive buffer.
    command = twi_receive_byte();

    switch (command)
    {
    case TWI_CMD_RESET:

        // Reset the servo.
        watchdog_hard_reset();

        break;

    case TWI_CMD_PWM_ENABLE:

        // Enable PWM to the servo motor.
        pwm_enable();

        break;

    case TWI_CMD_PWM_DISABLE:

        // Disable PWM to the servo motor.
        pwm_disable();

        break;

    case TWI_CMD_WRITE_ENABLE:

        // Enable write to read/write protected registers.
        registers_write_enable();

        break;

    case TWI_CMD_WRITE_DISABLE:

        // Disable write to read/write protected registers.
        registers_write_disable();

        break;

    case TWI_CMD_REGISTERS_SAVE:

        // Save register values into EEPROM.
        eeprom_save_registers();

        break;

    case TWI_CMD_REGISTERS_RESTORE:

        // Restore register values into EEPROM.
        eeprom_restore_registers();

        break;

    case TWI_CMD_REGISTERS_DEFAULT:

        // Restore register values to factory defaults.
        registers_defaults();
        break;

    case TWI_CMD_EEPROM_ERASE:

        // Erase the EEPROM.
        eeprom_erase();

        break;

    case TWI_CMD_VOLTAGE_READ:

        // Request a voltage reading.
        adc_read_voltage();

        break;

#if CURVE_MOTION_ENABLED
    case TWI_CMD_CURVE_MOTION_ENABLE:

        // Enable curve motion handling.
        motion_enable();

        break;

    case TWI_CMD_CURVE_MOTION_DISABLE:

        // Disable curve motion handling.
        motion_disable();

        break;

    case TWI_CMD_CURVE_MOTION_RESET:

        // Reset the motion to the current position.
#if ENCODER_ENABLED
        motion_reset(enc_get_position_value());
#else
        motion_reset(adc_get_position_value());
#endif

        break;

    case TWI_CMD_CURVE_MOTION_APPEND:

        // Append motion curve data stored in the registers.
        motion_append();

        break;
#endif

    default:

        // Ignore unknown command.
        break;
    }
}