Help me out with PWM

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Help me out with PWM

Postby soggybag » Mon Apr 06, 2009 10:33 pm

I have a simple project. It's goal is to increase and decrease the brightness of an LED using PWM. I've written a short program, which doesn't work, but I think it's in the ball park. Give me some direction, what am I missing?

Code: Select all
#include <avr/io.h>
#include <util/delay.h>

int main() {
   unit8_t brightness = 0;

   TCCR0A = 0x81;   // Timer/Counter Control Register A
   TCCR0B = 0x01;   // Timer/Counter Control Register B
   TCNT0  = 0x00;   // Timer/Counter Register
   OCR0A  = 0x12;   // Output Compare Register A
   OCR0B  = 0x00;   // Output Compare Register B
   
   DDRB   = 0x08;   // Set PB4 (Pin 3) as output

   while( 1 ) {
      // Increase brightness from 0 to 255
      for ( brightness = 0; brightness < 255; brightness++ ) {
         OCR0A = brightness;   // Set PWM duty based on brightness value
         _delay_ms( 10 );   // Delay 10 ms
      }
      
      // Decrease brightness from 255 to 0
      for ( brightness = 255; brightness > 0; brightness-- ) {
         OCR0A = brightness;   // Set PWM duty based on brightness value
         _delay_ms( 10 );   // Delay 10 ms
      }
   }
   return 1;
}
soggybag
 
Posts: 35
Joined: Sat Mar 01, 2008 2:28 am

Re: Help me out with PWM

Postby mtbf0 » Tue Apr 07, 2009 2:50 pm

which avr are you using?

something in the comments about what you expect your timer settings to accommplish would be helpful. as would a picture of the circuit or at least a description of what's hooked up where.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1642
Joined: Fri Nov 09, 2007 11:59 pm
Location: oakland ca

Re: Help me out with PWM

Postby soggybag » Tue Apr 07, 2009 3:34 pm

Thanks for the reply. I'm using the attiny13. I have an LED connected to Pin3, then to ground using a 470 ohm resistor. The LED should fade up to full brightness then fade out, and repeat.

I'm really just trying to test the PWM. I want to be able to use it another project. So I'm just trying to get the basics down here. I'm probably not using the optimal resistor, and my code is not optimized. I'm just hoping to get the PWM changing the brightness of the LED. Once I have that working it will give me an understanding of how to use PWM.
soggybag
 
Posts: 35
Joined: Sat Mar 01, 2008 2:28 am

Re: Help me out with PWM

Postby mtbf0 » Tue Apr 07, 2009 4:20 pm

looks like you want pin 5, i.e. PB0, i.e. OC0A.

your code puts the timer in mode 1, phase correct pwm mode. clear OC0A on match when upcounting, set on match when downcounting. prescaler is set to 1. this sort of information is useful in the comments. we know the register names.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1642
Joined: Fri Nov 09, 2007 11:59 pm
Location: oakland ca

Re: Help me out with PWM

Postby soggybag » Sun Apr 12, 2009 11:25 pm

Success! Thanks for all of the help.
soggybag
 
Posts: 35
Joined: Sat Mar 01, 2008 2:28 am


Return to Microcontrollers

Who is online

Users browsing this forum: Majestic-12 [Bot] and 1 guest

Stuff to buy from the Adafruit store and links to product documentation!


New Products [105]

Raspberry Pi[80]
 
FLORA[23]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[11]
Arduino[60]
 
NETduino[14]
 
BeagleBone[24]
 
Android[6]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[33]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[8]


 
Breakout Boards[33]
LCDs & Displays[48]
Components & Parts[69]
Batteries & Power[49]
EL Wire/Tape/Panel[52]
LEDs[108]
 
Wireless[14]
Cables[60]
 
Lasers[6]
Sensors/Parts[145]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[69]
 
iDevices[13]
Tools[71]
 
Wearables[39]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[24]


 
Stickers[41]
 
Skill badges[55]
 
Books[25]
 
Circuit Playground[7]
 
Gift Certificates[4]