Fixed execution prior to tick, because of nextTick overflow before timer-overflow

master
Alexander B 6 years ago
parent e81bda822e
commit ccda41769f

@ -31,6 +31,7 @@ uint16_t signalOutput_led_nextTick = 0;
// Prescaling of timer [timer-speed: cpu-clock / prescaler] // Prescaling of timer [timer-speed: cpu-clock / prescaler]
// Modes: [1, 8, 64, 256, 1024] // Modes: [1, 8, 64, 256, 1024]
uint8_t timer1_prescaler = 256; uint8_t timer1_prescaler = 256;
uint16_t timer1_overflow_value = (2^16) -1; // Just for info
int main() int main()
{ {
@ -40,9 +41,6 @@ int main()
// Inputs // Inputs
DDRD &= (0 << PORTD2); // Activate input PD2 DDRD &= (0 << PORTD2); // Activate input PD2
DDRD &= (0 << PORTD3); // Activate input PD3 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 // Outputs
DDRD |= (1 << PORTD4); DDRD |= (1 << PORTD4);
@ -69,50 +67,59 @@ int main()
/* /*
* HighVoltage boosting * HighVoltage boosting
*/ */
if(boost_highVoltage_nextTick > 0 && (uint16_t)TCNT1 >= boost_highVoltage_nextTick) // If we are on or after the tick it should be executed if(boost_highVoltage_nextTick > 0) // If there is a nextTick
{ {
// Flip pin state if((uint16_t)TCNT1 >= boost_highVoltage_nextTick && ((uint16_t)TCNT1-boost_highVoltage_nextTick) < (timer1_overflow_value/2)) // If we are on or after the tick it should be executed
PORTD ^= (1 << PORTD4); {
// Flip pin state
PORTD ^= (1 << PORTD4);
// Calculate when next high/low should be set // Calculate when next high/low should be set
boost_highVoltage_nextTick = (uint16_t)TCNT1 + F_CPU/timer1_prescaler * 1/(boost_frequency/2); // Half of time it should be high/low boost_highVoltage_nextTick = (uint16_t)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 if(boost_highVoltage_nextTick == 0) boost_highVoltage_nextTick++; // If its 0, it stops, we dont want that
}
} }
/* /*
* Signal output * Signal output
*/ */
// Piezo // Piezo
if(signalOutput_piezo_nextTick > 0 && (uint16_t)TCNT1 >= signalOutput_piezo_nextTick) // If we are on or after the tick it should be executed if(signalOutput_piezo_nextTick > 0) // If there is a nextTick
{ {
// Flip pin state if((uint16_t)TCNT1 >= signalOutput_piezo_nextTick && ((uint16_t)TCNT1-signalOutput_piezo_nextTick) < (timer1_overflow_value/2)) // If we are on or after the tick it should be executed
PORTD ^= (1 << PORTD5);
if(!(PIND & (1 << PORTD5)))
{ {
// We are on low (again), disable further execution // Flip pin state
signalOutput_piezo_nextTick = 0; PORTD ^= (1 << PORTD5);
if(!(PIND & (1 << PORTD5)))
{
// We are on low (again), disable further execution
signalOutput_piezo_nextTick = 0;
}
// When the signal should next flip
signalOutput_piezo_nextTick = (uint16_t)TCNT1 + F_CPU/timer1_prescaler * signalOutput_piezo_time/1000; // Calculate nextTick based on time in ms
if(signalOutput_piezo_nextTick == 0) signalOutput_piezo_nextTick++; // If its 0, it stops, we dont want that
} }
// When the signal should next flip
signalOutput_piezo_nextTick = (uint16_t)TCNT1 + F_CPU/timer1_prescaler * signalOutput_piezo_time/1000; // Calculate nextTick based on time in ms
if(signalOutput_piezo_nextTick == 0) signalOutput_piezo_nextTick++; // If its 0, it stops, we dont want that
} }
// LED // LED
if(signalOutput_led_nextTick > 0 && (uint16_t)TCNT1 >= signalOutput_led_nextTick) // If we are on or after the tick it should be executed if(signalOutput_led_nextTick > 0) // If there is a nextTick
{ {
// Flip pin state if((uint16_t)TCNT1 >= signalOutput_led_nextTick && ((uint16_t)TCNT1-signalOutput_led_nextTick) < (timer1_overflow_value/2)) // If we are on or after the tick it should be executed
PORTD ^= (1 << PORTD6);
if(!(PIND & (1 << PORTD6)))
{ {
// We are on low (again), disable further execution // Flip pin state
signalOutput_led_nextTick = 0; PORTD ^= (1 << PORTD6);
if(!(PIND & (1 << PORTD6)))
{
// We are on low (again), disable further execution
signalOutput_led_nextTick = 0;
}
// When the signal should next flip
signalOutput_led_nextTick = (uint16_t)TCNT1 + F_CPU/timer1_prescaler * signalOutput_led_time/1000; // Calculate nextTick based on time in ms
if(signalOutput_led_nextTick == 0) signalOutput_led_nextTick++; // If its 0, it stops, we dont want that
} }
// When the signal should next flip
signalOutput_led_nextTick = (uint16_t)TCNT1 + F_CPU/timer1_prescaler * signalOutput_led_time/1000; // Calculate nextTick based on time in ms
if(signalOutput_led_nextTick == 0) signalOutput_led_nextTick++; // If its 0, it stops, we dont want that
} }
} }

Loading…
Cancel
Save