Why I can't output a PWM signal using an ATTiny84

> Coding, hacking, computer graphics, game dev, and such...
Fankun
Posts: 1
Joined: Mon Apr 09, 2018 7:27 am

Why I can't output a PWM signal using an ATTiny84

Post by Fankun »

I've recently been moving from Arduino environments to AVR, and I'm programming a Tiny84 with the Sparkfun USBTiny programmer and Atmel Studio 7. For the life of me, I cannot figure out why I can't output a PWM signal.

I've tried so many different example codes, scoured the datasheet and set my own TCCR0A and TCCR0B bits to set the modes, but every program so far has either a) not lit the LED at all or b) lit the LED at full brightness, ie does not correlate to OCR0A at all. The LED MOSFET is on PB2 (OCR0A, right?) and works fine with a simple blink function. The code I'm trying currently: (this one outputs full brightness all the time. I've even tested with a normal 5V led on the output to make sure it wasn't the MOSFET having an extremely slow recovery time)

Code: Select all

#ifndef F_CPU
#define F_CPU 8000000UL // 8 MHz clock speed
#endif

#include <avr/io.h>
//#include <avr/interrupt.h>
#include <util/delay.h>


int main(void)
{   
    DDRB |= 1 << PB2;
    TCCR0A |= (1<<WGM00)|(1<<COM0A1);
    TCCR0B |= (1<<WGM02)|(1<<CS00);

    while(1){
        for (uint8_t i = 0; i < 255; i++) {
            OCR0A = i;
            _delay_ms(4);
        }

        for (uint8_t i = 255; i >= 0; i--) {
            OCR0A = i;
            _delay_ms(4);
        }
    }
}
As I understand, this should be phase correct PWM, clear on OCR0A match, and no pre-scaler

And the (full) schematic:
Image

I'm attempting to put PWM on the White control MOSFET, which is driving a small section of 12V LED strip. MOSFETs are NVR4501NT1G n-channel

This is on a custom-made board which had the problem of a broken ground plane where I haphazardly placed mousebites, so I have two bodge wires to fix that issue.

Any help would be greatly appreciated; even just an example of what should work would let me know whether this is a software issue or another problem with my board.
User avatar
fips
Site Admin
Posts: 166
Joined: Wed Nov 12, 2008 9:49 pm
Location: Prague
Contact:

Re: Why I can't output a PWM signal using an ATTiny84

Post by fips »

Sorry, I've never played with PWM modes on ATtiny, so I can't help much. However, after a bit of research, I've come across this page:

ATtiny2313 PWM – Pulse Width Modulation

It shows how to implement LED fading on ATtiny2313 (which I believe is similar to ATtiny84) using PWM.

BTW, I don't think it's related to the problem, but in the second for cycle, you will end up in an infinite loop due to the unsigned integer wrap-around. It should be rewritten, for example like this:

Code: Select all

for (uint8_t i = 255; i-- > 0;) {
    OCR0A = i;
    _delay_ms(4);
}
AlieLeite
Posts: 1
Joined: Tue Sep 25, 2018 5:05 pm

Re: Why I can't output a PWM signal using an ATTiny84

Post by AlieLeite »

Hi...That AVR model series is pretty new. On the surface, 16-bit timers 1 and 2 look pretty much like any other "normal" AVR8 16-bit timer. Same modes, capabilities.
The only addition I see at first glance is that instead of using the designated pin on the AVR for e.g. OC1A, you have the ability to map it to one of four possibilities using the TOCP registers.

pcb assembly process
ganxtr
Posts: 2
Joined: Wed Jan 14, 2015 7:15 pm

Re: Why I can't output a PWM signal using an ATTiny84

Post by ganxtr »

I haven't used the ATTiny84.

The “on time” of the PWM signal, controls the brightness of the LED. All you have to do is increase it in order to increase the brightness of the LED.
The Arduino (has Atmega328p in it) code, for changing the brightness of the LED using PWM, is available in the internet. Google it.

Learn to code. It is not very difficult.

________________________________
Fold-able mobile is future
Post Reply