Hello Guest it is July 18, 2019, 06:15:58 PM

Author Topic: Arduino Charge Pump Code/Sketch?  (Read 7477 times)

0 Members and 1 Guest are viewing this topic.

Arduino Charge Pump Code/Sketch?
« on: September 16, 2014, 10:52:52 AM »
Hello all. I am new to all of this, CNC, electronics, programming. You name it, I'm a newb!

What i am looking for is an example or snippet of code that allows the Arduino to "read" the mach3 charge pump signal.

I just want to arm or disarm 2 outputs based off the presence or lack their of of the mach3 charge pump signal.

I will post the code i was given, however i have not had any luck with it. Trying to troubleshoot the code myself at this time is proving daunting.

Also any external circuitry required to make it work would be appreciated!

I have of course searched this topic, quite a bit. And have gotten nowhere. Any pointing in the right direction would be much appreciated.

Quote
typedef enum
{
  sInitialize,
  sMonitor,
  sDead
}
state_t;

static uint8_t state;

static unsigned long PreviousMicros;

static bool PreviousClock;
static unsigned short Clocks;

const float WAVELENGTH = (1.0 / 10000.0) * 1000000.0;
const unsigned long WINDOW = 2.5 * WAVELENGTH;

void setup( void )
{
  state = sInitialize;
 
  // Make A1 and A0 outputs
  bitSet( DDRC, DDC1 );
  bitSet( DDRC, DDC0 );
 
  // Ensure they start LOW (not necessary but documents our intent)
  bitClear( PORTC, PORTC1 );
  bitClear( PORTC, PORTC0 );
 
  // Make digital pin 8 (PB0) and input (not necessary but documents our intent)
  bitClear( DDRB, DDB0 );
}

void loop( void )
{
  unsigned long CurrentMicros;
 
  CurrentMicros = micros();

  // Count toggles of digital pin 8 (PB0)

  if ( bitRead( DDRB, DDB0 ) )
  {
    if ( ! PreviousClock )
    {
      ++Clocks;
      PreviousClock = true;
    }
  }
  else
  {
    if ( PreviousClock )
    {
      ++Clocks;
      PreviousClock = false;
    }
  }

  switch ( state )
  {
    // Wait for the first toggle
    case sInitialize:
      if ( Clocks > 0 )
      {
        // Go live
        bitSet( PORTC, PORTC1 );
        bitSet( PORTC, PORTC0 );
        // Monitor the clock signal
        state = sMonitor;
        Clocks = 0;
        PreviousMicros = CurrentMicros;
      }
      else
      {
        // Light an LED to indicate waiting?
      }
      break;
     
    case sMonitor:
      // Check each window for...
      if ( CurrentMicros - PreviousMicros >= WINDOW )
      {
        // No clock signal?
        if ( Clocks == 0 )
        {
          // Go dead
          bitClear( PORTC, PORTC1 );
          bitClear( PORTC, PORTC0 );
          state = sDead;
        }
        else
        {
          // Reset for another window
          Clocks = 0;
          PreviousMicros = CurrentMicros;
        }
      }
      break;
     
    case sDead:
      // Light an LED?  Sound an alarm?  Cry in your beer?
      break;
  }
}
Re: Arduino Charge Pump Code/Sketch?
« Reply #1 on: September 16, 2014, 11:31:20 AM »
You'd be much better off using attachInterrupt to get an interrupt whenever the change pump input has a rising-edge transition.  On each interrupt, get the current time, using millis(), and calculate the time since the last interrupt, which will tell you the frequency.  Compare that measured frequency to to some reasonable range.  Nominal charge pump frequency is 12.5 kHz., so if you see transitions at a rate from 10-15 kHz, you can assume the charge pump is running.

There is also an Arduino library called FreqMeasure, which will directly measure the frequency of a digital input, and give you a frequency value, in Hz.  The sample sketch for that library will give you almost everything you need, except for testing the measured frequency is in a valid range.

