-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bs81x.c
91 lines (77 loc) · 2.41 KB
/
Bs81x.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "gpio.h"
#include "Bs81x.h"
#include "bs81x_Conf.h"
#if (_BS81X_USE_FREERTOS!=0)
osThreadId Bs81xHandle;
void Task_Bs81x(void const * argument);
#endif
Bs81x_t Bs81x;
//###################################################################################################################
uint16_t Bs81x_ReadData(void)
{
uint16_t Bs81X_Data=0;
if(HAL_GPIO_ReadPin(_BS81X_DATA_GPIO,_BS81X_DATA_PIN)==GPIO_PIN_SET)
return 0xFFFF;
HAL_GPIO_WritePin(_BS81X_CLOCK_GPIO,_BS81X_CLOCK_PIN,GPIO_PIN_RESET);
_BS81X_DELAY(1);
for(uint8_t i=0 ; i<_BS81X_KEY*2 ; i++)
{
HAL_GPIO_WritePin(_BS81X_CLOCK_GPIO,_BS81X_CLOCK_PIN,GPIO_PIN_SET);
for(uint32_t delay=0 ; delay<SystemCoreClock/500000 ; delay++)
__NOP();
if(HAL_GPIO_ReadPin(_BS81X_DATA_GPIO,_BS81X_DATA_PIN)==GPIO_PIN_SET)
Bs81X_Data|=1<<i;
HAL_GPIO_WritePin(_BS81X_CLOCK_GPIO,_BS81X_CLOCK_PIN,GPIO_PIN_RESET);
for(uint32_t delay=0 ; delay<SystemCoreClock/500000 ; delay++)
__NOP();
}
HAL_GPIO_WritePin(_BS81X_CLOCK_GPIO,_BS81X_CLOCK_PIN,GPIO_PIN_SET);
return Bs81X_Data;
}
//###################################################################################################################
#if (_BS81X_USE_FREERTOS==0)
void Bs81x_Init(void)
{
HAL_GPIO_WritePin(_BS81X_CLOCK_GPIO,_BS81X_CLOCK_PIN,GPIO_PIN_SET);
while(HAL_GetTick() < 1000) // need 1 second for calibration
_BS81X_DELAY(1);
Bs81x.LastKey=0;
}
#else
//###################################################################################################################
void Bs81x_Init(osPriority Priority)
{
}
#endif
//###################################################################################################################
int16_t Bs81x_Scan(void)
{
uint16_t Data=Bs81x_ReadData();
#if (_BS81X_KEY==4)
if(Data==0xFFFF) // no happend
{
Bs81x.ChangeState=false;
return -1;
}
// change key
else if((Data & 0x80) == 0x80)// check stop bit
{
Data = Data & 0x0F;
Data = ~Data;
Data=Data&0x000F;
if(Data!=Bs81x.LastKey)
Bs81x.ChangeState=true;
else
Bs81x.ChangeState=false;
Bs81x_UserKeyChangeState(Data);
Bs81x.LastKey=Data;
return Data;
}
#else
if((Data & 0xA000) == 0xA000)// check stop bit
{
}
#endif
return false;
}
//###################################################################################################################