if() else if() VS if() else if() in PWM control [closed]PWM Motor Speed ControlPIC32 PWM minimal examplePWM Control with MOSFET switchMotor control using PWMPWM 1HZ PIC18F14K50PWM Control vs Variable Voltage Control - Calculating Duty CycleSolenoid control over PWMDimming multiple LED panels without PWM to LEDs; modified DMX control or something else?PWM voltage control that can handle 20APWM Power Control

Is Social Media Science Fiction?

Was there ever an axiom rendered a theorem?

Ideas for 3rd eye abilities

aging parents with no investments

How is it possible for user's password to be changed after storage was encrypted? (on OS X, Android)

When blogging recipes, how can I support both readers who want the narrative/journey and ones who want the printer-friendly recipe?

I see my dog run

How to move the player while also allowing forces to affect it

Is there a familial term for apples and pears?

Does bootstrapped regression allow for inference?

How can I add custom success page

Is a vector space a subspace of itself?

What causes the sudden spool-up sound from an F-16 when enabling afterburner?

Copycat chess is back

How can I fix this gap between bookcases I made?

COUNT(*) or MAX(id) - which is faster?

Map list to bin numbers

How to make payment on the internet without leaving a money trail?

Can I find out the caloric content of bread by dehydrating it?

Does a dangling wire really electrocute me if I'm standing in water?

Is it legal to have the "// (c) 2019 John Smith" header in all files when there are hundreds of contributors?

Is ipsum/ipsa/ipse a third person pronoun, or can it serve other functions?

Can I legally use front facing blue light in the UK?

Denied boarding due to overcrowding, Sparpreis ticket. What are my rights?



if() else if() VS if() else if() in PWM control [closed]


PWM Motor Speed ControlPIC32 PWM minimal examplePWM Control with MOSFET switchMotor control using PWMPWM 1HZ PIC18F14K50PWM Control vs Variable Voltage Control - Calculating Duty CycleSolenoid control over PWMDimming multiple LED panels without PWM to LEDs; modified DMX control or something else?PWM voltage control that can handle 20APWM Power Control






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0












$begingroup$


I'm using timer0 and a gpio pin to generate a 300Hz PWM signal on an ATmega2560.



The details if you care: F_CPU=16M, timer clock prescaler /256, count_max=208



The frequency changes depending on if I use an "if() else if()" or an "if() else if()".
The first generates 300Hz and the second generates 244Hz. Below is the code that generates the correct frequency. If I delete the curly brackets after the first two else's, I get 244Hz.



void pwm_on(uint8_t duty)
if(TCNT0 <= duty)= (1<<7); //turn on PB7
else
if(TCNT0 >= duty) //portion of counter inactive
PORTB &= ~(1<<7);//turn off PB7
else
if(TCNT0 >= COUNT_MAX) //if counter full
TCNT0=0;//reset counter
else
//pwm_on


Why am I getting such a significant frequency change with code that doesn't change the functionality?



Also calling this function is the only thing I do in Main after initializing.










share|improve this question











$endgroup$



closed as off-topic by brhans, Finbarr, RoyC, Elliot Alderson, laptop2d Apr 3 at 16:40



  • This question does not appear to be about electronics design within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.











  • 5




    $begingroup$
    I'm voting to close this question as off-topic because it's a C syntax understanding problem and has nothing specifically to do with the hardware the code is running on.
    $endgroup$
    – brhans
    Mar 28 at 11:29










  • $begingroup$
    @brhans I edited the title of the question to specify it is for PWM control since that seems to be the largest point of contention for moving this question to stackoverflow. I am not a computer programmer and this is not a computer programming question. It is about how the syntax is affecting register level hardware timer0, NOT why doesn't this syntax work.
    $endgroup$
    – TammerTheHammer
    Mar 28 at 11:57






  • 1




    $begingroup$
    It doesn't really matter what the end application is - your problem is that you're not understanding standard C syntax. You would have this same problem if you were writing similarly structured code to run on a PC. The fact that you're writing firmware for a micro is only relevant in how this problem is presenting itself - it's just the symptom.
    $endgroup$
    – brhans
    Mar 28 at 13:13






  • 4




    $begingroup$
    Why aren't you using the perhipheral to generate the PWM for you? Which would allow you to dedicate zero CPU time to this task.
    $endgroup$
    – Attie
    Mar 28 at 13:20










  • $begingroup$
    @brhans I have only been asking about the symptom, that being the change in frequency. My understanding shouldn't matter since the question has always been about the symptom.
    $endgroup$
    – TammerTheHammer
    Mar 28 at 13:49

















0












$begingroup$


