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.

177 lines
4.0 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 - Speed from onboard oscillator
6 years ago
6 years ago
// Imports
6 years ago
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdint.h>
// Global variable declaration
uint16_t boost_frequency = 2000; // 2kHz frequency
6 years ago
uint8_t boost_enable = 0;
uint8_t boost_highVoltage_nextHigh = 1;
uint16_t boost_highVoltage_nextTick = 0;
6 years ago
// Todo [CRITICAL]: Clock does not exist! Use timer.
uint8_t signalOutput_enable = 0;
uint8_t signalOutput_nextHigh = 1;
uint16_t signalOutput_nextTick = 0;
6 years ago
int main()
6 years ago
{
// 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);
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
// Init timer1
timer1_init();
6 years ago
sei(); // Activate global interrupts
6 years ago
6 years ago
// Endless loop
6 years ago
while (1)
{
6 years ago
// HighVoltage boosting
if(boost_enable == 1)
6 years ago
{
if(TCNT1 >= boost_highVoltage_nextTick) // If we are on or after the tick it should be executed
6 years ago
{
// Set pin according to next exec
if(boost_highVoltage_nextHigh == 1) // Set it high
6 years ago
{
PORTD |= (1 << PORTD4);
boost_highVoltage_nextHigh = 0; // Next is low
6 years ago
}
else // Set it low
{
PORTD |= (0 << PORTD4);
boost_highVoltage_nextHigh = 1; // Next is high
6 years ago
}
// Calculate when next high/low should be set
boost_highVoltage_nextTick = TCNT1 + F_CPU/256 * 1/(boost_frequency/2); // Half of time it should be high/low
6 years ago
}
}else
{
if(boost_highVoltage_nextTick > 0) // If boosting is deactivated, but the nextTick was not reset yet
{
// Reset boost-state
PORTD |= (0 << PORTD4);
boost_highVoltage_nextHigh = 1;
6 years ago
boost_highVoltage_nextTick = 0;
}
}
// Signal output
if(signalOutput_enable)
6 years ago
{
if(TCNT1 >= signalOutput_nextTick) // If we are on or after the tick it should be executed
6 years ago
{
// Set pin according to next exec
if(signalOutput_nextHigh == 1) // Set it high
6 years ago
{
PORTD |= (1 << PORTD5);
PORTD |= (1 << PORTD6);
signalOutput_nextHigh = 0; // Next is low
6 years ago
}
else // Set it low
{
PORTD |= (0 << PORTD5);
PORTD |= (0 << PORTD6);
signalOutput_nextHigh = 1; // Next is high
signalOutput_enable = 0; // Disable signalOutput
6 years ago
}
// When the signal should stop
signalOutput_nextTick = TCNT1 + F_CPU/256 * 0.2; // nextTick is in 0.2s
6 years ago
}
}
6 years ago
}
}
void timer1_init()
{
// set up timer with prescaler = 256
TCCR1B |= (1 << CS12);
// initialize counter
TCNT1 = 0;
}
ISR(TIMER1_COMPA_vect){
//Code Kanal A Timer 1
}
ISR(TIMER1_COMPB_vect){
//Code Kanal B Timer 1
}
/*
* 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 & (1 << PORTD2))) // Falling edge
6 years ago
{
// Below ~400V, activate booster
boost_enable = 1;
6 years ago
}
else // Rising edge
{
// Reached ~400V, deactivate booster
boost_enable = 0;
6 years ago
}
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)
{
6 years ago
// Tick detected, signalOutput
signalOutput_enable = 1;
// Execute immediately
signalOutput_nextTick = 0;
reti(); // Exit interrupt-handler
6 years ago
}