pulse width modulation

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

ok here goes. im interested in making a program using WinAVR in C using the Atmega32 at 8mhz to enable the timer/counter reg to create a 1.5ms pulse.

I have looked alot through the data book and found it quite difficult to comprehend. where would be another good place to start learning be, i dont really want to copy code but get ideas from example.

thanks for your patience :wink:

:arrow: Dan :!:

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

I too have had problems and still do comprehending the datasheets however I have learned one thing about them. They are not set up as a tutorial as they jump around from page to page. Just get your mind around the names for each bit, and the names for the bytes, then for example on a timer, use a sheet of paper and go through and write down from the pages all of the byte registers that are used as well as all the bit names for each of those bytes. The datasheet explains each bit in depth with many referrals to other bits in other byte registers as you read so you have to organize the material into a logical step by step process. Of course when dealing with interrupts you must also have a background on what goes on in the hardware that actually executes the instructions.

For example Page 44 lays out specifics on interrupts and refers you to page 13 for a general discussion, there are 4 separate interrupts you can see in the table for Timer1 depending on how you set up the bits in the various byte registers. Page 70 refers you to page 84 for details on clock sources etc..

So the information is spread out over many pages but like you say just starting out with times you need some good info especially on the math that goes into prescalers .

Over at the bascom site if you search the forums you will find some very good information detailing exactly how to use timers. Luciano is one of the folks over there that really helped me out when I did not even realize that the names like ADCSRB could be used in a program let alone how you would make use of such an abstract looking variable. The posts go way back but search for TIMER/COUNTER0 and things like that and that should keep you busy for a while.

It is a very deep subject so posting specific questions on a specific thing you don’t understand on the datasheet will help you organize your thoughts. I wrote many posts that I never sent because I figured it out before I posted the question, but the act of writing the question down allowed me to think the material through. Also sleep on it, read it and sleep on it sometimes it takes a while to sink in.

Link to Bascom site forum.
http://www.mcselec.com/index2.ph...

The Thread I got a lot out of was this one:
http://www.mcselec.com/index2.ph...

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

thanks tons Metron9 for that very descriptive post.. i have a fair bit of reading ahead of me... excellent idea about writing down the question first.
i will do that in future.

i must admit pwm is a very interesting topic even though i only know the mere basics.

cheers!

:arrow: Dan :!:

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

just read through that second link... thanks... im going to go over more closely tonight

:arrow: Dan :!:

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

Dan,

The driving of servos (I assume that's what your 1.5ms pulse is for and that you intend to vary 1.0ms..2.0ms in fact) has been discussed here MANY times. I'll bet if you search for "servo AND pwm" you'll get loads of relevant stuff.

Cliff

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

clawson. yes ur right on the money. i found quite a bit with that search. thanks for ur reply. have u had experience driving servos? as im in the process of gathering info and knowledge to start a robot project

:arrow: Dan :!:

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

Driving servos from an AVRs PWM is one thing I've never tried which is kind of odd as I fly model planes and helicopters and actually use them a LOT but, until recently, it'd never crossed my mind to wire one up to an AVR and see if I could drive it that way. The next person who does this successfully though ought to prepare a tutorial and deposit it in the Tutorial Forum here as this is SUCH a common request !

Cliff

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

yes i fly rc helicopters also... and yeah would be a good project to post. well one day ill post something in there (when im not scared others will think poorly of it) :roll:

:arrow: Dan :!:

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

my ultimate desire is to build an AVR automated Heli that can use gps navigation and avoid things inteligently. all in good time.
but for now i think im going to work on a hexapod. :lol:

:arrow: Dan :!:

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

I think a problem is that there are too many ways to drive the servos... Easiest is one servo, all software.. dont need to worry about timers or interrupts... but in a real robot, the avr needs to do other stuff. It is possible to do it all using the timers and interrupts. But... what if you use both timers to run one servo, and you want to run 3 or 4? Of course, there are dedicated servo controllers that can drive 8 or 16 or 32 servos with a dedicated avr (I guess a running hexapod with 6 legs with 3 servos per leg would have 18 servos working out simultaneously!). I have done one servo using timer1 channel A output compare to interrupt every 20ms, and timer1 channel B output compare to interrupt at 1-2ms. Turn the pulse on in the 20ms handler, turn it off in the 2ms handler.

Imagecraft compiler user

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

JChristoff
Illinois

Last Edited: Thu. Jan 6, 2011 - 06:23 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks Bob -good point, a servo controller is probably the way to go. i have one that controls 8 servos.. however i thought id try and drive a servo via the timer/counter. u may also be able to help me with this while were talking robots, do u need to perform an ADC to use an ultrasonic ranger? guess it depends also whether you are using just the sonic modules or one on a pcb thats preprogrammed

:arrow: Dan :!:

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

did i just answer my own question :roll:

:arrow: Dan :!:

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

There've been posts about ultrasonic rangers on here before some have a simple (non ADC) interface where you just trigger them then switch the line ot be an input and measure the time until an echo pulse is given. That time relates to the distance. That just uses a single, simple IO line on your AVR.

Cliff

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

that dosent sound too hard. so just use a measurement u know is acurate to do the first echo then work from there.

:arrow: Dan :!:

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

Well first find your ultrasonic transducer!

I'm not saying they ALL work like that but there's been previous questions here on Freaks where there were SOME that did work like that.

I imagine a search on the word "ultrasonic" will reap benefits!

Cliff

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

Blimey, search is good isn't it? I had no problems finding the exact thread I was thinking of:

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

It seems there are a family of SRFxx transducers. (at least both SRF04 and SRF08)

Cliff

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

Evidently the little Straight-Line ultrasonic range finders use the speaker as the mic. There is a little tweeter down in a cone. You can hear it tic-tic-ticing when its measuring. I guess you need at least an opamp to preamp the received signal up enough to trigger the comparator.

Imagecraft compiler user