ATtiny25 timer1 issues...

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

Hiya,

I can control timer0 quite happily (thanks to Dean's great [TUT]), but I just can't get timer1 to start...
Obviously I'm overlooking something in the data sheet or timer1 is faulty (Is that even likely?)

In my main loop, I'm looking at TCNT1 but my triggers don't activate...

	while(1){
		if(TCNT1 > 0x80)
			PORTB != (1<<PB3);	// On
		else
		{
			PORTB &= ~(1 << PB3);  // Off
		}

	}

In the setup code for the timer I'm setting the control register TCCR1 (with pre-scaler value), the Output Compare Registers (A..C) OCR1A..C (with compare targets), the mask register TIMSK (with various interrupt enables) and as a final straw after about thirty code variations, the PLLCSR register to select different clock sources. The chip is running with internal RC oscillator (8MHz/8 = 1MHz). I've written ISR's for each variation on this theme... nothing seems to work for me. I don't have a hardware debugger.

This is not a simulation, I'm really writing to the chip :cry:

This is all with the CrossPack / MacPack for AVR. All my other programs work, so it's not an issue with the compiler. I've manually checked the io headers in case there was an error against the data sheet, but they too are fine.

As I stated, timer0 works as advertised, it counts, it fires compare interrupts, the whole nine yards, but timer1 just won't play ball.

Is there something I'm not seeing? My eyes are getting tired re-reading the data sheet. I've emulated several programs that google returns for timer1 in normal mode, but I can't get them to work (any of them).

Oh, and yes, I do call sei() at the end of the setup :)

I have another ATtiny25 in my bitsbox so by the weekend I should be able to verify if the chips behave the same (illiminating a duff timer).

