//*** Private Function ***// void adc_init(){ ADCSRB=0; ADCSRA=(1<<ADEN)|(1<<ADIF)|ADC_Prescaler; _delay_ms(10); ADCSRA=ADC_START; //First Convert *25 ADC Clock* adc_wait(); }
uint16_t adc_convert(uint8_t type){ ADMUX=ADMUX_DEF|type; #ifdef ADC_CONVERT_TYPE_SLEEP set_sleep_mode(SLEEP_MODE_ADC); sei(); sleep_mode(); #else ADCSRA=ADC_START; adc_wait(); #endif return ADC; }
int main() { initialize(); UBRR0H = UBRRH_VALUE; UBRR0L = UBRRL_VALUE; UCSR0B = (1 << RXCIE0) | (1 << RXEN0) | (1 << TXEN0); UCSR0C = (1<<UCSZ00) | (1<<UCSZ01); // enable interrupts for the timer sei(); /* Timer aufsetzen: nach 1 ms soll der Interrupt ausgelöst werden. */ /* 8 bit counter (TIMER0) */ /* normal mode */ TCCR0A = 0; /* CLK/64 */ TCCR0B = (1 << CS01) | (1 << CS00); /* timer ticks: 250 */ TCNT0 = 5; TIMSK0 = (1 << TOIE0); TIFR0 = (1 << TOV0); adcInit(); int16_t ii = 0; int16_t jj = 0; wdt_enable(WDTO_2S); /* for(;;i++) { uart_bin(PINB); uart_puts(" "); uart_bin(PIND); uart_puts(" "); uart_hex16(i); uart_puts("\r\n"); _delay_ms(1000); wdt_reset(); } */ uart_puts("Starting up\r\n"); for (ii = 0; ii < 4; ii++) { positions[ii] = 0; } // Count the number of cycles the autofocus is stuck in mid-state uint8_t mid_state_counter = 0; // Count the number of successive movements in the same direction before increasing speed uint16_t movement_counter = 0; uint16_t current_delay = INITIAL_DELAY; enum movement{NONE, UP, DOWN, TILT_DOWN, TILT_UP, LEVEL, EMERGENCY} last_movement, movement; last_movement = NONE; movement = NONE; // Reset autofocus position /* while(1) { adc_start_conversion(7); const uint16_t autofocus_result = adc_wait(); uart_hex16(autofocus_result); wdt_reset(); uart_puts("\r\n"); } // */ uint8_t autofocus_hit_counter = 0; movement_counter = 0; current_delay = INITIAL_DELAY; initialize(); adcInit(); adc_start_conversion(); uint16_t autofocus_result = adc_wait(); uint16_t no_movement_counter = 0; autofocusPosition = 2*HEIGHT_LIMIT; for(ii = 0; ; ii++){ wdt_reset(); //* if (current_delay > 1) { for (jj = 0; jj < current_delay; jj++) { _delay_us(1); } } // */ #ifdef DEBUG uart_hex16(current_delay); uart_puts("\r\n"); #endif // True if autofocus endstop not yet hit. const bool autofocus_switch_clear = (autofocus_result <= OPEN_VALUE + INTERVAL_SIZE && autofocus_result >= OPEN_VALUE - INTERVAL_SIZE); bool autofocus_clear = true; if (!autofocus_switch_clear) { if (autofocus_hit_counter > 10) { autofocus_clear = false; if ((ii % 1024) == 0) { uart_puts("Autofocus hit at: "); printPositions(); autofocusPosition = positions[0]; uart_puts("\r\n"); } if (autofocus_result < SHORT_CIRCUIT_VALUE) { uart_puts("Autofocus has short circuit, value is: "); uart_hex16(autofocus_result); uart_puts("\r\n"); } else if (autofocus_result > CABLE_BROKEN_VALUE) { uart_puts("Autofocus cable is broken, value is: "); uart_hex16(autofocus_result); uart_puts("\r\n"); } else if(autofocus_result > OPEN_VALUE + INTERVAL_SIZE && autofocus_result < CLOSED_VALUE - INTERVAL_SIZE) { if (mid_state_counter > 10) { uart_puts("Autofocus stuck in mid-state, value is: 0x"); uart_hex16(autofocus_result); uart_puts("\r\n"); } else { mid_state_counter++; } } } // Increment autofocus_hit_counter in order to detect series of hit autofocus. if (autofocus_hit_counter < 20) { autofocus_hit_counter++; } } else { mid_state_counter = 0; autofocus_hit_counter = 0; } movement = NONE; if (!buttonEmergency()) { if (buttonDown()) { if (!anyStopReached()) { stepAllDown(); movement = DOWN; } _delay_us(2.08); } else if (buttonUp()) { if (!anyHeightLimitReached() && autofocus_clear) { stepAllUp(); movement = UP; } } else if (buttonTiltFrontUp()) { if (!anyStopReached() && !anyHeightLimitReached() && (positions[3]-positions[1] < TILT_LIMIT) && autofocus_clear) { stepUp3(); stepDown1(); movement = TILT_UP; } _delay_us(0.96); } else if (buttonTiltFrontDown()) { if (!anyStopReached() && !anyHeightLimitReached() && (positions[1]-positions[3] < TILT_LIMIT) && autofocus_clear) { stepDown3(); stepUp1(); movement = TILT_DOWN; } _delay_us(1.52); } else if (buttonLevel()) { if (!anyStopReached() && autofocus_clear) { if (positions[1] > positions[3]+1) { stepDown1(); stepUp3(); movement = LEVEL; _delay_us(0.72); } else if (positions[1]+1 < positions[3]) { stepDown3(); stepUp1(); movement = LEVEL; } } _delay_us(2.22); } else if (buttonFocus()) { if ((positions[0] > (autofocusPosition - FOCUS_DISTANCE)) && !anyStopReached()) { stepAllDown(); movement = DOWN; } else if ((positions[0] < (int32_t)(autofocusPosition - FOCUS_DISTANCE)) && autofocus_clear) { stepAllUp(); movement = UP; } // Needed to fulfill the 2us hold constraint of the step pin } } if (movement == last_movement && movement != NONE) { if (movement_counter > 500) { current_delay--; if (current_delay < MIN_DELAY) { current_delay = MIN_DELAY; } movement_counter = 0; } movement_counter += current_delay; } else { current_delay = INITIAL_DELAY; movement_counter = 0; } // Do all the emergency stuff if (buttonEmergency()) { _delay_us(17); movement = EMERGENCY; if (buttonEmergency() && buttonFocus()) { gotoEndstops(); } if (buttonUp()) { if (buttonTiltFrontDown()) { stepUp1(); } if (buttonLevel()) { stepUp2(); } if (buttonTiltFrontUp()) { stepUp3(); } } if (buttonDown()) { if (buttonTiltFrontDown()) { stepDown1(); } if (buttonLevel()) { stepDown2(); } if (buttonTiltFrontUp()) { stepDown3(); } } if (buttonLevel()) { int32_t maxPosition = max(positions[1], max(positions[2], positions[3])); if (positions[1] < maxPosition) { stepUp1(); } if (positions[2] < maxPosition) { stepUp2(); } if (positions[3] < maxPosition) { stepUp3(); } } // In case of emergency, introduce a safe delay for the step hold time _delay_us(2); } last_movement = movement; if (movement == NONE) { no_movement_counter++; if (no_movement_counter > 2e3) { sleep(); no_movement_counter = 0; printPositions(); uart_puts("\r\n"); } } else { no_movement_counter = 0; if (sleeping) { wakeup(); _delay_ms(100); } } if(!(ADCSRA & (1<<ADSC))) { // auf Abschluss der Konvertierung warten autofocus_result = ADCW; adc_start_conversion(); } _delay_us(1.08); unsetStep1; unsetStep2; unsetStep3; } }