I am using an Arduino to drive a stepper motor with the Allegro A4988 chip. The arduino is currently providing a step signal (approximately 200-20k Hz). In order to study the stepper motor control on a scope I need a trigger signal that is synchronized with every 16th step pulse.
I used code from this post to generate my step signal
http://forum.arduino.cc/index.php?topic=116094.15
Any help in creating a second (trigger) signal on another pin at 1/16th the frequency would be greatly appreciated.
I am not sure if i will need to use a second timer for this trigger signal, if so a phase shift is fine.
attached is a picture of the waveforms i am trying to produce on the two pins
Thanks,
Ben

There's lots of different code in the thread you reference, but if you are using a timer to generate 50% duty cycle pulses, you can add a timer overflow interrupt on that timer. The overflow vector ISR should increment a count, and when the count reaches 16, the ISR trigger your second pulse.
Thanks for the help.
Here is the code I came up with in case anyone else has a similar application.
/////////////////////////////////////////////////////////////////////////////////////////////////////
//
//Description:
//square wave signal generator with scaled trigger signal
//
//Application notes:
//step signal frequency range 15Hz to 1MHz
//trigger signal frequency is a fraction of the step frequency set by trigger_scale
//step signal on pin 9
//trigger signal on pin 8
//
//target application:
//drive a stepper motor with the step signal
//trigger signal is used to sychronize an ossciliscope with the motor waveform
//for a 1/16th microstep it takes 64 step pulses to complete a full step
//setting trigger_scale to 64 allows for stable cptre of the motor waveform in this case
//
//https://forum.arduino.cc/index.php?topic=399086.0
//
/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////
//
//set these values as needed
//
//step frequency (can be adjusted while running via the serial monitor)
unsigned long frequency =8000 ;//Hz
//
//trigger frequency scaler
byte trigger_scale = 64;//( trigger_frequency = frequency / trigger_scale )
//
/////////////////////////////////////////////////////////////////////////////////////////////////////
#include <avr/io.h>
#include <avr/interrupt.h>
byte trigger = 0;
void setup(){
pinMode(9, OUTPUT);//output timer1A??
pinMode(8, OUTPUT);//
DFG(frequency);
Serial.begin(57600);
//output current step frequency
Serial.print(F("Step frequency = "));
Serial.print(frequency);
Serial.println(F(" Hz"));
Serial.println(F("To change step frequency enter an new vaue and press send."));
}
void DFG(unsigned long tempfreq){
cli();//disable interupts
//clear registers for timer 1
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
TIMSK1 = 0;
TCCR1A |= _BV(COM1A0) + _BV(COM1B0);//Clear OC1A/OC1B on Compare Match (Set output to low level)
TCCR1B |=_BV(WGM12); //CTC mode, OCR1A TOP, Immediate OCR1x update, MAX TOV1 flag
//set TOP value, number of clock cycles to reset counter
//set prescaling appropriate for frequency range
if(tempfreq > 122 && tempfreq < 1000001){
OCR1A = (8000000/tempfreq)-1;//TOP value
TCCR1B |= _BV(CS10);//clkI/O/1 (No prescaling)
}
else if(tempfreq <= 122 && tempfreq > 15){
OCR1A = (1000000/tempfreq)-1;//TOP value
TCCR1B |= _BV(CS11);//clkI/O/8 (From prescaler)
}
else if(tempfreq <= 15 && tempfreq > 4){
OCR1A = (125000/tempfreq)-1;//TOP value
TCCR1B |= _BV(CS10) + _BV(CS11);//clkI/O/64 (From prescaler)
}
TIMSK1 = _BV(OCIE1A);//TIMER1 Output Compare A Match Interrupt Enable
sei();//enable interupts
}
//trigger signal
ISR(TIMER1_COMPA_vect){//interupt on timer 1
trigger++;//incriment trigger count
if (trigger >= trigger_scale*2){//trigger count cycle
trigger = 0;//reset trigger counter to zero
}
if (trigger >= trigger_scale){
digitalWrite(8,HIGH);//trigger output high second half of cycle
}
else{
digitalWrite(8,LOW);//trigger output low first half of cycle
}
}
void loop(){
if (Serial.available() > 0) {//if value is sent
frequency = Serial.parseInt();//read in new frequency value
frequency = constrain(frequency, 15, 1000000);//limit frequency to output range
DFG(frequency);//adjust timer registers
//output current step frequency
Serial.print(F("Step frequency = "));
Serial.print(frequency);
Serial.println(F(" Hz"));
}
}
TCCR1A |= _BV(COM1A0) + _BV(COM1B0);//Clear OC1A/OC1B on Compare Match (Set output to low level)
The comment on this line is wrong, and obscures some of the important characteristics of your code. It should be
TCCR1A |= _BV(COM1A0) + _BV(COM1B0);//Toggle OC1A/OC1B on Compare Match
It helps explain why you have a 50% duty cycle, why the frequency is based on 8Mhz, and why the trigger count needs adjustment by 2.
Yes, it appears i copped the incorrect register description from the Atmel datasheet. your comment makes much more sense.
I do have a question regarding the outputs for the timer when using CTC mode. It makes sense that both outputs OC1A and OC1B would have the same frequency because the counter TCNTn is reset when OCR1A is reached regardless of the value in OCR1B. However is it possible to use the second output pin for another function while the timer is running or must it be used as the timer output or not at all?
In Mode 4 (CTC to OCR1A) OC1A can only compare at TOP=OCR1A.
You can use OCR1B for compare actions using OC1B at other points, as long as OCR1B<=OCR1A.
Some other Timer1 (CTC and PWM) modes use ICR1 as the TOP value, and allow for more freedom with use of the output pins.
With your scope and your data sheet you can have hours of ATmega328 fun. 