ATtiny2313 - Dean's Timer Tutorial Question

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

Hey Guys

I've been working through Dean's AVR timers tutorial. (btw, great tutorial Dean, really awesome work).

I've worked through the tutorial up to part 6: Pure Hardware CTC.

Everything is great (using Attiny 2313, internal OSC), working as it should according to the tutorial. I would like to add Timer0 to the exercise, jsut to play around and see if I could get it working. LED is wired up, added the pseudo code. Edited the code to C and now I'm stuck. Only timer1 seems to be working, timer0, nothing is happening on OC0A. I would appreciate it if someone can point out/in the right direction where the problem may be. I've been through the datasheet endless times and I can not see if something is missing or wrong.

/********************************
Hardware Setup
*********************************
Pin 14 - PB2 RGB LED - RED
PIN 15 - PB3 RGB LED - BLUE
********************************/

#include  
#include 

int main (void)								// Main
{ 
 
   DDRB |= (1 << 3);						// Set LED as output OC1A Blue
   DDRB |= (1 << 2);						// Set LED as output OC0A Red
      
   TCCR1B |= (1 << WGM12);					// Configure timer 1 for CTC mode 
   TCCR0A |= (1 << WGM01);					// Configure Timer 0 for CTC mode
   
   TCCR1A |= (1 << COM1A0);					// Enable Timer 1 Compare output channel A in toggle mode
   TCCR0A |= (1 << COM0A0);					// Enable Timer 0 Compare output channel A in toggle mode
      
   OCR1A = 15624;							// Set CTC compare value to 1Hz at 1MHz AVR clock, with a pre-scaler of 64 
   OCR0A = 195;								// Set CTC compare value to 0.5Hz AVR clock, with pre-scaler of 1024
   
   TCCR1B |= ((1 << CS10) | (1 << CS11));	// Start timer 1 at Fcpu/64 
   TCCR0A |= ((1 << CS00) | (1 << CS02));	// Start timer 0 at Fcpu/1024
   
   for (;;) 
   { 

   } 
}					

Thank you,

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

Your code looks about right. I am guessing that your F_CPU is 2MHz in order to get 1Hz at OC1A pin.

Your sums are wrong for Timer0. You would get 4.98Hz with F_CPU = 2MHz.

If you are really running at 1MHz, your LEDs would be 0.5Hz and 2.5Hz.

You had a typo:

   TCCR0B |= ((1 << CS00) | (1 << CS02));   // Start timer 0 at Fcpu/1024

David.

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

The clock selection bits for Timer 0 live in TCCR0B, not TCCR0A on that model.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

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

abcminiuser wrote:
The clock selection bits for Timer 0 live in TCCR0B, not TCCR0A on that model.

- Dean :twisted:

Thank you both for the reply. I missed that line.

david.prentice wrote:

If you are really running at 1MHz, your LEDs would be 0.5Hz and 2.5Hz.24

It seems that my calculations is running at 0.5Hz and 2.5Hz. As far as I know I'm running at 1MHz. Fuse settings is: SUT_CKSEL set to INTRCOSC_8MHz and CKDIV8 is enabled.

I want 1Hz and 5Hz, not 0.5Hz as mentioned in my previous post.

From Dean's Tutorial;

abcminiuser wrote:

Target Timer Count = (Input Frequency / Prescale) / Target Frequency - 1

So I have:

((1,000,000/64)/(1))-1=15624
and
((1,000,000)/1024)/5))-1=194.3125 (made this 195)

Am I missing something again?

Thanx guys. Appreciate the feedback and help.

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

Yes. The formula for toggling an o/p pin is:

Target Timer Count = (Input Frequency / Prescale / 2) / Target Frequency - 1

The reason being that you toggle the OCnx pin. You need two matches to make a full cycle.

I hope you are doing this on real hardware. Simply count 20 blinks against your wristwatch.

Dean's formula is obviously correct for calculating the number of interrupts in a second.

David.

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

Oo yes, that makes sense. It will only toggle on the interrupt which only happens once a second for timer 0.It seems that I'm not thinking straight anymore.

Thank you once again. I'll read through ctc again before continuing to the PWM section.

Thank you guys, enjoy your evening (if it's evening already in your part of the world).