Set frequency to 10kHz

Fixed timer1_overflow_value not being correctly calculated
master
Railz 6 years ago
parent 6a5b39f6d9
commit 3b78e14c2a

@ -19,7 +19,7 @@
* Global variable declaration * Global variable declaration
*/ */
// Set frequencys and general stuff // Set frequencys and general stuff
uint16_t boost_frequency = 2000; // 2kHz frequency uint16_t boost_frequency = 10000; // 10kHz frequency
uint16_t signalOutput_piezo_time = 200; // 200ms uint16_t signalOutput_piezo_time = 200; // 200ms
uint16_t signalOutput_led_time = 200; // 200ms uint16_t signalOutput_led_time = 200; // 200ms
@ -31,7 +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]
uint16_t timer1_prescaler = 256; uint16_t timer1_prescaler = 256;
uint16_t timer1_overflow_value = (2^16) -1; // Just for info uint16_t timer1_overflow_value = 65535; // For calculations
int main() int main()
{ {
@ -59,13 +59,13 @@ int main()
// Init timer1 // Init timer1
timer1_init(); timer1_init();
PORTB |= (1 << PORTB5); // Onboard-Led PORTB |= (boost_highVoltage_nextTick << PORTB5); // Onboard-Led
// Endless loop // Endless loop
while (1) while (1)
{ {
cli(); // Deactivate global interrupts (before checks) cli(); // Deactivate global interrupts (before checks)
/* /*
* HighVoltage boosting * HighVoltage boosting
*/ */
@ -77,7 +77,7 @@ int main()
PORTD ^= (1 << PORTD4); 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
} }
} }
@ -91,7 +91,7 @@ int main()
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 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
{ {
// Flip pin state // Flip pin state
PORTD ^= (1 << PORTD5); PORTD ^= (1 << PORTD5);
if(!(PIND & (1 << PORTD5))) if(!(PIND & (1 << PORTD5)))
{ {
@ -174,7 +174,8 @@ ISR(INT0_vect)
if(!(PIND & (1 << PORTD2))) // Falling edge if(!(PIND & (1 << PORTD2))) // Falling edge
{ {
// Below ~400V, activate booster // Below ~400V, activate booster
boost_highVoltage_nextTick = 1; // Run as soon as possible boost_highVoltage_nextTick = TCNT1; // Run as soon as possible
PORTB |= (1 << PORTB5); // Onboard-Led
PORTB |= (1 << PORTB5); // Onboard-Led PORTB |= (1 << PORTB5); // Onboard-Led
} }
@ -183,10 +184,10 @@ ISR(INT0_vect)
// Reached ~400V, deactivate booster // Reached ~400V, deactivate booster
boost_highVoltage_nextTick = 0; boost_highVoltage_nextTick = 0;
PORTB |= (0 << PORTB5); // Onboard-Led
// reset state // reset state
PORTD |= (0 << PORTD4); // pin on low PORTD |= (0 << PORTD4); // pin on low
PORTB |= (0 << PORTB5); // Onboard-Led
} }
reti(); // Exit interrupt-handler reti(); // Exit interrupt-handler
@ -197,8 +198,8 @@ ISR(INT0_vect)
ISR(INT1_vect) ISR(INT1_vect)
{ {
// Tick detected, signalOutput // Tick detected, signalOutput
signalOutput_piezo_nextTick = 1; // Run as soon as possible signalOutput_piezo_nextTick = TCNT1; // Run as soon as possible
signalOutput_led_nextTick = 1; signalOutput_led_nextTick = TCNT1;
reti(); // Exit interrupt-handler reti(); // Exit interrupt-handler
} }

Loading…
Cancel
Save