|
|
@ -17,17 +17,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
// Global variable declaration
|
|
|
|
// Global variable declaration
|
|
|
|
uint16_t boost_frequency = 2000; // 2kHz frequency
|
|
|
|
uint16_t boost_frequency = 2000; // 2kHz frequency
|
|
|
|
|
|
|
|
uint16_t signalOutput_time = 200; // 200ms
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t boost_enable = 0;
|
|
|
|
|
|
|
|
uint8_t boost_highVoltage_nextHigh = 1;
|
|
|
|
uint8_t boost_highVoltage_nextHigh = 1;
|
|
|
|
uint16_t boost_highVoltage_nextTick = 0;
|
|
|
|
uint16_t boost_highVoltage_nextTick = 0;
|
|
|
|
|
|
|
|
|
|
|
|
// Todo [CRITICAL]: Clock does not exist! Use timer.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t signalOutput_enable = 0;
|
|
|
|
|
|
|
|
uint8_t signalOutput_nextHigh = 1;
|
|
|
|
uint8_t signalOutput_nextHigh = 1;
|
|
|
|
uint16_t signalOutput_nextTick = 0;
|
|
|
|
uint16_t signalOutput_nextTick = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t timer1_prescaler = 256;
|
|
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -59,87 +57,93 @@ int main()
|
|
|
|
// Endless loop
|
|
|
|
// Endless loop
|
|
|
|
while (1)
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// HighVoltage boosting
|
|
|
|
/*
|
|
|
|
if(boost_enable == 1)
|
|
|
|
* HighVoltage boosting
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
if(boost_highVoltage_nextTick > 0 && TCNT1 >= boost_highVoltage_nextTick) // If we are on or after the tick it should be executed
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(TCNT1 >= 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 == 1)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Set pin according to next exec
|
|
|
|
// Set it high
|
|
|
|
if(boost_highVoltage_nextHigh == 1) // Set it high
|
|
|
|
PORTD |= (1 << PORTD4);
|
|
|
|
{
|
|
|
|
boost_highVoltage_nextHigh = 0; // Next is low
|
|
|
|
PORTD |= (1 << PORTD4);
|
|
|
|
|
|
|
|
boost_highVoltage_nextHigh = 0; // Next is low
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else // Set it low
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
PORTD |= (0 << PORTD4);
|
|
|
|
|
|
|
|
boost_highVoltage_nextHigh = 1; // Next is high
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
if(boost_highVoltage_nextTick > 0) // If boosting is deactivated, but the nextTick was not reset yet
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Reset boost-state
|
|
|
|
// Set it low
|
|
|
|
PORTD |= (0 << PORTD4);
|
|
|
|
PORTD |= (0 << PORTD4);
|
|
|
|
boost_highVoltage_nextHigh = 1;
|
|
|
|
boost_highVoltage_nextHigh = 1; // Next is high
|
|
|
|
boost_highVoltage_nextTick = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Calculate when next high/low should be set
|
|
|
|
|
|
|
|
boost_highVoltage_nextTick = 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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Signal output
|
|
|
|
/*
|
|
|
|
if(signalOutput_enable)
|
|
|
|
* Signal output
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
if(signalOutput_nextTick > 0 && TCNT1 >= signalOutput_nextTick) // If we are on or after the tick it should be executed
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(TCNT1 >= signalOutput_nextTick) // If we are on or after the tick it should be executed
|
|
|
|
// Set pin according to next exec
|
|
|
|
|
|
|
|
if(signalOutput_nextHigh == 1) // Set it high
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
PORTD |= (1 << PORTD5);
|
|
|
|
|
|
|
|
PORTD |= (1 << PORTD6);
|
|
|
|
|
|
|
|
signalOutput_nextHigh = 0; // Next is low
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else // Set it low
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Set pin according to next exec
|
|
|
|
PORTD |= (0 << PORTD5);
|
|
|
|
if(signalOutput_nextHigh == 1) // Set it high
|
|
|
|
PORTD |= (0 << PORTD6);
|
|
|
|
{
|
|
|
|
signalOutput_nextHigh = 1; // Next is high
|
|
|
|
PORTD |= (1 << PORTD5);
|
|
|
|
|
|
|
|
PORTD |= (1 << PORTD6);
|
|
|
|
signalOutput_nextTick = 0; // Disable signalOutput
|
|
|
|
signalOutput_nextHigh = 0; // Next is low
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else // Set it low
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
PORTD |= (0 << PORTD5);
|
|
|
|
|
|
|
|
PORTD |= (0 << PORTD6);
|
|
|
|
|
|
|
|
signalOutput_nextHigh = 1; // Next is high
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
signalOutput_enable = 0; // Disable signalOutput
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// When the signal should stop
|
|
|
|
|
|
|
|
signalOutput_nextTick = TCNT1 + F_CPU/256 * 0.2; // nextTick is in 0.2s
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// When the signal should stop
|
|
|
|
|
|
|
|
signalOutput_nextTick = TCNT1 + F_CPU/timer1_prescaler * signalOutput_time/1000; // nextTick is in signalOutput_time in ms
|
|
|
|
|
|
|
|
if(signalOutput_nextTick == 0) signalOutput_nextTick++; // If its 0, it stops, we dont want that
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
* Init timer1 (16-bit) in "normal mode"
|
|
|
|
|
|
|
|
*/
|
|
|
|
void timer1_init()
|
|
|
|
void timer1_init()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// set up timer with prescaler = 256
|
|
|
|
// set up timer with speed: cpu-clock / prescaler
|
|
|
|
TCCR1B |= (1 << CS12);
|
|
|
|
switch(timer1_prescaler){ // [CS12 CS11 CS10]
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
|
|
TCCR1B |= (1 << CS10); // [0 0 1 ]
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
|
|
|
|
TCCR1B |= (1 << CS11); // [0 1 0 ]
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 64:
|
|
|
|
|
|
|
|
TCCR1B |= (1 << CS10); // [0 1 1 ]
|
|
|
|
|
|
|
|
TCCR1B |= (1 << CS11);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 256:
|
|
|
|
|
|
|
|
TCCR1B |= (1 << CS12); // [1 0 0 ]
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 1024:
|
|
|
|
|
|
|
|
TCCR1B |= (1 << CS10); // [1 0 1 ]
|
|
|
|
|
|
|
|
TCCR1B |= (1 << CS12);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// initialize counter
|
|
|
|
// initialize counter
|
|
|
|
TCNT1 = 0;
|
|
|
|
TCNT1 = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ISR(TIMER1_COMPA_vect){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Code Kanal A Timer 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ISR(TIMER1_COMPB_vect){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Code Kanal B Timer 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Interrupt-handler
|
|
|
|
* Interrupt-handler
|
|
|
@ -152,12 +156,16 @@ ISR(INT0_vect)
|
|
|
|
if(!(PIND & (1 << PORTD2))) // Falling edge
|
|
|
|
if(!(PIND & (1 << PORTD2))) // Falling edge
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Below ~400V, activate booster
|
|
|
|
// Below ~400V, activate booster
|
|
|
|
boost_enable = 1;
|
|
|
|
boost_highVoltage_nextTick = 1; // Run as soon as possible
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // Rising edge
|
|
|
|
else // Rising edge
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Reached ~400V, deactivate booster
|
|
|
|
// Reached ~400V, deactivate booster
|
|
|
|
boost_enable = 0;
|
|
|
|
boost_highVoltage_nextTick = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// reset state
|
|
|
|
|
|
|
|
PORTD |= (0 << PORTD4); // pin on low
|
|
|
|
|
|
|
|
boost_highVoltage_nextHigh = 1; // next is high
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
reti(); // Exit interrupt-handler
|
|
|
|
reti(); // Exit interrupt-handler
|
|
|
@ -168,9 +176,7 @@ ISR(INT0_vect)
|
|
|
|
ISR(INT1_vect)
|
|
|
|
ISR(INT1_vect)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Tick detected, signalOutput
|
|
|
|
// Tick detected, signalOutput
|
|
|
|
signalOutput_enable = 1;
|
|
|
|
signalOutput_nextTick = 1; // Run as soon as possible
|
|
|
|
// Execute immediately
|
|
|
|
|
|
|
|
signalOutput_nextTick = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reti(); // Exit interrupt-handler
|
|
|
|
reti(); // Exit interrupt-handler
|
|
|
|
}
|
|
|
|
}
|
|
|
|