I'm using timer0 and a gpio pin to generate a 300Hz PWM signal on an ATmega2560.



The details if you care: F_CPU=16M, timer clock prescaler /256, count_max=208



The frequency changes depending on if I use an "if() else if()" or an "if() else if()".
The first generates 300Hz and the second generates 244Hz. Below is the code that generates the correct frequency. If I delete the curly brackets after the first two else's, I get 244Hz.



void pwm_on(uint8_t duty)
if(TCNT0 <= duty)= (1<<7); //turn on PB7
else
if(TCNT0 >= duty) //portion of counter inactive
PORTB &= ~(1<<7);//turn off PB7
else
if(TCNT0 >= COUNT_MAX) //if counter full
TCNT0=0;//reset counter
else
//pwm_on


Why am I getting such a significant frequency change with code that doesn't change the functionality?



Also calling this function is the only thing I do in Main after initializing.










share|improve this question











$endgroup$



closed as off-topic by brhans, Finbarr, RoyC, Elliot Alderson, laptop2d Apr 3 at 16:40



  • This question does not appear to be about electronics design within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.











  • 5




    $begingroup$
    I'm voting to close this question as off-topic because it's a C syntax understanding problem and has nothing specifically to do with the hardware the code is running on.
    $endgroup$
    – brhans
    Mar 28 at 11:29










  • $begingroup$
    @brhans I edited the title of the question to specify it is for PWM control since that seems to be the largest point of contention for moving this question to stackoverflow. I am not a computer programmer and this is not a computer programming question. It is about how the syntax is affecting register level hardware timer0, NOT why doesn't this syntax work.
    $endgroup$
    – TammerTheHammer
    Mar 28 at 11:57






  • 1




    $begingroup$
    It doesn't really matter what the end application is - your problem is that you're not understanding standard C syntax. You would have this same problem if you were writing similarly structured code to run on a PC. The fact that you're writing firmware for a micro is only relevant in how this problem is presenting itself - it's just the symptom.
    $endgroup$
    – brhans
    Mar 28 at 13:13






  • 4




    $begingroup$
    Why aren't you using the perhipheral to generate the PWM for you? Which would allow you to dedicate zero CPU time to this task.
    $endgroup$
    – Attie
    Mar 28 at 13:20










  • $begingroup$
    @brhans I have only been asking about the symptom, that being the change in frequency. My understanding shouldn't matter since the question has always been about the symptom.
    $endgroup$
    – TammerTheHammer
    Mar 28 at 13:49













0












0








0





$begingroup$


I'm using timer0 and a gpio pin to generate a 300Hz PWM signal on an ATmega2560.



The details if you care: F_CPU=16M, timer clock prescaler /256, count_max=208



The frequency changes depending on if I use an "if() else if()" or an "if() else if()".
The first generates 300Hz and the second generates 244Hz. Below is the code that generates the correct frequency. If I delete the curly brackets after the first two else's, I get 244Hz.



void pwm_on(uint8_t duty)
if(TCNT0 <= duty)= (1<<7); //turn on PB7
else
if(TCNT0 >= duty) //portion of counter inactive
PORTB &= ~(1<<7);//turn off PB7
else
if(TCNT0 >= COUNT_MAX) //if counter full
TCNT0=0;//reset counter
else
//pwm_on


Why am I getting such a significant frequency change with code that doesn't change the functionality?



Also calling this function is the only thing I do in Main after initializing.










share|improve this question











$endgroup$




I'm using timer0 and a gpio pin to generate a 300Hz PWM signal on an ATmega2560.



The details if you care: F_CPU=16M, timer clock prescaler /256, count_max=208



The frequency changes depending on if I use an "if() else if()" or an "if() else if()".
The first generates 300Hz and the second generates 244Hz. Below is the code that generates the correct frequency. If I delete the curly brackets after the first two else's, I get 244Hz.



void pwm_on(uint8_t duty)
if(TCNT0 <= duty)= (1<<7); //turn on PB7
else
if(TCNT0 >= duty) //portion of counter inactive
PORTB &= ~(1<<7);//turn off PB7
else
if(TCNT0 >= COUNT_MAX) //if counter full
TCNT0=0;//reset counter
else
//pwm_on


Why am I getting such a significant frequency change with code that doesn't change the functionality?



Also calling this function is the only thing I do in Main after initializing.







pwm timer firmware






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 11:51







TammerTheHammer

















asked Mar 28 at 8:02









TammerTheHammerTammerTheHammer

729




729




closed as off-topic by brhans, Finbarr, RoyC, Elliot Alderson, laptop2d Apr 3 at 16:40



  • This question does not appear to be about electronics design within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.







closed as off-topic by brhans, Finbarr, RoyC, Elliot Alderson, laptop2d Apr 3 at 16:40



  • This question does not appear to be about electronics design within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.







  • 5




    $begingroup$
    I'm voting to close this question as off-topic because it's a C syntax understanding problem and has nothing specifically to do with the hardware the code is running on.
    $endgroup$
    – brhans
    Mar 28 at 11:29










  • $begingroup$
    @brhans I edited the title of the question to specify it is for PWM control since that seems to be the largest point of contention for moving this question to stackoverflow. I am not a computer programmer and this is not a computer programming question. It is about how the syntax is affecting register level hardware timer0, NOT why doesn't this syntax work.
    $endgroup$
    – TammerTheHammer
    Mar 28 at 11:57






  • 1




    $begingroup$
    It doesn't really matter what the end application is - your problem is that you're not understanding standard C syntax. You would have this same problem if you were writing similarly structured code to run on a PC. The fact that you're writing firmware for a micro is only relevant in how this problem is presenting itself - it's just the symptom.
    $endgroup$
    – brhans
    Mar 28 at 13:13






  • 4




    $begingroup$
    Why aren't you using the perhipheral to generate the PWM for you? Which would allow you to dedicate zero CPU time to this task.
    $endgroup$
    – Attie
    Mar 28 at 13:20










  • $begingroup$
    @brhans I have only been asking about the symptom, that being the change in frequency. My understanding shouldn't matter since the question has always been about the symptom.
    $endgroup$
    – TammerTheHammer
    Mar 28 at 13:49












  • 5




    $begingroup$
    I'm voting to close this question as off-topic because it's a C syntax understanding problem and has nothing specifically to do with the hardware the code is running on.
    $endgroup$
    – brhans
    Mar 28 at 11:29










  • $begingroup$
    @brhans I edited the title of the question to specify it is for PWM control since that seems to be the largest point of contention for moving this question to stackoverflow. I am not a computer programmer and this is not a computer programming question. It is about how the syntax is affecting register level hardware timer0, NOT why doesn't this syntax work.
    $endgroup$
    – TammerTheHammer
    Mar 28 at 11:57






  • 1




    $begingroup$
    It doesn't really matter what the end application is - your problem is that you're not understanding standard C syntax. You would have this same problem if you were writing similarly structured code to run on a PC. The fact that you're writing firmware for a micro is only relevant in how this problem is presenting itself - it's just the symptom.
    $endgroup$
    – brhans
    Mar 28 at 13:13






  • 4




    $begingroup$
    Why aren't you using the perhipheral to generate the PWM for you? Which would allow you to dedicate zero CPU time to this task.
    $endgroup$
    – Attie
    Mar 28 at 13:20










  • $begingroup$
    @brhans I have only been asking about the symptom, that being the change in frequency. My understanding shouldn't matter since the question has always been about the symptom.
    $endgroup$
    – TammerTheHammer
    Mar 28 at 13:49







5




5




$begingroup$
I'm voting to close this question as off-topic because it's a C syntax understanding problem and has nothing specifically to do with the hardware the code is running on.
$endgroup$
– brhans
Mar 28 at 11:29




$begingroup$
I'm voting to close this question as off-topic because it's a C syntax understanding problem and has nothing specifically to do with the hardware the code is running on.
$endgroup$
– brhans
Mar 28 at 11:29












$begingroup$
@brhans I edited the title of the question to specify it is for PWM control since that seems to be the largest point of contention for moving this question to stackoverflow. I am not a computer programmer and this is not a computer programming question. It is about how the syntax is affecting register level hardware timer0, NOT why doesn't this syntax work.
$endgroup$
– TammerTheHammer
Mar 28 at 11:57




$begingroup$
@brhans I edited the title of the question to specify it is for PWM control since that seems to be the largest point of contention for moving this question to stackoverflow. I am not a computer programmer and this is not a computer programming question. It is about how the syntax is affecting register level hardware timer0, NOT why doesn't this syntax work.
$endgroup$
– TammerTheHammer
Mar 28 at 11:57




1




1




$begingroup$
It doesn't really matter what the end application is - your problem is that you're not understanding standard C syntax. You would have this same problem if you were writing similarly structured code to run on a PC. The fact that you're writing firmware for a micro is only relevant in how this problem is presenting itself - it's just the symptom.
$endgroup$
– brhans
Mar 28 at 13:13




$begingroup$
It doesn't really matter what the end application is - your problem is that you're not understanding standard C syntax. You would have this same problem if you were writing similarly structured code to run on a PC. The fact that you're writing firmware for a micro is only relevant in how this problem is presenting itself - it's just the symptom.
$endgroup$
– brhans
Mar 28 at 13:13




4




4




$begingroup$
Why aren't you using the perhipheral to generate the PWM for you? Which would allow you to dedicate zero CPU time to this task.
$endgroup$
– Attie
Mar 28 at 13:20




$begingroup$
Why aren't you using the perhipheral to generate the PWM for you? Which would allow you to dedicate zero CPU time to this task.
$endgroup$
– Attie
Mar 28 at 13:20












$begingroup$
@brhans I have only been asking about the symptom, that being the change in frequency. My understanding shouldn't matter since the question has always been about the symptom.
$endgroup$
– TammerTheHammer
Mar 28 at 13:49




$begingroup$
@brhans I have only been asking about the symptom, that being the change in frequency. My understanding shouldn't matter since the question has always been about the symptom.
$endgroup$
– TammerTheHammer
Mar 28 at 13:49










2 Answers
2






active

oldest

votes


















5












$begingroup$

This is more a code syntax question. Could be offtopic, might be moved to stackoverflow.



if() else if() and if() else if() are different.

With correct indentation and brackets the issue is immediately visible:



if() else if()



if(condition)
statement
else
statement

if(condition)
statement



And



if() else if()



if(condition)
statement
else
if(condition)
statement







share|improve this answer









$endgroup$












  • $begingroup$
    i'm asking about the change in frequency (hardware) caused by the change in syntax (code) which is why I asked it here.
    $endgroup$
    – TammerTheHammer
    Mar 28 at 8:29






  • 7




    $begingroup$
    @TammerTheHammer Of course, but you should learn to use if-else-elseif statements to solve the puzzle. It's not the hardware, but your code.
    $endgroup$
    – Marko Buršič
    Mar 28 at 8:46






  • 1




    $begingroup$
    electronics.stackexchange.com/help/on-topic / @TammerTheHammer you asked in the right stack. this has been discussed many times before and some people try to redirect others to stackOverflow while actually you should ask here for softwares related to programming microcontrollers
    $endgroup$
    – Hasan alattar
    Mar 28 at 9:12






  • 2




    $begingroup$
    @TammerTheHammer Well, looking at the question tittle, people would think you are asking how to use if then statements.
    $endgroup$
    – Marko Buršič
    Mar 28 at 9:27






  • 1




    $begingroup$
    @TammerTheHammer You haven't asked how to use if-else, but that's only because you don't realize it's the root of your problem. code that doesn't change the functionality - here it is.
    $endgroup$
    – Agent_L
    Mar 28 at 11:01



















2












$begingroup$

The reason why my frequency goes down and the period goes up with the "if() else if()" is because TCNT0 >= duty is always true when TCNT0 >= COUNT_MAX so the last statement that resets the counter at COUNT_MAX never runs so my counter overflows instead of resetting at a count that gets me a frequency of 300Hz.



I figured it out pretty quickly after posting, sorry if I am misusing this site, I'm posting a question on meta to see what the collective thinks so I can learn and use this resource as best as possible.






share|improve this answer











$endgroup$








  • 4




    $begingroup$
    Posting of your own solution to a problem is encouraged.
    $endgroup$
    – AndrejaKo
    Mar 28 at 8:51










  • $begingroup$
    if you compile without optimaziation i think you will get same frequency but much lower than 244Hz anyway. i find it wierd that you right else . check this out : cpp.sh/3rcrt
    $endgroup$
    – Hasan alattar
    Mar 28 at 9:21











  • $begingroup$
    @Hasanalattar I'll need to research more on compiling before I'll understand this, but why I use else is because i learned misra-c standard in school and it was drilled into my brain that all if() statements must have its accompanying else.
    $endgroup$
    – TammerTheHammer
    Mar 28 at 9:34






  • 2




    $begingroup$
    Sidenote: To my understanding misra requires else only at the end of if ... else if constructs, and not on plain if statements.
    $endgroup$
    – user694733
    Mar 28 at 11:08






  • 1




    $begingroup$
    @TammerTheHammer I tried looking for that MISRA rule, and as far as I can tell there is no such rule. There is one for chained if .. else if ... else if ... which sort of make sense but for a simple if there is no need for an empty else
    $endgroup$
    – r_ahlskog
    Mar 28 at 11:09

















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









5












$begingroup$

This is more a code syntax question. Could be offtopic, might be moved to stackoverflow.



if() else if() and if() else if() are different.

With correct indentation and brackets the issue is immediately visible:



if() else if()



if(condition)
statement
else
statement

if(condition)
statement



And



if() else if()



if(condition)
statement
else
if(condition)
statement







share|improve this answer









