diff --git a/Geigerzaehler/main.c b/Geigerzaehler/main.c index 22b8c5b..e4c63e2 100644 --- a/Geigerzaehler/main.c +++ b/Geigerzaehler/main.c @@ -10,41 +10,38 @@ */ #define F_CPU 16000000UL // Clock speed: 16 MHz - Speed from onboard oscillator -// 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 -#include -#include // Global variable declaration uint16_t boost_frequency = 2000; // 2kHz frequency -bool boost_enable = false; -bool boost_highVoltage_nextHigh = true; +uint8_t boost_enable = 0; +uint8_t boost_highVoltage_nextHigh = 1; clock_t boost_highVoltage_nextTick = 0; -bool signalOutput_enable = false; -bool signalOutput_nextHigh = true; +// Todo [CRITICAL]: Clock does not exist! Use timer. + +uint8_t signalOutput_enable = 0; +uint8_t signalOutput_nextHigh = 1; clock_t signalOutput_nextTick = 0; int main() { - DDRD &= ~(1<<2); // Activate input PD2 - DDRD &= ~(1<<3); // Activate input PD3 - - PORTD |= (1<<2); // Enable pull-up-resistor D2 - PORTD |= (1<<3); // Enable pull-up-resistor D3 + // Inputs + DDRD &= (0 << PORTD2); // Activate input PD2 + DDRD &= (0 << PORTD3); // Activate input PD3 + //PORTD |= (1 << PORTD2); // Enable pull-up-resistor D2 + //PORTD |= (1 << PORTD3); // Enable pull-up-resistor D3 + // Todo [CRITICAL]: External pull-down resistors 10kOhm + + // Outputs + DDRD |= (1 << PORTD4); + DDRD |= (1 << PORTD5); + DDRD |= (1 << PORTD6); // Interrupt for INT0 Pin-D2 High-voltage check EICRA |= (0 << ISC01)|(1 << ISC00); // On at any edge [Code: 01] @@ -60,32 +57,32 @@ int main() while (1) { // HighVoltage boosting - if(boost_enable) + if(boost_enable == 1) { if(clock() >= 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 + if(boost_highVoltage_nextHigh == 1) // Set it high { - bit_set(PD4, 1); - boost_highVoltage_nextHigh = false; // Next is low + PORTD |= (1 << PORTD4); + boost_highVoltage_nextHigh = 0; // Next is low } else // Set it low { - bit_set(PD4, 0); - boost_highVoltage_nextHigh = true; // Next is high + PORTD |= (0 << PORTD4); + boost_highVoltage_nextHigh = 1; // Next is high } // Calculate when next high/low should be set - boost_highVoltage_nextTick = clock() + F_CPU * 1/(boost_frequency/2) // Half of time it should be high/low + boost_highVoltage_nextTick = clock() + F_CPU * 1/(boost_frequency/2); // Half of time it should be high/low } }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; + PORTD |= (0 << PORTD4); + boost_highVoltage_nextHigh = 1; boost_highVoltage_nextTick = 0; } } @@ -97,23 +94,23 @@ int main() if(clock() >= signalOutput_nextTick) // If we are on or after the tick it should be executed { // Set pin according to next exec - if(signalOutput_nextHigh) // Set it high + if(signalOutput_nextHigh == 1) // Set it high { - bit_set(PD5, 1); - bit_set(PD6, 1); - signalOutput_nextHigh = false; // Next is low + PORTD |= (1 << PORTD5); + PORTD |= (1 << PORTD6); + signalOutput_nextHigh = 0; // Next is low } else // Set it low { - bit_set(PD5, 0); - bit_set(PD6, 0); - signalOutput_nextHigh = true; // Next is high + PORTD |= (0 << PORTD5); + PORTD |= (0 << PORTD6); + signalOutput_nextHigh = 1; // Next is high - signalOutput_enable = false; // Disable signalOutput + signalOutput_enable = 0; // Disable signalOutput } // When the signal should stop - signalOutput_nextTick = clock() + F_CPU * 0.2 // nextTick is in 0.2s + signalOutput_nextTick = clock() + F_CPU * 0.2; // nextTick is in 0.2s } } } @@ -127,15 +124,15 @@ int main() // Handles: Over/Below working voltage ISR(INT0_vect) { - if(PIND&0x20 == 0x00) // Falling edge + if(!(PIND & (1 << PORTD2))) // Falling edge { // Below ~400V, activate booster - boost_enable = true; + boost_enable = 1; } else // Rising edge { // Reached ~400V, deactivate booster - boost_enable = false; + boost_enable = 0; } reti(); // Exit interrupt-handler @@ -146,7 +143,7 @@ ISR(INT0_vect) ISR(INT1_vect) { // Tick detected, signalOutput - signalOutput_enable = true; + signalOutput_enable = 1; // Execute immediately signalOutput_nextTick = 0;