IF/END IF usage limit??

13 views
Skip to first unread message

flyway38

unread,
Aug 6, 2022, 12:00:19 PM8/6/22
to jallib
Hi all,

Is there any limit in using IF/ END IF conditions?
My code was running good untill added one more IF/END IF.
At first didn't realize about a limit in using these conditions code, but then figured out that any other IF/END IF removal would let my code run ok again...
Does anyone knows anything about this issue?
Thank you very much.

Cheers,
Filipe Santos.

vsurducan

unread,
Aug 6, 2022, 12:13:36 PM8/6/22
to jal...@googlegroups.com
This issue appears usually when you do not manage in a correct way the timing required by a specific hardware and the costs of your code length is higher than required for the device to run properly. Common workaround is to use interrupts or other sequential techniques.

Example: DS18B20 is a temperature sensor which requires 750ms or so for a 12bit conversion. If you have complex software involving USB interrupts, real time clock generation, 7segment LED multiplexed display, etc , and those are not shared correctly with your resources ( aka you try everything to be solved in pure software) at one moment your code will freeze at a specific code length. Removing some lines, no matter that are if/end if or something else will apparently make your code functional, but the issue is not where you suppose it is...

Instead of if/end if you may use "case of". However it will not do something special or different compared with if/end if, except a more understandable structure for your source.
best wishes,


--
You received this message because you are subscribed to the Google Groups "jallib" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallib+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jallib/3f8189dd-fd0a-4df9-8948-518503485e61n%40googlegroups.com.

flyway38

unread,
Aug 6, 2022, 12:39:55 PM8/6/22
to jallib
Hi Vasile,

Thank you for the quick reply.
Is have that feeling about something not right at some point of the code.
In fact tried to exchange some "if/end if" for "case of" but no luck.
Problem persists, but it seems only related to "if/end if" usage..

My code reads the ADC (PIC18F2550), for 100 times in a "for/end loop".
Then find the median of values read and send that median value to RS232.
Also has a button to do some extra actions.
All this code worked great even in a smaller PIC (16F18313).

Now have changed PIC for the 18F2550 and wanted to add some extra code to manage PWM and ADC reading times to find the median value.
Also added an oled display to show locally that value and the PWM/ ADC nr. of readings parameters. That's when this issue raised up.
Simptoms are the code running sluggish, not hangging...
Any ideas?
Thank you very much.

Cheers,
Filipe Santos


Rob CJ

unread,
Aug 6, 2022, 2:01:57 PM8/6/22
to jal...@googlegroups.com
Hi Filip,

Are you saying that the code is executing slow?

If you can share your program or at least the part that is problematic (so it can be reproduced) that could help.

BTW (for Vasile 🙂). I recently worked on a problem of Hans using the DS18B20. It did work on one PIC but not on a smaller one. Finally the root cause was the PIC running at 4 MHz which was too slow to create the correct one-wire timing for the DS18B20. Switching to 8 MHz solved the problem. This was the first time that I encountered that a library did not work because of a clock speed begin to low. Maybe we should add a warning to that library.

Kind regards,

Rob


Van: jal...@googlegroups.com <jal...@googlegroups.com> namens flyway38 <fsfo...@gmail.com>
Verzonden: zaterdag 6 augustus 2022 18:39
Aan: jallib <jal...@googlegroups.com>
Onderwerp: Re: [jallib] IF/END IF usage limit??
 

flyway38

unread,
Aug 6, 2022, 3:00:28 PM8/6/22
to jallib
Hi all,

Problem seems to be solved.
It was due to using AN1 for the button (digital behaviour)...

Right now am facing new problem.
The follwing code seems not working ok;

code
serial_hw_write_word(0xEF2A + word(RlyCtl))
/code

RlyCtl is a pin (bit)
When ON, sends to serial; EF2B (correct)
When OFF, sends to serial; EF00 (Should send EF2A)
Maybe some issue on receiver side, but I think that converting bit to word seems problematic... not sure.

Before was using "serial_hw_write" byte by byte but with longer delay in between (longer than NOP, wich is used by word sender)

Any ideas?
Thank you very much.

Cheers,
FS

flyway38

unread,
Aug 6, 2022, 5:19:02 PM8/6/22
to jallib
Hi again,

Second problem is on receiver side.
It seems PIC is sending the right codes.
Thank you to all.

Cheers,
Filipe Santos.

vsurducan

unread,
Aug 6, 2022, 11:59:49 PM8/6/22
to jal...@googlegroups.com
If you send more words on RS232 repeatedly you have either to use after each word a delay to allow completion of the transmission or to check if the previous word transmission ended before sending the next word. There is a bit for this on UART.

vsurducan

unread,
Aug 7, 2022, 12:14:23 AM8/7/22
to jal...@googlegroups.com
Hi Rob,
When using DS18B20 with an interrupted USB serial driver you also have to turn off the USB interrupts during communication with the sensor. This is mandatory if you have for example 8 sensors which are repeatedly sampled and readed.
You know well that you can not turn off GIE because the USB communication will then be lost. :)

BTW, I have an automation for two solar water heaters that I made 10 or 12 years ago which use DS18B20 and an 8MHz PIC16F886. Display is on multiplexed 8 digit. I needed a long time solving an issue with the code length, one extra instruction in the source and the stuff was inoperable, no matter which was that instruction. :)  The extra time of the loop ( one more instruction) was killing the communication with the sensor.

Perhaps multiple warnings would be necessary :) :P


Rob CJ

unread,
Aug 7, 2022, 2:26:54 AM8/7/22
to jal...@googlegroups.com
Hi Vasile,

The program that did not work at 4 MHz had no interrupts or other things running so the limitation was really the fact that the microcontroller was running at 4 MHz.

As far as I know this would be the only library that needs a warning not to be used at 4 MHz 🙂.

Kind regards,

Rob


Van: jal...@googlegroups.com <jal...@googlegroups.com> namens vsurducan <vsur...@gmail.com>
Verzonden: zondag 7 augustus 2022 06:14
Aan: jal...@googlegroups.com <jal...@googlegroups.com>
Onderwerp: Re: [jallib] DS18B20 was IF/END IF usage limit??
 

vsurducan

unread,
Aug 7, 2022, 1:16:55 PM8/7/22
to jal...@googlegroups.com
Ok, that was what you have found. :)
best wishes,

Reply all
Reply to author
Forward
0 new messages