cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with switch/case

Jakub B
Associate III
Posted on April 03, 2018 at 15:22

Hi

I got a problem with a switch in my MCU. I use custom board with STM32F030C8T6.

This here is part of my program:

void rozkaz3(void) {

switch(bufor[3]){

case 0:

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_RESET);

HAL_Delay(1000);

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_SET);

break;

case 1: HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET);

HAL_Delay(1000);

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_SET);

break;

case 2:

HAL_GPIO_WritePin(GPIOF, GPIO_PIN_7, GPIO_PIN_RESET);

HAL_Delay(1000);

HAL_GPIO_WritePin(GPIOF, GPIO_PIN_7, GPIO_PIN_SET);

break;

case 3:

HAL_GPIO_WritePin(GPIOF, GPIO_PIN_6, GPIO_PIN_RESET);

HAL_Delay(1000);

HAL_GPIO_WritePin(GPIOF, GPIO_PIN_6, GPIO_PIN_SET);

break;

case 5:

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_RESET);

HAL_Delay(1000);

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_SET);

break;

case 6:

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);

HAL_Delay(1000);

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);

break;

case 7:

case 4:

default:

break;

case 12:

NVIC_SystemReset();

break;

it switches on and off outputs i chose based on number. My board has 6 outputs. Case 4 and case 7 are not configured and not connected.

If i order case 0,1,2,3,5,6,11,12 all works well.

If i order case 4 or 7 my program works on case 0 then case 4/7. If i delete case 0 it will work on case 1 and then case 4/7 and so on.

It works first available case in switch then case i ordered.

Now if i change only this:

case 7:

case 4:

default:

HAL_Delay(1000);

break;

I works ok.

And with HAL_Delay(1) its not working again.

Whats happening and how to make it work right without HAL_Delay ?

Note: this post was migrated and contained many threaded conversations, some content may be missing.
34 REPLIES 34
Posted on April 03, 2018 at 16:25

I say the problem is in that part of code we don't see.

JW

T J
Lead
Posted on April 04, 2018 at 01:17

case 4 is out of order,

you must either leave it out,

or put it in order.

Case 12 is also out of order.

do you like Dials or Knobs ?

Jakub B
Associate III
Posted on April 04, 2018 at 09:22

case 4 works fine like that. Ive put it in order for test now and nothing changed.

i checked order of cases and i can switch hovewer i want it and it doesnt matter.

I did put for fun cases in order 1253067 and i worked with no changes.

edit: im a knob man

Jakub B
Associate III
Posted on April 04, 2018 at 11:22

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6ut&d=%2Fa%2F0X0000000bxw%2FnTxLuPjtz4KBL_xSDI7r3_vg6VpNEiy58KfZmlY31Xo&asPdf=false
Jakub B
Associate III
Posted on April 04, 2018 at 09:27

i deleted those cases that shouldnt be available (4 and 7) and first from top is used when order 4 or 7, default does nothing for me...

Andrew Neil
Evangelist III
Posted on April 04, 2018 at 11:34

If i order case 4 or 7 my program works on

case 0 then case 4/7

.

Are you describing what happens when you step the code in the debugger?

If so, this could be just down to optimisation

https://www.avrfreaks.net/forum/shortcuts-taken-optimized-code-may-occasionally-sic-be-surprising

 

Do you actually see the correct pins being toggled ?

Posted on April 04, 2018 at 09:44

case 4 is out of order, ...

No, it is not.

There is no requirement of the C language to put cases in a specific order.

I agree with Jan, there seems to be a problem with the code not shown.

Is there perhaps something changing the contents of the switch variable 

(bufor[3])

in the background ?
Posted on April 04, 2018 at 09:48

i checked the process in debugger and  bufor[3]  is not changed before exiting this part of code.

Posted on April 04, 2018 at 09:50

artistically , knob is the better word,  but its just a bit too colourful these days..