Skip to content

Instantly share code, notes, and snippets.

@ah01
Created May 5, 2012 13:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ah01/2602423 to your computer and use it in GitHub Desktop.
Save ah01/2602423 to your computer and use it in GitHub Desktop.
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