Hello. maybe i am misunderstanding about PlcCycle, let me explain what iīve been trying to do from what iīve undesrtood:
STEP 1: (generate ansi c code)
when i generate ansi c code i get this message:
"Call this function once per PLC cycle. You are responsible for calling it at the interval that you specified in the MCU configuration when you generated this code."
STEP 2: (Search information in the forum)
After iīve read this meessage (STEP 1) i made my own research in the forum and found some examples using ansi c code:
----------example 1----------
http://cq.cx/ladder-forum.pl?action=viewthread&parent=3579
(LDMICRO EJEMPLO PIC12F629.zip)
"In this example PlcCyle is called like this:
void main(void)
{
RESTART_WDT();
init();
while(true)
{
RESTART_WDT(); // clear WDT unlimited
delay_ms(100);// Time step for PlcCycle() 100 ms
// Check inputs, process outs..
// Main Ladder diagramm cycle is here
PlcCycle();
}
}
"
----------example 2----------
http://cq.cx/ladder-forum.pl?action=viewthread&parent=2005
(this one ---- > pic12_ladder_bathroom_light_3.zip)
"
In this example PlcCycle i called like this:
void main(void)
{
CLRWDT();
init();
while(1)
{
CLRWDT(); // clear WDT unlimited
__delay_ms(100);// Time step for PlcCycle() 100 ms
// Check inputs, process outs..
// Main Ladder diagramm cycle is here
PlcCycle();
}
}
"
-------------------------------------------------------------
("delay_ms(100) IS USED FOR CALLING PLC CYLE in both examples)
-------------------------------------------------------------
----------example 3----------
http://cq.cx/ladder-forum.pl?action=viewthread&parent=3296
"this an example calling Plc Cycle in arduino (it works ok):
/* Plc cycle interval, set this according to LDmicro settings. (micro sec.) */
#define PLC_INTERVAL 10000
void setup()
{
PlcSetup();
}
void loop()
{
if (IsPlcInterval())
{
PlcCycle();
}
}
/* Plc Cycle timing function. */
boolean IsPlcInterval()
{
static unsigned long last_run;
if (micros() - last_run >= PLC_INTERVAL)
{
last_run = micros();
return true;
}
return false;
}
"
--------------------------------------------------
(IN EXAMPLE 3 DELAY IS NOT USED)
--------------------------------------------------
STEP 3: (attempts)
After many atempts i finally could make ansi c code works in prueba 4.rar (attach before)
like this :
"
void main(void)
{
while(1)
{
delay_ms(100);// Time step for PlcCycle() 100 ms
PlcCycle();
}
}
"
STEP 4: (call Plc Cyle without using delay)
------------------------------------------------------------
My doubt, what i want to do is that i donīt want to use "delay", because delay freezes microcontroller so i tried to use "timer 0" and "for" like this:
------------------------------------------------------------
---------using timer 0--------- (attach in "prueba 7.rar")
void main(void)
{
long pulsos;
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
set_timer1(0);
while(true)
{pulsos=get_timer1();
// Main Ladder diagramm cycle is here
if (pulsos>=10000)
{
PlcCycle();
pulsos=0;
set_timer1(0);
}
}
}
----------using "for"---------- (attach in "prueba 8.rar)
"
void main(void)
int i;
{
// 1000 cycles times 10 ms gives 10 seconds execution
for(i = 0; i < 1000; i++) {
PlcCycle();
}
}
"
-------------------------------------------------------------
In both attempts ("timer0" and "for") it works with a differente cycle time, for example, led blink for 1 sec in ansi c using "timer" 0 or "for" it is never 1 sec blink it is more or less than 1 sec, when i use delay it is for 1 sec.
-------------------------------------------------------------
STEP 5: (conclusion)
why i want to do this?
Because if sometime i want to do multitasking,.... using "delay" is not a good option.
I may misunderstading this or maybe i donīt explain very well,
-------------------------------------------------------------
so, coming to the point, i donīt want to use delay for calling Plc Cyle, i wish to call Plc Cyle in another way to do multitasking.
-------------------------------------------------------------
Thanks for your time.