/** * \brief Initialize ADC. */ static void demo_config_adc( void ) { /* Enable peripheral clock. */ pmc_enable_periph_clk(ID_ADC); /* Initialize ADC. */ /* * Formula: ADCClock = MCK / ( (PRESCAL+1) * 2 ) * For example, MCK = 64MHZ, PRESCAL = 4, then: * ADCClock = 64 / ((4+1) * 2) = 6.4MHz; */ /* Formula: * Startup Time = startup value / ADCClock * Startup time = 64 / 6.4MHz = 10 us */ adc_init(ADC, sysclk_get_cpu_hz(), 6400000, ADC_STARTUP_TIME_4); /* Formula: * Transfer Time = (TRANSFER * 2 + 3) / ADCClock * Tracking Time = (TRACKTIM + 1) / ADCClock * Settling Time = settling value / ADCClock * * Transfer Time = (1 * 2 + 3) / 6.4MHz = 781 ns * Tracking Time = (1 + 1) / 6.4MHz = 312 ns * Settling Time = 3 / 6.4MHz = 469 ns */ adc_configure_timing(ADC, TRACKING_TIME, ADC_SETTLING_TIME_3, TRANSFER_PERIOD); adc_check(ADC, sysclk_get_cpu_hz()); /* Hardware trigger TIOA0. */ adc_configure_trigger(ADC, ADC_TRIG_TIO_CH_1, 0); /* Enable channels for x,y and z. */ adc_enable_channel(ADC, ADC_CHANNEL_POTENTIOMETER); /* Configure TC. */ demo_configure_tc0(); }
/** * \brief Initialize ADC. */ static void demo_config_adc( void ) { /* Enable peripheral clock. */ pmc_enable_periph_clk(ID_ADC); /* Initialize ADC. */ /* startup = 10: 640 periods of ADCClock * for prescale = 4 * prescale: ADCClock = MCK / ( (PRESCAL+1) * 2 ) => 64MHz / * ((4+1)*2) = 6.4MHz * ADC clock = 6.4 MHz */ adc_init(ADC, sysclk_get_cpu_hz(), 6400000, 10); adc_configure_timing(ADC, 0, ADC_SETTLING_TIME_3, 1); adc_check(ADC, sysclk_get_cpu_hz()); /* Hardware trigger TIOA0. */ adc_configure_trigger(ADC, ADC_TRIG_TIO_CH_1, 0); /* Enable channels for x,y and z. */ adc_enable_channel(ADC, ADC_CHANNEL_POTENTIOMETER); /* Configure TC. */ demo_configure_tc0(); }
/** * \brief adc_temp_sensor Application entry point. * * Initialize adc to 12-bit, enable channel 15,turn on * temp sensor, pdc channel interrupt for temp sensor * and start conversion. * * \return Unused (ANSI-C compatibility). */ int main(void) { /* Initialize the SAM system. */ sysclk_init(); board_init(); /* Disable watchdog. */ WDT->WDT_MR = WDT_MR_WDDIS; configure_console(); /* Output example information. */ puts(STRING_HEADER); /* 10 ms timer */ if (SysTick_Config(sysclk_get_cpu_hz() / 100)) { puts("-F- Systick configuration error\r"); while (1) { } } /* Enable peripheral clock. */ pmc_enable_periph_clk(ID_ADC); /* Initialize ADC. */ /* startup = 8: 512 periods of ADCClock * for prescale = 4 * prescale: ADCClock = MCK / ( (PRESCAL+1) * 2 ) => 64MHz / ((4+1)*2) = 6.4MHz * ADC clock = 6.4 MHz */ adc_init(ADC, sysclk_get_cpu_hz(), 6400000, 8); adc_configure_timing(ADC, 0, ADC_SETTLING_TIME_3, 1); adc_configure_trigger(ADC, ADC_TRIG_SW, 0); adc_check(ADC, sysclk_get_cpu_hz()); /* Enable channel for potentiometer. */ adc_enable_channel(ADC, ADC_TEMPERATURE_SENSOR); /* Enable the temperature sensor. */ adc_enable_ts(ADC); /* Enable ADC interrupt. */ NVIC_EnableIRQ(ADC_IRQn); /* Start conversion. */ adc_start(ADC); adc_read_buffer(ADC, gs_s_adc_values, BUFFER_SIZE); /* Enable PDC channel interrupt. */ adc_enable_interrupt(ADC, ADC_ISR_RXBUFF); while (1) { } }
void rt_adc_thread_entry(void* parameter) { rt_thread_t init_thread; u16 k; rt_hw_adc_init(); //adc_io_pin_init(); while(1) { adc_check(); rt_thread_delay(RT_TICK_PER_SECOND/2); } }
void poll_adc(void) { uint8_t i = poll_this; adc_t *pp; if (i >= N_ADC) { i=0; adc_changed_cache = max_seen; max_seen=0; } pp = &adcs[i]; if (adc_check(pp)) { i += 1; if (pp->flags & (ADC_IS_ALERT_L|ADC_IS_ALERT_H)) max_seen = i; } poll_this=i; }
/** * \brief Example entry point. * * Initialize ADC to 12-bit, enable channel "ADC_CHANNEL_POTENTIOMETER", then * enable hardware trigger with TIOA0 every second. Finally, start conversion. * * \return Unused (ANSI-C compatibility). */ int main(void) { uint8_t c_choice; int16_t s_adc_value; int16_t s_threshold = 0; /* Initialize the SAM system. */ sysclk_init(); board_init(); configure_console(); /* Output example information. */ puts(STRING_HEADER); /* Initialize threshold. */ gs_us_low_threshold = 0x0; gs_us_high_threshold = MAX_DIGITAL; /* Enable peripheral clock. */ pmc_enable_periph_clk(ID_ADC); /* Initialize ADC. */ /* startup = 10: 640 periods of ADCClock * for prescale = 4 * prescale: ADCClock = MCK / ( (PRESCAL+1) * 2 ) => 64MHz / ((4+1)*2) = 6.4MHz * ADC clock = 6.4 MHz */ adc_init(ADC, sysclk_get_cpu_hz(), 6400000, 10); #if SAM3S || SAM3XA || SAM4S adc_configure_timing(ADC, 0, ADC_SETTLING_TIME_3, 1); #elif SAM3N adc_configure_timing(ADC, 0); #endif adc_check(ADC, sysclk_get_cpu_hz()); /* Hardware trigger TIOA0. */ adc_configure_trigger(ADC, ADC_TRIG_TIO_CH_0, 0); /* Enable channels for x,y and z. */ adc_enable_channel(ADC, ADC_CHANNEL_POTENTIOMETER); /* Configure TC. */ configure_tc0(); /* Channel 5 has to be compared. */ adc_set_comparison_channel(ADC, ADC_CHANNEL_POTENTIOMETER); /* Compare mode, in the window. */ adc_set_comparison_mode(ADC, ADC_EMR_CMPMODE_IN); /* Set up Threshold. */ adc_set_comparison_window(ADC, gs_us_high_threshold, gs_us_low_threshold); /* Enable ADC interrupt. */ NVIC_EnableIRQ(ADC_IRQn); /* Start TC0 and hardware trigger. */ tc_start(TC0, 0); /* Display main menu. */ display_menu(); while (1) { while (uart_read(CONSOLE_UART, &c_choice)) { } printf("%c\r\n", c_choice); switch (c_choice) { case '0': s_adc_value = adc_get_channel_value(ADC, ADC_CHANNEL_POTENTIOMETER); printf("-I- Current voltage is %d mv, %d%% of ADVREF\n\r", (s_adc_value * VOLT_REF / MAX_DIGITAL), (s_adc_value * 100 / MAX_DIGITAL)); break; case '1': puts("Low threshold is set to(mv):"); s_threshold = get_voltage(); puts("\r"); if (s_threshold >= 0) { s_adc_value = s_threshold * MAX_DIGITAL / VOLT_REF; adc_set_comparison_window(ADC, s_adc_value, gs_us_high_threshold); /* Renew low threshold. */ gs_us_low_threshold = s_adc_value; float f_low_threshold = (float)gs_us_low_threshold * VOLT_REF / MAX_DIGITAL; uint32_t ul_low_threshold = f_to_int(f_low_threshold); printf("Setting low threshold to %u mv (reg value to 0x%x ~%d%%)\n\r", ul_low_threshold, gs_us_low_threshold, gs_us_low_threshold * 100 / MAX_DIGITAL); } break; case '2': puts("High threshold is set to(mv):"); s_threshold = get_voltage(); puts("\r"); if (s_threshold >= 0) { s_adc_value = s_threshold * MAX_DIGITAL / VOLT_REF; adc_set_comparison_window(ADC, gs_us_low_threshold, s_adc_value); /* Renew high threshold. */ gs_us_high_threshold = s_adc_value; float f_high_threshold = (float)gs_us_high_threshold * VOLT_REF / MAX_DIGITAL; uint32_t ul_high_threshold = f_to_int(f_high_threshold); printf("Setting high threshold to %u mv (reg value to 0x%x ~%d%%)\n\r", ul_high_threshold, gs_us_high_threshold, gs_us_high_threshold * 100 / MAX_DIGITAL); } break; case '3': puts("-a. Below low threshold.\n\r" "-b. Above high threshold.\n\r" "-c. In the comparison window.\n\r" "-d. Out of the comparison window.\n\r" "-q. Quit the setting.\r"); c_choice = get_comparison_mode(); adc_set_comparison_mode(ADC, c_choice); printf("Comparison mode is %c.\n\r", 'a' + c_choice); break; case 'm': case 'M': display_menu(); break; case 'i': case 'I': display_info(); break; case 's': case 'S': enter_asleep(); break; } puts("Press \'m\' or \'M\' to display the main menu again!\r"); } }