Fast PWM - Compare to OCR1A


hi everyone, application need use fast pwm output signal close 2 pulses variable frequency determined series of measured voltages.

i attempting configure fast pwm timer1 first, , plan on adding voltage readings later on. have been using http://arduino.cc/en/tutorial/secretsofarduinopwm guide along atmega328p datasheet, without complete success.

in testing can the pulse of 2 correct, cannot frequency adapt whatever value set ocr1a to. believe errors must setting com1a0/com1a1/com1b0/com1b1 values in tccr1a register.

when run following code 2 pulse width, frequency around 15.6 khz need able produce frequencies ranging 450 khz 50 khz. if has idea why timer isn't being compared ocr1a please let me know.

thank in advance, , let me know if there need clear in explanation.

code: [select]
void setup()
{
  // configure output pins
  pinmode(9, output);
  pinmode(10, output);
  tccr1a = _bv(com1a0) | _bv(com1b1) | _bv(wgm11) | _bv(wgm10); // fast pwm mode - compare ocr1a
  tccr1b = _bv(wgm12) | _bv(cs10); // no prescalar
 
  // value compared timer value (i believe)
  ocr1a = 64;
 
  // value set pulse width
  // 32 provides approximately 2 pulse
  ocr1b = 32;
}

void loop()
{
 
}

you change frequency combination of pre-scale , top.  depending on mode set top value stored in ocrxa or icrx.  note prescale value affect pulse width.  if have change prescale full frequency range need change pwm value desired pulse width.

the base clock 16 mhz 16 clocks per microsecond 32 counts per pulse prescale of 1.  if go prescale of 8 down 4 counts per pulse.

50 khz 320 counts @ prescale of 1.  means have use 16-bit timer or prescale.  if use prescale of 8 can use 8-bit timer , set top 40.

450 khz 35.555 counts @ prescale of 1.  work fine 8-bit timer. of course can't use 35.555 should use 36 (444.44 khz) or 35 (457.143 khz).


Arduino Forum > Using Arduino > Programming Questions > Fast PWM - Compare to OCR1A


arduino

Comments

Popular posts from this blog

VIDIOC_S_FMT error 16, Device or resource busy - Raspberry Pi Forums

using a laptop skeleton to build a pi laptop - Raspberry Pi Forums

Forum for Joomla? - Joomla! Forum - community, help and support