Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
SittingBull
PostPosted: Feb 03, 2013 - 09:36 PM
Newbie


Joined: Jan 14, 2013
Posts: 9


Hi all, I've been trying to learn how to generate a PWM signal to control the brightness of an led. All the Examples and articles I've read online have been confusing. I finally found one that makes a little sense and tried to write my own code,but its not working. I'm expecting to see a PWM signal on PB2 but there's nothing and I've tried probing all the other pins to make sure it wasn't on a pin I wasn't expecting. What am I doing wrong?

Code:

#include <avr/io.h>


int main(void)
{
   DDRA = 0xFF; //all porta pins are outputs
   
   TCCR0A |= (0<<WGM01)|(1<<WGM00)|(1<<COM0A0)|(1<<CS00);   //set up timer0 register
   


   
    while(1)
    {
      OCR0A = 0x3C;   // set duty cycle

    }
}
 
 View user's profile Send private message  
Reply with quote Back to top
Koshchi
PostPosted: Feb 03, 2013 - 11:46 PM
10k+ Postman


Joined: Nov 17, 2004
Posts: 13839
Location: Vancouver, BC

Code:
 TCCR0A |= (0<<WGM01)|(1<<WGM00)|(1<<COM0A0)|(1<<CS00);
You want to set either COM0A1 or both COM0A1 and COM0A0.

_________________
Regards,
Steve A.

The Board helps those that help themselves.
 
 View user's profile Send private message  
Reply with quote Back to top
digitool
PostPosted: Feb 04, 2013 - 02:15 AM
Posting Freak


Joined: Jul 02, 2003
Posts: 1029
Location: Tricky Dicky City

Not a C guy but
Quote:

DDRA = 0xFF; //all porta pins are outputs

does not do anything for
Quote:

I'm expecting to see a PWM signal on PB2
 
 View user's profile Send private message  
Reply with quote Back to top
SittingBull
PostPosted: Feb 04, 2013 - 04:42 AM
Newbie


Joined: Jan 14, 2013
Posts: 9


Still nothing Sad
 
 View user's profile Send private message  
Reply with quote Back to top
snigelen
PostPosted: Feb 04, 2013 - 07:49 AM
Posting Freak


Joined: Jan 08, 2009
Posts: 1153
Location: Lund, Sweden

After doing what? Did you make PB2 an output?
 
 View user's profile Send private message  
Reply with quote Back to top
david.prentice
PostPosted: Feb 04, 2013 - 08:34 AM
10k+ Postman


Joined: Feb 12, 2005
Posts: 16304
Location: Wormshill, England

Code:

DDRB |= (1<<2);    // enable OC0A pin (PB2) as PWM output
TCCR0A = (2<<COM0A0)|(3<<WGM00);  // non-inverting,  mode #3
TCCR0B = (1<<CS00);  // F_CPU/div1

Vary OCR0A register between 0x00 and 0xFF to adjust duty-cycle.
Untested. You just need to compare those statements with the data sheet descriptions of TCCR0A, TCCR0B etc.

David.
 
 View user's profile Send private message Send e-mail  
Reply with quote Back to top
SittingBull
PostPosted: Feb 04, 2013 - 08:25 PM
Newbie


Joined: Jan 14, 2013
Posts: 9


That did it thank you now I just need to teach myself why it works lol.
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits