Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
LDmicro, Arduino and PWM
LDmicro export text
for 'ANSI C Code', 4.000000 MHz crystal, 10.0 ms cycle time
LADDER DIAGRAM:
|| ||
|| ; Interier Light ||
1 || ; When you press the button (d2) led (pwm) will turn on and after some time (5 sec) fade out. ||
|| ||
|| ||
|| ||
|| ||
|| Xd2 Ron ||
2 ||-------] [------+---------------------------------------------------------------------------(S)-------||
|| | ||
|| | {pwm := } ||
|| +---------------------------------------------------------------------{ 255 MOV}--||
|| ||
|| ||
|| ||
|| ||
|| Ron Ton Rfade ||
3 ||-------] [---------[TON 5.000 s]------------------------------------------------------------( )-------||
|| ||
|| ||
|| ||
|| ||
|| Rfade Rstep Tstep {SUB pwm :=} ||
4 ||-------] [--------------]/[--------[TON 100.0 ms]-+---------------------------{ pwm - 8 }----------||
|| | ||
|| | Rstep ||
|| +-----------------------------------------( )-------||
|| ||
|| ||
|| ||
|| ||
|| Rfade [pwm <=] Ron ||
5 ||-------] [-----------[ 0 ]----+----------------------------------------------------------(R)-------||
|| | ||
|| | {pwm := } ||
|| +----------------------------------------------------{ 0 MOV}--||
|| ||
|| ||
|| ||
||------[END]-------------------------------------------------------------------------------------------||
|| ||
|| ||
#include "ladder.h"
/* Plc cycle interval, set this according to LDmicro settings. (micro sec.) */
#define PLC_INTERVAL 10000
void setup()
{
PlcSetup();
}
void loop()
{
if (IsPlcInterval())
{
PlcCycle();
// set PWM output
analogWrite(3, U_i_pwm);
}
}
/* Plc Cycle timing function. */
boolean IsPlcInterval()
{
static unsigned long last_run;
if (micros() - last_run >= PLC_INTERVAL)
{
last_run = micros();
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.