#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <string.h>
#ifndef _BV
#define _BV(bit) (1<<(bit))
#endif
#ifndef sbi
#define sbi(reg,bit) reg |= (_BV(bit))
#endif
#ifndef cbi
#define cbi(reg,bit) reg &= ~(_BV(bit))
#endif
#ifndef tbi
#define tbi(reg,bit) reg ^= (_BV(bit))
#endif
#define INPUT_1_PIN PD6
#define INPUT_2_PIN PD5
#define OUTPUT_PIN PD4
int main() {
DDRD |= (1 << OUTPUT_PIN);
DDRD &= ~(1 << INPUT_1_PIN);
DDRD &= ~(1 << INPUT_2_PIN);
PORTD |= (1 << INPUT_1_PIN);
PORTD |= (1 << INPUT_2_PIN);
int Q_state = 0;
while (1) {
// Odczytanie stanów wejść
int J_state = (PIND & (1 << INPUT_1_PIN)) == 0;
int K_state = (PIND & (1 << INPUT_2_PIN)) == 0;
// Logika przerzutnika JK
if (J_state == 0 && K_state == 0) {
} else if (J_state == 0 && K_state == 1) {
Q_state = 1;
} else if (J_state == 1 && K_state == 0) {
Q_state = 0;
} else if (J_state == 1 && K_state == 1) {
Q_state = !Q_state;
}
// Ustawienie stanu wyjścia
if (Q_state) {
PORTD |= (1 << OUTPUT_PIN);
} else {
PORTD &= ~(1 << OUTPUT_PIN);
}
}
return 0;
}