Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Control frequency and duty cycle by AVR ??

Status
Not open for further replies.

#MAAM#

Full Member level 2
Joined
Nov 21, 2009
Messages
131
Helped
29
Reputation
58
Reaction score
27
Trophy points
1,308
Location
Egypt
Activity points
1,920
Hi guys,

I want to control frequency and duty cycle by using built in PWM with ATMEGA8. I read already a lot of tutorials but confused about the mode of PWM. I need sample program or any advice on this problem. thanks in advance.
 

sky_123

Advanced Member level 4
Joined
Feb 26, 2011
Messages
1,170
Helped
279
Reputation
566
Reaction score
273
Trophy points
1,363
Activity points
9,602
Well, here is the code I wrote for ATtiny13 in the past. Most of the AVRs are similar-ish in concept, so use these ideas
to an extent, but adapt for your particular device by checking the user manual for the ATmega8.

Code:
// Port B
#define PWM_PIN 0x01

// outputs
#define PWM_PIN_ON PORTB |= PWM_PIN
#define PWM_PIN_OFF PORTB &= ~PWM_PIN

int
main(void)
{

  // initialise
  PORTB=0;
  DDRB=0x01; // Set first pin of port B as an output
  PORTB=0x3e; // enable pull-ups on all input pins
  PWM_PIN_OFF;  // set it to a known state
  
  OCR0A = 255; // set the PWM level to 255 (zero percent duty cycle)
  TCCR0A = 0xc3; // set COM0A1, COM0A0, WGM00, WGM01
  TCCR0B=0x01; // clear WGM02. Set the prescaler to divide by 1
  // end of initialisation  


  // now set the duty cycle to whatever.
  // A value of 0 corresponds to 100%
  // A value of 255 corresponds to 0%

  OCR0A = 128; // set the PWM duty cycle to 50%

}
 

alexxx

Advanced Member level 4
Joined
Apr 17, 2011
Messages
1,013
Helped
273
Reputation
552
Reaction score
270
Trophy points
1,383
Location
Greece
Activity points
7,938
I want to control frequency and duty cycle by using built in PWM with ATMEGA8. I read already a lot of tutorials but confused about the mode of PWM. I need sample program or any advice on this problem.
For a general purpose pwm, fast pwm mode is just fine. Start with this and you are OK for beginning.

https://www.avrfreaks.net/index.php?module=Freaks Tools&func=viewItem&item_id=507
https://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&p=351000
https://www.electronicsblog.net/atm...rityduty-cycle-example-tutorial-with-program/
**broken link removed**
 

D.A.(Tony)Stewart

Advanced Member level 5
Joined
Sep 26, 2007
Messages
7,059
Helped
1,721
Reputation
3,441
Reaction score
1,716
Trophy points
1,413
Location
Richmond Hill, ON, Canada
Activity points
51,813
Link fixed **broken link removed**
browse site for other useful info. but attached is quicker (4MB)

General VHDL and specific to H bridge sin PWM

You need to define your own State Table for Quadrature H bridge drive and PWM on one leg or two.
 

Attachments

  • vhdl.pdf
    4.1 MB · Views: 13

alexan_e

Administrator
Joined
Mar 16, 2008
Messages
11,895
Helped
2,021
Reputation
4,158
Reaction score
2,032
Trophy points
1,393
Location
Greece
Activity points
64,375
I don't understand why you insist on posting an answer that refers to VHDL code in a thread that refers to an AVR microcontroller?
How exactly is this usable?

Alex
 

D.A.(Tony)Stewart

Advanced Member level 5
Joined
Sep 26, 2007
Messages
7,059
Helped
1,721
Reputation
3,441
Reaction score
1,716
Trophy points
1,413
Location
Richmond Hill, ON, Canada
Activity points
51,813
the common effort is on Page 36-37 which is creating a FSM state diagram with logic code to support quadrature H bridge drive with PWM enabled. If already understood, and need solution rather than direction, just ignore.

(oops I mixed up sine ? with Duty Cycle ? )
 
Last edited:

alexan_e

Administrator
Joined
Mar 16, 2008
Messages
11,895
Helped
2,021
Reputation
4,158
Reaction score
2,032
Trophy points
1,393
Location
Greece
Activity points
64,375
But he is just asking how to control the PWM peripheral in AVR.
He didn't mention anything about an H bridge.

Alex
 

D.A.(Tony)Stewart

Advanced Member level 5
Joined
Sep 26, 2007
Messages
7,059
Helped
1,721
Reputation
3,441
Reaction score
1,716
Trophy points
1,413
Location
Richmond Hill, ON, Canada
Activity points
51,813
PWM11 PWM10 Description
0 0 PWM operation of Timer/Counter1 is disabled
0 1 Timer/Counter1 in 8-bit PWM Mode
1 0 Timer/Counter1 in 9-bit PWM Mode
1 1 Timer/Counter1 in 10-bit PWM Mode

The pre-scalar source for Timer/Counter1 can be selected with the help of clock select bits in TCCR1B register (more information please check datasheet at page 37).