$endgroup$












  • $begingroup$
    i'm asking about the change in frequency (hardware) caused by the change in syntax (code) which is why I asked it here.
    $endgroup$
    – TammerTheHammer
    Mar 28 at 8:29






  • 7




    $begingroup$
    @TammerTheHammer Of course, but you should learn to use if-else-elseif statements to solve the puzzle. It's not the hardware, but your code.
    $endgroup$
    – Marko Buršič
    Mar 28 at 8:46






  • 1




    $begingroup$
    electronics.stackexchange.com/help/on-topic / @TammerTheHammer you asked in the right stack. this has been discussed many times before and some people try to redirect others to stackOverflow while actually you should ask here for softwares related to programming microcontrollers
    $endgroup$
    – Hasan alattar
    Mar 28 at 9:12






  • 2




    $begingroup$
    @TammerTheHammer Well, looking at the question tittle, people would think you are asking how to use if then statements.
    $endgroup$
    – Marko Buršič
    Mar 28 at 9:27






  • 1




    $begingroup$
    @TammerTheHammer You haven't asked how to use if-else, but that's only because you don't realize it's the root of your problem. code that doesn't change the functionality - here it is.
    $endgroup$
    – Agent_L
    Mar 28 at 11:01
















5












$begingroup$

This is more a code syntax question. Could be offtopic, might be moved to stackoverflow.



if() else if() and if() else if() are different.

With correct indentation and brackets the issue is immediately visible:



if() else if()



if(condition)
statement
else
statement

if(condition)
statement



And



if() else if()



if(condition)
statement
else
if(condition)
statement







share|improve this answer









$endgroup$












  • $begingroup$
    i'm asking about the change in frequency (hardware) caused by the change in syntax (code) which is why I asked it here.
    $endgroup$
    – TammerTheHammer
    Mar 28 at 8:29






  • 7




    $begingroup$
    @TammerTheHammer Of course, but you should learn to use if-else-elseif statements to solve the puzzle. It's not the hardware, but your code.
    $endgroup$
    – Marko Buršič
    Mar 28 at 8:46






  • 1




    $begingroup$
    electronics.stackexchange.com/help/on-topic / @TammerTheHammer you asked in the right stack. this has been discussed many times before and some people try to redirect others to stackOverflow while actually you should ask here for softwares related to programming microcontrollers
    $endgroup$
    – Hasan alattar
    Mar 28 at 9:12






  • 2




    $begingroup$
    @TammerTheHammer Well, looking at the question tittle, people would think you are asking how to use if then statements.
    $endgroup$
    – Marko Buršič
    Mar 28 at 9:27






  • 1




    $begingroup$
    @TammerTheHammer You haven't asked how to use if-else, but that's only because you don't realize it's the root of your problem. code that doesn't change the functionality - here it is.
    $endgroup$
    – Agent_L
    Mar 28 at 11:01














5












5








5





$begingroup$

This is more a code syntax question. Could be offtopic, might be moved to stackoverflow.



if() else if() and if() else if() are different.

With correct indentation and brackets the issue is immediately visible:



if() else if()



if(condition)
statement
else
statement

if(condition)
statement



And



if() else if()



if(condition)
statement
else
if(condition)
statement







share|improve this answer









$endgroup$



This is more a code syntax question. Could be offtopic, might be moved to stackoverflow.



if() else if() and if() else if() are different.

With correct indentation and brackets the issue is immediately visible:



if() else if()



if(condition)
statement
else
statement

if(condition)
statement



And



if() else if()



if(condition)
statement
else
if(condition)
statement








share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 28 at 8:26









Jeroen3Jeroen3

11.7k1748




