Clear OCx on compare match issue

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello everyone,

i am trying to generate pulses with timer in CTC or Normal mode using clear / set  OC0B pin on compare match (at Attiny2313). Is there any way to generate "one pulse" of variable length in this mode please ? I thought that this simple program should set PD5(OC0B) at the beggining of program execution and after approximately 100 tics (100us at 1MHz) it should clear PD5 (OC0B) due compare match of OCR0 and TCNT0. That means it should generate approximately 100us pulse. I've checked dissasembler if C code isn't misinterpreted, and asm code is correct. Results of test (at presented waveform) shows, that PD5 is cleared in the moment when COM0B1 is set (approximately 3us - 3 tics after program start). It corresponds with simulator results.

#include <avr/io.h> 
#define F_CPU 1000000

int main(void){ 
PORTD = (1<<PORTD5);
DDRD = (1<<DDD5);
OCR0A = 100;
TCCR0A = (1<<COM0B1);
TCCR0B = (1<<CS00);

while(1){
asm("nop");
	} 
}

 

The same results i have with Atmega16A nad OC0 pin. Is there any way how to see OCx pin to be cleared at compare match ? 

thanks,
Gripennn

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When you enable the compare feature, it connects an internal flip/flop to the output pin. This internal f/f is set/cleared or toggled by the compare match. To set this f/f to high there maybe some 'force' bits in the timer registers or failing this, set an initial compare to set the output, then the compare to clear it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks, thats it. First set up timer in "set on compare match", then force compare match by FOC0B and then set timer in "clear on compare match". Maybe it should be mentioned in datasheet.

#include <avr/io.h> 
#define F_CPU 1000000

int main(void){ 

DDRD = (1<<DDD5);
OCR0B = 100;
TCCR0A = (1<<COM0B1) | (1<<COM0B0);
TCCR0B |= (1<<FOC0B);
TCCR0A = (1<<COM0B1);
TCCR0B = (1<<CS00);

while(1){
asm("nop");
	} 
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Especially when doing a "one shot" operation, clear the flag (OCF0B in TIFR) before starting the timer.  I can't give you chapter and verse about which flags in which peripherals might be set in an apparently spurious manner, or whether the timer needs to be started.  IIRC input capture is one.  Compare match may well be another.

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.