ATTINY 85 Timer 0 in Compare match modus

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

Hello All,

 

Just started with AVR microcontrollers a week of two ago and did some study of the datasheet , examples , reading books, tutorials etc.

The best way of learning it is playing with it.

However the first attempt to get timer 0 working in CTC mode with interrupts (no toggling mode )gives me already a lot of problems which I will hope that the more experienced people on this forum can help me out.

 

I'm using the STK600 development board for my experiments.

The ATTINY 85 is used in his default clock mode meaning 1 Mhz/ 1us clock rate

 

What I want to achieve is generation of an interrupt every 100 us.

I wrote a test program where I just toggled a port pin in the IRQ. But it seems that the IRQ is never invoked. It must be something simple for sure but can't find it. I searched on it for a day of two nowfrown

I measure the port pins with a scana plus . This is an USB logic analyzer. Which I believe is capable to measure the output of the toggled pin.
The  clock of the device is measured on an external pin and looks OK.This is done by setting the CKOUT fuse.

 

Any help , feedback is welcome here

 

Regards,

 

#include <avr/io.h>
#include <avr/interrupt.h>

// initialize timer, interrupt and variable
void timer0_init()
{
 // set up timer with prescaler = 1 and CTC mode
 TCCR0A |= (1<<WGM01);
 TCCR0B |= (0<<CS02)|(0<<CS01)|(1<<CS00);
 
 // initialize counter
 TCNT0 = 0;
 
 // initialize compare value
 OCR0A = 99; //100us
 
 // clear interrupt flag as a precaution
 TIFR |= 0x01;   
     
 // enable compare interrupt
 TIMSK |= (1 << OCIE0A);
 
 // enable global interrupts
 sei();
}

int main(void)
{
    DDRB = 0xFF;   //all pins output
 PORTB &=~(1<<PB0);  // PBO low
 timer0_init();
 sei();     // enable global interrupts
 while(1)    //loop forever
 return 0;
}

//TIMER 0 COMPARE match
ISR (TIMER0_COMPA_vect)
{
 PORTB |=(1<<PB0);
 PORTB &=~(1<<PB0);
}

 

This topic has a solution.

Last Edited: Tue. Jan 27, 2015 - 02:50 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello, you missed a tiny detail

 while(1)    //loop forever
 return 0;

when you probably mean

 while(1);   //loop forever
 return 0;

Or a little easier to read

 while(1)    //loop forever
 {
 }
 return 0;

so your program will return from main, to another forever loop. But before that loop is entered, global interrupts will be disabled.

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

But it seems that the IRQ is never invoked.

How did you determine that?

 

Your code will only make a 1us "spike" on the output pin.

 

ISR (TIMER0_COMPA_vect)
{

Did you get a warning on that?  According to http://www.nongnu.org/avr-libc/u... it would be

TIM0_COMPA_vect

Timer/Counter Compare Match A

ATtiny13, ATtiny43U, ATtiny24, ATtiny44, ATtiny84, ATtiny45, ATtiny25, ATtiny85

 

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.

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

Hello Sniglen,

 

Yes you are correct. I missed the ";" at the end of the while (1).

It works like a charm now.

I was reading it over and over again but didn't noticed the missing ";"

shame on me .

 

Thanks for looking at it.

 

 

 

 

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

 

Hello Theush,

 

 

theusch wrote:

But it seems that the IRQ is never invoked.

How did you determine that?

 

Your code will only make a 1us "spike" on the output pin.

 

I was aware of the 1us spike and was even expecting it.

The resolution of the analyzer tool I use is high enough to  catch those small spikes.

I saw that on a previous experiment.

See snapshot of the analyzer tool below after fixing the program.

CH1 is the interrupt. CH4 is de CPU clock.

 

 

 

theusch wrote:

ISR (TIMER0_COMPA_vect)
{

Did you get a warning on that?  According to http://www.nongnu.org/avr-libc/user-manual/group__avr__interrupts.html it would be

TIM0_COMPA_vect

Timer/Counter Compare Match A

ATtiny13, ATtiny43U, ATtiny24, ATtiny44, ATtiny84, ATtiny45, ATtiny25, ATtiny85

 

 

No I didn't get a warning on that . Atmel studio was proposing this so I used this notation.

Last Edited: Tue. Jan 27, 2015 - 04:18 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Atmel studio was proposing this so I used this notation.

 

Very interesting.  Are you absolutely sure you are building for the correct AVR model?

 

But it works!?!  Is the avr-libc table out of date?  Experienced GCC gurus will need to comment.

 

Oh, wait -- I've got a fresh copy of Studio 6.2 installed... Yep, the .XML has

       <interrupt index="10" name="TIMER0_COMPA" caption="Timer/Counter0 Compare Match A"/>
 

LOL -- we are both right.  From iotnx5.h

/* Timer/Counter0 Compare Match A */
#define TIM0_COMPA_vect_num		10
#define TIM0_COMPA_vect			_VECTOR(10)
#define TIMER0_COMPA_vect_num	10
#define TIMER0_COMPA_vect		_VECTOR(10)
#define SIG_OUTPUT_COMPARE0A	_VECTOR(10)

...they define them both.

 

Now that you have the equivalent of "blinky LED"...

 

=>  I'd start slower than 1us pulses with 100us repetition.  But suit yourself.

=>  Take a trip to the Tutorials forum and find threads with "timer" in the title.

=>  Next step is to have CTC toggle the pin for you (no ISR required in that case)

=>  Search the datasheet for "toggle".  Modern AVR8 models let you write to the PIN register to toggle an output pin.

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.

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

IME sub-microsecond flashes are visible to humans.

Took me a looong time to realize they were sub-microsecond flashes.

Iluvatar is the better part of Valar.

Last Edited: Tue. Jan 27, 2015 - 05:25 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

IME sub-microsecond flashes are visible to humans.

I didn't believe you, so went to Google, and one of the top hits was from ... wait for it ... 'Freaks in 2003

https://www.avrfreaks.net/forum/s...

where Yaric reported he could see 100ns pulses (with proper eye "preparation"). 

 

Now I believe you. ;)

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.

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

I have a 'Vetinari Clock' app which flashes an LED as a status indicator.  The shortest flash turns the LED on for two CPU cycles at 32.768 kHz.  That's 61 microseconds.  Easily visible.  I haven't tried shorter than that, although I expect I could get quite a bit shorter.

 

EDIT: I just ran a quick test on an UNO.  I can just make out 125 ns, but I cannot see 62.5 ns.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Tue. Jan 27, 2015 - 07:30 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

EDIT: I just ran a quick test on an UNO.  I can just make out 125 ns, but I cannot see 62.5 ns.

LOL--this stuff is fun, isn't it?  Didja jump off the garage using an umbrella when you were a kid, just to see  if you could float down like Mary Poppins?  Or were you a bit smarter than that, and talked your little brother into trying it?

 

Didja do any special prep like Yaric did, dark room and eye delay?

 

As you said "Uno", we'll just call that "normal current for a common indicator LED of that size/type", right?  If you really cared, you'd tack on a parallel resistor to increase the current and see if you can get below 100ns.  ;)

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.

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

