switch ReadyState() freezes

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

Post Reply
User avatar
mikn
Posts: 209
Joined: Mon Mar 03, 2014 10:11 pm
Has thanked: 54 times
Been thanked: 41 times
Contact:

switch ReadyState() freezes

Post by mikn »

I have a Switch Push PCB on PortA.3, Active Low, debounce 10.
When it's calling the function Readystate() it freezes the loop on this step.
Once I press the button it continues the loop and then stop again on this function. What's wrong? I thought it must pass by and return either 0 or 1, but it reacts only on 1 (pressed).
Same for other swithces, on same function ReadyState.
Attachments
sc-but1.GIF
sc-but1.GIF (2.53 KiB) Viewed 7687 times
FC 6.1.3.2 (18.02.2016)

User avatar
mikn
Posts: 209
Joined: Mon Mar 03, 2014 10:11 pm
Has thanked: 54 times
Been thanked: 41 times
Contact:

Re: switch ReadyState() freezes

Post by mikn »

Found during experiments that when I lower the debounce value to 2-4 ms then it shows like the button is pressed (=1) many times (some noise in the port). When I grow the value back to 9-10ms then it stops again on this step forever until I press the button.
FC 6.1.3.2 (18.02.2016)

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: switch ReadyState() freezes

Post by medelec35 »

Hi mikn,
I have different results to you.
Im using ICT mode of EB006 V9to measure real time and display on the scope.
As you can see when switch is not pressed, portA0 is toggling every 11ms (10ms debounce + 1 ms delay)
If switch is pressed then A0 stays high as expected:
Real time Measurement.png
(93.76 KiB) Downloaded 3786 times
If you have not got a EB006 V9 then the simulator should show if readstate works as expected:
Simulation Time.png
(106.18 KiB) Downloaded 3786 times
The only difference is simulated time is much different from real time.

After compiling take a look at compiler messages and see if you have a CASM error. possible stack corruption.

If you would like to post your flowchart, I can take a look at it for you.


Martin
Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: switch ReadyState() freezes

Post by medelec35 »

I have tried on active high and active low, and both flowchart simulation and hardware are working as expected with no freezing.
Martin

User avatar
mikn
Posts: 209
Joined: Mon Mar 03, 2014 10:11 pm
Has thanked: 54 times
Been thanked: 41 times
Contact:

Re: switch ReadyState() freezes

Post by mikn »

Hi Martin,

I don't have EBlocks and I test the code right on my complete hardware.
Original code is too big so I created new project with same parameters and ports. Same problem is here.
Look to the code. In the loop it must send to uart 1212121212.... and when I press the button it must be like 1231212...
Look to the screenshot, I pressed several times the button and it worked, but again every time it stops on readstate untill I press the button.
Attachments
btest.fcfx
(6.47 KiB) Downloaded 205 times
terminal screenshot
terminal screenshot
sc-123123.GIF (7.79 KiB) Viewed 7659 times
Last edited by mikn on Mon Mar 31, 2014 12:16 am, edited 1 time in total.
FC 6.1.3.2 (18.02.2016)

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: switch ReadyState() freezes

Post by medelec35 »

Hi mikn,
This may sound a bit daft, but can you measure the microcontroller supply a before switch is pressed and while switch is pressed.
Perhaps you can post a circuit diagram?

I have tried your flowchart on a 16F1937 as that's all I have got set up ans your flowchart is working fine:
Pic version.png
(127.24 KiB) Downloaded 3779 times
So either there is an issue with your hardware or there is a bug but is just with your type of microcontroller?
If its not sorted by tomorrow, I will see if I can dig a a similar target device but I'm not sure if I have one or not?
Martin

User avatar
mikn
Posts: 209
Joined: Mon Mar 03, 2014 10:11 pm
Has thanked: 54 times
Been thanked: 41 times
Contact:

Re: switch ReadyState() freezes

Post by mikn »

