Skip to content

Commit

Permalink
Use timer interrupt for button state
Browse files Browse the repository at this point in the history
- Move Timer2 setup into btn_init()
- Move button polling and debouncing internal to btn.c
- Function pointer btn_report_gesture
  • Loading branch information
ivanwick committed Nov 21, 2021
1 parent c149a41 commit 4b5eb15
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 45 deletions.
1 change: 1 addition & 0 deletions bekantfirmware.X/bekant/bui.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "bui.h"
#include "bctrl.h"
#include <stdlib.h> // abs()

typedef enum {
BUI_STOP,
Expand Down
49 changes: 48 additions & 1 deletion bekantfirmware.X/btn/btn.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@


#include "btn.h"

#include <pic.h>
#include <stdbool.h> /* For true/false definition */
#include <stdint.h> /* For uint8_t definition */

// A mapping to PORTB
// Determined by PCB traces
typedef union {
PORTBbits_t PORTB;
struct {
unsigned DOWN : 1;
unsigned UP : 1;
unsigned : 6;
};
} ButtonState_t;

#define PRESSED(b) (!b)
#define RELEASED(b) (b)
#define BUTTON_CHANGE(a, b) ((a.UP != b.UP) || (a.DOWN != b.DOWN))
Expand Down Expand Up @@ -43,6 +55,8 @@ bool btn_debounce(ButtonState_t now_btn) {
}
}

bool btn_debounce(ButtonState_t now_btn);

typedef struct {
uint8_t save_hold;
INPUT_t state;
Expand Down Expand Up @@ -137,3 +151,36 @@ INPUT_t btn_gesture(ButtonState_t btn) {

return input.state;
}

void (*btn_report_gesture)(INPUT_t gesture);

void btn_timer() {
static INPUT_t last_input = INPUT_IDLE;

ButtonState_t button_state = (ButtonState_t)PORTBbits;

if (btn_debounce(button_state)) {
INPUT_t input = btn_gesture(button_state);

if (input != last_input) {
last_input = input;

btn_report_gesture(input);
}
}
}

void btn_init(void) {
// Timer2 clock input is Fosc/4 (instruction clock)
// System Fosc: 16 Mhz
// Instruction clock: Fosc / 4 = 4 Mhz
// 4 Mhz / 100 period / 10 postscaler = 4000 Hz
// 0.00025 sec
// 250 usec
T2CONbits.T2CKPS = 0b00; // Prescaler is 1
PR2bits.PR2 = 100; // Timer2 period
T2CONbits.T2OUTPS = 0b1001; // 1:10 Postscaler

T2CONbits.TMR2ON = 1; // Timer is on
PIE1bits.TMR2IE = 1; // Enable Timer2 interrupt
}
20 changes: 3 additions & 17 deletions bekantfirmware.X/btn/btn.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,7 @@

#include <pic.h>
#include <stdbool.h> /* For true/false definition */

#ifndef BTN_H
#define BTN_H

// A mapping to PORTB
// Determined by PCB traces
typedef union {
PORTBbits_t PORTB;
struct {
unsigned DOWN : 1;
unsigned UP : 1;
unsigned : 6;
};
} ButtonState_t;

bool btn_debounce(ButtonState_t now_btn);

typedef enum {
INPUT_IDLE,
INPUT_UP,
Expand All @@ -27,6 +11,8 @@ typedef enum {
INPUT_SAVE,
} INPUT_t;

INPUT_t btn_gesture(ButtonState_t btn);
void btn_init(void);
void btn_timer(void);
extern void (*btn_report_gesture)(INPUT_t gesture);

#endif /* BTN_H */
4 changes: 4 additions & 0 deletions bekantfirmware.X/interrupts.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "lin/lin_d.h"
#include "bekant/bctrl.h"
#include "btn/btn.h"

void __interrupt() isr(void)
{
Expand All @@ -14,5 +15,8 @@ void __interrupt() isr(void)
} else if (PIR3bits.TMR4IF) {
bctrl_timer();
PIR3bits.TMR4IF = false;
} else if (PIR1bits.TMR2IF) {
btn_timer();
PIR1bits.TMR2IF = false;
}
}
17 changes: 2 additions & 15 deletions bekantfirmware.X/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ void main(void) {

ANSELB = 0b00000000;
TRISB = 0b00000011;
INPUT_t last_input = INPUT_IDLE;

LATC = 1; // Enables LIN input to echo back from the bus
TRISC = 0b10000000; // C7: input (serial RX)
Expand All @@ -38,6 +37,8 @@ void main(void) {
bctrl_init();

bui_init();

btn_init();

// TEST CODE TO DEBUG IN SIMULATOR
//lin_id = 0x11;
Expand All @@ -49,20 +50,6 @@ void main(void) {
//lin_tx_frame();

while (1) {
ButtonState_t button_state = (ButtonState_t)PORTBbits;

if (btn_debounce(button_state)) {
INPUT_t input = btn_gesture(button_state);

if (input != last_input) {
last_input = input;

bui_input(input);
}
}

CLRWDT();
while (!PIR1bits.TMR2IF);
PIR1bits.TMR2IF = false;
}
}
12 changes: 0 additions & 12 deletions bekantfirmware.X/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,4 @@ void ConfigureOscillator(void)
OPTION_REGbits.TMR0SE = 1; // TMR0SE Increment on high-to-low transition on T0CKI pin
OPTION_REGbits.PSA = 0; // PSA Prescaler is assigned to the Timer0 module
OPTION_REGbits.PS = 0b010; // 010 Prescaler 1:8

// Timer2 clock input is Fosc/4 (instruction clock)
// System Fosc: 16 Mhz
// Instruction clock: Fosc / 4 = 4 Mhz
// 4 Mhz / 100 period / 10 postscaler = 4000 Hz
// 0.00025 sec
// 250 usec
PR2bits.PR2 = 100; // Timer2 period

T2CONbits.T2OUTPS = 0b1001; // 1:10 Postscaler
T2CONbits.T2CKPS = 0b00; // Prescaler is 1
T2CONbits.TMR2ON = 1; // Timer is on
}
3 changes: 3 additions & 0 deletions bekantfirmware.X/user.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "lin/lin_d.h"
#include "bekant/bctrl.h"
#include "bekant/bui.h"
#include "btn/btn.h"

/******************************************************************************/
/* User Functions */
Expand All @@ -29,6 +30,8 @@ void InitApp(void)
lin_frame_finish = bctrl_rx_lin;
// hook up BEKANT control module to BEKANT UI module
bctrl_report_pos = bui_set_pos;
// Pass button gesture to BEKANT UI input
btn_report_gesture = bui_input;

/* TODO Initialize User Ports/Peripherals/Project here */

Expand Down

0 comments on commit 4b5eb15

Please sign in to comment.