WeatherStation/74C922.c

51 lines
1.3 KiB
C
Raw Permalink Normal View History

2023-12-03 13:37:53 +01:00
#include "config.h"
#include <avr/io.h>
#include <util/delay.h>
#include <stdbool.h>
// Pin Definitions
#define ENCODER_A_PIN PD6
#define ENCODER_B_PIN PD5
#define ENCODER_C_PIN PD4
#define ENCODER_D_PIN PD3
#define ENCODER_OE_PIN PD2
#define ENCODER_DA_PIN PD1
void prepare_interrupt() {
EICRA |= (1 << ISC00); // Any logical change triggers interrupt
EIMSK |= (1 << INT0); // INT0 external interrupt
PCICR |= (1 << PCIE0);
}
// Encoder Module
void encoder_init() {
// Set A, B, C, D, and DA pins as inputs
DDRD &= ~((1 << ENCODER_A_PIN) | (1 << ENCODER_B_PIN) | (1 << ENCODER_C_PIN) | (1 << ENCODER_D_PIN) | (1 << ENCODER_DA_PIN));
// Set OE as output
DDRD |= (1 << ENCODER_OE_PIN);
// Enable internal pull-up resistors for A, B, C, D, and OE pins
PORTD |= (1 << ENCODER_A_PIN) | (1 << ENCODER_B_PIN) | (1 << ENCODER_C_PIN) | (1 << ENCODER_D_PIN) | (1 << ENCODER_DA_PIN);
}
uint8_t encoder_read() {
uint8_t encoder_data = 0;
// Set OE (Output Enable) pin low to enable the encoder
PORTD &= ~(1 << ENCODER_OE_PIN);
_delay_us(1);
// Read the state of the encoder
encoder_data = PIND >> (ENCODER_A_PIN - 3);
encoder_data &= 0x0F;
// Set OE pin high to disable the encoder
PORTD |= (1 << ENCODER_OE_PIN);
return encoder_data;
}
bool encoder_available() {
return (PIND & (1 << ENCODER_DA_PIN));
}