You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
3.8 KiB
C

6 years ago
/*
* geigerzaehler.c
6 years ago
*
6 years ago
* Created : 28.02.2019 09:07:22
* Author : John Ditgens, Alexander Brandscheidt
* Git-Repository : https://gitea.Railduction.eu/JohnD/Geigerzaehler.git
*
* MController : AtMega328P
* Board : Arduino Nano
6 years ago
*/
#define F_CPU 16000000UL // Clock speed: 16 MHz - Maximum of AtMega328P
6 years ago
// 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))
6 years ago
// Imports
6 years ago
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdint.h>
#include <time.h>
// Global variable declaration
6 years ago
uint32_t currTick = 0; // Todo: Method/Interrupt to get the current-tick
bool boost_enable = false;
bool boost_highVoltage_nextHigh = true;
uint32_t boost_highVoltage_nextTick = 0;
bool signalOutput_enable = false;
bool signalOutput_nextHigh = true;
uint32_t signalOutput_nextTick = 0;
6 years ago
int main()
6 years ago
{
DDRD &= ~(1<<2); // Activate input PD2
DDRD &= ~(1<<3); // Activate input PD3
6 years ago
PORTD |= (1<<2); // Enable pull-up-resistor D2
PORTD |= (1<<3); // Enable pull-up-resistor D3
6 years ago
// Interrupt for INT0 Pin-D2 High-voltage check
EICRA |= (0 << ISC01)|(1 << ISC00); // On at any edge [Code: 01]
6 years ago
EIMSK |= (1 << INT0); // Activate Interrupt INT0
6 years ago
// Interrupt for INT1 Pin-D3 Counter-click
EICRA |= (1 << ISC11)|(1 << ISC10); // On rising edge [Code: 11]
6 years ago
EIMSK |= (1 <<INT1); // Activate Interrupt INT1
6 years ago
6 years ago
sei(); // Activate global interrupts
6 years ago
6 years ago
// Endless loop
6 years ago
while (1)
{
// HighVoltage boosting
if(boost_enable)
{
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(signalOutput_enable)
{
if(currTick >= 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
{
bit_set(PD5, 1);
bit_set(PD6, 1);
signalOutput_nextHigh = false; // Next is low
}
else // Set it low
{
bit_set(PD5, 0);
bit_set(PD6, 0);
signalOutput_nextHigh = true; // Next is high
}
// When the signal should stop
signalOutput_nextTick = currTick + 101 // Todo: Add calculation for signalOutput low time
}
}
6 years ago
}
}
/*
* Interrupt-handler
* ISR - Interrupt service routine
*/
// Address: 0x001 INT0 - On any edge [Code: 01]
// Handles: Over/Below working voltage
ISR(INT0_vect)
6 years ago
{
if(PIND&0x20 == 0x00) // Falling edge
{
// Below ~400V, activate booster
boost_enable = true;
}
else // Rising edge
{
// Reached ~400V, deactivate booster
boost_enable = false;
}
reti(); // Exit interrupt-handler
6 years ago
}
// Address: 0x002 INT1 - On rising edge [Code: 11]
// Handles: Counter tube "tick"
6 years ago
ISR(INT1_vect)
{
// Tick detected, signalOutput
signalOutput_enable = true;
// Execute immediately
signalOutput_nextTick = 0;
reti(); // Exit interrupt-handler
}