[TUT][GCC] PWM AVR Tutorial

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

ATmega32/16 PWM tutorial
http://aquaticus.info/pwm

P.S.
Not sure links allowed here

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

Quote:

Not sure links allowed here

No problem - nice guide by the way.

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

Looks like a really nice guide, I'll have to bookmark it now and use it later when I have a project that uses PWM.

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

good post, reinforced info i nneded for my project! thanks!

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

thx dude,

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

thank you

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

Thankyou for a great guide. This is one of the best guides on PWM or any other AVR subject I have seen. I loved the layout so easy to follow and understand the maths, also the example figures where good too, and the code examples where good and covered a wide application for PWM useage.

Awesome 10/10

Rich

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

tuts for usin timer?
plz help me

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

Quote:

tuts for usin timer?
plz help me

Oh dear. This has got to be one of the most clueless posts here this year. Did you even bother to go through the different threads here in the Tutorials forum?

Nevermind. Just open your mouth, and you will be fed:

http://www.avrfreaks.net/index.p...

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:
Quote:

tuts for usin timer?
plz help me

Oh dear. This has got to be one of the most clueless posts here this year. Did you even bother to go through the different threads here in the Tutorials forum?

Nevermind. Just open your mouth, and you will be fed:

http://www.avrfreaks.net/index.p...

I thought you'd already nominated a candidate. Are you keeping a list so that we can have a vote on Jan 1 2011?

Smiley

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

is there a good guide/tutorial on soft pwm?

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

Quote:

is there a good guide/tutorial on soft pwm?

Well it's not definitive but I posted a very simple example in this other PWM tutorial:

http://www.avrfreaks.net/index.p...

That simply uses an overflow and a compare interrupt. The overflow sets the PWM frequency and the compare sets the duty cycle.

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

