Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

PWM on ATtiny13
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

PWM on ATtiny13

by freeat12five on Sat Aug 06, 2011 2:10 pm

I am completely new to AVR in general and the tiny13 in particular, and I am having a devil of a time getting a simple PWM demo to run on a tiny13.

Simple config


||--- Vcc
||
||
G ||----LED|>-----R|=|-----GND


The code compiles and uploads no problem using a USBtinyISP (no issues uploading a simple flashy code). I don't even see anything on PBO (I don't have a logic analyzer or oscilloscope - caveat), but I would expect to see some movement on the multimeter. I know I am missing something obvious. I have tried both source and sink (flipping the LED and sourcing at Vcc). I have tried both 5 V and 3.3 V.

Any thoughts?

thanks,

Pete

Here is the code (courtesy of avrbasiccode.wikispaces.com):

Code: Select all | TOGGLE FULL SIZE
#define F_CPU 1000000UL // Define software reference clock for delay duration
 #include <avr/io.h>
 #include <util/delay.h>
 #define LED PB0
 int i ;
 
 int main (void)
 {
 DDRB |= (1 << LED); // OC0A on PB0
 
 TCCR0A |= ((1 << COM0A1) | (1 << COM0A0) // COM0A1 - COM0A0 (Set OC0A on Compare Match, clear OC0A at TOP)
 | (1 << WGM01) | (1 << WGM00)); // WGM01 - WGM00 (set fast PWM)
 OCR0A = 0; // initialize Output Compare Register A to 0
 TCCR0B |= (1 << CS01); // Start timer at Fcpu / 256
 
 for (;;)
 {
 
 for (i = 0 ; i < 255 ; i++ ) // For loop (Up counter 0 - 255)
 {
 OCR0A = i; // Update Output Compare Register (PWM 0 - 255)
 _delay_ms(100);
 }
 
 for (i = 255 ; i > 1 ; i-- ) // For loop (down counter 255 - 0 )
 {
 OCR0A = i; // Update Output Compare Register (PWM 0 - 255)
 _delay_ms(100);
 }
 }
 }
freeat12five
 
Posts: 10
Joined: Sun May 22, 2011 10:54 am

Re: PWM on ATtiny13

by freeat12five on Sun Aug 07, 2011 10:46 am

Problem Solved. Must have been the spaghetti code that I lazily didn't clean up. At any rate, I am posting the code with a few mods.

1. LEDs saturate long before they reach the top of the loop (and it is easier to change the code than a resistor in a proof of concept :lol: )
2. The project works in both source and sink mode. The schematic below is source. In sink mode you will want to change the for loop. See the comment in the code.
3. works either 3.3 or 5v


Code: Select all | TOGGLE FULL SIZE
#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>
#define LED PB0
int i;
int main (void)
{
        DDRB |= (1 << LED); // OC0A on PB0
        TCCR0A |= ((1 << COM0A1) | (1 << COM0A0) | (1 << WGM01) | (1 << WGM00)); // COM0A1 - COM0A0 (Set OC0A on Compare Match, clear OC0A at TOP) WGM01 - WGM00 (set fast PWM)
        OCR0A = 0; // initialize Output Compare Register A to 0
        TCCR0B |= (1 << CS01); // Start timer at Fcpu / 256

        for (;;)
        {
                for (i = 0 ; i < 100 ; i++ ) // For loop (Up counter 0 - 100 .  Source Config.  Change to 155-255 in sink)
                {
                        OCR0A = i; // Update Output Compare Register (PWM 0 - 100)
                        _delay_ms(15);
                }
                for (i = 100 ; i > 0 ; i-- ) // For loop (down counter 100 - 0. Change to 155-255 in sink)
                {
                        OCR0A = i; // Update Output Compare Register (PWM 0 - 255)
                        _delay_ms(15);
                }
        }
}

tiny13pwm.png
schematic of solution
tiny13pwm.png (65.85 KiB) Viewed 3946 times
freeat12five
 
Posts: 10
Joined: Sun May 22, 2011 10:54 am

Please be positive and constructive with your questions and comments.