Measured the voltage, it's always 3.14v. I power the device from Pickit3 directly.
The hardware works well I am sure 100%, because I have another firmware (without source code) for it and it works fine. So I think the problem is somewhere inside FC code.
UPD: tested with power from 3.7LiPo battery, disconnected from Pickit. Same thing, it stays and waits for the button pressed.
UPD2:
Perhaps you can post a circuit diagram?
Not sure I have it with me. But the button is simple connected to the ground. The button shorts the PortA.2 to the GND.
FC 6.1.3.2 (18.02.2016)

User avatar
mikn
Posts: 209
Joined: Mon Mar 03, 2014 10:11 pm
Has thanked: 54 times
Been thanked: 41 times
Contact:

Re: switch ReadyState() freezes

Post by mikn »

Some more experiments.
I've disabled Readstate and added calculation.
Send 1 to portA.2 , then read it immediately. And it works ok. But when I remove portA.2=1 it always gives me pressed button (terminal prints 132132132...)
After I changed back to Readstate, it freezes again on this step. Tried to add PortA.2=1 before Readstate - it didn't help.
Attachments
sc-123123333.GIF
sc-123123333.GIF (21.88 KiB) Viewed 7654 times
FC 6.1.3.2 (18.02.2016)

Jojo1981
Posts: 28
Joined: Wed Feb 29, 2012 9:23 pm
Been thanked: 3 times
Contact:

Re: switch ReadyState() freezes

Post by Jojo1981 »

I don't know if it helps you but it is also a mistake in the SWITCH ReadState macro.

Switch0 is connect to Port A5 on a PIC16F676 device and is active high with 20ms deburning.

In simulation, I got the value 1 back if I pressed the button. but in the device, I got 0b00010000 back ?????

Turn the polarity of the switch to low, the return gets from 0b00000000 to 0b00000001 ! this is in my mind the right value.

A short look into the FC5_PIC_SWITCH.c

Code: Select all

	  #if (%a_SW_POLE == 0)
		if (switchval)		//Invert logic if required
			switchval = 0;
		else
			switchval = 1;
	  #endif
But II think it must on both locations:

Code: Select all

  #if (%a_SW_POLE == 0)
	if (switchval)			//Invert logic if required
		switchval = 0;
	else
		switchval = 1;
  #else					//Rückgabewert auf 0 oder 1
	if (switchval)			
		switchval = 1;
	else
		switchval = 0;
  #endif
Iam sure about this or is it required to get this different returns?

kind regards,
Jojo

User avatar
mikn
Posts: 209
Joined: Mon Mar 03, 2014 10:11 pm
Has thanked: 54 times
Been thanked: 41 times
Contact:

Re: switch ReadyState() freezes

Post by mikn »

Thanks for advice. I work in FC6 and it has closed code of components.
I made temporary code with manual retrieving data from buttons and doing debounce loop. But will look forward if Matrix guys will review this issue so I could use button component instead of huge block code.
For now I have the following algo to read the button and do debounce (maybe it will help to someone sometime):
Attachments
sc-butread.GIF
sc-butread.GIF (7.47 KiB) Viewed 7613 times
FC 6.1.3.2 (18.02.2016)

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: switch ReadyState() freezes

Post by Benj »

Hello,

I have looked into this using a 16F877A on E-blocks and this is my findings.

Single switch connected to pin RA2, in simulation the switch read state returns 0 for not pressed and 1 for pressed.

The hardware does exactly the same with no lock up.

You said that there is some noise on your port, if the port pin never settles for at least 10ms then a switch with 10ms debounce will never return.

It sounds like when you press the switch it works so what about when the switch is not pressed?

Are you pulling the port pin low with a resistor when the switch is not pressed or just leaving it floating as this would certainly explain the problem your seeing. Refer to the schematic for the EB007 switch E-block if you need an example.

http://www.matrixmultimedia.com/resourc ... .php?id=83

User avatar
mikn
Posts: 209
Joined: Mon Mar 03, 2014 10:11 pm
Has thanked: 54 times
Been thanked: 41 times
Contact:

Re: switch ReadyState() freezes

Post by mikn »

