Hi Rob,
here's my take on it using a mega328p but the code will be easily modified to any other AVR.
I'm using the idea of a mode output from UCcnc (input to the AVR). Then you can use gcode (macro), plugin, screen button, etc to put the AVR in "normal" mode or "synchronous" mode where it only listens to M10/11.
Now remember, in UCCNC, loss of M3 (i.e. M5) will also take away M10. And also in UCCNC you can't have M10 without first having M3. My code takes advantage of that to simplify things. So basically if the AVR is in "synchronous" mode it's only listening to M10/11 and ignores M3.
By the way if you Google about "GOTO" in C programming there's a zillion hits about it being a bad way of programming. I haven't looked into the reasons why but I just take the clever guys word for it, got enough on my plate at the moment without hurting my brain any more.
This code is based on active high inputs and outputs so if I made this circuit I'd use "pull DOWN" resistors on the inputs. With a noisy plasma environment I prefer external pull up/down resistors that allow plenty current to give better noise immunity.
I did this code with Codevision so it's got some slight differences from Atmel Studio. I initially tried in AS but when I did the single stepping the execution went stupid as usual. I put the optimisation to off in AS then it single stepped fine but that generates really crap lengthy code and I've found sometimes it just doesn't work. I do the same code with Codevision on low optimisation and it just works. Maybe I'm thick or something but I don't know how others survive with AS.
- Code: Select all
/*
* M3 M10 SYNCH CONTROL.c
*
* Created: 9/11/2016 3:51:59 PM
* Author: BEEFY
* NOTE - inputs and outputs are active HIGH so will need pull DOWN resistors
*/
#include <io.h>
#include <stdint.h>
#define synchMode (PINB & (1<<PINB2)) // Pin B2 input to set normal or synchronous torch on/off mode
#define M3 PINB & (1<<PINB3) // Pin B3 as input for spindle on/off (M3/M5)
#define M10 PINB & (1<<PINB4) // Pin B4 as input for laser on/off (M10/M11)
#define Torch_ON PORTB |= (1<<PORTB0)
#define Torch_OFF PORTB &= ~(1<<PORTB0)
void main(void)
{
DDRB &= ~((DDB2) | (1<<DDB3) | (1<<DDB4)); // Port B2, B3 and B4 as input
DDRB |= (1<<DDB0); // Port B0 as output
while (1)
{
if (synchMode == 0) // NOT in synchronous torch on mode
{
if (M3)
{
Torch_ON;
}
else
{
Torch_OFF;
}
}
else // Then we ARE in synchronous torch mode
{
if (M10) // Requires M10 for torch to fire (and M10 requires M3 in any case)
{
Torch_ON;
}
else // Torch off if lose M10 (and loss of M3 (M5) will cause loss of M11 too)
{
Torch_OFF;
}
}
}
}
I single stepped through it all and I think I covered every scenario but if you see anywhere that I stuffed up, or any improvements, please let me know.
Keith.