ATMega8 16bit PWM

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

Hi,

reading the section in ATMega8 datasheet about the 16bit Timer1 is really confusing. All the different PWM Modes and this funny timing diagramms... O.K. For some days i'm trying know to get my ATMega8 generating a PWM Signal. Without any results. I do not event get a Signal on the the Output Compare Pins. Is it possible to generate a 16bit PWM? If yes can anyone send me a short code with the correct timer initialisation?

Best Regards
Tobias

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

If you post the relavent code from your program we can debug it here for you. This way you are more likely to learn what you did wrong.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

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

O.K, here's the code:
DDRB = 0xFF;
PORTB = 0x00;

TCCR1B = 0x00; // stop timer

TCCR1A = 0xA2;
OCR1A = 0x01FF; // this should be the pulsewidth
ICR1 = 0x8000; // here we have the periode
TCCR1B = 0x1A; // start timer

This piece of code generates a PWM Signal with my wanter periode of about 16ms. But now the funny part (funny for me) beginns. OCR1A seems to accept 9bit Values only. So OCR1A = 0x01FF; produces the same signal as OCR1A = 0x03FF;. Funny, isn't it?

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

When you first set TCCR1B = 0x00 then TCCR1A = 0xA2; this is mode 2 (stopped).

When you 'start' the timer by setting TCCR1B = 0x1A, in addition you're simultaneously switching to mode 14. I don't know if the PWM likes this... and from your results it's acting like it did stay in mode 2 (9 bits).

admin's test signature
 

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

As an experiment, try treating TCCR1A, TCCR1B as a "16-bit" register. Write TCCR1B first, then TCCR1A.

Examples are given on pages 78-79 of the ATmega8 data sheet (2486H or 2486J).

The data sheet doesn't say so, but it looks like maybe WGM1:3 and WGM1:2 (in TCCR1B) might be only updated when WGM1:1 and WGM1:0 are written (in TCCR1A).

I had this problem before, and never figured out why some code worked and other code didn't!

admin's test signature
 

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

Thank you Mike, you solved my problem :)

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

So, WGM1:3..0 are considered as a "word"? Shocking! Noted nowhere in the data sheets or app notes !!!!

Just a lucky guess in my part.

It looks like all of the AVR and MEGA parts need to publish an ERRATA on this hardware / programming issue.

admin's test signature
 

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

There are some issues not mentioned in the AVR datasheets. Had some problem with the build in ADC a few month ago. The ATMEL support was not able to help me but the Phorum on Avrfreaks was! Sometimes the AVRs behave strange when not initialising in the "correct" order.

But altogether the AVR ATMega series are the best 8bit uC i ever used.

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

Having made this discovery and confirmed it, what is the procedure to get Atmel to consider publishing it in the next errata?

admin's test signature
 

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

This morning i got a mail from the ATMEL AVR Support:

"There is possibly something incorrect in the simulation of this. Please specify your platform, are you simulating or running on a real device or perhaps an ICE40/50?"

I answered with your way to set up the timer correct and mentioned that an Errata should be published. Let's have a look whats happening.

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

Thanks for the update!

From the published PWM code on this site, it appears that the programming sequence is different depending on which AVR is used.

admin's test signature
 

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

Hi there,

16 BIT PWM does not work!!!!!

This is a core problem on Mega's..........16 and 8535 and surely8 as well.

Only 10 bit PWM

Step the above code and load with a 16 bit value...it will load only 10 bit and drop rest.

John

admin's test signature
 

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

John,

The data sheet says that the "fast" pwm mode is limited to 10 bits, and other modes are 16 bits. Tobias is using the "fast" mode, so it's limited to 10 bits.

Is this what you're referring to?

-Mike

admin's test signature
 

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

Tobias is not using "fast" mode, so it's not limited to 10bit. Tobias is using Mode 14, fast PWM mode with ICR1A (a 16bit register) as TOP. So it's a 16bit PWM.

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

Hi all,

Tobias is not using "fast" mode, so it's not limited to 10bit. Tobias is using Mode 14, fast PWM

So he is Not , Not using Fast PWM mode???

Unless he is not using FAST PWM.

Then I get your point.

John

admin's test signature
 

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

I misread the description of mode 14 on the data sheet. It's a 16 bit PWM. I still don't understand why John Doe is saying that 16 bit PWM does not work. It appears to work for me.

admin's test signature
 

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

Page 88 in the datasheet:

"The PWM resolution for fast PWM can be fixed to 8-, 9-, or 10-bit, or defined by either
ICR1 or OCR1A.The minimum resolution allowed is 2-bit (ICR1 or OCR1A set to
0x0003), and the maximum resolution is 16-bit (ICR1 or OCR1A set to MAX)..."

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

That's the sentence that I initially misread to mean something else.

admin's test signature
 

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

Hi,

Sorry mate - It does not work in FAST PWM as stated earlier.

John Doe

admin's test signature
 

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

It's working with Mode 14 on my ATMega8...