Generate a fixed PWM of 3.125KHz by using Atmega 164A at timer counter 2 and Phase correct mode using prescalor 8

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

Dear Members,
I am new in Atmel Microcontrollers and I want to generate a fixed and constant PWM by using the AtMega164A Timer counter 2 and Phase correct PWM mode and pre-scaler 1/8. kindly inform me how I can generate this constant PWM and if you have any tutorials then let me know please, it is very helpful to me.
Thanks in advance.

ibrar

Last Edited: Sun. Dec 9, 2018 - 08:39 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And I was used 20MHz Crystal for external Oscillation.

ibrar

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

Why do you want pwm if it is fixed? Do you want a square wave at 3.125kHz?

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

Yes, Sir,
I want a square wave of 3.125KHz by using 20MHz crystal

.

ibrar

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

Anyone, please explain this code, I don,t understand this code.

 

 

 

 

 

void PWM_init(){
     //Initialize Lock-In-PWM-Signal
     PRR0 &= ~(1<<PRTIM2);                                                    // Power Reduction Register: Timer2 Enable
     TCCR2A=(1<<COM2A0) | (1<< WGM20);                                        // WGM22:0 = 101 PWM, Phase Correct Mode; COM2A0=1 Toggle on Compare Match
     TCCR2B=(1<<CS21) | (1<<WGM22);                                            // 1/8 prescaling,
     //set_LOCK_PWM(62);                                                        // PWM: 10,08 kHz, 50% Duty Cycle
     //set_LOCK_PWM(250);                                                    // PWM: 2,50 kHz
     set_LOCK_PWM(200);                                                        // PWM: 3,125 kHz
 }
 
 void enable_LOCK_PWM(){
     TCCR2A |= (1<<COM2A0);                        // Phase correct PWM Mode, Toggle OC1A on Compare Match
 }
 
 void disable_LOCK_PWM(){
     TCCR2A &= ~(1<<COM2A0);                    // Non PWM Mode, Normal port operation, OC1A disconnected 
 }
 
 void set_LOCK_PWM(uint8_t pwm_val){
     OCR2A = pwm_val;
 }

ibrar

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

You need to read up on timers. Refer to the datasheet and there is a tutorial on this site.
I have no idea why they use a pwm mode when CTC mode is more suitable.

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

Dear Sir,
Can you explain me the reason for this value

 "set_LOCK_PWM(200)"

ibrar

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

You should have read this before posting https://www.avrfreaks.net/forum/...

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Quite a strange original request.  If OP has already selected the AVR model, the clock speed, which timer, which mode, and which prescaler -- then what is left to do?!?

 

Heck, use the parameters and brute-force check each compare value until you get what you want.

 

As hinted, why force phase-correct PWM when CTC will do nicely?

 

What does a tool such as kavrcalc suggest?

http://web.archive.org/web/20130...

 

Or CodeVisionAVR Wizard:

Now, that will have a timer period of 320 microseconds, as you requested.  Is that what you want?  Probably not...

 

 

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.

Last Edited: Mon. Dec 10, 2018 - 01:29 AM