Added debouncing section from the old code.

This commit is contained in:
Jake Goodwin 2025-02-10 14:01:57 -08:00
parent aed602668a
commit ed788a8583
1 changed files with 94 additions and 13 deletions

View File

@ -1,3 +1,18 @@
/*
* Author: Jake G Email: jakegoodwin@gorge.works
* FileName: main.c
* Date: 2025
* Descripton: Program for Atiny13A controllers
*/
#ifndef MCU
#define MCU atiny13a
#endif
#ifndef F_CPU
#define F_CPU 4800000UL
#endif
#include "stdio.h"
#include <avr/io.h>
@ -9,6 +24,10 @@
//Globals
//#############################
//Debounce check number.
#define MAX_CHECKS 10
#define LED_PORT (1<<5)
#define LED_PIN PB5
#define BLINK_DELAY_MS 5000
@ -72,19 +91,81 @@ int main(int argc, char **argv)
//FUNCTIONS
//#############################
/*
* Input: None
* Output: None
* Description: Toggles the pin for the LED indicator.
*/
void led_blink(void) {
//Set the DDR for output.
DDRB |= LED_PORT;
PORTB ^= (1<<LED_PIN);
_delay_ms(500);
PORTB ^= (1<<LED_PIN);
_delay_ms(500);
/*
* ############################
* DEBOUNCING CODE
* ############################
*/
/*
* INPUT: The port to check.
* OUTPUT: None
* DESCRIPTION: Updates the global debounced state. This function
* should be called in a ISR a set rate using the hardware timer.
*/
static inline void debounce_switch() {
uint8_t i, j;
db_state[idx] = PINB & 0xFF;
idx+=1;
j = 0xff;
/*Loop through a number of checks*/
for(i = 0; i < MAX_CHECKS; i++) {
j = j & db_state[i];
}
debounced_state = j;
if(idx >= MAX_CHECKS) {
idx = 0;
}
}
/*Setup the timer0 on the AVR*/
static inline void init_timer0() {
/*Zero out the tick_count var*/
tick_count = 0;
/*config to normal mode.*/
TCCR0A = 0x00; //stop timer
TCCR0B = 0x00; //zero timer
/*set prescaler*/
//Set to div64
TCCR0B |= (1<<CS01)|(1<<CS00);
/*Enable global interrupts*/
sei();
/*Enabling timer0 interrupt*/
TCNT0 = 0;
TIMSK0 |= (1<<TOIE0);
}
//ISR(TIMER0_OVF_vect)
/*The interrupt service routine for the timer0*/
ISR(TIM0_OVF_vect)
{
/*Disable global interrupts*/
cli();
/*Check the state of the switches*/
debounce_switch();
/*Update the tick_count*/
if(btn1.timer_enabled && btn1.pressed_ticks <= UINT8_MAX){
btn1.pressed_ticks += 1;
}
if(btn2.timer_enabled && btn2.pressed_ticks <= UINT8_MAX){
btn2.pressed_ticks += 1;
}
/*Re-Enable global interrupts*/
sei();
}