AVR PWM:OCR0B问题

时间:2018-11-24 17:24:19

标签: avr pwm

我通过一本书学习AVR编程。其中有一个程序可以在任何引脚上实现PWM

这是程序

// 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 */
    }

它工作正常,模拟器OCR0A & OCR0B中的信号在这里

然后我更改了代码,以使PWM仅用于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 */
    }

它也可以正常工作,这是信号OCR0A

然后我这次仅更改了代码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 */
    }

有点不起作用,OCR0B

为什么?

0 个答案:

没有答案