atmega - Using Arduino timers -
i using arduino code generate 5v, 200 khz pulse 50% pulse-width.
void setup() { // put setup code here, run once: pinmode(pwmpin, output); pinmode(fbo, input); pinmode(fbi, input); nointerrupts(); // disable interrupts tccr0a = 0; tccr0b = 0; tcnt0 = 0; ocr0a = 40; // compare match register duty cycle * 16mhz/200khz ocr0b = 40; // compare match register 1 - duty cycle * 16mhz/200khz timsk0 |= (1 << ocie0a); // enable timer compare interrupt digitalwrite(pwmpin,high); tccr0b |= (1 << cs00); interrupts(); // enable interrupts } isr(timer0_compa_vect) { digitalwrite(pwmpin, digitalread(pwmpin) ^ 1); //timsk0 |= (1 << ocie0b); // enable timer compare interrupt //timsk0 &= ~(1 << ocie0a); // disable timer compare interrupt } instead of 5v 200 khz pulse, showing me approximately 2v 30 khz signal. can tell me problem is?
the problem operating timer/counter0 in normal mode instead of ctc mode.
try adding
tccr0a |= (1 << wgm01); to setup routine enable counter reset on compare match.
normal mode lets timer count bottom (0) max (255) regardless of value set ocr0a to. trigger interrupt every 256 clock cycles (62.5 khz clock frequency of 16 mhz). isr needs toggle output pin twice perform 1 full pwm cycle yielding observed pwm frequency of round 30 khz.
from atmega datasheet:
15.7.1 normal mode
[...] in mode counting direction (incrementing), , no counter clear performed. counter overruns when passes maximum 8-bit value (top = 0xff) , restarts bottom (0x00).
and
15.7.2 clear timer on compare match (ctc) mode
in clear timer on compare or ctc mode (wgm02:0 = 2), ocr0a register used manipulate counter resolution. in ctc mode counter cleared 0 when counter value (tcnt0) matches ocr0a. [...]
for 2v, guess you're measuring mean voltage (4.?v @ 50% duty cycle) instead of pwm peak voltage.
Comments
Post a Comment