volatile int step = 6553;
volatile bool flag_INT1 = 1;
volatile bool flag_INT2 = 1;
volatile int cat = 0;
ISR(INT1_vect)
{
TCCR2B |= (1 << CS22) | (1 << CS21) | (1 << CS20);
}
ISR(INT2_vect)
{
TCCR2B |= (1 << CS22) | (1 << CS21) | (1 << CS20);
}
int8_t EXTERNAL_IRQ_0_init()
{
EICRA = (0 << ISC11) | (1 << ISC10);
EIMSK = (1 << INT1);
EICRA = (0 << ISC21) | (1 << ISC20);
EIMSK = (1 << INT2);
}
int8_t TIMER_1_init() // шим
{
/* Enable TC1 */
PRR0 &= ~(1 << PRTIM1);
TCCR1A = (0 << COM1A1) | (0 << COM1A0) /* Normal port operation, OCA disconnected */
| (1 << COM1B1) | (0 << COM1B0) /* Clear OCB on Compare Match, set OCB on BOTTOM (non-inverting mode) */
| (1 << WGM11) | (1 << WGM10); /* TC16 Mode 15 Fast PWM */
TCCR1B = (1 << WGM13) | (1 << WGM12) /* TC16 Mode 15 Fast PWM */
| 0 << ICNC1 /* Input Capture Noise Canceler: disabled */
| 0 << ICES1 /* Input Capture Edge Select: disabled */
| (0 << CS12) | (0 << CS11) | (1 << CS10); /* IO clock divided by */
// ICR1 = 0x0; /* Input capture value, used as top counter value in some modes: 0x0 */
OCR1A = 0xffff; /* Output compare A: 0xffff длительность*/
OCR1B = 0; /* Output compare B: 0x100 ЯРКОСТЬ */
// GTCCR = 0 << TSM /* Timer/Counter Synchronization Mode: disabled */
// | 0 << PSRASY /* Prescaler Reset Timer/Counter2: disabled */
// | 0 << PSRSYNC; /* Prescaler Reset: disabled */
TIMSK1 = 1 << OCIE1B /* Output Compare B Match Interrupt Enable: enabled */
| 1 << OCIE1A /* Output Compare A Match Interrupt Enable: enabled */
| 0 << ICIE1 /* Input Capture Interrupt Enable: disabled */
| 0 << TOIE1; /* Overflow Interrupt Enable: disabled */
return 0;
}
int8_t TIMER__debounce_init()
{
/* Enable TC2 */
PRR0 &= ~(1 << PRTIM2);
// TCCR2A = (0 << COM2A1) | (0 << COM2A0) /* Normal port operation, OCA disconnected */
// | (0 << COM2B1) | (0 << COM2B0) /* Normal port operation, OCB disconnected */
// | (0 << WGM21) | (0 << WGM20); /* TC8 Mode 0 Normal */
TCCR2B = 0; /* TC8 Mode 0 Normal */
// | (0 << CS22) | (1 << CS21) | (0 << CS20); /* IO clock divided by 256 */
TIMSK2 = 0 << OCIE2B /* Output Compare B Match Interrupt Enable: disabled */
| 0 << OCIE2A /* Output Compare A Match Interrupt Enable: disabled */
| 1 << TOIE2; /* Overflow Interrupt Enable: enabled */
// OCR2A = 0x0; /* Output compare A: 0x0 */
// OCR2B = 0x0; /* Output compare B: 0x0 */
// GTCCR = 0 << TSM /* Timer/Counter Synchronization Mode: disabled */
// | 0 << PSRASY /* Prescaler Reset Timer/Counter2: disabled */
// | 0 << PSRSYNC; /* Prescaler Reset: disabled */
return 0;
}
int main(void)
{
TIMER_1_init();
TIMER__debounce_init();
EXTERNAL_IRQ_0_init();
sei();
Serial.begin(115200);
DDRB|=(1<<PB7);
while (1)
{
PORTB^=(1<<PB7);
_delay_ms(500);
}
}
ISR(TIMER1_COMPB_vect)
{
}
ISR(TIMER1_COMPA_vect)
{
Serial.print(OCR1B);
Serial.print(" ");
Serial.println(cat);
if (flag_INT2)
{
OCR1B = step * cat;
}
}
// Не понятно в чем проблема. Левая кнопка срабатывает и должна закидывать нас сюда ---|, но закидывает в нижний PIND.
ISR(TIMER2_OVF_vect) // |
{ // |
TCCR2B &= ~((1 << CS22) | (1 << CS21) | (1 << CS20)); // |
// |
if (((PIND) & (1 << PD1)) == 0)//<---------------------------------------------------
{
flag_INT1 = 0;
cat--;
}
else
{
flag_INT1 = 1;
}
if (((PIND) & (1 << PD2)) == 0)
{
flag_INT2 = 0;
cat++;
if (cat > 10)
{
cat = 10;
}
}
else
{
flag_INT2 = 1;
}
}