![]() |
ATMEGA16 PWM
Ok so here is my problem,
I have a AVR ATMEGA16 uC made by atmel. I have normal timers working but I cannot figure out from the data sheet how to get a PWM output. I have posted on avrfreaks.net but none will post an example code for me. All they do is redirect me to ode that is not for my uC. So i decided since I know a few people on here use them if they could post some very basic example code for me which just includes basic PWM timer operation and to have to code commented so I can look at it and learn from it. The datasheet can be found at: http://www.atmel.com/dyn/resources/p...ts/doc2466.pdf thank you in advanced, John |
Re: ATMEGA16 PWM
2 Attachment(s)
Here is a pwm module I wrote. It is currently set up for an ATMega8, so the register names may need to be changed slightly, but I have used a version of this code on an ATMega16.
This does direct motor control PWM, 0 - 100%, not RC servo PWM. If that is what you want, you need to adjust the period and limit the range. |
Re: ATMEGA16 PWM
Usually from what I have seen for the atmegax series you can use any code from a lower processor on a higher level processor with out changing register names. I don't know if it is true but I guess I can try your code and find out.
What compiler do you use? I am currently running AVR studio-GCC I am going to look at some things but do you think it will be safe to hook the servo up to the STK500. wow....that is above my reading level. If possible seeing as how I am a begginer would you mind commenting some, I mean most of that code. I have never seen the #endif and all those # commands before other than #include and #define Could you possible offer an explanation? Also I need "main.h" and "stdint.h" |
Re: ATMEGA16 PWM
Quote:
|
Re: ATMEGA16 PWM
Quote:
Quote:
Quote:
Quote:
You should look at the register settings this code does, and look at the manual sections for those registers to understand how it uses the timer to do PWM. Then you can write you own code or modify this to do what you want. main.h isn't very much: Code:
#define F_CPU 14745600UL |
Re: ATMEGA16 PWM
If you want to generate a pulswidth of 0 - period (0-100%) I generally used Timer 1 because it has to Output Compare Units (A and B).
The code that I use to set up is in asm, because I'm an assembler junkie. Code:
;Init TimersIf you want to control RC servos, which are a PWM wave with the PWM from anywhere from .5 ms to 2.5 ms every ~50 hz (20 ms) then I have a pretty inefficient code, but it gets the job done. BTW this is all using a 16mHz crystal. Code:
;Timer 0 (to call servo routine every 10 msCode:
Servo: ;update servo positions, should call every 20ish ms called by interuptall of the names temp, temp2, Serloop, ServSt, Servo1- 4 are all registers. To change the servo position change the value of Servo1-4. AND one more thing, Delay200, Delay7 are two delay loops. Their code: Code:
Delay7:Code:
Delay40:[Edit for code tags, thanks Matt Krass] |
Re: ATMEGA16 PWM
Quote:
Might I recommend the use of [ code] and [ /code] tags? Code:
;Init TimersCode:
Delay7: |
Re: ATMEGA16 PWM
Do you happen to have anything in C? Just a quick and easy code that will show me how to do it.
What should my timer be set at? 8mhz I am guess to get the most efficient use of your processor. What Timer should I use? What is the easiest way to run 2 servos at once? |
| All times are GMT -5. The time now is 03:14. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi