ATMEGA8L 16-Bit Timer Problem

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

hi guys,

 i have been trying to implement a PWM with the Atmega8l, I used the 8 bit timer, and everything worked out just fine, but i was uncomfortable with the fixed top value (0xFF). I decided to sitch to the 16 bit timer, but the problem i keep facing is the fact that the OCR1A register is used to define both the top and the Compare parameter with the TCNT1 register. is there a way, have a different op value and OCR1A value using the 16 bit timer?

This topic has a solution.

despacito!

Last Edited: Fri. Aug 18, 2017 - 11:53 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Assuming Fast PWM: Use mode 14 or 15, with TOP in OCR1A or ICR1 respectively.

 

[EDIT: The below is wrong, mixed up mode 14 and 15.]

With mode 14 you only have one PWM output (since OCR1A is taken for TOP) with duty cycle in OCR1B.

With mode 15 you have two PWM outputs with duty cycle in OCR1A and OCR1B.

[Correct statement is in post below]

 

To get a grip on this, start with looking at table 39 for an overview of modes, and then go back and read the Fast PWM section.

 

Ask here, after reading, if anything is unclear!

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Thu. Aug 17, 2017 - 08:12 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Thanks for your reply; i think i presented my question wrongly;

here is my code

#include <avr/io.h>

#include<avr/iom8.h>

#include<avr/pgmspace.h>

#define F_CPU 1000000UL

#include<util/delay.h>

int main(void)

{

DDRB=0XFF;

PORTB=0XFF;

//============================

TCCR1A=TCCR1B=TIMSK=0B00000000;

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

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

TIMSK|=(1<TICIE1)|(1<<TOIE1)|(1<<OCIE1A);

OCR1A=0XFF;

}

..i am currently using the mode 15, but the problem is that once the counter reaches the OCR1A value, it stays high and stays that way, OC1A doesn't clear, or reset. since OCR1A is the TOP value is it also the compare value?

despacito!

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

thanks johan, i found a solution on the old thread http://www.avrfreaks.net/forum/a...

... turns out, using ICR1 as top and OCR1A as compare works well;

despacito!

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

igbokwe wrote:
since OCR1A is the TOP value is it also the compare value?

No, OCR1B is the compare value. For the one PWM channel you have in mode 15. It's output is on OC1B.

 

Above I messed up, mixing up mode 14 and 15. This is the corrected version:

With mode 14 you have two PWM outputs with duty cycle in OCR1A and OCR1B.

With mode 15 you only have one PWM output (since OCR1A is taken for TOP) with duty cycle (i.e. compare value) in OCR1B. PWM output is on pin OC1B.

 

Take a look again at table 39 and compare with what I'm saying - make sure I'm right this time. ;-)

 

ADDENDUM: If you need two PWM channels, or if you're tied by your hardware to have a single PWM channel on OC1A then switch to mode 14. Do note though, that if you're going to change the PWM frequency, i.e. write to ICR1, then you need to take special measures. There is no hardware governing that writes to ICR1 are synchronized with where in the counting sequence the timer is - writing at the wrong time might lead to "one-shot" momentary glitches in the signal for a PWM period.

 

The whole point with having the TOP value in OCR1A in mode 15 is that there are no such special provisions needed when writing OCR1A. That register is synchronized with the timer. Write anytime, but value is stored in a temporary register and OCR1A is actually set when the timer wraps.

 

Bottom line: If you are going to have a fixed PWM frequency, then you might as well go with mode 14 instead of 15. Write ICR1 before you start the timer.

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Thu. Aug 17, 2017 - 08:26 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

If you want to zero out all bits apart from the ones you explicitly set then this

TCCR1A=TCCR1B=TIMSK=0B00000000;
TCCR1A|=(1<<COM1A0)|(1<<WGM11)|(1<<WGM10)|(1<<COM1A1);
TCCR1B|=(1<<WGM12)|(1<<WGM13)|(1<<CS10);
TIMSK|=(1<TICIE1)|(1<<TOIE1)|(1<<OCIE1A);
OCR1A=0XFF;

is a little awkward. Why not simply

TCCR1A = (1<<COM1A0) | (1<<WGM11) | (1<<WGM10) | (1<<COM1A1);
TCCR1B = (1<<WGM12) | (1<<WGM13) | (1<<CS10);
TIMSK = (1<TICIE1) | (1<<TOIE1) | (1<<OCIE1A);
OCR1A = 0XFF;

?

 

Also, spaces are tremendous for readability.. ;-)

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

I see we posted at the same time. You found mode 14 - excellent!

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

JohanEkdahl wrote:

I see we posted at the same time. You found mode 14 - excellent!


Thank you very much!... Learnt new stuff today, Thanks again!

despacito!