PPG (Programmable Puls Generator)

This library can generate pulse signals of up to four phases. The pin numbers for each phase are fixed as follows. To use, specify #include <PPG.h>

Phase0(pin0): 1
Phase1(pin1): 3
Phase2(pin2): 5
Phase3(pin3): 7

begin

Description
Set the phase and pulse cycle to be used.
Syntax
PPG.begin(bool pin0, bool pin1, bool pin2, bool pin3, uint32_t interval, uint8_t polarity = 0b0000)
Parameters
pin0: True/False (Used/Not used)
pin1: True/False (Used/Not used)
pin2: True/False (Used/Not used)
pin3: True/False (Used/Not used)
interval: Period (microsecond), can be specified from approximately 100µs to 500ms
polarity: In each bit, when 0 is specified, non-inverted, when 1 is specified, inverted output
   Example: In the case of 0b0001, the first phase is inverted output
Returns
None

end

Description
Release the set pin.
Syntax
PPG.end()
Parameters
None
Returns
None

setTrigger

Description
Set the trigger position of each phase. Application is performed collectively for all phases with enableTrigger.
Syntax
PPG.setTrigger(uint8_t phase, uint32_t triggerA_time, uint32_t triggerB_time)
Parameters
phase: Phase number (0 to 3)
triggerA_time: Time to reverse first
triggerB_time: Time to reverse second
Returns
None

enableTrigger

Description
Apply the setting of setTrigger ()
Syntax
PPG.enableTrigger()
Parameters
None
Returns
None

start

Description
Start pulse output.
Syntax
PPG.start()
Parameters
None
Returns
None

stop

Description
Stop the pulse output.
Syntax
PPG.stop()
Parameters
None
Returns
None

Sample Program

This sample controls a two-phase DC motor. The speed is determined by the values of A0 and A1.


#include <Arduino.h>
#include <PPG.h>
 
uint32_t    motor_speed[2];
uint32_t    motor_pwm_cycle = 500;  // 500[micro-second]
void    calc_speed();
 
void    setup()
{
  PPG.begin(true, true, true, true, motor_pwm_cycle, 0b0101);
  PPG.setTrigger(0, 0, motor_pwm_cycle / 2);
  PPG.setTrigger(1, 0, motor_pwm_cycle / 2);
  PPG.setTrigger(2, 0, motor_pwm_cycle / 2);
  PPG.setTrigger(3, 0, motor_pwm_cycle / 2);
  PPG.start();
}
 
void    loop()
{
  calc_speed();
  PPG.setTrigger(0, 0, motor_speed[0]);
  PPG.setTrigger(1, 0, motor_speed[0]);
  PPG.setTrigger(2, 0, motor_speed[1]);
  PPG.setTrigger(3, 0, motor_speed[1]);
  PPG.enableTrigger();
  delay(100);
}
 
void    calc_speed()
{
  // Set spped according to A0, A1
  motor_speed[0] = map(analogRead(A0), 0, (1U<<12), 0, motor_pwm_cycle);
  motor_speed[1] = map(analogRead(A1), 0, (1U<<12), 0, motor_pwm_cycle);
}