if() else if() VS if() else if() in PWM control The Next CEO of Stack OverflowPWM 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

Shade part of a Venn diagram

Example of a Mathematician/Physicist whose Other Publications during their PhD eclipsed their PhD Thesis

I believe this to be a fraud - hired, then asked to cash check and send cash as Bitcoin

If I blow insulation everywhere in my attic except the door trap, will heat escape through it?

WOW air has ceased operation, can I get my tickets refunded?

Where to find order of arguments for default functions

How to write papers efficiently when English isn't my first language?

Is a stroke of luck acceptable after a series of unfavorable events?

Science fiction (dystopian) short story set after WWIII

What's the point of interval inversion?

Customer Requests (Sometimes) Drive Me Bonkers!

How to write the block matrix in LaTex?

How easy is it to start Magic from scratch?

How to count occurrences of text in a file?

Anatomically Correct Mesopelagic Aves

Return the Closest Prime Number

Is it my responsibility to learn a new technology in my own time my employer wants to implement?

How to get regions to plot as graphics

Why do remote companies require working in the US?

Apart from "berlinern", do any other German dialects have a corresponding verb?

The King's new dress

Robert Sheckley short story about vacation spots being overwhelmed

How do spells that require an ability check vs. the caster's spell save DC work?

How does practicing restraint and performing actions of merit purify the mind?



if() else if() VS if() else if() in PWM control



The Next CEO of Stack OverflowPWM 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










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$







  • 1




    $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
    19 hours ago










  • $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
    18 hours ago






  • 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
    17 hours ago






  • 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
    17 hours ago










  • $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
    16 hours ago















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$







  • 1




    $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
    19 hours ago










  • $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
    18 hours ago






  • 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
    17 hours ago






  • 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
    17 hours ago










  • $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
    16 hours ago













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 18 hours ago







TammerTheHammer

















asked 22 hours ago









TammerTheHammerTammerTheHammer

729




729







  • 1




    $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
    19 hours ago










  • $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
    18 hours ago






  • 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
    17 hours ago






  • 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
    17 hours ago










  • $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
    16 hours ago












  • 1




    $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
    19 hours ago










  • $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
    18 hours ago






  • 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
    17 hours ago






  • 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
    17 hours ago










  • $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
    16 hours ago







1




1




$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
19 hours ago




$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
19 hours ago












$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
18 hours ago




$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
18 hours ago




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
17 hours ago




$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
17 hours ago




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
17 hours ago




$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
17 hours ago












$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
16 hours ago




$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
16 hours ago










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
    22 hours ago






  • 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č
    22 hours ago






  • 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
    21 hours ago






  • 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č
    21 hours ago






  • 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
    19 hours ago



















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
    21 hours ago










  • $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
    21 hours ago











  • $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
    21 hours ago






  • 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
    19 hours ago






  • 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
    19 hours ago











Your Answer





StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
return StackExchange.using("schematics", function ()
StackExchange.schematics.init();
);
, "cicuitlab");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "135"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2felectronics.stackexchange.com%2fquestions%2f429453%2fif-else-if-vs-if-else-if-in-pwm-control%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























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
    22 hours ago






  • 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č
    22 hours ago






  • 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
    21 hours ago






  • 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č
    21 hours ago






  • 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
    19 hours ago
















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
    22 hours ago






  • 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č
    22 hours ago






  • 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
    21 hours ago






  • 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č
    21 hours ago






  • 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
    19 hours ago














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 22 hours ago









Jeroen3Jeroen3

11.6k1748




11.6k1748











  • $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
    22 hours ago






  • 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č
    22 hours ago






  • 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
    21 hours ago






  • 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č
    21 hours ago






  • 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
    19 hours ago

















  • $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
    22 hours ago






  • 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č
    22 hours ago






  • 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
    21 hours ago






  • 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č
    21 hours ago






  • 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
    19 hours ago
















$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
22 hours ago




$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
22 hours ago




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č
22 hours ago




$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č
22 hours ago




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
21 hours ago




$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
21 hours ago




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č
21 hours ago




$begingroup$
@TammerTheHammer Well, looking at the question tittle, people would think you are asking how to use if then statements.
$endgroup$
– Marko Buršič
21 hours ago




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
19 hours ago





$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
19 hours ago














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
    21 hours ago










  • $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
    21 hours ago











  • $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
    21 hours ago






  • 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
    19 hours ago






  • 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
    19 hours ago















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
    21 hours ago










  • $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
    21 hours ago











  • $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
    21 hours ago






  • 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
    19 hours ago






  • 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
    19 hours ago













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 21 hours ago

























answered 22 hours ago









TammerTheHammerTammerTheHammer

729




729







  • 4




    $begingroup$
    Posting of your own solution to a problem is encouraged.
    $endgroup$
    – AndrejaKo
    21 hours ago










  • $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
    21 hours ago











  • $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
    21 hours ago






  • 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
    19 hours ago






  • 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
    19 hours ago












  • 4




    $begingroup$
    Posting of your own solution to a problem is encouraged.
    $endgroup$
    – AndrejaKo
    21 hours ago










  • $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
    21 hours ago











  • $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
    21 hours ago






  • 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
    19 hours ago






  • 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
    19 hours ago







