Yahoo Groups archive

AVR-Chat

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

Thread

ATmega 168 Timer 1 mode 14, fast PWM, problem

ATmega 168 Timer 1 mode 14, fast PWM, problem

2007-04-08 by anding_eunding

Hi,

I am totally perplexed!  I'm trying to set up fast PWM on 16-bit 
Timer 1 of the ATMega 168 in mode 14 (i.e. fast PWM, TOP = ICR1).

It "doesn't work", and three strange things are happening which I've 
investigated thoroughly on in the AVR Studio debugger:

- I cannot write 16 bit values to OCR1A or OCR1B, they get masked off 
as 9 or 10 bit values insted during the write.

- I cannot write Clock Select = 2, when I try to, the clock source 
remains zero

- If I write a Clock Select = 1, I get dual slope PWM, not single 
slope fast PWM as expected for mode 14.

Here is the code.  Any ideas greatly appreciated!

void PWM_init(void) {

	ICR1 = 45455;

	TCCR1A = (1<<COM1A1) | (0<<COM1A0) | (1<<COM1B1) | 
(0<<COM1B0) | (1<<WGM11) | (0<<WGM10);

	TCCR1B = (1<<WGM13) | (1<<WGM12);

	OCR1A = 4384;   // Gets masked off to 9 or 10 bit value!!!
	OCR1B = 3600;

	DDRB |= (1<<1) | (1<<2);

	TCCR1B |= (0<<CS12) | (1<CS11) | (0<CS10);  // CS remains 0!!!

	}

Solved! Re: ATmega 168 Timer 1 mode 14, fast PWM, problem

2007-04-09 by anding_eunding

Thanks Bob, and John

1.  I had a silly typing error in my code that simply stopped the 
real AVR from working:
TCCR1B |= (0<<CS12) | (1<CS11) | (0<CS10);  // Missing '<' !!!!

2.  The AVR debugger is completely spurious in emulating this 
function.  When I tried to investigate the issue on the debugger I 
was totally lead astray. (I need a Dragon! :-)

3.  Combination of 1. and 2. had me absolutely baffled.  

Thanks again and apologies to the group for wasting your time with my 
silly mistakes.

Happy Easter to all!

Andrew

PS Bob, I want to do servo control, so I set ICR1 to ~40,000 to give 
me the 55Hz pulse repeat, then change OCR1A and OCR1B for the channel 
width.  I only need 2 channels, so 16bit PWM on Timer 1 is the easy 
way...


--- In AVR-Chat@yahoogroups.com, "anding_eunding" 
<anding_eunding@...> wrote:
>
> Hi,
> 
> I am totally perplexed!  I'm trying to set up fast PWM on 16-bit 
> Timer 1 of the ATMega 168 in mode 14 (i.e. fast PWM, TOP = ICR1).
> 
> It "doesn't work", and three strange things are happening which 
I've 
> investigated thoroughly on in the AVR Studio debugger:
> 
> - I cannot write 16 bit values to OCR1A or OCR1B, they get masked 
off 
Show quoted textHide quoted text
> as 9 or 10 bit values insted during the write.
> 
> - I cannot write Clock Select = 2, when I try to, the clock source 
> remains zero
> 
> - If I write a Clock Select = 1, I get dual slope PWM, not single 
> slope fast PWM as expected for mode 14.
> 
> Here is the code.  Any ideas greatly appreciated!
> 
> void PWM_init(void) {
> 
> 	ICR1 = 45455;
> 
> 	TCCR1A = (1<<COM1A1) | (0<<COM1A0) | (1<<COM1B1) | 
> (0<<COM1B0) | (1<<WGM11) | (0<<WGM10);
> 
> 	TCCR1B = (1<<WGM13) | (1<<WGM12);
> 
> 	OCR1A = 4384;   // Gets masked off to 9 or 10 bit value!!!
> 	OCR1B = 3600;
> 
> 	DDRB |= (1<<1) | (1<<2);
> 
> 	TCCR1B |= (0<<CS12) | (1<CS11) | (0<CS10);  // CS remains 0!!!
> 
> 	}
>

Re: [AVR-Chat] Solved! Re: ATmega 168 Timer 1 mode 14, fast PWM, problem

2007-04-09 by John Samperi

At 08:13 PM 9/04/2007, you wrote:
>(1<CS11) | (0<CS10);  // Missing '<' !!!!

Didn't the compiler complain? Or are both << and < acceptable?

Regards

John Samperi

********************************************************
Ampertronics Pty. Ltd.
11 Brokenwood Place Baulkham Hills, NSW 2153 AUSTRALIA
Tel. (02) 9674-6495       Fax (02) 9674-8745
Email: john@ampertronics.com.au
Website  http://www.ampertronics.com.au
*Electronic Design * Custom Products * Contract Assembly
********************************************************

Re: [AVR-Chat] Solved! Re: ATmega 168 Timer 1 mode 14, fast PWM, problem

2007-04-09 by Dave Hylands

Hi John,

On 4/9/07, John Samperi <samperi@ampertronics.com.au> wrote:
> At 08:13 PM 9/04/2007, you wrote:
> >(1<CS11) | (0<CS10);  // Missing '<' !!!!
>
> Didn't the compiler complain? Or are both << and < acceptable?

Both are acceptable.

(a < b) is an expression that yields 1 if a is < b and 0 otherwise.

It's just as valid to do:

int x = a < b;
if ( x )
{
   ...blah...
}

as it is to do:

if ( a < b )
{
   ...blah...
}

-- 
Dave Hylands
Vancouver, BC, Canada
http://www.DaveHylands.com/

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.