11.7k1748











  • $begingroup$
    i'm asking about the change in frequency (hardware) caused by the change in syntax (code) which is why I asked it here.
    $endgroup$
    – TammerTheHammer
    Mar 28 at 8:29






  • 7




    $begingroup$
    @TammerTheHammer Of course, but you should learn to use if-else-elseif statements to solve the puzzle. It's not the hardware, but your code.
    $endgroup$
    – Marko Buršič
    Mar 28 at 8:46






  • 1




    $begingroup$
    electronics.stackexchange.com/help/on-topic / @TammerTheHammer you asked in the right stack. this has been discussed many times before and some people try to redirect others to stackOverflow while actually you should ask here for softwares related to programming microcontrollers
    $endgroup$
    – Hasan alattar
    Mar 28 at 9:12






  • 2




    $begingroup$
    @TammerTheHammer Well, looking at the question tittle, people would think you are asking how to use if then statements.
    $endgroup$
    – Marko Buršič
    Mar 28 at 9:27






  • 1




    $begingroup$
    @TammerTheHammer You haven't asked how to use if-else, but that's only because you don't realize it's the root of your problem. code that doesn't change the functionality - here it is.
    $endgroup$
    – Agent_L
    Mar 28 at 11:01

















  • $begingroup$
    i'm asking about the change in frequency (hardware) caused by the change in syntax (code) which is why I asked it here.
    $endgroup$
    – TammerTheHammer
    Mar 28 at 8:29






  • 7




    $begingroup$
    @TammerTheHammer Of course, but you should learn to use if-else-elseif statements to solve the puzzle. It's not the hardware, but your code.
    $endgroup$
    – Marko Buršič
    Mar 28 at 8:46






  • 1




    $begingroup$
    electronics.stackexchange.com/help/on-topic / @TammerTheHammer you asked in the right stack. this has been discussed many times before and some people try to redirect others to stackOverflow while actually you should ask here for softwares related to programming microcontrollers
    $endgroup$
    – Hasan alattar
    Mar 28 at 9:12






  • 2




    $begingroup$
    @TammerTheHammer Well, looking at the question tittle, people would think you are asking how to use if then statements.
    $endgroup$
    – Marko Buršič
    Mar 28 at 9:27






  • 1




    $begingroup$
    @TammerTheHammer You haven't asked how to use if-else, but that's only because you don't realize it's the root of your problem. code that doesn't change the functionality - here it is.
    $endgroup$
    – Agent_L
    Mar 28 at 11:01
















$begingroup$
i'm asking about the change in frequency (hardware) caused by the change in syntax (code) which is why I asked it here.
$endgroup$
– TammerTheHammer
Mar 28 at 8:29




$begingroup$
i'm asking about the change in frequency (hardware) caused by the change in syntax (code) which is why I asked it here.
$endgroup$
– TammerTheHammer
Mar 28 at 8:29




7




7




$begingroup$
@TammerTheHammer Of course, but you should learn to use if-else-elseif statements to solve the puzzle. It's not the hardware, but your code.
$endgroup$
– Marko Buršič
Mar 28 at 8:46




$begingroup$
@TammerTheHammer Of course, but you should learn to use if-else-elseif statements to solve the puzzle. It's not the hardware, but your code.
$endgroup$
– Marko Buršič
Mar 28 at 8:46




1




1




$begingroup$
electronics.stackexchange.com/help/on-topic / @TammerTheHammer you asked in the right stack. this has been discussed many times before and some people try to redirect others to stackOverflow while actually you should ask here for softwares related to programming microcontrollers
$endgroup$
– Hasan alattar
Mar 28 at 9:12




$begingroup$
electronics.stackexchange.com/help/on-topic / @TammerTheHammer you asked in the right stack. this has been discussed many times before and some people try to redirect others to stackOverflow while actually you should ask here for softwares related to programming microcontrollers
$endgroup$
– Hasan alattar
Mar 28 at 9:12




2




2




$begingroup$
@TammerTheHammer Well, looking at the question tittle, people would think you are asking how to use if then statements.
$endgroup$
– Marko Buršič
Mar 28 at 9:27




$begingroup$
@TammerTheHammer Well, looking at the question tittle, people would think you are asking how to use if then statements.
$endgroup$
– Marko Buršič
Mar 28 at 9:27




1




1




$begingroup$
@TammerTheHammer You haven't asked how to use if-else, but that's only because you don't realize it's the root of your problem. code that doesn't change the functionality - here it is.
$endgroup$
– Agent_L
Mar 28 at 11:01





$begingroup$
@TammerTheHammer You haven't asked how to use if-else, but that's only because you don't realize it's the root of your problem. code that doesn't change the functionality - here it is.
$endgroup$
– Agent_L
Mar 28 at 11:01














2












$begingroup$

The reason why my frequency goes down and the period goes up with the "if() else if()" is because TCNT0 >= duty is always true when TCNT0 >= COUNT_MAX so the last statement that resets the counter at COUNT_MAX never runs so my counter overflows instead of resetting at a count that gets me a frequency of 300Hz.



I figured it out pretty quickly after posting, sorry if I am misusing this site, I'm posting a question on meta to see what the collective thinks so I can learn and use this resource as best as possible.






share|improve this answer