hi guys i need ur help in program timer0 atmega8 please can any one help me out in that :(

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

Please start your own thread for this kind of question (and in the correct forum). And we will need a heck of a lot more information if you want us to answer.

Regards,
Steve A.

The Board helps those that help themselves.

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

And we will start by pointing to the timer tutorial in the Tutorials forum. This is something that you can locate yourself with minimum effort, and read for a good start on timers.

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

ya i am just a beginner of avr i had enough pdf's on atmega8 and now even i had brought a new text book on avr but i am unluckey the author had explained the avr using ATmega16 i am loading the all timer registers with proper value here is my code for generating a square wave on portc pls check this out n help me if there is any fault in that

#include
void main (void)
{
        DDRC = 0x0ff;//setting portc as output port
        while(1)
        {
                PORTC=~PORTC;
 //turning on timer with no prescalar
                TCCR0=0x01; 
                TCNT= 0;//initializing count 
               while(TIFR==0);//checking for over flow
                TIFR=0x01;//making tovo to 0
                TCCR0=00;
        }
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

and this is the program which i had used in avr studio4

#include
void main (void)
{
  DDRC = 0x0ff;//setting portc as output port
  while(1)
  {
    PORTC=~PORTC;
    //turning on timer with no prescalar
    TCCR0=0x01;
    TCNT= 0;//initializing count
    while(TIFR==0);//checking for over flow
    TIFR=0x01;//making tovo to 0
    TCCR0=00;
  }
}

thanking u sir mr.JohanEkdahl &mr.Koshchi

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

But you haven't said what the problem is?

(I also edited [code] tags into your posts and fixed the indentation - please get into the habit of doing this yourself in future)

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

[But you haven't said what the problem is? ]

yes sir i got you and sorry for not mentioning the problem the code i have written is for generating 1 to 2 kHz square wave approximately but i am getting nearly 100 kHz wave here i am using the internal oscillator of 1 MHz for that it have to generate 1 to 2 kHz wave but it is not condition i don't know whats wrong in my code what do u suggest for this problem sir i hope you can solve my problem sir thanking u Mr.clawson
with best regards
Harish.G

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

@harish_spt: You need to fix your keyboard. It frequently drops characters (particularly punctuation characters). There are also serious problems with your shift key.

    while(TIFR==0);//checking for over flow 
    TIFR=0x01;//making tovo to 0 

How exactly do you think that these two lines of cod work? Be very specific, talk me through what you think is going on in the mcu here. Be warned here: if you haven't fixed your keyboard problem, I'm not likely to bother reading your answer.

Regards,
Steve A.

The Board helps those that help themselves.

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

@Koshchi: sir, apologies:-)
I am actually trying to generate a square wave of freq = 1kHz on the AVR ATMEGA8 microcontroller. The code which I have sent you is for the same.

When I built the program it showed 0 errors and 0 warnings

However while simulating the Timer_Counter (TCNT0) is incrementing by 7 instead of 1 for every clock cycle.

I use the polling method to check if the flag 't0v0' flag is raised which is housed in the register 'TIFR'.
If it happens to be raised we break the while loop and
write the value 0x01 to the register TIFR(that way the 't0v0' bit is cleared).

Also is there any problem with the compiler I use(AVR studio4 v 4.13)?

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

Quote:

However while simulating the Timer_Counter (TCNT0) is incrementing by 7 instead of 1 for every clock cycle.

But I already told you in another thread that this was NORMAL. It's counting 7 cycles because you are stepping one C statement that takes 7 cycles to execute. Switch to the interleaved C+Asm view if you want to step the 1 or 2 cycle opcodes.

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

@clawson: sir yes then according to you if it was taking 7 cycles for execution then it should generate the square wave correctly in mcu but it is not the case. i am getting nearly 100khz. and i don't know how to use c+ASM can you please help me out in this or else can please send me the code for other timers can you help me out in this please :(

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

When I correct the TCNT=0 in your post above then build/simulate the code for mega8 with a breakpoint on the PORTC=~PORTC line the breakpoint is hit at 148 cycles then 416 cycles, then 684, 952, 1220. So the output is switching every 268 CPU cycles. On 1MHz where one cycle is 1us this means the output toggles every 268us and a complete on/off cycle lasts 536us. So the output should be 1,865.67Hz. If the CPU is running faster than 1MHz then scale this accordingly. For example on 8MHz the output would be 14,925Hz (14.925kHz).

In this program, while it's blocking on the "while (TIFR=0);" line the TCNT0 is increasing by 4 for each step. When you look at the underlying code it is:

    while(TIFR==0);//checking for over flow 
  58:	88 b7       	in	r24, 0x38	; 56
  5a:	88 23       	and	r24, r24
  5c:	e9 f3       	breq	.-6      	; 0x58 

where IN is 1 cycle, AND is 1 cycle and BREQ is 2 cycles while the result is 0. So it's 1+1+2=4 cycles as was observed.

To step the opcodes rather than the C use "Disassembler" on the "View" menu. Now when you step it steps one opcode, rather than one whole C statement at a time.

Cliff

Last Edited: Sun. Apr 25, 2010 - 01:45 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@clawson:
sir,please apologies me and don't mistake me i am just a beginner cani know how can i disassemble the c file in avr studio please and as per u it should increment 4 steps at a time but it is incrementing by 7

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

hi :) i got the output finally for which i was struggling and i got the out put through another software called code version avr there i used interrupt method to clear the flag and i don't know how to polling in this can any one help me in this by giving a sample code of c source and i am using internal clock of 1MHz and i want to shift it to 4 MHz can you please help me

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

Still haven't fixed your keyboard? I'm sure that there is a question in there somewhere, but without proper punctuation and capitalization, I'm not even going to try to decipher it.

Regards,
Steve A.

The Board helps those that help themselves.

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

The PWM link in the first post seems to be dead.
getting "Unable to connect to database" error.

Any chance of getting an updated link?

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

Ok i will fix my key board problem. now i am working on serial communication.can u please send me some example programs or links for that.

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

harish_spt wrote:
Ok i will fix my key board problem. now i am working on serial communication.can u please send me some example programs or links for that.

Before we put in the effort to locate those for you, tell us what you have found yourself already. Just so that we can avoid "doubles" and maybe save some of your possibly precious time.

Eg should I locate the UART tutorial here for you? It only takes a few seconds, but if you've already read it I fear that you might be irritated for getting links that are of little value for you.

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:
No sir i am not irritated the work i had done on usart is i had configured the usart in asynchronous mode with 9600 baud rate. i don't what's the problem in the program. that i am not getting the output. Please i request you to send some example c code for both transmission and reception or send me the link where i can get the code

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

Quote:
or send me the link where i can get the code

There is a USART tutorial here at AVRfreaks, by member 'abcminiuser'. Locate it. Read it.

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
Ya sir i had checked those those programs support avr studio but i am working with code vision avr.But the code vision avr does not support this type of instruction. Which are mentioned below

[ UCSRB |= (1 << RXEN) | (1 << TXEN); // Turn on the transmission and reception circuitry
UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); /]

Quote:
[/code]
The error which i was encountering is i am getting the data output as "<0>" this type.The major part of program is mentioned below please check out the code once[// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: Off
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 9600
UCSRA=0x00;
UCSRB=0x08;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x71;
while (1)
{ for (i=0;i<=7;i++) {
UDR='c';
do {
x=UCSRA&0x40;

}
while (x==0);
UCSRA = 0x40;
};

/* Put data into buffer, sends the data */

};
}]

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

Quote:

But the code vision avr does not support this type of instruction. Which are mentioned below

[ UCSRB |= (1 << RXEN) | (1 << TXEN); // Turn on the transmission and reception circuitry
UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); /]

Funny. I was fairly convinced that CodeVision was compliant with C standards. What are the compilation errors you get?

It might that CodeVision does not have the defines for the bit numbers, but they can either be generated or you can supply them.

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

Quote:

It might that CodeVision does not have the defines for the bit numbers, but they can either be generated or you can supply them.

Either that or you have not switched them on - the latest CV does have bit as well as register names but (to avoid breaking existing code I guess) it's turned off by default.

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

mogor wrote:
ATmega32/16 PWM tutorial
http://aquaticus.info/pwm

P.S.
Not sure links allowed here

nice tutorial! i'm late but thanks:) i was busy making stuff!

BTW, it would be nice if you added how to choose resolution! i found out at datasheet :)