Я изучаю программирование AVR через книгу.В которой есть программа для реализации ШИМ на любом выводе
Это программа
// 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
Затем я изменил код так, чтобы ШИМ только с 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
Почему?