Fast PWM: unable to set CS10 bit

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

I want to use Fast PWM on Timer1 (ATmega168), with TOP defined in ICR1 and compare value in OCR1A, in order to generate a 1ms pulse wave with dc=10%.
fclock is 4MHz

Here it is the code.

AVRstudio stucks on setting of CS10 bit.

Where am I mistaking? Thanks in advance.

#include 


int main (void)
{

   DDRB = (1 << PB1);  // set PB1(OC1A) as output

   ICR1 = 3999;  // Ts=1ms @ fc=4MHz

   OCR1A = 399;  // d.c.=10% (Ton=.1ms)

   TCCR1A |= (1 << COM1A1) | (1 << WGM11); // non inverting - fast PWM (ICR1=TOP), 1st part

   TCCR1B |= (1 << WGM13) | (1 << WGM12); // fast PWM (ICR1=TOP), 2nd part

   TCCR1B |= (1 << CS10); // Unable to set this bit!


   while(1)
   {
     /* timer1 do the job... */
   }
}

My (HW && SW) Setup: (MyAVR USB Programmer | bread-board | Butterfly | Arduino 10k | ATtiny2313 | ATmega8) && (WinAVR | AVR asm | AVRstudio)

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

Quote:

AVRstudio stucks on setting of CS10 bit.

You mean the simulator? I hope for your sake it's Sim2 not Sim1 !!

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

clawson, yes simulator...
following my AVRStudio About -> Version
My os: Windows Seven(64bit)

AVR Studio		4.18.715  
GUI Version		4, 18, 0, 685
AVR Simulator		1, 0, 2, 1
ATmega168		201

Operating System
Major			6
Minor			1
PlatformID		2
Build			7600


Plugins:

AvrPluginAvrAsmObject	1, 0, 0, 48
AvrPluginavrgccplugin	1, 0, 0, 11
Stk500Dll			1, 0, 1, 15

My (HW && SW) Setup: (MyAVR USB Programmer | bread-board | Butterfly | Arduino 10k | ATtiny2313 | ATmega8) && (WinAVR | AVR asm | AVRstudio)

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

ok...at startup of wizard, if I select sim v2 instead of sim v1 I have same issue though

My (HW && SW) Setup: (MyAVR USB Programmer | bread-board | Butterfly | Arduino 10k | ATtiny2313 | ATmega8) && (WinAVR | AVR asm | AVRstudio)

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

with VMlab too...but this sim. is more verbose than AVRstudio:

[PC = $0069, Time =   16.68 ms, {TIM}]: TIMER is in PWM mode: some OC pin not defined as output

OC1A/PB1 is defined as output (checked DDRB)

