Added the debouncing code

Added the debouncing code and the interrupt service routines.
This commit is contained in:
Jake Goodwin 2025-02-10 14:35:34 -08:00
parent 3d64b1aa7f
commit be2f259118
1 changed files with 62 additions and 31 deletions

View File

@ -1,10 +1,3 @@
/*
* Author: Jake G Email: jakegoodwin@gorge.works
* FileName: main.c
* Date: 2025
* Descripton: Program for Atiny13A controllers
*/
#ifndef MCU
#define MCU atiny13a
#endif
@ -13,9 +6,8 @@
#define F_CPU 4800000UL
#endif
#include "stdio.h"
#include <avr/io.h>
#include "main.h"
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include <util/delay.h>
@ -24,23 +16,17 @@
//Globals
//#############################
/* This line isn't really quite right.*/
//uint8_t saved_position EEMEM; // EEPROM storage for fader position
//Debounce check number.
#define MAX_CHECKS 10
btn_state btn1;
#define LED_PORT (1<<5)
#define LED_PIN PB5
#define BLINK_DELAY_MS 5000
/*Some variables for managing the debouncing*/
volatile uint8_t debounced_state;
volatile uint8_t db_state[MAX_CHECKS];
volatile uint8_t idx;
#define PWM_PIN1 PB0 // Pin 5 - Motor PWM 1
#define PWM_PIN2 PB1 // Pin 6 - Motor PWM 2
#define ADC_PIN PB3 // Pin 2 - Fader position reading
#define BUTTON_PIN PB4 // Pin 3 - Button input
#define LONG_PRESS_TIME 1000 // 1 second hold
#define SHORT_PRESS_TIME 50 // Debounce time
uint8_t saved_position EEMEM; // EEPROM storage for fader position
volatile uint16_t tick_count;
//#############################
//Prototypes
@ -48,7 +34,6 @@ uint8_t saved_position EEMEM; // EEPROM storage for fader position
void led_blink(void);
void setup(void);
uint16_t readADC(void);
void motorCoast(void);
void motorMove(int direction);
@ -62,6 +47,7 @@ uint8_t readButton(void);
int main(int argc, char **argv)
{
/*
while(1) {
uint16_t fader_pos = readADC() >> 2; // Scale 10-bit ADC to 8-bit (0-255)
uint8_t buttonState = readButton();
@ -82,7 +68,8 @@ int main(int argc, char **argv)
led_blink();
}
*/
return 0;
}
@ -92,6 +79,55 @@ int main(int argc, char **argv)
//#############################
void init_prog(void)
{
/*Set the debounced state to all high*/
debounced_state = 0xFF;
init_btn(&btn1, PIN_SW1, PIN_ACTIVE1);
/*Wait 5ms for pull-up resistors voltage to become stable.*/
_delay_ms(5);
/*check if the eeprom has info. */
while(! eeprom_is_ready()) {} //Blocks until eeprom is ready.
//Checks against a bit pattern we defined to represent the start of data.
if(eeprom_read_byte((uint8_t *)ROM_SP_ADR) == START_PATTERN) {
//Reads the two bytes representing the two states.
btn1.is_active = eeprom_read_byte((uint8_t *)ROM_BP1_ADR);
}
else {
//otherwise we write the init values for the start pattern and states.
eeprom_write_byte((uint8_t *)ROM_SP_ADR, START_PATTERN);
eeprom_write_byte((uint8_t *)ROM_BP1_ADR, 0x0);
}
btn1.is_pressed = ((PINB & (1<<btn1.input_pin)) == 0);
/*This is to read if the user wants to change the saved states.*/
/*Manually read the current switch state*/
/*
if(btn1.is_pressed){
btn1.is_active = ! btn1.is_active;
eeprom_write_byte((uint8_t *)ROM_BP1_ADR, btn1.is_active);
blink_bypass1();
}
if(btn2.is_pressed){
btn2.is_active = ! btn2.is_active;
eeprom_write_byte((uint8_t *)ROM_BP2_ADR, btn2.is_active);
blink_bypass2();
}
if(btn1.is_active){PORTB |= (1<<btn1.output_pin);}
if(btn2.is_active){PORTB |= (1<<btn2.output_pin);}
*/
init_timer0();
}
/*
* ############################
* DEBOUNCING CODE
@ -161,11 +197,6 @@ ISR(TIM0_OVF_vect)
btn1.pressed_ticks += 1;
}
if(btn2.timer_enabled && btn2.pressed_ticks <= UINT8_MAX){
btn2.pressed_ticks += 1;
}
/*Re-Enable global interrupts*/
sei();
}