diff --git a/Geigerzaehler/main.c b/Geigerzaehler/main.c index 0eb2be1..a64f973 100644 --- a/Geigerzaehler/main.c +++ b/Geigerzaehler/main.c @@ -15,22 +15,28 @@ #include #include -// Global variable declaration -uint16_t boost_frequency = 2000; // 2kHz frequency -uint16_t signalOutput_time = 200; // 200ms - -uint8_t boost_highVoltage_nextHigh = 1; -uint16_t boost_highVoltage_nextTick = 1; - -uint8_t signalOutput_nextHigh = 1; -uint16_t signalOutput_nextTick = 0; +/* + * Global variable declaration + */ +// Set frequencys and general stuff +uint16_t boost_frequency = 2000; // 2kHz frequency +uint16_t signalOutput_piezo_time = 200; // 200ms +uint16_t signalOutput_led_time = 200; // 200ms -uint8_t timer1_prescaler = 1; +// Used for execution-timing (with timer) [0 = disabled, >0 = nextTimerTick] (1 is next avaible execution) +uint16_t boost_highVoltage_nextTick = 1; // Execute at start +uint16_t signalOutput_piezo_nextTick = 0; +uint16_t signalOutput_led_nextTick = 0; -uint16_t test = 0; +// Prescaling of timer [timer-speed: cpu-clock / prescaler] +// Modes: [1, 8, 64, 256, 1024] +uint8_t timer1_prescaler = 256; int main() { + /* + * Init + */ // Inputs DDRD &= (0 << PORTD2); // Activate input PD2 DDRD &= (0 << PORTD3); // Activate input PD3 @@ -44,40 +50,29 @@ int main() DDRD |= (1 << PORTD6); DDRB |= (1 << PORTB5); // Onboard-Led - // Interrupt for INT0 Pin-D2 High-voltage check + // Interrupts + // for INT0 Pin-D2 High-voltage check EICRA |= (0 << ISC01)|(1 << ISC00); // On at any edge [Code: 01] EIMSK |= (1 << INT0); // Activate Interrupt INT0 - - // Interrupt for INT1 Pin-D3 Counter-click + // for INT1 Pin-D3 Counter-click EICRA |= (1 << ISC11)|(1 << ISC10); // On rising edge [Code: 11] EIMSK |= (1 < 0 && (uint16_t)TCNT1 >= boost_highVoltage_nextTick) // If we are on or after the tick it should be executed { - // Set pin according to next exec - if(boost_highVoltage_nextHigh == 1) - { - // Set it high - PORTD |= (1 << PORTD4); - boost_highVoltage_nextHigh = 0; // Next is low - } - else - { - // Set it low - PORTD |= (0 << PORTD4); - boost_highVoltage_nextHigh = 1; // Next is high - } + // Flip pin state + PORTD ^= (1 << PORTD4); // Calculate when next high/low should be set boost_highVoltage_nextTick = (uint16_t)TCNT1 + F_CPU/timer1_prescaler * 1/(boost_frequency/2); // Half of time it should be high/low @@ -87,29 +82,41 @@ int main() /* * Signal output */ - if(signalOutput_nextTick > 0 && (uint16_t)TCNT1 >= signalOutput_nextTick) // If we are on or after the tick it should be executed + // Piezo + if(signalOutput_piezo_nextTick > 0 && (uint16_t)TCNT1 >= signalOutput_piezo_nextTick) // If we are on or after the tick it should be executed { - // Set pin according to next exec - if(signalOutput_nextHigh == 1) // Set it high + // Flip pin state + PORTD ^= (1 << PORTD5); + + if(!(PIND & (1 << PORTD5))) { - PORTD |= (1 << PORTD5); - PORTD |= (1 << PORTD6); - signalOutput_nextHigh = 0; // Next is low - - // When the signal should stop - signalOutput_nextTick = (uint16_t)TCNT1 + F_CPU/timer1_prescaler * signalOutput_time/1000; // nextTick is in signalOutput_time in ms - if(signalOutput_nextTick == 0) signalOutput_nextTick++; // If its 0, it stops, we dont want that + // We are on low (again), disable further execution + signalOutput_piezo_nextTick = 0; } - else // Set it low + + // When the signal should next flip + signalOutput_piezo_nextTick = (uint16_t)TCNT1 + F_CPU/timer1_prescaler * signalOutput_piezo_time/1000; // Calculate nextTick based on time in ms + if(signalOutput_piezo_nextTick == 0) signalOutput_piezo_nextTick++; // If its 0, it stops, we dont want that + } + // LED + if(signalOutput_led_nextTick > 0 && (uint16_t)TCNT1 >= signalOutput_led_nextTick) // If we are on or after the tick it should be executed + { + // Flip pin state + PORTD ^= (1 << PORTD6); + + if(!(PIND & (1 << PORTD6))) { - PORTD |= (0 << PORTD5); - PORTD |= (0 << PORTD6); - signalOutput_nextHigh = 1; // Next is high - - signalOutput_nextTick = 0; // Disable signalOutput + // We are on low (again), disable further execution + signalOutput_led_nextTick = 0; } + + // When the signal should next flip + signalOutput_led_nextTick = (uint16_t)TCNT1 + F_CPU/timer1_prescaler * signalOutput_led_time/1000; // Calculate nextTick based on time in ms + if(signalOutput_led_nextTick == 0) signalOutput_led_nextTick++; // If its 0, it stops, we dont want that } } + + sei(); // Activate global interrupts (after checks) [when an interrupt was fired, it will now be executed] } /* @@ -149,7 +156,6 @@ void timer1_init() /* - * Interrupt-handler * ISR - Interrupt service routine */ // Address: 0x001 INT0 - On any edge [Code: 01]