From 3b78e14c2a7f7cddef9f459e6f515dd97a3cd7e5 Mon Sep 17 00:00:00 2001 From: Railz Date: Fri, 8 Mar 2019 19:47:51 +0100 Subject: [PATCH] Set frequency to 10kHz Fixed timer1_overflow_value not being correctly calculated --- Geigerzaehler/main.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/Geigerzaehler/main.c b/Geigerzaehler/main.c index 720403c..c291437 100644 --- a/Geigerzaehler/main.c +++ b/Geigerzaehler/main.c @@ -19,7 +19,7 @@ * Global variable declaration */ // Set frequencys and general stuff -uint16_t boost_frequency = 2000; // 2kHz frequency +uint16_t boost_frequency = 10000; // 10kHz frequency uint16_t signalOutput_piezo_time = 200; // 200ms uint16_t signalOutput_led_time = 200; // 200ms @@ -31,7 +31,7 @@ uint16_t signalOutput_led_nextTick = 0; // Prescaling of timer [timer-speed: cpu-clock / prescaler] // Modes: [1, 8, 64, 256, 1024] uint16_t timer1_prescaler = 256; -uint16_t timer1_overflow_value = (2^16) -1; // Just for info +uint16_t timer1_overflow_value = 65535; // For calculations int main() { @@ -59,13 +59,13 @@ int main() // Init timer1 timer1_init(); - PORTB |= (1 << PORTB5); // Onboard-Led + PORTB |= (boost_highVoltage_nextTick << PORTB5); // Onboard-Led // Endless loop while (1) { cli(); // Deactivate global interrupts (before checks) - + /* * HighVoltage boosting */ @@ -77,7 +77,7 @@ int main() 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 + boost_highVoltage_nextTick = (uint16_t)TCNT1 + F_CPU/timer1_prescaler * 1/boost_frequency/2; // Half of time it should be high/low if(boost_highVoltage_nextTick == 0) boost_highVoltage_nextTick++; // If its 0, it stops, we dont want that } } @@ -91,7 +91,7 @@ int main() if((uint16_t)TCNT1 >= signalOutput_piezo_nextTick && ((uint16_t)TCNT1-signalOutput_piezo_nextTick) < (timer1_overflow_value/2)) // If we are on or after the tick it should be executed { // Flip pin state - PORTD ^= (1 << PORTD5); + PORTD ^= (1 << PORTD5); if(!(PIND & (1 << PORTD5))) { @@ -174,7 +174,8 @@ ISR(INT0_vect) if(!(PIND & (1 << PORTD2))) // Falling edge { // Below ~400V, activate booster - boost_highVoltage_nextTick = 1; // Run as soon as possible + boost_highVoltage_nextTick = TCNT1; // Run as soon as possible + PORTB |= (1 << PORTB5); // Onboard-Led PORTB |= (1 << PORTB5); // Onboard-Led } @@ -183,10 +184,10 @@ ISR(INT0_vect) // Reached ~400V, deactivate booster boost_highVoltage_nextTick = 0; + PORTB |= (0 << PORTB5); // Onboard-Led + // reset state PORTD |= (0 << PORTD4); // pin on low - - PORTB |= (0 << PORTB5); // Onboard-Led } reti(); // Exit interrupt-handler @@ -197,8 +198,8 @@ ISR(INT0_vect) ISR(INT1_vect) { // Tick detected, signalOutput - signalOutput_piezo_nextTick = 1; // Run as soon as possible - signalOutput_led_nextTick = 1; + signalOutput_piezo_nextTick = TCNT1; // Run as soon as possible + signalOutput_led_nextTick = TCNT1; reti(); // Exit interrupt-handler }