Changing the pulse width

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);
}

It is working here

void setup()
{
    Serial.begin(9600);
    TCCR2A = 0x00;  //always reset 
    TCCR2B = 0x00; //always reset
    TCCR2A = (1<<COM2A1)|(1<<COM2B1) | (0<<WGM21)|(1<<WGM20); //Mode-1
    TCCR2B = (1<<CS22)|(1<<CS21)|(0<<CS20)|(0<<WGM22); //Mode-1 and prescale 256
    pinMode(11, OUTPUT);  //Ch-A
    pinMode(3, OUTPUT);     //Ch-B
    //-------------------------------
    OCR2A = 127;    //50% duty cycle
    OCR2B =  64;    //about 25% duty cycle
}

void loop()
{

}

but not in program post #1

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.