The power is from a USBtinyISP via my Mac (at least 100ma available... there are just 2 LED's on PB3 and 4).

Has anyone a code example that is proven to work for timer1 in normal mode? Or even a better debugging process for me to follow?

Any help would be great!

--greg
Still learning, don't shout at me, educate me.
Starting the fire is easy; the hardest part is learning how to keep the flame!

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

Quote:
I do call sei() at the end of the setup
But you are not showing how you have the timer set up. At times bits move in registers for different chips.
edit
PORTB != (1<<PB3);   // On 

That should be |= not !=

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Hello,

Try PORTB |= ... instead of PORTB != ...
If that won't help it would be nice to see how you set up the timer.

Edit: I'm too slow.

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

Quote:
That should be |= not !=

Quite right! My font need's to be bigger for me to spot stoopid mistakes like that :)

Thanks, I'll go over my code base again and make sure I've not replicated that error elsewhere.

Quote:
But you are not showing how you have the timer set up

I've tried so many variations on a theme that haven't worked I thought it would be pointless at this time. Can you spot any error in the (long handed version) setup logic?

--greg
Still learning, don't shout at me, educate me.
Starting the fire is easy; the hardest part is learning how to keep the flame!

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

Quote:

Quite right! My font need's to be bigger for me to spot stoopid mistakes like that

No. You need, AT ALL TIMES POSSIBLE, copy/paste actual code into your posts, instead of typing something in that might resemble what you actually have in your project. Apart from creating confusion, such typos are nothing but a double-ended time-waster.

Quote:

Can you spot any error in the (long handed version) setup logic?

I believe most of us prefer to see some attempt of yours and try to rectify that. There's nothing to be embarrassed about. We just need something hard to chew at.

Apart from that, my most common mistakes are
- when moving between timers, to miss to change the timer number digit somewhere in a register or bit name. E.g. Not changing OCR0A to OCR1A
- using a bit number on the wrong register, e.g. TCCR1A |= 1<<XYZ when the XYZ bit is actually in TCCR1B.
- not doing |=, &= or ~ correctly. Example: I have code setting a bit (using |=) but now want to clear it instead (so changing to &=) but miss to invert (~) the bit mask.
Double check all such stuf.

HTH!

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Quote:
No. You need, AT ALL TIMES POSSIBLE, copy/paste actual code into your posts

That IS the copy/paste of my code! :( What I meant was, I need a bigger font in my code editor :) The compiler hasn't thown it out because it's perfectly valid syntax, although completely not what I intended.

Quote:
I believe most of us prefer to see some attempt of yours and try to rectify that.

After I've doubled checked the 'existing' code for the stoopid typo's already identified (shamed face), I'll copy/paste the entire (little) program if it's still not working.

Just to be clear, the process I've followed (1) Load the control register with prescaler values, (2) Load the compare register, (3) Load the mask register and (4) enable global interupts IS correct? There is not some other action needed that I've overlooked?

Thanks for all your input, it is very helpfull.

--greg
Still learning, don't shout at me, educate me.
Starting the fire is easy; the hardest part is learning how to keep the flame!

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

Personally I'd leave setting the prescaler for the very last step - that's what actually starts the timer and interrupts. Make sure everything else is setup before starting it.

I posted a very simple timer interrupt example for someone else, the other day, here:

https://www.avrfreaks.net/index.p...

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

Quote:
Personally I'd leave setting the prescaler for the very last step - that's what actually starts the timer and interrupts.

Thanks. Oh, and the example program is also good, but won't run/compile on a ATtiny25 :) As noted, I'm fine with timer0 it's just timer1 that's bugging me :) As pointed out earlier in the thread, there could well be some stoopid typo's in my code. I can't check till I get home :(

Thanks again.

--greg
Still learning, don't shout at me, educate me.
Starting the fire is easy; the hardest part is learning how to keep the flame!

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

Quote:

Just to be clear, the process I've followed (1) Load the control register with prescaler values, (2) Load the compare register, (3) Load the mask register and (4) enable global interupts IS correct?

With the same moderation as clawon (doing the prescaler last) that looks reasonable. But there are so many things that can go wrong in the actual code (like things I listed above).

I can't really understand your reluctance to show some code that we could start talking about and rectify. Right now you say "I did this, sort of..." and all we can answer is "That seems resonable, sort of...".

Agan: I hope this isn't about embarrassment. We all mess up code. We all go blind to the obvious. The things I listed above really are mistakes that I have done many more times than just once.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Quote:
I can't really understand your reluctance to show some code that we could start talking about and rectify.

Heh, I'm not reluctant at all :) I'm actually at work doing my day job (software engineer on Unix (for over thirty years ) and Microsoft Platforms (for about three years )) and don't actually have access to my code base which is at home :)

The only embarrassment I'm currently suffering from is that stoopid typo at the start of thread :)

Thanks for all the help -- it's encouraging to know that people are eager to help.

--greg
Still learning, don't shout at me, educate me.
Starting the fire is easy; the hardest part is learning how to keep the flame!

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

So...

How stoopid do I feel now? I'll tell ya... pretty f**king stoopid.

Quote:
That should be |= not !=

Um... every occurrence of |= was in fact !=. No wonder nothing was happening :) (Copy/Paste is NOT ALWAYS your friend).

Thanks for everybody's input. I've been looking at this problem for a few days!

Right, off now to change my flippin font :)

Thanks again!

--greg
Still learning, don't shout at me, educate me.
Starting the fire is easy; the hardest part is learning how to keep the flame!

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

Quote:

How stoopid do I feel now? I'll tell ya... pretty f**king stoopid.

As a long time member, let me be the first one to welcome you to TFFSC (The Feeling Friggin' Stoopid Club). I've been writing software for more than 30 years. Each year I try to denounce my membership, but before the end of the winter holidays I'm a member again. We all do stoopid stuff.

And we all read what we want to see, rather than what is actually written. Insight of this, and being humble when faced with strange inexplicable errors, will make you a better programmer.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Quote:
I've been writing software for more than 30 years.

Ditto :)
Quote:
And we all read what we want to see, rather than what is actually written

At work we call it 'wood from the tree's!

I've increased the font size in my code editor to 14 point now, so that != and |= are very obviously different :)

Quote:
being humble when faced with strange inexplicable errors, will make you a better programmer.

Anything which helps me to learn and get better is good in my book, which is why I posted in the first place ;)

Thanks again all :)

--greg
Still learning, don't shout at me, educate me.
Starting the fire is easy; the hardest part is learning how to keep the flame!