$endgroup$








  • 4




    $begingroup$
    Posting of your own solution to a problem is encouraged.
    $endgroup$
    – AndrejaKo
    Mar 28 at 8:51










  • $begingroup$
    if you compile without optimaziation i think you will get same frequency but much lower than 244Hz anyway. i find it wierd that you right else . check this out : cpp.sh/3rcrt
    $endgroup$
    – Hasan alattar
    Mar 28 at 9:21











  • $begingroup$
    @Hasanalattar I'll need to research more on compiling before I'll understand this, but why I use else is because i learned misra-c standard in school and it was drilled into my brain that all if() statements must have its accompanying else.
    $endgroup$
    – TammerTheHammer
    Mar 28 at 9:34






  • 2




    $begingroup$
    Sidenote: To my understanding misra requires else only at the end of if ... else if constructs, and not on plain if statements.
    $endgroup$
    – user694733
    Mar 28 at 11:08






  • 1




    $begingroup$
    @TammerTheHammer I tried looking for that MISRA rule, and as far as I can tell there is no such rule. There is one for chained if .. else if ... else if ... which sort of make sense but for a simple if there is no need for an empty else
    $endgroup$
    – r_ahlskog
    Mar 28 at 11:09















2












$begingroup$

The reason why my frequency goes down and the period goes up with the "if() else if()" is because TCNT0 >= duty is always true when TCNT0 >= COUNT_MAX so the last statement that resets the counter at COUNT_MAX never runs so my counter overflows instead of resetting at a count that gets me a frequency of 300Hz.



I figured it out pretty quickly after posting, sorry if I am misusing this site, I'm posting a question on meta to see what the collective thinks so I can learn and use this resource as best as possible.






share|improve this answer











$endgroup$








  • 4




    $begingroup$
    Posting of your own solution to a problem is encouraged.
    $endgroup$
    – AndrejaKo
    Mar 28 at 8:51










  • $begingroup$
    if you compile without optimaziation i think you will get same frequency but much lower than 244Hz anyway. i find it wierd that you right else . check this out : cpp.sh/3rcrt
    $endgroup$
    – Hasan alattar
    Mar 28 at 9:21











  • $begingroup$
    @Hasanalattar I'll need to research more on compiling before I'll understand this, but why I use else is because i learned misra-c standard in school and it was drilled into my brain that all if() statements must have its accompanying else.
    $endgroup$
    – TammerTheHammer
    Mar 28 at 9:34






  • 2




    $begingroup$
    Sidenote: To my understanding misra requires else only at the end of if ... else if constructs, and not on plain if statements.
    $endgroup$
    – user694733
    Mar 28 at 11:08






  • 1




    $begingroup$
    @TammerTheHammer I tried looking for that MISRA rule, and as far as I can tell there is no such rule. There is one for chained if .. else if ... else if ... which sort of make sense but for a simple if there is no need for an empty else
    $endgroup$
    – r_ahlskog
    Mar 28 at 11:09













2












2








2





$begingroup$

The reason why my frequency goes down and the period goes up with the "if() else if()" is because TCNT0 >= duty is always true when TCNT0 >= COUNT_MAX so the last statement that resets the counter at COUNT_MAX never runs so my counter overflows instead of resetting at a count that gets me a frequency of 300Hz.



I figured it out pretty quickly after posting, sorry if I am misusing this site, I'm posting a question on meta to see what the collective thinks so I can learn and use this resource as best as possible.






share|improve this answer











$endgroup$



The reason why my frequency goes down and the period goes up with the "if() else if()" is because TCNT0 >= duty is always true when TCNT0 >= COUNT_MAX so the last statement that resets the counter at COUNT_MAX never runs so my counter overflows instead of resetting at a count that gets me a frequency of 300Hz.



I figured it out pretty quickly after posting, sorry if I am misusing this site, I'm posting a question on meta to see what the collective thinks so I can learn and use this resource as best as possible.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 28 at 9:39

























answered Mar 28 at 8:44









TammerTheHammerTammerTheHammer

729