4




4




$begingroup$
Posting of your own solution to a problem is encouraged.
$endgroup$
– AndrejaKo
21 hours ago




$begingroup$
Posting of your own solution to a problem is encouraged.
$endgroup$
– AndrejaKo
21 hours ago












$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
21 hours ago





$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
21 hours ago













$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
21 hours ago




$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
21 hours ago




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
19 hours ago




$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
19 hours ago




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
19 hours ago




$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
19 hours ago

















draft saved

draft discarded
















































Thanks for contributing an answer to Electrical Engineering Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2felectronics.stackexchange.com%2fquestions%2f429453%2fif-else-if-vs-if-else-if-in-pwm-control%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wikipedia:Vital articles Мазмуну Biography - Өмүр баян Philosophy and psychology - Философия жана психология Religion - Дин Social sciences - Коомдук илимдер Language and literature - Тил жана адабият Science - Илим Technology - Технология Arts and recreation - Искусство жана эс алуу History and geography - Тарых жана география Навигация менюсу

Club Baloncesto Breogán Índice Historia | Pavillón | Nome | O Breogán na cultura popular | Xogadores | Adestradores | Presidentes | Palmarés | Historial | Líderes | Notas | Véxase tamén | Menú de navegacióncbbreogan.galCadroGuía oficial da ACB 2009-10, páxina 201Guía oficial ACB 1992, páxina 183. Editorial DB.É de 6.500 espectadores sentados axeitándose á última normativa"Estudiantes Junior, entre as mellores canteiras"o orixinalHemeroteca El Mundo Deportivo, 16 setembro de 1970, páxina 12Historia do BreogánAlfredo Pérez, o último canoneiroHistoria C.B. BreogánHemeroteca de El Mundo DeportivoJimmy Wright, norteamericano do Breogán deixará Lugo por ameazas de morteResultados de Breogán en 1986-87Resultados de Breogán en 1990-91Ficha de Velimir Perasović en acb.comResultados de Breogán en 1994-95Breogán arrasa al Barça. "El Mundo Deportivo", 27 de setembro de 1999, páxina 58CB Breogán - FC BarcelonaA FEB invita a participar nunha nova Liga EuropeaCharlie Bell na prensa estatalMáximos anotadores 2005Tempada 2005-06 : Tódolos Xogadores da Xornada""Non quero pensar nunha man negra, mais pregúntome que está a pasar""o orixinalRaúl López, orgulloso dos xogadores, presume da boa saúde económica do BreogánJulio González confirma que cesa como presidente del BreogánHomenaxe a Lisardo GómezA tempada do rexurdimento celesteEntrevista a Lisardo GómezEl COB dinamita el Pazo para forzar el quinto (69-73)Cafés Candelas, patrocinador del CB Breogán"Suso Lázare, novo presidente do Breogán"o orixinalCafés Candelas Breogán firma el mayor triunfo de la historiaEl Breogán realizará 17 homenajes por su cincuenta aniversario"O Breogán honra ao seu fundador e primeiro presidente"o orixinalMiguel Giao recibiu a homenaxe do PazoHomenaxe aos primeiros gladiadores celestesO home que nos amosa como ver o Breo co corazónTita Franco será homenaxeada polos #50anosdeBreoJulio Vila recibirá unha homenaxe in memoriam polos #50anosdeBreo"O Breogán homenaxeará aos seus aboados máis veteráns"Pechada ovación a «Capi» Sanmartín e Ricardo «Corazón de González»Homenaxe por décadas de informaciónPaco García volve ao Pazo con motivo do 50 aniversario"Resultados y clasificaciones""O Cafés Candelas Breogán, campión da Copa Princesa""O Cafés Candelas Breogán, equipo ACB"C.B. Breogán"Proxecto social"o orixinal"Centros asociados"o orixinalFicha en imdb.comMario Camus trata la recuperación del amor en 'La vieja música', su última película"Páxina web oficial""Club Baloncesto Breogán""C. B. Breogán S.A.D."eehttp://www.fegaba.com

What should I write in an apology letter, since I have decided not to join a company after accepting an offer letterShould I keep looking after accepting a job offer?What should I do when I've been verbally told I would get an offer letter, but still haven't gotten one after 4 weeks?Do I accept an offer from a company that I am not likely to join?New job hasn't confirmed starting date and I want to give current employer as much notice as possibleHow should I address my manager in my resignation letter?HR delayed background verification, now jobless as resignedNo email communication after accepting a formal written offer. How should I phrase the call?What should I do if after receiving a verbal offer letter I am informed that my written job offer is put on hold due to some internal issues?Should I inform the current employer that I am about to resign within 1-2 weeks since I have signed the offer letter and waiting for visa?What company will do, if I send their offer letter to another company