/** * \brief Callback function for ADCIFE enter compasion window interrupt. */ static void adcife_set_wm_flag(void) { /* Disable Window Monitor Interrupt. */ adc_disable_interrupt(&g_adc_inst, ADC_WINDOW_MONITOR); g_uc_enter_win_flag = 1; adc_clear_status(&g_adc_inst, ADCIFE_SCR_WM); }
/** * \brief Callback function for ADCIFE interrupt. */ static void adcife_read_conv_result(void) { if ((adc_get_status(&g_adc_inst) & ADCIFE_SR_SEOC) == ADCIFE_SR_SEOC) { g_adc_sample_data[0] = adc_get_last_conv_value(&g_adc_inst); g_uc_condone_flag = 1; adc_clear_status(&g_adc_inst, ADCIFE_SCR_SEOC); } }
/** * \brief Callback function for ADCIFE interrupt. */ static void adcife_set_conv_flag(void) { if ((adc_get_status(&g_adc_inst) & ADCIFE_SR_SEOC) == ADCIFE_SR_SEOC) { g_uc_condone_flag = 1; adc_clear_status(&g_adc_inst, ADCIFE_SCR_SEOC); adc_disable_interrupt(&g_adc_inst, ADC_SEQ_SEOC); } }
uint16_t getLightSens() { uint16_t result = 0; adc_start_conversion(&adc1); while(adc_get_status(&adc1) != ADC_STATUS_RESULT_READY); adc_read(&adc1, &result); result = result * 16; adc_clear_status(&adc1, ADC_STATUS_RESULT_READY); return result; }
/** * \brief Callback function for ADCIFE interrupt. */ static void adcife_wm_handler(void) { uint32_t ul_mode; uint16_t us_adc; /* Disable Window Monitor Interrupt. */ adc_disable_interrupt(&g_adc_inst, ADC_WINDOW_MONITOR); if ((adc_get_status(&g_adc_inst) & ADCIFE_SR_WM) == ADCIFE_SR_WM) { ul_mode = adc_get_wm_mode(&g_adc_inst); us_adc = adc_get_last_conv_value(&g_adc_inst); switch (ul_mode) { case 1: printf("-ISR-:DAC output voltage %d mv is above the low threshold:%d mv!\n\r", (int)(us_adc * VOLT_REF / MAX_DIGITAL), (int)(gs_us_low_threshold * VOLT_REF / MAX_DIGITAL)); break; case 2: printf("-ISR-:DAC output voltage %d mv is below the high threshold:%d mv!\n\r", (int)(us_adc * VOLT_REF / MAX_DIGITAL), (int)(gs_us_high_threshold * VOLT_REF / MAX_DIGITAL)); break; case 3: printf("-ISR-:DAC output voltage %d mv is in the comparison window:%d-%d mv!\n\r", (int)(us_adc * VOLT_REF / MAX_DIGITAL), (int)(gs_us_low_threshold * VOLT_REF / MAX_DIGITAL), (int)(gs_us_high_threshold * VOLT_REF / MAX_DIGITAL)); break; case 4: printf("-ISR-:DAC output voltage %d mv is out of the comparison window:%d-%d mv!\n\r", (int)(us_adc * VOLT_REF / MAX_DIGITAL), (int)(gs_us_low_threshold * VOLT_REF / MAX_DIGITAL), (int)(gs_us_high_threshold * VOLT_REF / MAX_DIGITAL)); break; } adc_clear_status(&g_adc_inst, ADCIFE_SCR_WM); } }
int main(void) { system_init(); delay_init(); //! [setup_init] configure_adc(); //! [setup_init] //! [main] //! [start_conv] adc_start_conversion(&adc_instance); //! [start_conv] //! [get_res] uint16_t result=0; configure_console(); //! [get_res] //! [inf_loop] while (1) { /* Infinite loop */ //adc_read(&adc_instance, &result); do { /* Wait for conversion to be done and read out result */ } while (adc_read(&adc_instance, &result) == STATUS_BUSY); printf("The result is %d\n",result); uint32_t far = 9.0/5.0*((float)result*.0002441406*6.0/.01)+32.0; printf(" The temp is %d", far); adc_clear_status(&adc_instance,adc_get_status(&adc_instance)); adc_start_conversion(&adc_instance); delay_ms(500); } //! [inf_loop] //! [main] }
/** * \brief Example entry point. * * \return Unused (ANSI-C compatibility). */ int main(void) { uint8_t c_choice; int16_t s_adc_value; int16_t s_dac_value; int16_t s_threshold = 0; float f_dac_data; uint32_t ul_dac_data; /* Initialize the SAM system. */ sysclk_init(); board_init(); configure_console(); /* Output example information. */ puts(STRING_HEADER); /* Initialize threshold. */ gs_us_low_threshold = 500; gs_us_high_threshold = 2000; struct adc_config adc_cfg = { /* System clock division factor is 16 */ .prescal = ADC_PRESCAL_DIV16, /* The APB clock is used */ .clksel = ADC_CLKSEL_APBCLK, /* Max speed is 150K */ .speed = ADC_SPEED_150K, /* ADC Reference voltage is 0.625*VCC */ .refsel = ADC_REFSEL_1, /* Enables the Startup time */ .start_up = CONFIG_ADC_STARTUP }; struct adc_seq_config adc_seq_cfg = { /* Select Vref for shift cycle */ .zoomrange = ADC_ZOOMRANGE_0, /* Pad Ground */ .muxneg = ADC_MUXNEG_1, /* DAC Internal */ .muxpos = ADC_MUXPOS_3, /* Enables the internal voltage sources */ .internal = ADC_INTERNAL_3, /* Disables the ADC gain error reduction */ .gcomp = ADC_GCOMP_DIS, /* Disables the HWLA mode */ .hwla = ADC_HWLA_DIS, /* 12-bits resolution */ .res = ADC_RES_12_BIT, /* Enables the single-ended mode */ .bipolar = ADC_BIPOLAR_SINGLEENDED }; struct adc_ch_config adc_ch_cfg = { .seq_cfg = &adc_seq_cfg, /* Internal Timer Max Counter */ .internal_timer_max_count = 60, /* Window monitor mode is off */ .window_mode = ADC_WM_MODE_3, /* The equivalent voltage value is 500 * VOLT_REF / 4095 = 251mv. */ .low_threshold = gs_us_low_threshold, /* The equivalent voltage value is 2000 * VOLT_REF / 4095 = 1002mv. */ .high_threshold = gs_us_high_threshold, }; start_dac(); if(adc_init(&g_adc_inst, ADCIFE, &adc_cfg) != STATUS_OK) { puts("-F- ADC Init Fail!\n\r"); while(1); } if(adc_enable(&g_adc_inst) != STATUS_OK) { puts("-F- ADC Enable Fail!\n\r"); while(1); } adc_ch_set_config(&g_adc_inst, &adc_ch_cfg); adc_configure_trigger(&g_adc_inst, ADC_TRIG_CON); adc_configure_gain(&g_adc_inst, ADC_GAIN_1X); adc_set_callback(&g_adc_inst, ADC_WINDOW_MONITOR, adcife_wm_handler, ADCIFE_IRQn, 1); /* Display main menu. */ display_menu(); while (1) { scanf("%c", (char *)&c_choice); printf("%c\r\n", c_choice); switch (c_choice) { case '0': adc_disable_interrupt(&g_adc_inst, ADC_WINDOW_MONITOR); printf("DAC output is set to(mv) from 0mv to %dmv: ", (int32_t)VOLT_REF); s_dac_value = get_voltage(); puts("\r"); f_dac_data = (float)s_dac_value * DACC_MAX_DATA / VDDANA; ul_dac_data = f_to_int(f_dac_data); if (s_dac_value >= 0) { dacc_write_conversion_data(DACC, ul_dac_data); } delay_ms(100); adc_clear_status(&g_adc_inst, ADCIFE_SCR_WM); adc_enable_interrupt(&g_adc_inst, ADC_WINDOW_MONITOR); break; case '1': adc_disable_interrupt(&g_adc_inst, ADC_WINDOW_MONITOR); printf("Low threshold is set to(mv) from 0mv to %dmv: ", (int32_t)VOLT_REF); s_threshold = get_voltage(); puts("\r"); if (s_threshold >= 0) { s_adc_value = s_threshold * MAX_DIGITAL / VOLT_REF; adc_configure_wm_threshold(&g_adc_inst, 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); } adc_clear_status(&g_adc_inst, ADCIFE_SCR_WM); adc_enable_interrupt(&g_adc_inst, ADC_WINDOW_MONITOR); break; case '2': adc_disable_interrupt(&g_adc_inst, ADC_WINDOW_MONITOR); printf("High threshold is set to(mv)from 0mv to %dmv:", (int32_t)VOLT_REF); s_threshold = get_voltage(); puts("\r"); if (s_threshold >= 0) { s_adc_value = s_threshold * MAX_DIGITAL / VOLT_REF; adc_configure_wm_threshold(&g_adc_inst, 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); } adc_clear_status(&g_adc_inst, ADCIFE_SCR_WM); adc_enable_interrupt(&g_adc_inst, ADC_WINDOW_MONITOR); break; case '3': adc_disable_interrupt(&g_adc_inst, ADC_WINDOW_MONITOR); puts("-a. Above low threshold.\n\r" "-b. Below 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_wm_mode(); adc_configure_wm_mode(&g_adc_inst, c_choice); printf("Comparison mode is %c.\n\r", 'a' + c_choice - 1); adc_clear_status(&g_adc_inst, ADCIFE_SCR_WM); adc_enable_interrupt(&g_adc_inst, ADC_WINDOW_MONITOR); break; case 'm': display_menu(); break; case 'i': display_info(); adc_clear_status(&g_adc_inst, ADCIFE_SCR_WM); adc_enable_interrupt(&g_adc_inst, ADC_WINDOW_MONITOR); break; } puts("Press \'m\' or \'M\' to display the main menu again!\r"); } }