ATTiny45 Timer 1

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

I thought I was understanding these timers, then I hit timer 1 of a Tiny 45.

I want it to count up to OCR1A and give me an interrupt., but it never increments the counter. Obviously there's something I've overlooked.

   TCCR1 = 0b00000011 ;  //sysclk / 4
   GTCCR = 0b00000000 ;
   TIMSK = 0b01000000 ;
   PLLCSR = 0b00000000 ;
   OCR1A = 61 ;

My timer 0 is quite happily generating 2 pwm signals for me.

Anybody untangle my bits?

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

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

Are you sure it is not counting? Maybe you have not enabled interrupts globally. The symptom would be the same.

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

I have a loop and I use the simulator to stop and see what the TCNT is.

	sei();
	for (;;)
	{
	   Samples += 1 ;
	   if (Samples == 1024)
	   {
	      Samples = 0 ;
	   }

	}

So I can put a breakpoint on the Samples = 0 line and peek at the counter every few us.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

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

How does it behave if you compile with no optimization? It might be treating the loop as empty.

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

t45 is lovely little chip; but there is a catch (or a feature -it depends on the viewing angle) called "ATtiny15 compatibility mode."

I bet that the CKSEL fuses are written to "0011" which forces TC1 to operate in the ATtiny15 compatibility mode. In this case, the CS13:CS10 value of 0b0011 selects the PLL_Clk/4 as a clock source for TC1, and since the PLL is off TC1 is also stopped.

The solution is simple, by either setting CKSEL to anything but "0011" in order to deselect the ATtiny15 compatibility mode, or by keeping this mode and setting CS13:CS10 to 0b0111 in order to start TC1 at Clk/4.

-George

I hope for nothing; I fear nothing; I am free. (Nikos Kazantzakis)

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

Quote:
I use the simulator
From the simulator manual under "Known Issues":
Quote:
Timer/Counter1, including the Dead Time Generator, is currently not supported in the AVR Simulator.

Regards,
Steve A.

The Board helps those that help themselves.

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

The user manual also says:

Quote:
Notice: Starting with AVR Studio 4.14, AVR Simulator 2 is the official simulator for most new AVR devices. In particular, the new ATxmega family is only suppported by Simulaotor 2. In the transition period, quite a few other devices are supported by both versions. However, we recommend using Simulator 2 whenever possible.

and
Quote:
Simulator 2 is a new simulator based on software models derived directly from the hardware designs, unlike the old simulator that is based on hand-written code. This results in significantly more accurate and complete simulation of digital peripherals than the old simulator. Starting with AVR Studio 4.14, Simulator 2 is the primary simulator for AVR Studio, and most new devices will only be supported in Simulator 2.

and
Quote:
Supported devices

ATtiny25/45/85


and
Quote:
Known Issues in AVR Simulator 1

The Known Issues list is organized in two major sections; the first describes general simulator issues, while the second describes device specific issues.

If your device is supported by AVR Simulator 2, we recommend using that to avoid the issues listed below.

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

Hmm. I just downloaded and reinstalled studio to get my dragon trained, so I imagine I have simulator 2. How can I tell?

I saw the section on Tiny15 compatability, but ignored it. I'll read through that and see if it's running that mode.

(I originally designed for Tiny44, but couldn't get it from my supplier.)

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

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

Quote:
so I imagine I have simulator 2. How can I tell?

On the debugger menu you have "select platform and device". Under that there are all of "Simulator", "Simulator V2" and "Dragon" listed. Clearly THE very most accurate will be seen using the Dragon to connect to a real AVR but after that you want "Simulator V2".

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

Shirley if you have a Dragon you could debug with debugWire.

Then you can put a breakpoint in your ISR() or do anything else that you like!

The Simulator is magic for timing functions or loops. But if you can step with dW or JTAG, life is easier for debugging.

David.

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

Quote:
[...] so I imagine I have simulator 2. How can I tell?
I was going to answer "through the AVR Studio Menu >> Debug >> Select platform and device" but I was beaten to that with more inclusive responses!

Quote:
I saw the section on Tiny15 compatibility, but ignored it.
Since t45 was the upgrade path for the then maturing and now obsolete t15L, the enable of the backwards compatibility mode on the newer part changes the clock frequencies (see the "Device Clocking Options") and the TC1 control registers of t45 to match those of the --comparatively limited functionality and slower-- older chip.

-George

I hope for nothing; I fear nothing; I am free. (Nikos Kazantzakis)

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

Ah! And with Simulator 2, my interrupt interrupts!

Now, I know the code is close to correct;)

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

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

