void dc_init(void) { #ifdef ENCODER pid_init(&pid, PID_K_P_DEFAULT, PID_K_I_DEFAULT, PID_K_D_DEFAULT, PID_SAMPLE_TIME_DEFAULT, PID_MAX_OUT_DEFAULT, PID_MIN_OUT_DEFAULT); encoder_init(); #endif Pin dc_pins[] = {PINS_DC}; PIO_Configure(dc_pins, PIO_LISTSIZE(dc_pins)); // Configure and enable power measurements Pin dc_power_management_pins[] = {VOLTAGE_STACK_PIN, VOLTAGE_EXTERN_PIN, VOLTAGE_STACK_SWITCH_PIN, CURRENT_CONSUMPTION_PIN}; PIO_Configure(dc_power_management_pins, PIO_LISTSIZE(dc_power_management_pins)); // Initialize PWM PMC->PMC_PCER0 = 1 << ID_PWM; dc_update_pwm_frequency(); adc_channel_enable(VOLTAGE_EXTERN_CHANNEL); adc_channel_enable(VOLTAGE_STACK_CHANNEL); adc_channel_enable(CURRENT_CONSUMPTION_CHANNEL); }
OSStatus platform_adc_take_sample( const platform_adc_t* adc, uint16_t* output ) { OSStatus err = kNoErr; platform_mcu_powersave_disable(); require_action_quiet( adc != NULL, exit, err = kParamErr); channel_num = adc->channel; adc_channel_enable(ADC, adc->channel); adc_set_callback(ADC, adc->interrupt, adc_end_conversion, 1); /* Start conversion */ adc_start_software_conversion(ADC); adc_start_calibration(ADC); while (adc_get_interrupt_status(ADC) & (1 << adc->channel)); *output = adc_channel_get_value(ADC, adc->channel); mico_thread_msleep(1); adc_channel_disable(ADC, adc->channel); exit: platform_mcu_powersave_enable(); return err; }
void constructor(void) { _Static_assert(sizeof(BrickContext) <= BRICKLET_CONTEXT_MAX_SIZE, "BrickContext too big"); PIN_OVER_CURRENT.type = PIO_INPUT; PIN_OVER_CURRENT.attribute = PIO_PULLUP; BA->PIO_Configure(&PIN_OVER_CURRENT, 1); BC->over_current = false; BC->current_avg = 0; adc_channel_enable(BS->adc_channel); uint16_t data; BA->bricklet_select(BS->port - 'a'); BA->i2c_eeprom_master_read(BA->twid->pTwi, EEPROM_POSITION, (char *)&data, 2); BA->bricklet_deselect(BS->port - 'a'); if(data == 0xFFFF) { BC->offset = 0; } else { BC->offset = data; } simple_constructor(); }
/** * \brief Application entry point. * * \return Unused (ANSI-C compatibility). */ int main(void) { int32_t ul_vol; int32_t ul_temp; /* Initialize the SAM system. */ sysclk_init(); board_init(); configure_console(); /* Output example information. */ puts(STRING_HEADER); adc_enable(); struct adc_config adc_cfg; adc_get_config_defaults(&adc_cfg); adc_init(ADC, &adc_cfg); adc_channel_enable(ADC,ADC_TEMPERATURE_SENSOR); adc_set_trigger(ADC, ADC_TRIG_SW); struct adc_temp_sensor_config adc_temp_sensor_cfg; adc_temp_sensor_get_config_defaults(&adc_temp_sensor_cfg); adc_temp_sensor_set_config(ADC, &adc_temp_sensor_cfg); adc_set_callback(ADC, ADC_INTERRUPT_EOC_16, adc_temp_sensor_end_conversion, 1); adc_start_software_conversion(ADC); while (1) { if (is_conversion_done == true) { is_conversion_done = false; ul_vol = g_ul_value * VOLT_REF / MAX_DIGITAL; /* * According to datasheet, The output voltage VT = 1.44V at 27C * and the temperature slope dVT/dT = 4.7 mV/C */ ul_temp = (ul_vol - 1440) * 100 / 470 + 27; printf("-- Temperature is: %4d\r\n", (int)ul_temp); delay_ms(1000); adc_start_software_conversion(ADC); } } }
bool adc_config (adc_t adc) { adc_channel_enable (adc); if (adc == adc_config_last) return 1; adc_config_last = adc; /* Set mode register. */ ADC->ADC_MR = adc->MR; return 1; }
/** * \brief Configure the ADC for the light sensor. */ static void configure_adc(void) { struct adc_config adc_cfg; /* Configure ADC pin for light sensor. */ gpio_configure_pin(LIGHT_SENSOR_GPIO, LIGHT_SENSOR_FLAGS); /* Enable ADC clock. */ pmc_enable_periph_clk(ID_ADC); /* Configure ADC. */ adc_enable(); adc_get_config_defaults(&adc_cfg); adc_init(ADC, &adc_cfg); adc_channel_enable(ADC, ADC_CHANNEL_0); adc_set_trigger(ADC, ADC_TRIG_SW); }
/** * \brief Application entry point. * * \return Unused (ANSI-C compatibility). */ int main(void) { int32_t ul_vol; /* Initialize the SAM system. */ sysclk_init(); board_init(); configure_console(); /* Output example information. */ puts(STRING_HEADER); adc_enable(); struct adc_config adc_cfg; adc_get_config_defaults(&adc_cfg); adc_init(ADC, &adc_cfg); adc_channel_enable(ADC, ADC_CHANNEL_11); adc_set_trigger(ADC, ADC_TRIG_TIO_CH_0); adc_set_callback(ADC, ADC_INTERRUPT_EOC_11, adc_end_conversion, 1); set_adc_resolution(); adc_start_calibration(ADC); /* Configure TC */ configure_tc_trigger(); while (1) { /* Check if ADC sample is done. */ if (is_conversion_done == true) { ul_vol = g_ul_value * VOLT_REF / g_max_digital; printf("-- Voltage is: %4dmv\r\n", (int)ul_vol); is_conversion_done = false; } } }
void stepper_init(void) { Pin pins_stepper[] = {PINS_STEPPER}; PIO_Configure(pins_stepper, PIO_LISTSIZE(pins_stepper)); Pin stepper_power_management_pins[] = {VOLTAGE_STACK_PIN, VOLTAGE_EXTERN_PIN, VOLTAGE_STACK_SWITCH_PIN, STEPPER_CURRENT_PIN}; PIO_Configure(stepper_power_management_pins, PIO_LISTSIZE(stepper_power_management_pins)); // Initialize and enable DACC to set VREF and DECAY pins DACC_Initialize(DACC, ID_DACC, 0, // Hardware triggers are disabled 0, // External trigger 0, // Half-Word Transfer 0, // Normal Mode (not sleep mode) BOARD_MCK, 1, // refresh period 0, // Channel 0 selection 1, // Tag Selection Mode enabled 16); // value of the start up time DACC_EnableChannel(DACC, VREF_CHANNEL); DACC_EnableChannel(DACC, DECAY_CHANNEL); // Enable peripheral clock for TC PMC->PMC_PCER0 = 1 << ID_TC0; // Configure and enable TC interrupts NVIC_DisableIRQ(TC0_IRQn); NVIC_ClearPendingIRQ(TC0_IRQn); NVIC_SetPriority(TC0_IRQn, PRIORITY_STEPPER_TC0); NVIC_EnableIRQ(TC0_IRQn); tc_channel_init(&STEPPER_TC_CHANNEL, TC_CMR_TCCLKS_TIMER_CLOCK5 | TC_CMR_CPCTRG); // Interrupt in compare tc_channel_interrupt_set(&STEPPER_TC_CHANNEL, TC_IER_CPCS); PMC->PMC_PCER0 = 1 << ID_TC1; tc_channel_init(&SINGLE_SHOT_TC_CHANNEL, TC_CMR_WAVE | TC_CMR_TCCLKS_TIMER_CLOCK4 | TC_CMR_EEVT_XC0 | TC_CMR_ASWTRG_SET | TC_CMR_ACPC_CLEAR | TC_CMR_WAVSEL_UP | TC_CMR_CPCDIS | TC_CMR_CPCSTOP); SINGLE_SHOT_COUNTER = 1; tc_channel_start(&SINGLE_SHOT_TC_CHANNEL); stepper_set_output_current(VREF_DEFAULT_CURRENT); stepper_set_step_mode(STEP_MODE_EIGTH); stepper_set_decay(DECAY_DEFAULT_VALUE); adc_channel_enable(VOLTAGE_EXTERN_CHANNEL); adc_channel_enable(VOLTAGE_STACK_CHANNEL); adc_channel_enable(STEPPER_CURRENT_CHANNEL); }