729







  • 4




    $begingroup$
    Posting of your own solution to a problem is encouraged.
    $endgroup$
    – AndrejaKo
    Mar 28 at 8:51










  • $begingroup$
    if you compile without optimaziation i think you will get same frequency but much lower than 244Hz anyway. i find it wierd that you right else . check this out : cpp.sh/3rcrt
    $endgroup$
    – Hasan alattar
    Mar 28 at 9:21











  • $begingroup$
    @Hasanalattar I'll need to research more on compiling before I'll understand this, but why I use else is because i learned misra-c standard in school and it was drilled into my brain that all if() statements must have its accompanying else.
    $endgroup$
    – TammerTheHammer
    Mar 28 at 9:34






  • 2




    $begingroup$
    Sidenote: To my understanding misra requires else only at the end of if ... else if constructs, and not on plain if statements.
    $endgroup$
    – user694733
    Mar 28 at 11:08






  • 1




    $begingroup$
    @TammerTheHammer I tried looking for that MISRA rule, and as far as I can tell there is no such rule. There is one for chained if .. else if ... else if ... which sort of make sense but for a simple if there is no need for an empty else
    $endgroup$
    – r_ahlskog
    Mar 28 at 11:09












  • 4




    $begingroup$
    Posting of your own solution to a problem is encouraged.
    $endgroup$
    – AndrejaKo
    Mar 28 at 8:51










  • $begingroup$
    if you compile without optimaziation i think you will get same frequency but much lower than 244Hz anyway. i find it wierd that you right else . check this out : cpp.sh/3rcrt
    $endgroup$
    – Hasan alattar
    Mar 28 at 9:21











  • $begingroup$
    @Hasanalattar I'll need to research more on compiling before I'll understand this, but why I use else is because i learned misra-c standard in school and it was drilled into my brain that all if() statements must have its accompanying else.
    $endgroup$
    – TammerTheHammer
    Mar 28 at 9:34






  • 2




    $begingroup$
    Sidenote: To my understanding misra requires else only at the end of if ... else if constructs, and not on plain if statements.
    $endgroup$
    – user694733
    Mar 28 at 11:08






  • 1




    $begingroup$
    @TammerTheHammer I tried looking for that MISRA rule, and as far as I can tell there is no such rule. There is one for chained if .. else if ... else if ... which sort of make sense but for a simple if there is no need for an empty else
    $endgroup$
    – r_ahlskog
    Mar 28 at 11:09







4




4




$begingroup$
Posting of your own solution to a problem is encouraged.
$endgroup$
– AndrejaKo
Mar 28 at 8:51




$begingroup$
Posting of your own solution to a problem is encouraged.
$endgroup$
– AndrejaKo
Mar 28 at 8:51












$begingroup$
if you compile without optimaziation i think you will get same frequency but much lower than 244Hz anyway. i find it wierd that you right else . check this out : cpp.sh/3rcrt
$endgroup$
– Hasan alattar
Mar 28 at 9:21





$begingroup$
if you compile without optimaziation i think you will get same frequency but much lower than 244Hz anyway. i find it wierd that you right else . check this out : cpp.sh/3rcrt
$endgroup$
– Hasan alattar
Mar 28 at 9:21













$begingroup$
@Hasanalattar I'll need to research more on compiling before I'll understand this, but why I use else is because i learned misra-c standard in school and it was drilled into my brain that all if() statements must have its accompanying else.
$endgroup$
– TammerTheHammer
Mar 28 at 9:34




$begingroup$
@Hasanalattar I'll need to research more on compiling before I'll understand this, but why I use else is because i learned misra-c standard in school and it was drilled into my brain that all if() statements must have its accompanying else.
$endgroup$
– TammerTheHammer
Mar 28 at 9:34




2




2




$begingroup$
Sidenote: To my understanding misra requires else only at the end of if ... else if constructs, and not on plain if statements.
$endgroup$
– user694733
Mar 28 at 11:08




$begingroup$
Sidenote: To my understanding misra requires else only at the end of if ... else if constructs, and not on plain if statements.
$endgroup$
– user694733
Mar 28 at 11:08




1




1




$begingroup$
@TammerTheHammer I tried looking for that MISRA rule, and as far as I can tell there is no such rule. There is one for chained if .. else if ... else if ... which sort of make sense but for a simple if there is no need for an empty else
$endgroup$
– r_ahlskog
Mar 28 at 11:09




$begingroup$
@TammerTheHammer I tried looking for that MISRA rule, and as far as I can tell there is no such rule. There is one for chained if .. else if ... else if ... which sort of make sense but for a simple if there is no need for an empty else
$endgroup$
– r_ahlskog
Mar 28 at 11:09



Popular posts from this blog

Adding axes to figuresAdding axes labels to LaTeX figuresLaTeX equivalent of ConTeXt buffersRotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?TikZ scaling graphic and adjust node position and keep font sizeNumerical conditional within tikz keys?adding axes to shapesAlign axes across subfiguresAdding figures with a certain orderLine up nested tikz enviroments or how to get rid of themAdding axes labels to LaTeX figures

Tähtien Talli Jäsenet | Lähteet | NavigointivalikkoSuomen Hippos – Tähtien Talli

Do these cracks on my tires look bad? The Next CEO of Stack OverflowDry rot tire should I replace?Having to replace tiresFishtailed so easily? Bad tires? ABS?Filling the tires with something other than air, to avoid puncture hassles?Used Michelin tires safe to install?Do these tyre cracks necessitate replacement?Rumbling noise: tires or mechanicalIs it possible to fix noisy feathered tires?Are bad winter tires still better than summer tires in winter?Torque converter failure - Related to replacing only 2 tires?Why use snow tires on all 4 wheels on 2-wheel-drive cars?