Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Thread

More timer confusion

More timer confusion

2011-09-28 by Philippe Habib

As it turned out, I need timer 0 to run PWM on a pin so I have to use timer 1 for my timer tick.

Using what I learned from timer 0, I was hoping it would be easy to generate a 10ms tick using timer 1.

Here is my setup:

	TCCR1A = (0 << WGM10) | (0 << WGM11);  // Make sure these are low for CTC
	TCCR1B |= (0 << WGM12);  // CTC mode
	TCCR1B |= (1 << CS12);  // Use system clock
	TCCR1B |= (1 << CS10) | (0 << CS11) | (0 << CS12);  // no div, full clock
	TIMSK1 = (1 << OCIE1A);	  // Set int for match on A

	OCR1A = 64000;  // compare value
//	OCR1AH = 0xFF;  // MSB
//	OCR1AL = 0x00;	// LSB

Here is my ISR

ISR(TIMER1_COMPA_vect)
{
	PORTD ^= (1 << 6);  //Toggle LED
	return;
}

Using the system clock, I am getting times in the seconds.  Am I doing something totally wrong with my timebase, or something else?

Thank you for any light you can shed on this.

Re: [AVR-Chat] More timer confusion

2011-09-28 by R E Purcella

1) your code
TCCR1A = (0 << WGM10) | (0 << WGM11);
TCCR1B |= (0 << WGM12); // CTC mode
These 2 lines create a basic CTC mode with maximum count of 0xffff; 
which means that the vlaue in OCR1A is ignored. This would not be the 
correct CTC mode. set WGM12 to 1.

TCCR1B |= (1 << WGM12); // proper CTC mode max is = to value in OCR1A

2) your code
TCCR1B |= (1 << CS12); // Use system clock     CS12 = 1
TCCR1B |= (1 << CS10) | (1 << CS11) | (0 << CS12); // no div, full clock
result is that CS12 = 1 OR 0 = 1,  CS11 =0, and   CS10 = 1
so then from these two lines CS12..10 = 101  which gives a divide clk by 
1024.

dividing clk by 1024 and then counting that to 0xFFFF (65535) and 
toggling divides by 2  that should be a few seconds indeed.

change: TCCR1B |= (0 << WGM12);      to TCCR1B |= (1 << WGM12);
remove : TCCR1B |= (1 << CS12); // Use system clock       now CS12..10 
will be 001   or divide by 1

then tweak the compare match value

rep
Show quoted textHide quoted text
On 9/27/2011 10:41 PM, Philippe Habib wrote:
>
> As it turned out, I need timer 0 to run PWM on a pin so I have to use 
> timer 1 for my timer tick.
>
> Using what I learned from timer 0, I was hoping it would be easy to 
> generate a 10ms tick using timer 1.
>
> Here is my setup:
>
> TCCR1A = (0 << WGM10) | (0 << WGM11); // Make sure these are low for CTC
> TCCR1B |= (0 << WGM12); // CTC mode
> TCCR1B |= (1 << CS12); // Use system clock
> TCCR1B |= (1 << CS10) | (0 << CS11) | (0 << CS12); // no div, full clock
> TIMSK1 = (1 << OCIE1A); // Set int for match on A
>
> OCR1A = 64000; // compare value
> // OCR1AH = 0xFF; // MSB
> // OCR1AL = 0x00; // LSB
>
> Here is my ISR
>
> ISR(TIMER1_COMPA_vect)
> {
> PORTD ^= (1 << 6); //Toggle LED
> return;
> }
>
> Using the system clock, I am getting times in the seconds. Am I doing 
> something totally wrong with my timebase, or something else?
>
> Thank you for any light you can shed on this.
>
>

Re: [AVR-Chat] More timer confusion

2011-09-28 by Jim Wagner

What is your system clock?

Jim Wagner

On Sep 27, 2011, at 10:41 PM, Philippe Habib wrote:

> As it turned out, I need timer 0 to run PWM on a pin so I have to  
> use timer 1 for my timer tick.
>
> Using what I learned from timer 0, I was hoping it would be easy to  
> generate a 10ms tick using timer 1.
>
> Here is my setup:
>
> TCCR1A = (0 << WGM10) | (0 << WGM11); // Make sure these are low for  
> CTC
> TCCR1B |= (0 << WGM12); // CTC mode
> TCCR1B |= (1 << CS12); // Use system clock
> TCCR1B |= (1 << CS10) | (0 << CS11) | (0 << CS12); // no div, full  
> clock
> TIMSK1 = (1 << OCIE1A);	 // Set int for match on A
>
> OCR1A = 64000; // compare value
> //	OCR1AH = 0xFF; // MSB
> //	OCR1AL = 0x00;	// LSB
>
> Here is my ISR
>
> ISR(TIMER1_COMPA_vect)
> {
> PORTD ^= (1 << 6); //Toggle LED
> return;
> }
>
> Using the system clock, I am getting times in the seconds. Am I  
> doing something totally wrong with my timebase, or something else?
>
> Thank you for any light you can shed on this.
> 



[Non-text portions of this message have been removed]

Re: [AVR-Chat] More timer confusion

2011-09-28 by Philippe Habib

I should have said.  I am using the internal 8M oscillator using these fuse settings:

FUSES = 
{
    .low = (FUSE_CKSEL1 & FUSE_SUT1) ,
    .high = (FUSE_BODLEVEL1 & FUSE_EESAVE & FUSE_SPIEN),
    .extended = EFUSE_DEFAULT,
};
Show quoted textHide quoted text
----- Original Message -----
From: "Jim Wagner" <wagnerj@proaxis.com>
To: AVR-Chat@yahoogroups.com
Sent: Wednesday, September 28, 2011 6:47:33 AM
Subject: Re: [AVR-Chat] More timer confusion

What is your system clock?

Jim Wagner

On Sep 27, 2011, at 10:41 PM, Philippe Habib wrote:

> As it turned out, I need timer 0 to run PWM on a pin so I have to  
> use timer 1 for my timer tick.
>
> Using what I learned from timer 0, I was hoping it would be easy to  
> generate a 10ms tick using timer 1.
>
> Here is my setup:
>
> TCCR1A = (0 << WGM10) | (0 << WGM11); // Make sure these are low for  
> CTC
> TCCR1B |= (0 << WGM12); // CTC mode
> TCCR1B |= (1 << CS12); // Use system clock
> TCCR1B |= (1 << CS10) | (0 << CS11) | (0 << CS12); // no div, full  
> clock
> TIMSK1 = (1 << OCIE1A);	 // Set int for match on A
>
> OCR1A = 64000; // compare value
> //	OCR1AH = 0xFF; // MSB
> //	OCR1AL = 0x00;	// LSB
>
> Here is my ISR
>
> ISR(TIMER1_COMPA_vect)
> {
> PORTD ^= (1 << 6); //Toggle LED
> return;
> }
>
> Using the system clock, I am getting times in the seconds. Am I  
> doing something totally wrong with my timebase, or something else?
>
> Thank you for any light you can shed on this.
> 



[Non-text portions of this message have been removed]



------------------------------------

Yahoo! Groups Links

Re: [AVR-Chat] More timer confusion

2011-09-28 by Clark Martin

On Sep 28, 2011, at 6:47 AM, Jim Wagner wrote:

> What is your system clock?
>
> Jim Wagner
>
> On Sep 27, 2011, at 10:41 PM, Philippe Habib wrote:
>
>> As it turned out, I need timer 0 to run PWM on a pin so I have to
>> use timer 1 for my timer tick.
>>
>> Using what I learned from timer 0, I was hoping it would be easy to
>> generate a 10ms tick using timer 1.
>>
>> Here is my setup:
>>
>> TCCR1A = (0 << WGM10) | (0 << WGM11); // Make sure these are low for
>> CTC
>> TCCR1B |= (0 << WGM12); // CTC mode
>> TCCR1B |= (1 << CS12); // Use system clock

Don't know why you have this here.

>> TCCR1B |= (1 << CS10) | (0 << CS11) | (0 << CS12); // no div, full
>> clock

Combined with the above line you have a clock select value of 5 or  
divide by 1024, not 1


>> TIMSK1 = (1 << OCIE1A);	 // Set int for match on A
>>
>> OCR1A = 64000; // compare value
>> //	OCR1AH = 0xFF; // MSB
>> //	OCR1AL = 0x00;	// LSB

64000 decimal is 0xFA00

This gives you a interrupt rate of .122 Hz or about every 8 seconds.   
(assuming 8MHz clock which I believe is the case).

To get 100mS you would need:

TCCR1B |= (0 << CS10) | (1 << CS11) | (0 << CS12); 		// divide by 8
OCR1A	= 10000;

>
>

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.