Missed something else? :(

My (HW && SW) Setup: (MyAVR USB Programmer | bread-board | Butterfly | Arduino 10k | ATtiny2313 | ATmega8) && (WinAVR | AVR asm | AVRstudio)

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

Quote:
AVRstudio stucks on setting of CS10 bit.
What do you mean by "stuck"? My guess is that it is not stuck at all, but is just merrily doing what you told it to do: loop infinitely. If you go into the disassembly window and put a break point on the RJMP that is the while(1), you will find that it stops there and you can examine the registers.

Regards,
Steve A.

The Board helps those that help themselves.

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

Koshchi wrote:
you will find that it stops there and you can examine the registers.

With VMLAB scope now I view the pulse wave, but it doesn't appear as expected.

Register TCCR1B is not correctly set:

00001001

while it should be (for FastPWM with ICR1=TOP):

00011001

and infact VMLAB releaves configuration as FastPWM 9bits

And still the strange warning in VMLAB log:

[PC = $006C, Time =    0.79 ms, {TIM}]: TIMER is in PWM mode: some OC pin not defined as output

My (HW && SW) Setup: (MyAVR USB Programmer | bread-board | Butterfly | Arduino 10k | ATtiny2313 | ATmega8) && (WinAVR | AVR asm | AVRstudio)

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

Seems a VMLAB issue: http://www.amctools.com/cgi-bin/...

I'll try to install/use this model:

http://sites.google.com/site/thv...

which should fix the issue

My (HW && SW) Setup: (MyAVR USB Programmer | bread-board | Butterfly | Arduino 10k | ATtiny2313 | ATmega8) && (WinAVR | AVR asm | AVRstudio)

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

If you aren't going to follow Steve's advice and single step the RJMP (in which case you will see the IO update/settle) then at the very least add the following:

   TCCR1B |= (1 << CS10); // Unable to set this bit! 

   PORTB = PORTB; // ++++ dummy operation

   while(1)

Now single step the initialisation and stop on the PORTB=PORTB line. Do not step into the while(1). What do you see?

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

All fixed, thanks to new ATmega168b lib of VMLAB which let us to use correctly this mcu under VMLAB:

http://sites.google.com/site/thv...

I guess the problem was in the fact that ICR1 register must defined with TOP value _only_ after that FastPWM (with ICR1=TOP) was set since ICR1 normally is a read-only register unless is used as TOP.

So if I define ICR1 _before_ FastPWM (with ICR1=TOP) config, there is an error (ICR1 is default read only) and VMLAB thanks to this newer ATmega168b.lib, reported this issue.

The newer ATmega168b lib fixed also set of WGM13 bit and now all works fine.

For anyone who wants to use VMLAB with ATmega168lib I suggest this post on VMLAB forum (over there I am mixjoe :)

http://www.amctools.com/cgi-bin/...

where the author explain how setup project file in order to use clock of other frequency than default 8MHz

P.S. Sorry for my little English

Following the fixed code, that now works fine:

#include 


int main (void)
{

 DDRB |= (1 << PB1);  // set PB1(OC1A) as output
 OCR1A = 399;  // d.c.=10% (Ton=.1ms)
 TCCR1A |= (1 << WGM11); // FastPWM (ICR1=TOP)
 TCCR1B |= (1 << WGM13) | (1 << WGM12); // FastPWM (ICR1=TOP)

 ICR1 = 3999;  // Ts=1ms @ fc=4MHz

 TCCR1A |= (1 << COM1A1); // non inverting mode

 TCCR1B |= (1 << CS10); // no prescaler

  while(1)
   {
     /* timer1 do the job... */
   }
}

My (HW && SW) Setup: (MyAVR USB Programmer | bread-board | Butterfly | Arduino 10k | ATtiny2313 | ATmega8) && (WinAVR | AVR asm | AVRstudio)

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

Quote:

I guess the problem was in the fact that ICR1 register must defined with TOP value _only_ after that FastPWM (with ICR1=TOP) was set since ICR1 normally is a read-only register unless is used as TOP.


Huh? Maybe with your simulator. I see no notations about any read-only status in the datasheet.

Quote:
15.11.7 ICR1H and ICR1L – Input Capture Register 1
...
Read/Write R/W R/W R/W R/W R/W R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0

[I always fail to see the fascination with trying to do full-app simulation of AVR apps. "Just do it." I suspect OP has spent more time trying to track down simulator handling of complex timer modes than it would have taken to complete that part of the app. And don't you now have a nagging in the back of your head about other peripheral subsystems?!?]

Lee

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

I agree with Lee, for that kind of work I would make a small terminal setup where all the relevant bits/bytes can be changed on the fly, and then test the output on a scope.

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

I have serious difficulty to read your native english and to write decent english so I decided to not bore you with my posts. My apologies again.-

My (HW && SW) Setup: (MyAVR USB Programmer | bread-board | Butterfly | Arduino 10k | ATtiny2313 | ATmega8) && (WinAVR | AVR asm | AVRstudio)

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

Quote:

I have serious difficulty to read your native english and to write decent english so I decided to not bore you with my posts. My apologies again.-

ICR1 is >>not<< read-only.

Lee

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.