hey folks, ive written a code of generating square pulse in arduino mega. Now i want a phase difference of 3us between pin 11 and pin 6. Can anyone tell me?
the mentioned code is given below:
// 4 PWM signal generator using Timer1 and Timer3 on Arduino Mega 2560
void setup() {
pinMode(11, OUTPUT); // output A (PWM) using Timer1
pinMode(12, OUTPUT); // output B (PWM) using Timer1
pinMode(5, OUTPUT); // output C (PWM) using Timer3
pinMode(2, OUTPUT); // output D (PWM) using Timer3
pinMode(6, OUTPUT); // output A (PWM) using Timer4
pinMode(7, OUTPUT); // output B (PWM) using Timer4
pinMode(46, OUTPUT); // output C (PWM) using Timer5
pinMode(45, OUTPUT); // output D (PWM) using Timer5
// Timer1 setup
TCCR1A = 0; // clear Timer1 registers
TCCR1B = 0;
TCNT1 = 0;
// Timer3 setup
TCCR3A = 0; // clear Timer3 registers
TCCR3B = 0;
TCNT3 = 0;
// Timer4 setup
TCCR4A = 0; // clear Timer4 registers
TCCR4B = 0;
TCNT4 = 0;
// Timer5 setup
TCCR5A = 0; // clear Timer5 registers
TCCR5B = 0;
TCNT5 = 0;
// ICR1 and Prescaler sets frequency for Timer1
TCCR1B |= _BV(CS10); // no prescaler for Timer1
ICR1 = 193; // PWM mode counts up and back down for 80 counts (Timer1)
// ICR3 and Prescaler sets frequency for Timer3
TCCR3B |= _BV(CS30); // no prescaler for Timer3
ICR3 = 193; // PWM mode counts up and back down for 80 counts (Timer3)
// ICR4 and Prescaler sets frequency for Timer4
TCCR4B |= _BV(CS40); // no prescaler for Timer4
ICR4 = 193; // PWM mode counts up and back down for 80 counts (Timer4)
// ICR5 and Prescaler sets frequency for Timer5
TCCR5B |= _BV(CS50); // no prescaler for Timer5
ICR5 = 193; // PWM mode counts up and back down for 80 counts (Timer5)
// Pin match and PWM mode setup for Timer1
OCR1A = 101; // Pin 11 match for Timer1
TCCR1A |= _BV(COM1A1) | _BV(COM1A0); // output A set rising/clear falling for Timer1
OCR1B = 92; // Pin 12 match for Timer1
TCCR1A |= _BV(COM1B1); // output B clear rising/set falling for Timer1
// Pin match and PWM mode setup for Timer3
OCR3A = 101; // Pin 5 match for Timer3
TCCR3A |= _BV(COM3A1) | _BV(COM3A0); // output A set rising/clear falling for Timer3
OCR3B = 92; // Pin 2 match for Timer3
TCCR3A |= _BV(COM3B1); // output B clear rising/set falling for Timer3
// Pin match and PWM mode setup for Timer4
OCR4A = 101; // Pin 11 match for Timer4
TCCR4A |= _BV(COM4A1) | _BV(COM4A0); // output A set rising/clear falling for Timer4
OCR4B = 92; // Pin 12 match for Timer4
TCCR4A |= _BV(COM4B1); // output B clear rising/set falling for Timer4
// Pin match and PWM mode setup for Timer5
OCR5A = 101; // Pin 5 match for Timer5
TCCR5A |= _BV(COM5A1) | _BV(COM5A0); // output A set rising/clear falling for Timer5
OCR5B = 92; // Pin 2 match for Timer5
TCCR5A |= _BV(COM5B1); // output B clear rising/set falling for Timer5
// PWM mode with ICR1 and ICR3 Mode 10 setup
TCCR1B |= _BV(WGM13); // Timer1 PWM mode with ICR1 Mode 10
TCCR1A |= _BV(WGM11); // Timer1 WGM13:WGM10 set 1010
TCCR3B |= _BV(WGM33); // Timer3 PWM mode with ICR3 Mode 10
TCCR3A |= _BV(WGM31); // Timer3 WGM33:WGM30 set 1010
// PWM mode with ICR4 and ICR5 Mode 10 setup
TCCR4B |= _BV(WGM43); // Timer4 PWM mode with ICR1 Mode 10
TCCR4A |= _BV(WGM41); // Timer4 WGM13:WGM10 set 1010
TCCR5B |= _BV(WGM53); // Timer5 PWM mode with ICR3 Mode 10
TCCR5A |= _BV(WGM51); // Timer5 WGM33:WGM30 set 1010
}
void loop() {
}