Quote:

Shirley

Who? :D

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:

Quote:

Shirley

Who? Very Happy


http://www.imdb.com/title/tt0080...

Quote:
Rumack: Can you fly this plane, and land it?
Ted Striker: Surely you can't be serious.
Rumack: I am serious... and don't call me Shirley.

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

Had I put it in my chip instead of checking it first, it would have done what I expected.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

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

Quote:

Had I put it in my chip instead of checking it first, it would have done what I expected.

Indeed and with the Dragon you could have watched it doing it in a way that would be far more accurate than any simulator is ever likely to be.

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

Dragon is new. Haven't tried debugging with it yet. The gadget is tiny and doesn't have a header, so I use pogo pins on the programming cable, so I didn't think I'd be able to hold it in place while fiddling with a debugger ;) I do good to hold it still long enough to load the program.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

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

You really are missing its main selling point if you don't try to use it for debugging - the programming is almost incidental

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

Revisiting an old thread because again, I am trying to use this TCC1 timer.

I think it's NOT in compatibility mode 'cause I get my interrupt when I set CS12 to 1, not when I set CS13 to 1.

Anyhow, I'm trying to get a PWM output on pin 3, which is OCR1B, and now and then, a pulse on pin 2.

The complete program

/*
 * RotatingBeacon45.c
 *
 * Created: 9/1/2014 2:13:15 PM
 *  Author: Tom
 
	This simulates 2 rotating beacons, like in old fire engines using 2 LEDs.
	One led is on OC1B
	One led is on OC0B
	
	To make a bright flash, PB0 connects the led at OC0B directly to ground
	and PB3 connects the LED at OC1B directly to ground.
	
	So the lights aren't in sync, one timer is set part way through the cycle
 */ 

#include 
#include 

void StartTimer0(void)
{
	TCCR0A = (1<<COM0B1) | (1<<COM0B0) | (1<<WGM01) | (1<<WGM00) ;
	TCCR0B = (1<<CS01) ;  // 1MHz / 8 clock
	OCR0B = 0 ;
	TIMSK |= (1<<TOIE0) ;
}

void StartTimer1(void)
{
	TCCR1 = (1<<CS12) ;
	GTCCR |= (1<<PWM1B) | (1<<COM1B1) | (1<<COM1B0) ;
	OCR1B = 33 ;
	TIMSK |= (1<<TOIE1) ;
}

uint8_t AIndex = 0 ;
uint8_t BIndex = 85 ;

uint8_t f( uint8_t A)
{
	if (A < 64) return (255-(2*A)) ;
	if (A < 129) return 0 ;
	return (255-(2*(128-A))) ;
}

ISR(TIMER0_OVF_vect)
{
	AIndex += 1 ;
	if (AIndex==192) 
	{
		AIndex = 0 ;
		PORTB = PORTB & ~(1<<0) ;
	}
	else
	{
		PORTB = PORTB | (1<<0) ;
	}
	OCR0B = f(AIndex) ;
}

ISR(TIMER1_OVF_vect)
{
	BIndex += 1 ;
	if (BIndex==192)
	{
		BIndex = 0 ;
		PORTB = PORTB & ~(1<<3) ;
	}
	else
	{
		PORTB = PORTB | (1<<3) ;
	}
	OCR1B = f(BIndex);

}

int main(void)
{

	DDRB = 0b00111111 ;
	PORTB = 255 ;
	GTCCR = 0 ;
	
	StartTimer0();
	StartTimer1();
	sei();
    while(1)
    {
        //TODO:: Please write your application code 
    }
}

My OC1B pin gives me what looks like a 4ms pulse, not my varying pulses I'm looking for like the OC0A pin does.

I came back to this thread thinking, "I had trouble last time I tried to use this timer."

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut.