Generate PWM Signal with C Code?

Thread Starter

blah2222

Joined May 3, 2010
582
Hi so I'm fairly new to using MCUs but I am slowly learning and would like some insight on how I would be able to generate PWM signals for a servo that is written in C.

I have an AVR Studio 4.0, an AVR Programmer and an ATMEGA168 MCU. I don't think the code should be too difficult to interpret, but I have no idea how to make use of a 'C' file with the AVR Studio or even the MCU circuit for a servo, for that matter.

Any help is greatly appreciated!

Thank you,
JP
 

ELECTRONERD

Joined May 26, 2009
1,147
See this tutorial thread for help. Typical servo motors center at a period of 1.5mS (666.67 Hz). Any frequency greater or less than the center frequency will rotate the servo either clockwise or counter-clockwise. I think you should be able to get a general idea of what algorithms to implement for your code, I used C18 and it's pretty simple. You might also be aware that the frequency range at which servos operate is within the audible hearing frequencies.
 

thatoneguy

Joined Feb 19, 2009
6,359
Here (click) is a premade library for servo control from an AVR. Just add to your AVR Studio C Project, Instructions are at the link.

If you are having problems with how to program an AVR, there are several threads here that can be found with the Forum Search (or google for many offsite howtos).

I'm more of a PIC guy, so I am not much help beyond this.
 

AlexR

Joined Jan 16, 2008
732
Assuming you are running some version of Windows you will need to get a C compiler for AVR. One of the best and cheapest (it's free) is AVR-GCC for windows which you can get here http://winavr.sourceforge.net/

If you have AVR Studio installed on your PC when you install AVR-GCC it will get integrated into Studio so you will be able to write and compile C code from Studio.

The following site has a simple AVR servo application written in C http://www.digisoft.com.pk/Projects/dc-servo-motor-control

Lastly for all things AVR inducing sample code, tutorials and forums I recommend the AVR Freaks site at http://www.avrfreaks.net/
 

Thread Starter

blah2222

Joined May 3, 2010
582
Thank you again for the replies. They are both insightful but still somewhat difficult for me to put the whole picture together.

I guess my main issue, is: How do I actually wire this circuit?

I have my AVRISPmk2 Programmer, ATMEGA168V MCU, and Hitec HS-311 Servo, but how do they all go together for instance, for the PWM file that "thatoneguy" kindly posted above? GND and VCC connections are obvious, but after reading the datasheets for both the programmer/mcu I am stumped.

I can get AVR Studio up and running but I have no clue how to wire this guy up.

The Programmer has 6 holes:
1 - MISO
2 - VCC
3 - SCK
4 - MOSI
5 - RESET
6 - GND

They all have to be plugged in somewhere, but besides VCC/GND I don't know where they connect to on the circuit.

Then comes the MCU wiring. How am I supposed to know which pins to connect (or not to connect) for this example? Then where should the servo be attached?

Again I'm sorry if I seem to be rambling on question after question but I think once I get this wiring down the first time, it will become clear to me for future projects.

Thank you again,
JP
 

hgmjr

Joined Jan 28, 2005
9,027
The signal names on the programming connector correspond to the pin names on the ATMEGA168. Consult the datasheet for the ATMEGA168 and you will spot the pins involved.

Don't forget to include a 10K pullup resistor from reset to +5V.

hgmjr
 

Thread Starter

blah2222

Joined May 3, 2010
582
Wow thank you! I set everything up but I hit my final roadblock. I am running on a Mac which makes this a little trickier. I am using VMWare with Windows 7 on it. I plug in the programmer and it is identified, the light goes red, but whenever I try to connect through AVR Studio, no dice... It says connection failed. Gah, I'm googling like crazy, but any suggestions?

Thanks again!
JP
 

thatoneguy

Joined Feb 19, 2009
6,359
Get a real computer? :p

I recall somebody here does programming from a Mac, they may be along to help with that, or search the forum again. LOTS of wisdom in the archives here. Most questions have been answered about 10 times (though not all 10 answers are the same, or correct...)

If you can get your hands on a Pentium or higher PC, which are Very cheap these days, you can load it up with Ubuntu Linux, it has an option for a Mac Like Interface, and will compile the source and program the AVR quite painlessly.

I'm not sure how VMWare on Mac works with devices, I haven't had problems with it running XP on top of Linux, but I have had problems doing it the other way around. Since Mac OS X is essentially a *nix variant, try running a virtual Ubuntu window instead of WIn 7.
 

Thread Starter

blah2222

Joined May 3, 2010
582
Get a real computer? :p

I recall somebody here does programming from a Mac, they may be along to help with that, or search the forum again. LOTS of wisdom in the archives here. Most questions have been answered about 10 times (though not all 10 answers are the same, or correct...)

If you can get your hands on a Pentium or higher PC, which are Very cheap these days, you can load it up with Ubuntu Linux, it has an option for a Mac Like Interface, and will compile the source and program the AVR quite painlessly.

I'm not sure how VMWare on Mac works with devices, I haven't had problems with it running XP on top of Linux, but I have had problems doing it the other way around. Since Mac OS X is essentially a *nix variant, try running a virtual Ubuntu window instead of WIn 7.
Yeah, I made the daring switch to an arsty computer, but I do like it. I think I'm just going to setup BOOTCAMP again.

One thing I noticed though, when the programmer is plugged in via the usb port is it supposed to have any LEDs on? My circuit is set up and connected to the programmer. When I disconnect the programmer my laptop recognizes that, and when I reconnect it it notices it and the programmer briefly flashes a green LED then off.
 

Thread Starter

blah2222

Joined May 3, 2010
582
Alright I figured out that I plugged in the programmer in backwards... lol so the green light goes on and all drivers are setup to AVR Studio. No communication problem, but still no dice on my operational circuit.

One thing that I was curious about: my MCU model is an ATMEGA168V and in AVR Studio they don't have that option of MCU, they just have 168, 168P, and 168PA. Might this be a problem, or should it just work on the 168 setting?
 

thatoneguy

Joined Feb 19, 2009
6,359
Alright I figured out that I plugged in the programmer in backwards... lol so the green light goes on and all drivers are setup to AVR Studio. No communication problem, but still no dice on my operational circuit.

One thing that I was curious about: my MCU model is an ATMEGA168V and in AVR Studio they don't have that option of MCU, they just have 168, 168P, and 168PA. Might this be a problem, or should it just work on the 168 setting?
Unsure, hgmjr should be able to answer that though. Google might have hints as well.
 

Thread Starter

blah2222

Joined May 3, 2010
582
Unsure, hgmjr should be able to answer that though. Google might have hints as well.
Huzzah! Finally got it to work. The ATMEGA168 works for the 168V model. Programmed an LED to light up, and I couldn't of been more happy!

Very well made beginner tutorial for AVRISPmkII and ATMEGA... users:
http://andersonmicro.com/2009/getting-started-with-atmel-avr-part-i/

Thanks for the help again! Finally got the hang of the circuit, now for the programming...

Anyway, going to call it a night!
 
Top