| Author |
Message |
|
|
Posted: Feb 03, 2013 - 09:36 PM |
|

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
}
}
|
|
|
| |
|
|
|
|
|
Posted: Feb 03, 2013 - 11:46 PM |
|

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.
|
| |
|
|
|
|
|
Posted: Feb 04, 2013 - 02:15 AM |
|

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
|
|
|
| |
|
|
|
|
|
Posted: Feb 04, 2013 - 04:42 AM |
|

Joined: Jan 14, 2013
Posts: 9
|
|
Still nothing  |
|
|
| |
|
|
|
|
|
Posted: Feb 04, 2013 - 07:49 AM |
|


Joined: Jan 08, 2009
Posts: 1153
Location: Lund, Sweden
|
|
| After doing what? Did you make PB2 an output? |
|
|
| |
|
|
|
|
|
Posted: Feb 04, 2013 - 08:34 AM |
|

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. |
|
|
| |
|
|
|
|
|
Posted: Feb 04, 2013 - 08:25 PM |
|

Joined: Jan 14, 2013
Posts: 9
|
|
| That did it thank you now I just need to teach myself why it works lol. |
|
|
| |
|
|
|
|
|