Home
Prox / RFID
Verichips
Ladder Logic
[interfacing] †
Tube Joints
Key Code From Photo
SolveSpace (3d CAD)
SketchFlat (2d CAD)
Photographs
Miscellany
Resume / Consulting
Contact Me

LDmicro Forum - The SUBPROG function only works in simulation

(you are viewing a thread; or go back to list of threads)

The SUBPROG function only works in simulation (by shmulik)
Hello
I am working with PIC16F887
And I have some problems.
I have a long program and the cycle time is long
And because I work with an encoder it is not accurate
I thought to use SUBPROG and thus shorten the cycle time of the main program
Is it possible?
In any case, it seems that the function of the SUBPROG does not work
(only in simulation)
Thanks
Sat Apr 8 2023, 15:36:29
(no subject) (by MGP)
If you know how the duty cycle works, you also know that a SUBPROG does not shorten the time.

If you stay in the SUBPROG for too long and the duty cycle time is exceeded, the program will run even slower and the timers will no longer work correctly.

LDmicro is not suitable for reading encoders, perhaps the slow encoders.
Sat Apr 8 2023, 16:49:35
(no subject) (by shmulik)
okay
Thanks
Sat Apr 8 2023, 18:37:47
(no subject) (by shmulik)
Hello
Another question on the same topic
If I change the processor from 16F887 to 18F4520 or another processor with a higher clock
Faster than 20 MHZ, can I reduce the cycle time?
Thanks
Sun Apr 9 2023, 05:33:29
(no subject) (by MGP)
Yes..BUT.. with a 18Fxxxx pic you can only generate a C-file that you then have to convert to hex with a C-compiler.

A 16F1527 runs at 32MHz and there you can reduce the cycle time to half depending on the size of your program.

You can also connect the encoder to a separate controller with a Cycletime of 0 and count the output pulses with the main controller.
Sun Apr 9 2023, 06:01:21
(no subject) (by shmulik)
Thanks
Sun Apr 9 2023, 08:07:57
The SUBPROG function only works in simulation (by shmulik)
The led in the subrutine is not working,
the other one, outside the subrutine is working fine.
What did I do worng?

I'm working with this version:
Release 5.4.1.1, built 21:01:03 Mar 22 2021.

Thanks
Fri Apr 14 2023, 05:14:48, download attachment sub_test.ld
(no subject) (by Flenser)
Check the C code that is generated.

When I generate ANSIC using your sub_test.ld file in 5.4.1.1 the body of the subroutine is empty:

void Call_SUBPROG_Rung4() { // LabelRung1
}


I have reported this as issue #266:
https://github.com/LDmicro/LDmicro/issues/226
Fri Apr 14 2023, 07:44:50
(no subject) (by shmulik)
Thanks for the quick response
I will check
Fri Apr 14 2023, 07:49:03
(no subject) (by MGP)
This works in de simulator, try this on your 887 and let us know...program and cycletime adjusted.
Sat Apr 15 2023, 02:35:29, download attachment sub_test_V2.ld
(no subject) (by MGP)
I did several tests on a 16F1512 and they all worked.
No errors noticed.

For more:https://github.com/LDmicro/LDm...L-and-GOTO-instruction.-Part-2
Sat Apr 15 2023, 05:19:01
(no subject) (by shmulik)
Hi MGP,

Thanks for the reply.
As Flenser mentioned, when compiling to C The subroutine
is empty - therefor not working
Fri Apr 21 2023, 02:59:44
(no subject) (by MGP)
Here's a program excerpt that works and as you can see the Gosub instruction is executed several times per cycletime and when I compile that for a pic in HI-techC you see everything compiled normally.

I think that Timers and Contacts don't compile, which wouldn't work by the way.
Fri Apr 21 2023, 12:56:39, download attachment Snap291.jpg
(no subject) (by shmulik)
Hi MGP,

What do you mean by "everything compiled normally"?
Read what Flenser wrote about this, in this thread,
on Fri Apr 14 2023

The program will not work for 2 reasons:
1. Empty subroutine/function
2. Multiple implentation of the same subroutine/function


The compile in Hi-Tech C for subroutine is definatly
not working!!!
Tue Apr 25 2023, 03:11:45
(no subject) (by Flenser)
MGP,

I have downloaded shmulik's example program sub_test.ld and done "Compile HI-TECH C for PIC" using LDmicro v5.4.1.1 and generatdd the attached output file sub_test.c

sub_test.c has this empty subroutine definition:
void Call_SUBPROG_Rung4() { // LabelRung1
}

and this code for rung 2, which is where the subroutine is called:
// ======= START RUNG 2 =======
LabelRung2:;
Write_Ib_rung_top(1);
// start series [
// ELEM_GOSUB speed
if(Read_Ib_rung_top()) {
Call_SUBPROG_Rung4();
}
// ] finish series

So when rung 2 executes it will call subroutine Call_SUBPROG_Rung4() which is empty and so does nothing.

and this is the main body of the program in sub_test.c:
void PlcCycle(void) {
// INIT TABLES

// ======= START RUNG 1 =======
LabelRung1:;
Write_Ib_rung_top(1);
// start series [
// ELEM_CONTACTS
if(!Read_Ub_Xin2()) {
Write_Ib_rung_top(0);
}
// ELEM_COIL
Write_Ub_Yled2(Read_Ib_rung_top());
// ] finish series

// ======= START RUNG 2 =======
LabelRung2:;
Write_Ib_rung_top(1);
// start series [
// ELEM_GOSUB speed
if(Read_Ib_rung_top()) {
Call_SUBPROG_Rung4();
}
// ] finish series

// ======= START RUNG 3 =======
LabelRung3:;
Write_Ib_rung_top(1);
// start series [
// ELEM_SUBPROG speed
goto LabelRung6;
// ] finish series

// ======= START RUNG 4 =======
LabelRung4:;
Write_Ib_rung_top(1);
// start series [
// ELEM_CONTACTS
if(!Read_Ub_Xj2()) {
Write_Ib_rung_top(0);
}
// ELEM_COIL
Write_Ub_YLED(Read_Ib_rung_top());
// ] finish series

// ======= START RUNG 5 =======
LabelRung5:;
Write_Ib_rung_top(1);
// start series [
// ELEM_ENDSUB speed
return;
// ] finish series
LabelRung6:;
// Latest INT_OP here
}

In this code rungs 0003 to 0005 never get executed because if you look at the code for rung 0003 above it executes the statement goto LabelRung6, which skips over rungs 0004 & 0005.


v4.2.2 is the last version I could find where the code for the subroutine was correct. Note that the name generated for the subroutine has changed between v4.2.2 and v5.4.1.1

When I compile sub_test.ld using "Compile HI-TECH C for PIC" using LDmicro v4.2.2 rungs 0004 to 0005 are now included in the body of subroutine Call_SUBPROG_speed() and so get executed when the subroutine is called:
void Call_SUBPROG_speed() // LabelRung4
{
// ] finish series

// ======= START RUNG 4 =======
LabelRung4:;
Write_I_b_rung_top(1);
// start series [
// ELEM_CONTACTS
if(!Read_U_b_Xj2()) {
Write_I_b_rung_top(0);
}
// ELEM_COIL
Write_U_b_YLED(Read_I_b_rung_top());
// ] finish series

// ======= START RUNG 5 =======
LabelRung5:;
Write_I_b_rung_top(1);
// start series [
// ELEM_ENDSUB speed rung 5
return;
}

and the body of the program now only has rungs 0001 to 0003:
void PlcCycle(void)
{

// ======= START RUNG 1 =======
LabelRung1:;
Write_I_b_rung_top(1);
// start series [
// ELEM_CONTACTS
if(!Read_U_b_Xin2()) {
Write_I_b_rung_top(0);
}
// ELEM_COIL
Write_U_b_Yled2(Read_I_b_rung_top());
// ] finish series

// ======= START RUNG 2 =======
LabelRung2:;
Write_I_b_rung_top(1);
// start series [
// ELEM_GOSUB speed
if(Read_I_b_rung_top()) {
Call_SUBPROG_speed(); // LabelRung4
}
// ] finish series

// ======= START RUNG 3 =======
LabelRung3:;
Write_I_b_rung_top(1);
// start series [
// ELEM_SUBPROG speed
goto LabelRung6; // speed
// ] finish series
LabelRung6:;
}
Tue Apr 25 2023, 06:26:07, download attachment sub_test.c
(no subject) (by MGP)
Ok maybe I'm missing something because I never compile in C code... all I can say is that SUBPROG instruction works for me.

I'm doing a test setup of a 16F1512 and an antique AY-3-8912 sound chip and it works pretty well.
It works with a SUBPROG instruction for address and data registers and already produces tones.

Later I will post a video with the code when my other project is finished for the control of my home energy with my solar panels.
Tue Apr 25 2023, 06:54:26
(no subject) (by Flenser)
MGP,

I'm not sure I understand. If you don't compile in C then what option from the ldmicro Compile menu are you using for the 16F1512 program with a subprogram that works for you?
Tue Apr 25 2023, 13:02:06
(no subject) (by MGP)
I compile directly in hex and with a Pickit3 I program the controller.
Compiling to C is for controllers that are not in the menu list.
Tue Apr 25 2023, 13:34:48, download attachment Snap292.jpg
(no subject) (by shmulik)
MGP,

As for the test file - Your method is really working.
But, unfortunately, it does not work when I2C function is in use.
Fri Apr 28 2023, 03:07:44, download attachment Screenshot 2023-04-28 095821.png
(no subject) (by MGP)
There are no more developers as far as I know, we will have to learn to live with LDmicro's limitations.

For my projects LDmicro is sufficient, there are other bugs in it but I avoid them as much as possible.
Fri Apr 28 2023, 06:15:52
Post a reply to this comment:
Your Name:
Your Email:
Subject:
(no HTML tags; use plain text, and hit Enter for a line break)
Attached file (if you want, 5 MB max):