Oh, yeah, you microsecond-LED-pulse-watchers:  OP was doing it every 100us.  You can then distinguish with your eye the difference between "solid on at a low level" and "1us pulse every 100us"?

 

[the responses were to my suggestion " =>  I'd start slower than 1us pulses with 100us repetition.  But suit yourself.", right?]

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.

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

Oh, yeah, welcome to Gandalf, and kudos on the posting which had the information we wish all posts of this type had:

..AVR model

..clock speed

..measurement method

..complete test program

 

[about the only thing missing, and can be inferred, is toolchain used and version.  Sometimes build options (e.g. optimization level) and fuse settings are important (e.g. WDTON could cause "interesting" symptoms)]

 

 

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.

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

theusch wrote:
LOL--this stuff is fun, isn't it?  Didja jump off the garage using an umbrella when you were a kid, just to see  if you could float down like Mary Poppins?  Or were you a bit smarter than that, and talked your little brother into trying it?
I was the little brother ;)

 

Quote:
Didja do any special prep like Yaric did, dark room and eye delay?
No special prep.  Just shielded the LED and my eyes with my hand.

 

Quote:
As you said "Uno", we'll just call that "normal current for a common indicator LED of that size/type", right?
3mm red LED directly across the I/O pin and ground, no series resistor, so I expect the instantaneous current was higher than the rated continuous current, but not higher than the rated peak current.

 

Quote:
If you really cared, you'd tack on a parallel resistor to increase the current and see if you can get below 100ns.  ;)
OK I'm being punked now, right?  How would current flowing through a parallel resistor help me?

 

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

joeymorin wrote:

OK I'm being punked now, right? 

 

There is that verb again... guess I will have to get out the dictionary again.

 

Ross McKenzie ValuSoft Melbourne Australia

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

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

OK I'm being punked now, right?  How would current flowing through a parallel resistor help me?

 

 

I was thinking that the LED in question was an indicator LED already mounted on the Uno, with an appropriate current-limiting resistor.  So adding a parallel resistor would boost the current a bit, and you might get different results.  Since you were driving direct--never mind.

 

Come to think of it, I had an app a couple years ago with dimming LEDs.  Using a fairly fast PWM off an 8-bit AVR8 timer, I thought 1/255 would be close enough to "off".  Not.  Nicely visible in all but the brightest ambient conditions.  And that would be fairly close to OP's 1us/100us.

 

[All that said, I like a nice slow sanity blinky.  My typical is 1Hz. Easy to see hiccups.  Amenable to "one one thousand two one thousand three one thousand" clock speed checking.]

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.

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

There is an indicator on PB5, but it isn't driven directly. It is driven by an op-amp output which follows PB5 on its high impedance input. This it does to permit that I/O pin to behave as expected when using it as an input with no pullup.
I'm sure that opamp follower has sub-microsecond response, but I wanted to cut it out of the picture just in case.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]