[solved]Timer1 in Phase correct PWM mode turning on and off?

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

Hi guys...

I wanted to use timer 1 as PWM Phase and Frequency Correct (ICR1 as top)
My question is can I change top each time I turn on timer like in the code below?

Data sheet warns about changing ICR1 while timer is running but when it is stopped, set and then turned on could it be save. How ICR1 is updated? immediately?

Anyway I post this question because in Proteus VSM software some of the frequencies don`t appear on OC1B and OC1A pins. In AVRStudio they seems to work.
I think I should start debug it on real chip.

TCCR1B|= (1<<WGM13); //ICR1 as TOP

//Turn on
target_timer_count=F_CPU/(frequency_A) + 1;
ICR1= target_timer_count; //Top
OCR1A= (target_timer_count * 5/ 10); 
OCR1B= (target_timer_count * 9/ 10); 
TCCR1A|= (1<<COM1A1)|(1<<COM1B1);
TCCR1B|= (1<<CS10);


//Turn off

TCCR1A&=~(1<<COM1A1); 
TCCR1A&=~(1<<COM1B1); 
TCCR1B&=~((1<<CS12)|(1<<CS11)|(1<<CS10)); 
TCNT1=0x00;

PS. I use atmega 8 and avrstudio 4.19

Last Edited: Mon. Oct 10, 2011 - 09:10 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
My question is can I change top each time I turn on timer like in the code below?
Sure, why not? And you can do it while it is running as well, you just have to make sure that the compare values and the current value of the timer are less than the new TOP value.

Regards,
Steve A.

The Board helps those that help themselves.

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

Hi Steve

Exactly and everything works in AVRstudio and I also tested it on STK500 and it works on it too. The only problem is in Proteus VSM which does not toggle OC1A and OC1B with certain values of OCR1A, OCR1B and ICR1.
When I watch values of OCR1A, OCR1B and ICR1 they are correct. Timer counts but outputs seems to hang for certain frequencies. Tomorrow or even today I will write to them.

Adam