Regards,
Ray L.
Regards,
Ray L.
Re: Arduino Charge Pump Code/Sketch?
« Reply #2 on: September 16, 2014, 02:17:46 PM »
Thank you very much for your reply.

I did not author this code, it was given to me on the arduino forum.

I have also never used an arduino before. I purchased the inventors kit and am currently working my way through spark fun's 8hour introduction video {super noob}

My poor results in searching stem from the fact that i really don't know what I'm looking for, or looking at!

I will experiment with the freq measure sketch and do some more research on attachInterrupt, and how i can implement this into code.

I agree the charge pump monitor should be fairly simple, and have a fairly wide range for "on".

The last charge pump solution i built used the hardware approach posted on the mach3 website. It works, but its a little noisy.

my long term goal is to have this on an attiny45 for imbedding easily into various machines.
Re: Arduino Charge Pump Code/Sketch?
« Reply #3 on: September 17, 2014, 01:05:11 AM »
So far i have made a little progress.

I looked up the FreqMeasure library, and as per their suggestion, tried the FreqCount sketch.

Using analog pin 5 as my input, i am able to watch the frequency monitor output its sample about once a second to the serial monitor window.

With Mach3 running i am seeing 12680 steadily and 0 when i click the reset button {flashing}.

Now i need to figure how to enable or kill my desired outputs using this data.

I thank you for the great suggestion, it seems to have me pointed in the right direction.

Code: [Select]
#include <FreqCount.h>

void setup() {
  Serial.begin(57600);
  FreqCount.begin(1000);
}

void loop() {
  if (FreqCount.available()) {
    unsigned long count = FreqCount.read();
    Serial.println(count);
  }
}
Re: Arduino Charge Pump Code/Sketch?
« Reply #4 on: September 17, 2014, 09:51:08 PM »
If you get a "Charge Pump board" from CNC4PC, and connect the CP input terminal to the Charge Pump output in "Ports and  Pins", the Charge pump relay will open and close, as well as light up an LED showing the Charge Pump output 12.5 khz signal is on or off. The charge pump is a little circuit board about 2" square, with screw terminals for all I/O, and is 5v. The relay has N/C, N/O, and Com terminals.

However, no programming is required, so it may not suit your requirements.

John
 
Re: Arduino Charge Pump Code/Sketch?
« Reply #5 on: September 17, 2014, 10:41:19 PM »
The cnc4pc board doesn't really fit the bill. {though on paper it more or less does what i need}

Ahh but how simple life would be if it did.



Re: Arduino Charge Pump Code/Sketch?
« Reply #6 on: September 18, 2014, 12:02:33 AM »
I am curious as to why it doesn't "fit the bill" as you say.

For what it is worth, I am also a big fan of the Arduino, and have made lots of things with them, including a Modbus/Mach3 bench setup with an Arduino, and a PP board C10 by CNC4PC,  for Feed rate and Rapid rate and some pushbutton control of basic functions.

John
Re: Arduino Charge Pump Code/Sketch?
« Reply #7 on: September 18, 2014, 04:14:45 AM »
because its too big too fit in my package, and teaches me nothing.

simple enough?

good.

arduino DOES NOT control anything in this machine, except the charge pump.

{stop trying to figure out what I'm building, you'll never guess} lol

Offline dude1

*
  •  1,253 1,253
    • View Profile
Re: Arduino Charge Pump Code/Sketch?
« Reply #8 on: September 18, 2014, 06:03:33 AM »
being rude wont get you any help

Offline ZASto

*
  •  157 157
    • View Profile
Re: Arduino Charge Pump Code/Sketch?
« Reply #9 on: September 18, 2014, 02:32:40 PM »
Search for "Charge Pump Safety" and you will find a schematic drawn by Mariss Freimanis, aka GeckoMan :). It will relieve your headache for measuring the freq with any microcontroller. Pure "1" or "0" :D

Next time, if you keep tyour ChewingGum attitude, do  not expect any relevant answer.
Make no mistake between my personality and my attitude.
My personality is who I am.
My attitude depends on who you are.