From c720cc76e2e2698c7f48498eaea3400c21baa3d4 Mon Sep 17 00:00:00 2001 From: Railz Date: Thu, 28 Feb 2019 17:51:54 +0100 Subject: [PATCH 1/5] Changed INT0 to any edge Added execution for highVoltage and signalOutput Added highVoltage boosting Added signal output --- Geigerzaehler/main.c | 101 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 89 insertions(+), 12 deletions(-) diff --git a/Geigerzaehler/main.c b/Geigerzaehler/main.c index 90f3dca..05b8f11 100644 --- a/Geigerzaehler/main.c +++ b/Geigerzaehler/main.c @@ -1,8 +1,6 @@ /* * geigerzaehler.c * -======= ->>>>>>> f39ee6c90de22ce1514bb4f9e7e3e3116efac22f * Created : 28.02.2019 09:07:22 * Author : John Ditgens, Alexander Brandscheidt * Git-Repository : https://gitea.Railduction.eu/JohnD/Geigerzaehler.git @@ -12,13 +10,30 @@ */ #define F_CPU 20000000UL // Clock speed: 20 MHz - Maximum of AtMega328P +// Macros +#define bit_get(p,m) ((p) & (m)) +#define bit_set(p,m) ((p) |= (m)) +#define bit_clear(p,m) ((p) &= ~(m)) +#define bit_flip(p,m) ((p) ^= (m)) +#define bit_write(c,p,m) (c ? bit_set(p,m) : bit_clear(p,m)) +#define BIT(x) (0x01 << (x)) +#define LONGBIT(x) ((unsigned long)0x00000001 << (x)) + // Imports #include #include #include // Global variable declaration -uint8_t test = 0; +uint32_t currTick = 0; + +bool enable_boost = false; +bool boost_highVoltage_nextHigh = true; +uint32_t boost_highVoltage_nextTick = 0; + +bool enable_signalOutput = false; +bool signal_Output_nextHigh = true; +uint32_t signal_Output_nextTick = 0; int main() @@ -26,16 +41,11 @@ int main() DDRD &= ~(1<<2); // Activate PD2 DDRD &= ~(1<<3); // Activate PD3 - PORTD |= (1<<2); // Enable Pull Up Resistor Pin D2 - PORTD |= (1<<3); // Enable Pull Up Resistor Pin D3 + PORTD |= (1<<2); // Enable pull-up-resistor Pin-D2 + PORTD |= (1<<3); // Enable pull-up-resistor Pin-D3 - EICRA |= (1 << ISC11)|(1 << ISC10); // Only at raising edge - EIMSK |= (1 << INT1); // Activate Interrupt INT0 - - EICRA |= (1 << ISC00); // Only at falling edge - EIMSK |= (1 << INT0); // Activate Interrupt INT1 // Interrupt for INT0 Pin-D2 High-voltage check - EICRA |= (1 << ISC01)|(0 << ISC00); // Only at falling edge [1|0] + EICRA |= (0 << ISC01)|(1 << ISC00); // Only at any edge [0|1] EIMSK |= (1 << INT0); // Activate Interrupt INT0 // Interrupt for INT1 Pin-D3 Counter-click @@ -47,6 +57,61 @@ int main() // Endless loop while (1) { + // HighVoltage boosting + if(enable_boost) + { + if(currTick >= 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) // Set it high + { + bit_set(PD4, 1); + boost_highVoltage_nextHigh = false; // Next is low + } + else // Set it low + { + bit_set(PD4, 0); + boost_highVoltage_nextHigh = true; // Next is high + } + + // Calculate when next high/low should be set + boost_highVoltage_nextTick = currTick + 101 // Todo: Add calculation for pin high/low time + } + }else + { + if(boost_highVoltage_nextTick > 0) // If boosting is deactivated, but the nextTick was not reset yet + { + // Reset boost-state + bit_set(PD4, 0); + boost_highVoltage_nextHigh = true; + boost_highVoltage_nextTick = 0; + } + } + + + // Signal output + if(enable_signalOutput) + { + if(currTick >= signal_Output_nextTick) // If we are on or after the tick it should be executed + { + // Set pin according to next exec + if(boost_highVoltage_nextHigh) // Set it high + { + bit_set(PD5, 1); + bit_set(PD6, 1); + boost_highVoltage_nextHigh = false; // Next is low + } + else // Set it low + { + bit_set(PD5, 0); + bit_set(PD6, 0); + boost_highVoltage_nextHigh = true; // Next is high + } + + // When the signal should stop + boost_highVoltage_nextTick = currTick + 101 // Todo: Add calculation for pin high/low time + } + } } } @@ -57,13 +122,25 @@ int main() // Address: 0x001 INT0 ISR(INT0_vect) { - + if(PIND&0x20 == 0x00) // Falling edge + { + // Below ~400V, activate booster + enable_boost = true; + } + else // Rising edge + { + // Reached ~400V, deactivate booster + enable_boost = false; + } + reti(); // Exit interrupt-handler } ISR(INT1_vect) { + // Tick detected, signalOutput + enable_signalOutput = true; reti(); // Exit interrupt-handler } \ No newline at end of file From 85f2b1edd6a5440e386c2a3505648328ef803e72 Mon Sep 17 00:00:00 2001 From: Railz Date: Thu, 28 Feb 2019 17:54:20 +0100 Subject: [PATCH 2/5] Added Todo's --- Geigerzaehler/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Geigerzaehler/main.c b/Geigerzaehler/main.c index 05b8f11..0390f7a 100644 --- a/Geigerzaehler/main.c +++ b/Geigerzaehler/main.c @@ -25,7 +25,7 @@ #include // Global variable declaration -uint32_t currTick = 0; +uint32_t currTick = 0; // Todo: Method/Interrupt to get the current-tick bool enable_boost = false; bool boost_highVoltage_nextHigh = true; @@ -109,7 +109,7 @@ int main() } // When the signal should stop - boost_highVoltage_nextTick = currTick + 101 // Todo: Add calculation for pin high/low time + boost_highVoltage_nextTick = currTick + 101 // Todo: Add calculation for PWM high/low time } } } From 23417be79f6dd9a7008299ffc7f16a42173646e0 Mon Sep 17 00:00:00 2001 From: Alexander B Date: Thu, 28 Feb 2019 18:08:03 +0100 Subject: [PATCH 3/5] README.md added --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..a462590 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +## School project - Geiger counter based on a Geiger-Müller counter tube + +Microcontroller code written in C + +Microcontroller: AtMega328P +Board: Arduino Nano \ No newline at end of file From 2aab28a8c5b42d5d97e90f929e815a6fd0b80f93 Mon Sep 17 00:00:00 2001 From: Alexander B Date: Thu, 28 Feb 2019 18:09:38 +0100 Subject: [PATCH 4/5] =?UTF-8?q?=E2=80=9EREADME.md=E2=80=9C=20=C3=A4ndern?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a462590..5338905 100644 --- a/README.md +++ b/README.md @@ -2,5 +2,5 @@ Microcontroller code written in C -Microcontroller: AtMega328P -Board: Arduino Nano \ No newline at end of file +Microcontroller: AtMega328P
+Board: Arduino Nano \ No newline at end of file From dc36f3c7da4aac60566b7b5052f41a8f9c714bfb Mon Sep 17 00:00:00 2001 From: Railz Date: Mon, 4 Mar 2019 15:31:08 +0100 Subject: [PATCH 5/5] Fixed 2. If (wrong procedures) Commented ISR-handlers --- Geigerzaehler/main.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/Geigerzaehler/main.c b/Geigerzaehler/main.c index 0390f7a..1ad883b 100644 --- a/Geigerzaehler/main.c +++ b/Geigerzaehler/main.c @@ -45,11 +45,11 @@ int main() PORTD |= (1<<3); // Enable pull-up-resistor Pin-D3 // Interrupt for INT0 Pin-D2 High-voltage check - EICRA |= (0 << ISC01)|(1 << ISC00); // Only at any edge [0|1] + EICRA |= (0 << ISC01)|(1 << ISC00); // Only at any edge [Code: 01] EIMSK |= (1 << INT0); // Activate Interrupt INT0 // Interrupt for INT1 Pin-D3 Counter-click - EICRA |= (1 << ISC11)|(1 << ISC10); // Only at rising edge [1|1] + EICRA |= (1 << ISC11)|(1 << ISC10); // Only at rising edge [Code: 11] EIMSK |= (1 <= signal_Output_nextTick) // If we are on or after the tick it should be executed { // Set pin according to next exec - if(boost_highVoltage_nextHigh) // Set it high + if(signal_Output_nextHigh) // Set it high { bit_set(PD5, 1); bit_set(PD6, 1); - boost_highVoltage_nextHigh = false; // Next is low + signal_Output_nextHigh = false; // Next is low } else // Set it low { bit_set(PD5, 0); bit_set(PD6, 0); - boost_highVoltage_nextHigh = true; // Next is high + signal_Output_nextHigh = true; // Next is high } // When the signal should stop - boost_highVoltage_nextTick = currTick + 101 // Todo: Add calculation for PWM high/low time + signal_Output_nextTick = currTick + 101 // Todo: Add calculation for signalOutput low time } } } @@ -119,7 +119,8 @@ int main() * Interrupt-handler * ISR - Interrupt service routine */ -// Address: 0x001 INT0 +// Address: 0x001 INT0 - Only at any edge [Code: 01] +// Handles: Over/Below working voltage ISR(INT0_vect) { if(PIND&0x20 == 0x00) // Falling edge @@ -136,11 +137,14 @@ ISR(INT0_vect) reti(); // Exit interrupt-handler } - +// Address: 0x002 INT1 - Only at rising edge [Code: 11] +// Handles: Counter tube "tick" ISR(INT1_vect) { // Tick detected, signalOutput enable_signalOutput = true; + // Execute immediately + signal_Output_nextTick = 0; reti(); // Exit interrupt-handler } \ No newline at end of file