Remove use of timer 2
This commit is contained in:
parent
b3801ccf98
commit
79a9722c90
@ -1,6 +1,5 @@
|
|||||||
#include <mcs51/8052.h>
|
|
||||||
|
|
||||||
#include <arch/delay.h>
|
#include <arch/delay.h>
|
||||||
|
#include <arch/time.h>
|
||||||
#include <board/gpio.h>
|
#include <board/gpio.h>
|
||||||
#include <board/kbc.h>
|
#include <board/kbc.h>
|
||||||
#include <board/kbscan.h>
|
#include <board/kbscan.h>
|
||||||
@ -24,22 +23,30 @@ void kbscan_init(void) {
|
|||||||
KSOHGCTRL = 0xFF;
|
KSOHGCTRL = 0xFF;
|
||||||
KSOHGOEN = 0;
|
KSOHGOEN = 0;
|
||||||
KSOH2 = 0;
|
KSOH2 = 0;
|
||||||
|
|
||||||
// Make sure timer 2 is stopped
|
|
||||||
T2CON = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Debounce time in milliseconds
|
||||||
|
#define DEBOUNCE_DELAY 20
|
||||||
|
|
||||||
void kbscan_event(void) {
|
void kbscan_event(void) {
|
||||||
static uint8_t kbscan_layer = 0;
|
static uint8_t kbscan_layer = 0;
|
||||||
uint8_t layer = kbscan_layer;
|
uint8_t layer = kbscan_layer;
|
||||||
static uint8_t kbscan_last[KM_OUT] = { 0 };
|
static uint8_t kbscan_last[KM_OUT] = { 0 };
|
||||||
|
|
||||||
// If timer 2 is finished
|
static bool debounce = false;
|
||||||
if (TF2) {
|
static uint32_t debounce_time = 0;
|
||||||
// Stop timer 2 running
|
|
||||||
TR2 = 0;
|
// If debounce complete
|
||||||
// Clear timer 2 finished flag
|
if (debounce) {
|
||||||
TF2 = 0;
|
uint32_t time = time_get();
|
||||||
|
//TODO: time test with overflow
|
||||||
|
if (time < debounce_time) {
|
||||||
|
// Overflow, reset debounce_time
|
||||||
|
debounce_time = time;
|
||||||
|
} else if (time >= (debounce_time + DEBOUNCE_DELAY)) {
|
||||||
|
// Finish debounce
|
||||||
|
debounce = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
@ -79,27 +86,22 @@ void kbscan_event(void) {
|
|||||||
bool new_b = new & (1 << j);
|
bool new_b = new & (1 << j);
|
||||||
bool last_b = last & (1 << j);
|
bool last_b = last & (1 << j);
|
||||||
if (new_b != last_b) {
|
if (new_b != last_b) {
|
||||||
// If timer 2 is running
|
// If debouncing
|
||||||
if (TR2) {
|
if (debounce) {
|
||||||
// Debounce presses and releases
|
// Debounce presses and releases
|
||||||
if (new_b) {
|
if (new_b) {
|
||||||
// Restore bit, so that this press can be handled later
|
// Restore bit, so that this press can be handled later
|
||||||
new &= ~(1 << j);
|
new &= ~(1 << j);
|
||||||
// Skip processing of press
|
|
||||||
continue;
|
|
||||||
} else {
|
} else {
|
||||||
// Restore bit, so that this release can be handled later
|
// Restore bit, so that this release can be handled later
|
||||||
new |= (1 << j);
|
new |= (1 << j);
|
||||||
// Skip processing of release
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
// Skip processing of press
|
||||||
|
continue;
|
||||||
} else {
|
} else {
|
||||||
// Begin debouncing on press or release
|
// Begin debounce
|
||||||
// Run timer 2 for 20 ms
|
debounce = true;
|
||||||
// 65536-(20000 * 69 + 89)/90 = 0xC419
|
debounce_time = time_get();
|
||||||
TH2 = 0xC4;
|
|
||||||
TL2 = 0x19;
|
|
||||||
TR2 = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t key = keymap(i, j, kbscan_layer);
|
uint16_t key = keymap(i, j, kbscan_layer);
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
#include <mcs51/8052.h>
|
|
||||||
|
|
||||||
#include <arch/delay.h>
|
#include <arch/delay.h>
|
||||||
|
#include <arch/time.h>
|
||||||
#include <board/gpio.h>
|
#include <board/gpio.h>
|
||||||
#include <board/kbc.h>
|
#include <board/kbc.h>
|
||||||
#include <board/kbled.h>
|
#include <board/kbled.h>
|
||||||
@ -25,22 +24,30 @@ void kbscan_init(void) {
|
|||||||
KSOHGCTRL = 0xFF;
|
KSOHGCTRL = 0xFF;
|
||||||
KSOHGOEN = 0;
|
KSOHGOEN = 0;
|
||||||
KSOH2 = 0;
|
KSOH2 = 0;
|
||||||
|
|
||||||
// Make sure timer 2 is stopped
|
|
||||||
T2CON = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Debounce time in milliseconds
|
||||||
|
#define DEBOUNCE_DELAY 20
|
||||||
|
|
||||||
void kbscan_event(void) {
|
void kbscan_event(void) {
|
||||||
static uint8_t kbscan_layer = 0;
|
static uint8_t kbscan_layer = 0;
|
||||||
uint8_t layer = kbscan_layer;
|
uint8_t layer = kbscan_layer;
|
||||||
static uint8_t kbscan_last[KM_OUT] = { 0 };
|
static uint8_t kbscan_last[KM_OUT] = { 0 };
|
||||||
|
|
||||||
// If timer 2 is finished
|
static bool debounce = false;
|
||||||
if (TF2) {
|
static uint32_t debounce_time = 0;
|
||||||
// Stop timer 2 running
|
|
||||||
TR2 = 0;
|
// If debounce complete
|
||||||
// Clear timer 2 finished flag
|
if (debounce) {
|
||||||
TF2 = 0;
|
uint32_t time = time_get();
|
||||||
|
//TODO: time test with overflow
|
||||||
|
if (time < debounce_time) {
|
||||||
|
// Overflow, reset debounce_time
|
||||||
|
debounce_time = time;
|
||||||
|
} else if (time >= (debounce_time + DEBOUNCE_DELAY)) {
|
||||||
|
// Finish debounce
|
||||||
|
debounce = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
@ -71,27 +78,22 @@ void kbscan_event(void) {
|
|||||||
bool new_b = new & (1 << j);
|
bool new_b = new & (1 << j);
|
||||||
bool last_b = last & (1 << j);
|
bool last_b = last & (1 << j);
|
||||||
if (new_b != last_b) {
|
if (new_b != last_b) {
|
||||||
// If timer 2 is running
|
// If debouncing
|
||||||
if (TR2) {
|
if (debounce) {
|
||||||
// Debounce presses and releases
|
// Debounce presses and releases
|
||||||
if (new_b) {
|
if (new_b) {
|
||||||
// Restore bit, so that this press can be handled later
|
// Restore bit, so that this press can be handled later
|
||||||
new &= ~(1 << j);
|
new &= ~(1 << j);
|
||||||
// Skip processing of press
|
|
||||||
continue;
|
|
||||||
} else {
|
} else {
|
||||||
// Restore bit, so that this release can be handled later
|
// Restore bit, so that this release can be handled later
|
||||||
new |= (1 << j);
|
new |= (1 << j);
|
||||||
// Skip processing of release
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
// Skip processing of press
|
||||||
|
continue;
|
||||||
} else {
|
} else {
|
||||||
// Begin debouncing on press or release
|
// Begin debounce
|
||||||
// Run timer 2 for 20 ms
|
debounce = true;
|
||||||
// 65536-(20000 * 69 + 89)/90 = 0xC419
|
debounce_time = time_get();
|
||||||
TH2 = 0xC4;
|
|
||||||
TL2 = 0x19;
|
|
||||||
TR2 = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t key = keymap(i, j, kbscan_layer);
|
uint16_t key = keymap(i, j, kbscan_layer);
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
#include <mcs51/8052.h>
|
|
||||||
|
|
||||||
#include <arch/delay.h>
|
#include <arch/delay.h>
|
||||||
|
#include <arch/time.h>
|
||||||
#include <board/gpio.h>
|
#include <board/gpio.h>
|
||||||
#include <board/kbc.h>
|
#include <board/kbc.h>
|
||||||
#include <board/kbled.h>
|
#include <board/kbled.h>
|
||||||
@ -25,22 +24,30 @@ void kbscan_init(void) {
|
|||||||
KSOHGCTRL = 0xFF;
|
KSOHGCTRL = 0xFF;
|
||||||
KSOHGOEN = 0;
|
KSOHGOEN = 0;
|
||||||
KSOH2 = 0;
|
KSOH2 = 0;
|
||||||
|
|
||||||
// Make sure timer 2 is stopped
|
|
||||||
T2CON = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Debounce time in milliseconds
|
||||||
|
#define DEBOUNCE_DELAY 20
|
||||||
|
|
||||||
void kbscan_event(void) {
|
void kbscan_event(void) {
|
||||||
static uint8_t kbscan_layer = 0;
|
static uint8_t kbscan_layer = 0;
|
||||||
uint8_t layer = kbscan_layer;
|
uint8_t layer = kbscan_layer;
|
||||||
static uint8_t kbscan_last[KM_OUT] = { 0 };
|
static uint8_t kbscan_last[KM_OUT] = { 0 };
|
||||||
|
|
||||||
// If timer 2 is finished
|
static bool debounce = false;
|
||||||
if (TF2) {
|
static uint32_t debounce_time = 0;
|
||||||
// Stop timer 2 running
|
|
||||||
TR2 = 0;
|
// If debounce complete
|
||||||
// Clear timer 2 finished flag
|
if (debounce) {
|
||||||
TF2 = 0;
|
uint32_t time = time_get();
|
||||||
|
//TODO: time test with overflow
|
||||||
|
if (time < debounce_time) {
|
||||||
|
// Overflow, reset debounce_time
|
||||||
|
debounce_time = time;
|
||||||
|
} else if (time >= (debounce_time + DEBOUNCE_DELAY)) {
|
||||||
|
// Finish debounce
|
||||||
|
debounce = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
@ -71,27 +78,22 @@ void kbscan_event(void) {
|
|||||||
bool new_b = new & (1 << j);
|
bool new_b = new & (1 << j);
|
||||||
bool last_b = last & (1 << j);
|
bool last_b = last & (1 << j);
|
||||||
if (new_b != last_b) {
|
if (new_b != last_b) {
|
||||||
// If timer 2 is running
|
// If debouncing
|
||||||
if (TR2) {
|
if (debounce) {
|
||||||
// Debounce presses and releases
|
// Debounce presses and releases
|
||||||
if (new_b) {
|
if (new_b) {
|
||||||
// Restore bit, so that this press can be handled later
|
// Restore bit, so that this press can be handled later
|
||||||
new &= ~(1 << j);
|
new &= ~(1 << j);
|
||||||
// Skip processing of press
|
|
||||||
continue;
|
|
||||||
} else {
|
} else {
|
||||||
// Restore bit, so that this release can be handled later
|
// Restore bit, so that this release can be handled later
|
||||||
new |= (1 << j);
|
new |= (1 << j);
|
||||||
// Skip processing of release
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
// Skip processing of press
|
||||||
|
continue;
|
||||||
} else {
|
} else {
|
||||||
// Begin debouncing on press or release
|
// Begin debounce
|
||||||
// Run timer 2 for 20 ms
|
debounce = true;
|
||||||
// 65536-(20000 * 69 + 89)/90 = 0xC419
|
debounce_time = time_get();
|
||||||
TH2 = 0xC4;
|
|
||||||
TL2 = 0x19;
|
|
||||||
TR2 = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t key = keymap(i, j, kbscan_layer);
|
uint16_t key = keymap(i, j, kbscan_layer);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user