main(void) { serial_baud_9600(); serial_mode_8e1(); serial_transmitter_enable(); sleep_mode_idle(); /* setup timer2 to trigger interrupt a * once every millisecond * 128 * (124 + 1) / 16MHz = 1ms */ timer2_mode_ctc(); timer2_clock_d128(); timer2_compare_a_set(124); timer2_interrupt_a_enable(); adc_reference_internal_5v(); adc_pin_select(5); adc_clock_d128(); adc_trigger_freerunning(); adc_trigger_enable(); adc_interrupt_enable(); adc_enable(); sei(); while (1) { char *p; p = sprint_uint16_b10(buf, value); *p++ = '\n'; *p = '\0'; serial_puts(buf); } }
/* * alt_adc_register_callback * * Associate a user-specific routine with the adc interrupt handler. * If a callback is registered, all enable interrupts that will cause * the callback to be executed. The callback runs as part of the interrupt * service routine, and great care must be taken to follow the guidelines * for acceptable interrupt service routine behaviour as described in the * Nios II Software Developer's Handbook. * * Note: To disable callbacks and interrupt after registering one, this routine * may be called passing 0x0 to the callback argument. * * Arguments: * - *dev: Pointer to adc device (instance) structure. * - callback: Pointer to callback routine to execute at interrupt level * - *context: Pointer to adc device structure. * - sample_store_base: Base address of the sample store micro core. */ void alt_adc_register_callback( alt_modular_adc_dev *dev, alt_adc_callback callback, void *context, alt_u32 sample_store_base) { dev->callback = callback; dev->callback_context = context; dev->sample_store_base = sample_store_base; if(NULL != callback) { adc_clear_interrupt_status(sample_store_base); adc_interrupt_enable(sample_store_base); } else { adc_interrupt_disable(sample_store_base); adc_clear_interrupt_status(sample_store_base); } return ; }
main() { uint8_t state = 0; serial_baud_9600(); serial_mode_8e1(); serial_transmitter_enable(); sleep_mode_idle(); pin13_mode_output(); pin13_low(); /* setup timer2 to trigger interrupt a * once every millisecond * 128 * (124 + 1) / 16MHz = 1ms */ timer2_mode_ctc(); timer2_clock_d128(); timer2_compare_a_set(124); timer2_interrupt_a_enable(); adc_reference_internal_5v(); adc_pin_select(5); adc_clock_d128(); adc_trigger_freerunning(); adc_trigger_enable(); adc_interrupt_enable(); adc_enable(); sei(); while (1) { uint16_t value; cli(); if (!new_value) { sleep_enable(); sei(); sleep_cpu(); sleep_disable(); continue; } sei(); value = adc_data(); new_value = 0; if (state && value < BOUND_LOW) { uint16_t now = time; char *p; timer2_clock_reset(); time = 0; pin13_low(); p = sprint_uint16_b10(buf, now); *p++ = '\n'; *p = '\0'; serial_puts(buf); state = 0; continue; } if (value > BOUND_HIGH) { pin13_high(); state = 1; } } }