Thanks, Benj. I understand it. But according to your words my own code also must not work and won't pass my debounce loop, if there's such big noise. How does debounce loop in your component look like? My variant works 100% correct now. Is your debounce loop the same?

Also even there will be a noise, Readystate must return "Not pressed" result and continue working, doesn't it? Now it's stucking in place with no way to continue the program.
FC 6.1.3.2 (18.02.2016)

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: switch ReadyState() freezes

Post by medelec35 »

My suggestion would be to manually with a flying lead connect the microcontroller input to either 0V or positive supply voltage.
The low resistance will eliminate all electrical noise.
Are you using and internal pull-up or you using a fixed resistor.
If using a fixed resistor, what is it's value?
Martin

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: switch ReadyState() freezes

Post by Benj »

Hello Mikn,

Your code looks for a stable press with 1ms intervals and exits the loop. It does not take into account if the pin changes state during the 1ms delays.

To do de-bounce correctly you stay in the loop and sample the input at high speed until you have a perfectly stable input for the time period set. If the input changes state at any point then you must reset the counter and start again.

The fact that the function is not returning is indicating to me that your input pin is constantly transitioning at a rate greater then 100Hz. As Martin says what resistor values are you using and do you have a pull down or pull up resistor? A simple schematic of your hardware setup is probably the easiest way for us to see what's going wrong.

User avatar
mikn
Posts: 209
Joined: Mon Mar 03, 2014 10:11 pm
Has thanked: 54 times
Been thanked: 41 times
Contact:

Re: switch ReadyState() freezes

Post by mikn »

There are no pulling down resistors, that's the problem for this device. Can't add them because it's finished product...
To do de-bounce correctly you stay in the loop and sample the input at high speed until you have a perfectly stable input for the time period set. If the input changes state at any point then you must reset the counter and start again.
Aha, understand now. This way takes me into infinite loop. But then what does Debounce parameter in the button component mean? Doesn't it mean that if the button fails the debounce test for 10ms, then the loop must return "not pressed", no matter what's going on there: constant noise or temporary ticks? IMHO, it would be better to let the loop continue after some timeout to avoid same freezing for other cases (magnetic fields or bad schematic as mine).
FC 6.1.3.2 (18.02.2016)

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: switch ReadyState() freezes

Post by medelec35 »

mikn wrote:There are no pulling down resistors, that's the problem for this device. Can't add them because it's finished product...
Hi mikn,
You must have pull-down or pull-up resistors or your hardware will not work as intended.
If you leave the i/p floating then the voltage on the i/p will keep rising above the high threshold value due the the very high input resistance as Ben was saying.

If you can't add the resistors, then you can use the internal pull-up resistors , and connect switch between i/p pin and GND. When switch is activated, then the i/p will go from supply voltage to 0V, so that will need to be taken into account when designing your flowchart.
This is from page 106 of the datasheet:
Each CN pin also has a weak pull-up connected to it.
The pull-ups act as a current source that is connected
to the pin, and eliminate the need for external resistors
when push button or keypad devices are connected.
The pull-ups are enabled separately using the CNPU1
and CNPU2 registers, which contain the control bits for
each of the CN pins. Setting any of the control bits
enables the weak pull-ups for the corresponding pins
To enable the week pull-up you will need to use a C code block to set the register bits high.
If you search on here for WPU then it should help even though the references will be for 16F or 18F microcontrollers.

I don't have much free time during the working day to help, but if your still stuck I can look into it more this evening.

Martin
Martin

User avatar
mikn
Posts: 209
Joined: Mon Mar 03, 2014 10:11 pm
Has thanked: 54 times
Been thanked: 41 times
Contact:

Re: switch ReadyState() freezes

Post by mikn »

Thanks, Martin! Damn, I missed that this PIC has internal resistors. Will enable them, of course. It must work for sure.
FC 6.1.3.2 (18.02.2016)

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: switch ReadyState() freezes

Post by medelec35 »

Hi mikn,
Your welcome.
mikn wrote:Will enable them, of course. It must work for sure.
I'm sure it will.
Can you let us know if it did sort the issue out or not?
Martin

Post Reply