• AVR Freaks

AnsweredHot![Answered]multi Switch case inside switch case

Author
MOHAEGY
Starting Member
  • Total Posts : 55
  • Reward points : 0
  • Joined: 2020/05/27 11:33:24
  • Location: 0
  • Status: offline
2020/06/16 16:02:54 (permalink)
0

[Answered]multi Switch case inside switch case

Hi all 
I use xc8 compiler and write a multi tasks project my question is :
Can i use SWITCH-CASE to schedule the main tasks in my project then use another SWITCH- CASE inside very case of the main SWITCH-CASE?
By another words  IS there any problem if i do the following:
 
SWITCH main_tasks:{
                                 case1:{
                                             SWITCH task1_mode:{
                                                                                 case1: Do some actions 
                                                                                 case2: do some actions }
                                 case2:{
                                           
                                               SWITCH task1_mode:{
                                                                                 case1: Do some actions 
                                                                                 case2: do some actions }
                         and so on  for may be more than 10 cases
is it right?
 
post edited by MOHAEGY - 2020/06/16 17:04:04
#1
ric
Super Member
  • Total Posts : 27652
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: multi Switch case inside switch case 2020/06/16 16:11:27 (permalink)
+2 (2)
Yes, you can have switch statements inside switch statements, but your syntax is wrong.
For starters they are not upper case.
Next, the variable you are switching on must be inside parenthesis.
Also, you are missing the required "break" statements.
 
Time to go back to your C textbook to see how to write switch statements!
 
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#2
1and0
Access is Denied
  • Total Posts : 10906
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: multi Switch case inside switch case 2020/06/16 16:12:33 (permalink)
0
Did the compiler complaint? ;)  You're missing some closing braces.  Nested switch statements are valid.
 
Edit: Ric finds more issues!
#3
MOHAEGY
Starting Member
  • Total Posts : 55
  • Reward points : 0
  • Joined: 2020/05/27 11:33:24
  • Location: 0
  • Status: offline
Re: multi Switch case inside switch case 2020/06/16 16:26:19 (permalink)
0
OK i just explain my question with just code outline not with the real code syntax 
 
Now for the Switch case variables(like task1_mode which i use in my example ) this variables will changed by ISR  So i must declare this variables as VOLATILE and must give them an initial values 
Am right?
 
#4
ric
Super Member
  • Total Posts : 27652
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: multi Switch case inside switch case 2020/06/16 16:46:51 (permalink) ☼ Best Answerby MOHAEGY 2020/06/16 16:57:54
+2 (2)
Robotics011
OK i just explain my question with just code outline not with the real code syntax

Why?
Don't ask "is this right" when it's not real code you are showing. Plainly the answer is "no".
 

Now for the Switch case variables(like task1_mode which i use in my example ) this variables will changed by ISR  So i must declare this variables as VOLATILE and must give them an initial values 
Am right?

Yes.
I hope they are eight bit variables. If they are any wider, you have other things to worry about too.
post edited by ric - 2020/06/16 17:16:04

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#5
MOHAEGY
Starting Member
  • Total Posts : 55
  • Reward points : 0
  • Joined: 2020/05/27 11:33:24
  • Location: 0
  • Status: offline
Re: multi Switch case inside switch case 2020/06/16 17:02:29 (permalink)
0
OK thanks RIC your replies really very useful 
I understand the problem of using 16bit variable  in main loop and ISR but for my project uint8_t  is more enough for me 
Thanks 
#6
MOHAEGY
Starting Member
  • Total Posts : 55
  • Reward points : 0
  • Joined: 2020/05/27 11:33:24
  • Location: 0
  • Status: offline
Re: multi Switch case inside switch case 2020/06/21 05:20:23 (permalink)
-2 (2)
Just for all read this thread " After many searches and ask many experts i found that "we must a void to use nested SWITCH-CASE" in our design" also we must avoid nested if statements just use 2 or maximum 3 nested if statements
 
Thanks to all
Mohamed Megahed
#7
ric
Super Member
  • Total Posts : 27652
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: multi Switch case inside switch case 2020/06/21 05:26:12 (permalink)
+2 (2)
Look for some new experts.
None of those "rules" you just stated are valid.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#8
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3950
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: multi Switch case inside switch case 2020/06/21 06:35:36 (permalink)
+1 (1)
Robotics011
Just for all read this thread " After many searches and ask many experts i found that "we must a void to use nested SWITCH-CASE" in our design" also we must avoid nested if statements just use 2 or maximum 3 nested if statements
 
Thanks to all
Mohamed Megahed


I'll make a note of that.

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#9
NorthGuy
Super Member
  • Total Posts : 6174
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: multi Switch case inside switch case 2020/06/21 08:25:56 (permalink) ☄ Helpfulby MOHAEGY 2020/06/22 10:12:54
+5 (5)
Robotics011
Just for all read this thread " After many searches and ask many experts i found that "we must a void to use nested SWITCH-CASE" in our design" also we must avoid nested if statements just use 2 or maximum 3 nested if statements



There always were lots of people who suggest that following certain rules will somehow guard you from bugs and errors. There's no truth to this. Don't waste your time reading their rules.
 
Instead spend time studying the architecture and the language you use. Then go ahead, program, get messy, and make mistakes. Mistakes are good for you because you learn from them.
#10
Jump to:
© 2020 APG vNext Commercial Version 4.5