User Tools

Site Tools

한국어

modularfaduino:managedlibexample1

Convert Logic to C

Let's learn how to convert logic to C language.

Ladder Logic C language
Normal Open Y01 = X01;
Normal Close Y01 = !X01;
AND Y02 = X01 & X02;
OR Y03 = X01 | X02;
Timer onTimer(X01,1,100);
Y04 = Tstat[1];
Counter counter(X01,0,X02,1,100);

If we describe it as executable code, it would be as follows.

#include "cfManagedBase.h"
 
void user_setup()
{
}
 
void loop() 
{
}
 
void logic_proc()
{
  Y01 = X01;
  Y02 = X01 & X02;
  Y03 = X01 | X02;
  onTimer(X01,1,100);
  Y04 = Tstat[1];
  counter(X01,0,X02,1,100);
}

Monitoring

Let's display the contact(or variable) value in the serial monitor window.

  • debug(“string”,var) : display “string :” and value
  • debugcr : just make new line.
#include "cfManagedBase.h"
 
void user_setup() { 
Serial.begin(115200);
} 
 
void loop() { 
  debug ("X01",X01);
  debug ("X02",X02);
  debug ("Y01",Y01);
  debug ("Y02",Y02);
  debug ("Y03",Y03);
  debug ("Y04",Y04);
  debugcr;
  delay(100);
} 
 
void logic_proc() { 
  Y01 = X01;
  Y02 = X01 & X02;
  Y03 = X01 | X02;
  onTimer(X01,1,100);
  Y04 = Tstat[1];
  counter(X01,0,X02,1,100);
}

More Convert example

Rising Edge detect Y01 = rTrig(X01,0);
Falling Edge detect Y01 = fTrig(X01,1);
Keep Set state if (X01==1) Y01=1;
Keep ReSet state if (X01==1) Y01=0;

If there are multiple AND situations, use parentheses.

Y01 = (X01 | X02) & X03;
Y01 = (X01 | X02) & (X03 | X04);

The same goes for OR.

Y01 = (X01 & X03) | (X02 & X04);

Example 1 : Self-holding

Self-sustaining is the first circuit you learn when studying ladder logic (or sequence circuits).

When you press the START switch, the motor starts to turn, and when you press the STOP switch, the motor stops. The ladder program for this is as follows.

Connect the PUSH switches to X00 and X01. Connect the motor to Y00.

This is the operation timing chart.

When you press the X00 push switch, the motor connected to Y00 turns on. And this state is maintained. That's why it's called “self-holding.” To stop, press the X01 push switch.

The code in C language is as follows.

#include "cfManagedBase.h"
 
void user_setup()
{
 
}
 
void loop() {
 
}
 
void logic_proc()
{
  Y00 = (X00 | Y00) & !X01;
}

Example 2 : Blink (On/Off repeat every 1 second)

This is a program that repeats On/Off every 1 second. As mentioned earlier, logic_proc is executed every 20mS. If it repeats 50 times, it becomes 1 second.

This program declares a global variable time_var, increases it by 1 every time logic_proc is executed, and checks if this variable has become 50 and inverts the output Y00.

#include "cfManagedBase.h"
 
int time_var=0;
 
void user_setup()
{
}
 
void loop() 
{
}
 
void logic_proc()
{
  time_var++; // increse every 20mS
  if (time_var == 50) {
    Y00 = !Y00;
    time_var = 0;
  }
}

If you want to execute logic processing once every second, code it like this.

#include "cfManagedBase.h"
 
int time_var=0;
 
void user_setup()
{
}
 
void loop() 
{
}
 
void onesec_proc()
{
  // logic process every 1 sec. 
} 
 
void logic_proc() { 
  time_var++; // increase every 20mS 
  if (time_var == 50) { 
    time_var = 0;
    onesec_proc();
  } 
} 

Notification

Please use this source at your own risk. If this source is applied to an actual project, we are not responsible for any errors or accidents. Please use this source only if you agree to this. We are not accepting technical inquiries regarding this source. Please understand this.

Modular FADUINO

modularfaduino/managedlibexample1.txt · Last modified: 2024/10/31 05:05 by COMFILE Technology