Hi
How to change the pulse width?
// This code demonstrates how to generate two output signals
// with variable phase shift between them using an AVR Timer
// The output shows up on Arduino pin 9 and 10 (these are connected to the Timer1 A and B outputs respectively)
// More AVR Timer Tricks at http://josh.com
void setup() {
//Timer1
pinMode( 9 , OUTPUT ); // Arduino Pin 9 = OCR1A
pinMode( 10 , OUTPUT ); // Arduino Pin 10 = OCR1B
}
#define PRESCALER 1
#define PRESCALER_BITS 0x01
void startWaveforms( uint16_t half_period , uint16_t shift , byte invert_b ) {
TCCR1B = 0;
OCR1A = 0;
OCR1B = 0;
TCNT1 = 0;
if (invert_b) {
TCCR1A = _BV( COM1A1 ) | _BV( COM1B1 ) | _BV( COM1B0 );
} else {
TCCR1A = _BV( COM1A1 ) | _BV( COM1B1 );
}
TCCR1C = _BV( FOC1A ) | _BV( FOC1B );
TCCR1A = _BV( COM1A0 ) | _BV( COM1B0 );
OCR1A = 0;
OCR1B = shift;
TCNT1 = 0xffff;
ICR1 = half_period - 1; // frequency
//ICR1 = half_period/2;
TCCR1B = _BV( WGM13) | _BV( WGM12) | _BV( CS10 );
}
void stopWaveforms() {
TCCR1A = _BV( COM1A1 ) | _BV( COM1B1 ) ;
}
void startQuadrature( unsigned long freq_hz , int shift_deg ) {
unsigned long clocks_per_toggle = (F_CPU / freq_hz) / 2;
while ( shift_deg < 0 ) {
shift_deg += 360;
}
while ( shift_deg >= 360 ) {
shift_deg -= 360;
}
byte invert = 0 ;
if (shift_deg >= 180 ) {
invert = 1 ;
shift_deg -= 180;
}
unsigned long offset_clocks;
offset_clocks = (clocks_per_toggle * shift_deg) / 180UL;
startWaveforms( clocks_per_toggle , offset_clocks , invert );
}
void stopQuadrature() {
stopWaveforms();
}
void loop() {
startQuadrature( 10000 , 90 );
delay(10);
}