Width of pulse is loaded in the timer output compare registers OCR1A (OCR1AH ; OCR1AL) and OCR1B (OCR1BH ; OCR1BL). Timer/Counter1 acts as an up/down counter, counting up from $0000 to TOP (see table below), where it turns and counts down again to zero before cycle is repeated. When the counter value matches the content of 10 least significant bits of OCR1A or OCR1B, the PD5 (OC1A)/OC1B pins are set or cleared according to the settings of COM1A1/COM1A0 or COM1B1/COM1B0 bits in Timer/Counter1 Control register (TCCR1A), see table below.

PWM
Resolution Timer Top Value Frequency
8-bit PWM $00FF (255) Ftck1/510
9-bit PWM $01FF (511) Ftck1/1022
10-bit PW $03FF (1023) Ftck1/2046


COM1X1
COM1X0
Effect on OCX1
0 0 Not Connected
0 1 Not Connected
1 0 Cleared on compare match, up-counting. Set on compare match down-counting (non-inverted PWM)
1 1 Cleared on compare match, down-counting. Set on compare match up-counting (inverted PWM)
 
Last edited:

#MAAM#

Full Member level 2
Joined
Nov 21, 2009
Messages
131
Helped
29
Reputation
58
Reaction score
27
Trophy points
1,308
Location
Egypt
Activity points
1,920
I'm so sorry. i still confused. i want to generate pwm with variable frequency and duty cycle on the same pin of atmega8 pin15 (OC1A). i need the mode of operation and equation of duty cycle. i read datasheet in this section i find only the equation of frequency. are you have any idea to help me. thanks in advance
 

alexan_e

Administrator
Joined
Mar 16, 2008
Messages
11,895
Helped
2,021
Reputation
4,158
Reaction score
2,032
Trophy points
1,393
Location
Greece
Activity points
64,375
You can't separate the frequency from the duty cycle.

Basically you have a counter working in a specific frequency, in order to specify the pwm frequency you select the counter top for example count 0 to 100 and then you set a value to the compare match which is between these counter values (0 to top) to set the duty , for example 50 to get 50%.
If you change the top of the counter from 100 to 200 then the compare value of 50 will no longer be 50% but 25%

In addition the top of the counter which sets the frequency sets the resolution too, in my previous example with a top of 100 you can use 100 possible compare values so basically 1% steps but if you use a counter top of 200 then you will have 200 possible compare values which results to 0.5% steps etc.
The lowest the pwm frequency the more resolution you will have.

Alex

P.S. thanks for the points
 
  • Like
Reactions: #MAAM#

    #MAAM#

    Points: 2
    Helpful Answer Positive Rating

#MAAM#

Full Member level 2
Joined
Nov 21, 2009
Messages
131
Helped
29
Reputation
58
Reaction score
27
Trophy points
1,308
Location
Egypt
Activity points
1,920
You can't separate the frequency from the duty cycle.

Basically you have a counter working in a specific frequency, in order to specify the pwm frequency you select the counter top for example count 0 to 100 and then you set a value to the compare match which is between these counter values (0 to top) to set the duty , for example 50 to get 50%.
If you change the top of the counter from 100 to 200 then the compare value of 50 will no longer be 50% but 25%

In addition the top of the counter which sets the frequency sets the resolution too, in my previous example with a top of 100 you can use 100 possible compare values so basically 1% steps but if you use a counter top of 200 then you will have 200 possible compare values which results to 0.5% steps etc.
The lowest the pwm frequency the more resolution you will have.

Alex

P.S. thanks for the points

What do you think to solve this problem? are you have any idea about this ?
 

alexan_e

Administrator
Joined
Mar 16, 2008
Messages
11,895
Helped
2,021
Reputation
4,158
Reaction score
2,032
Trophy points
1,393
Location
Greece
Activity points
64,375
Why do you want to change the frequency and duty at the same time?
 

alexan_e

Administrator
Joined
Mar 16, 2008
Messages
11,895
Helped
2,021
Reputation
4,158
Reaction score
2,032
Trophy points
1,393
Location
Greece
Activity points
64,375
When you change the frequency then the duty will be affected, I don't know of a pwm that works differently unless it is an analog one and not based on a counter like in a microcontrollers.
Maybe you can change them at the same time to keep the same duty with the changed frequency but you have to experiment and select the correct timing to do the change.
 
  • Like
Reactions: #MAAM#

    #MAAM#

    Points: 2
    Helpful Answer Positive Rating

#MAAM#

Full Member level 2
Joined
Nov 21, 2009
Messages
131
Helped
29
Reputation
58
Reaction score
27
Trophy points
1,308
Location
Egypt
Activity points
1,920
When you change the frequency then the duty will be affected, I don't know of a pwm that works differently unless it is an analog one and not based on a counter like in a microcontrollers.
Maybe you can change them at the same time to keep the same duty with the changed frequency but you have to experiment and select the correct timing to do the change.

thank you again for post. i will try. :)
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top