-
Notifications
You must be signed in to change notification settings - Fork 0
/
buttons.c
68 lines (56 loc) · 1.57 KB
/
buttons.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <mega8.h>
#include "buttons.h"
#define MASK_BUTTONS (1<<BUTTON_1)|(1<<BUTTON_2) //|(1<<BUTTON_3)|(1<<BUTTON_4)
#define ClearBit(reg, bit) (reg) &= (~(1<<(bit)))
#define SetBit(reg, bit) (reg) |= (1<<(bit))
#define BitIsClear(reg, bit) ((reg & (1<<(bit))) == 0)
#define BitIsSet(reg, bit) ((reg & (1<<(bit))) != 0)
volatile unsigned char pressedKey = 0;
unsigned char comp = 0;
//Èíèöèàëèçàöèÿ ïîðòîâ
void BUT_Init(void)
{
DDRX_BUTTON &= ~(MASK_BUTTONS);
PORT_BUTTON |= MASK_BUTTONS;
}
//Îïðîñ êëàâèàòóðû ñ àíòèäðåáåçãîì
void BUT_Debrief(void)
{
unsigned char key;
//ïîñëåäîâàòåëüíûé îïðîñ âûâîäîâ ìê
if (BitIsClear(PIN_BUTTON, BUTTON_1))
key = KEY_BUTTON_1;
else if (BitIsClear(PIN_BUTTON, BUTTON_2))
key = KEY_BUTTON_2;
else if (BitIsClear(PIN_BUTTON, BUTTON_3))
key = KEY_BUTTON_3;
else if (BitIsClear(PIN_BUTTON, BUTTON_4))
key = KEY_BUTTON_4;
else {
key = KEY_NULL;
}
//åñëè âî âðåìåííîé ïåðåìåííîé ÷òî-òî åñòü
if (key) {
//è åñëè êíîïêà óäåðæèâàåòñÿ äîëãî
//çàïèñàòü åå íîìåð â áóôåð
if (comp == THRESHOLD) {
comp = THRESHOLD+10;
pressedKey = key;
return;
}
else if (comp < (THRESHOLD+5)) comp++;
}
else comp=0;
}
//×òåíèå ïåðåìåííîé ñ êîäîì êíîïîê è îáíóëåíèå ïåðåìåííîé
unsigned char BUT_GetKey(void)
{
unsigned char key = pressedKey;
pressedKey = KEY_NULL;
return key;
}
//Çàïèñü êîäà êíîïêè â áóôåð
void BUT_SetKey(unsigned char key)
{
pressedKey = key;
}