AVR PWM en cualquier pin: No funciona con OCR0B

0

Estoy aprendiendo programación AVR a través de un libro. En el que hay un programa para implementar PWM en cualquier pin

Este es el programa

// Quick and dirty demo of how to get PWM on any pin with interrupts
// ------- Preamble -------- //
#define F_CPU 1000000UL

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "pinDefines.h"
#define DELAY 3
volatile uint8_t brightnessA;
volatile uint8_t brightnessB;

// -------- Functions --------- //
static inline void initTimer0(void) {
    /* must be /64 or more for ISR timing */
    TCCR0B |= (1 << CS01) | (1 << CS00);
    /* both output compare interrupts */
    TIMSK0 |= (1 << OCIE0A);
    TIMSK0 |= (1 << OCIE0B);
    TIMSK0 |= (1 << TOIE0); /* overflow interrupt enable */
    sei();
}

ISR(TIMER0_OVF_vect) {
    LED_PORT = 0xFF;
    OCR0A = brightnessA;
    OCR0B = brightnessB;
}

ISR(TIMER0_COMPA_vect) {
    LED_PORT &= 0b11110000; // turn off low four LEDs 
}

ISR(TIMER0_COMPB_vect) {
    LED_PORT &= 0b00001111; // turn off high four LEDs 
}

int main(void) {
    // -------- Inits --------- //
    uint8_t i;
    LED_DDR = 0xff;
    initTimer0();
    // ------ Event loop ------ //
    while (1) {
        for (i = 0; i < 255; i++) {
            _delay_ms(DELAY);
            brightnessA = i;
            brightnessB = 255-i;
        }
        for (i = 254; i > 0; i--) {
            _delay_ms(DELAY);
            brightnessA = i;
            brightnessB = 255-i;
        }
        } /* End event loop */
        return (0); /* This line is never reached */
    }

Funciona bien y aquí hay una señal en el simulador OCR0A & OCR0B

Luego cambié el código para que PWM solo con OCR0A

// Quick and dirty demo of how to get PWM on any pin with interrupts
// ------- Preamble -------- //
#define F_CPU 1000000UL

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "pinDefines.h"
#define DELAY 3
volatile uint8_t brightnessA;
volatile uint8_t brightnessB;

// -------- Functions --------- //
static inline void initTimer0(void) {
    /* must be /64 or more for ISR timing */
    TCCR0B |= (1 << CS01) | (1 << CS00);
    /* both output compare interrupts */
    TIMSK0 |= (1 << OCIE0A);
    TIMSK0 |= (1 << TOIE0); /* overflow interrupt enable */
    sei();
}

ISR(TIMER0_OVF_vect) {
    LED_PORT = 0xFF;
    OCR0A = brightnessA;
}

ISR(TIMER0_COMPA_vect) {
    LED_PORT &= 0b11110000; // turn off low four LEDs 
}

int main(void) {
    // -------- Inits --------- //
    uint8_t i;
    LED_DDR = 0xff;
    initTimer0();
    // ------ Event loop ------ //
    while (1) {
        for (i = 0; i < 255; i++) {
            _delay_ms(DELAY);
            brightnessA = i;
            //brightnessB = 255-i;
        }
        for (i = 254; i > 0; i--) {
            _delay_ms(DELAY);
            brightnessA = i;
            //brightnessB = 255-i;
        }
        } /* End event loop */
        return (0); /* This line is never reached */
    }

También funciona bien, aquí está la señal OCR0A

Luego cambié el código esta vez solo OCR0B

// Quick and dirty demo of how to get PWM on any pin with interrupts
// ------- Preamble -------- //
#define F_CPU 1000000UL

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "pinDefines.h"
#define DELAY 3
volatile uint8_t brightnessA;
volatile uint8_t brightnessB;

// -------- Functions --------- //
static inline void initTimer0(void) {
    /* must be /64 or more for ISR timing */
    TCCR0B |= (1 << CS01) | (1 << CS00);
    /* both output compare interrupts */
    //TIMSK0 |= (1 << OCIE0A);
    TIMSK0 |= (1 << OCIE0B);
    TIMSK0 |= (1 << TOIE0); /* overflow interrupt enable */
    sei();
}

ISR(TIMER0_OVF_vect) {
    LED_PORT = 0xFF;
    //OCR0A = brightnessA;
    OCR0B = brightnessB;
}

/*ISR(TIMER0_COMPA_vect) {
    LED_PORT &= 0b11110000; // turn off low four LEDs 
}*/

ISR(TIMER0_COMPB_vect) {
    LED_PORT &= 0b00001111; // turn off high four LEDs 
}

int main(void) {
    // -------- Inits --------- //
    uint8_t i;
    LED_DDR = 0xff;
    initTimer0();
    // ------ Event loop ------ //
    while (1) {
        for (i = 0; i < 255; i++) {
            _delay_ms(DELAY);
            //brightnessA = i;
            brightnessB = 255-i;
        }
        for (i = 254; i > 0; i--) {
            _delay_ms(DELAY);
            //brightnessA = i;
            brightnessB = 255-i;
        }
        } /* End event loop */
        return (0); /* This line is never reached */
    }

Bit no funciona, OCR0B

¿Por qué?

    
pregunta Athul

0 respuestas

Lea otras preguntas en las etiquetas