BIAZI.ENG.BR
SZS ez430-RF2480
1.0
ZigBee Sensor Network with synchronized time and time-stamped measurements.
<<
Programming
Electronics
Contact
License/Rights
>>
Main Page
Related Pages
Modules
Classes
Files
License
File List
File Members
but.c
Go to the documentation of this file.
1
/*
2
* but.c
3
*
4
* Date: 19/06/2014
5
* Author: Fernando Biazi Nascimento
6
* Copyright © 2014 Fernando Biazi Nascimento. All rights reserved.
7
*
8
* License of use and copy on file license.txt
9
*
10
*/
11
22
/*
23
* ======== Standard MSP430 includes ========
24
*/
25
#include <msp430.h>
26
27
/*
28
* ======== Includes ========
29
*/
30
31
#include "
but.h
"
32
#include "
nwk.h
"
33
34
/*
35
* ======== Constants ========
36
*/
37
38
/*
39
* ======== Macros ========
40
*/
41
45
#define BUT_SW0BIT BIT2
46
47
/*
48
* ======== Types ========
49
*/
50
54
typedef
enum
but_state
{
55
BUT_S_IDLE
,
56
BUT_S_PRESSED
,
57
BUT_S_DEBOUNCE
58
}
but_state
;
59
60
/*
61
* ======== Global Variables ========
62
*/
63
64
/*
65
* ======== Local Variables ========
66
*/
67
83
but_state
but_currentState
=
BUT_S_IDLE
;
87
UInt32
but_presses
;
91
UInt8
but_pendingPress
;
92
93
/*
94
* ======== Local Functions ========
95
*/
96
97
/*
98
* ================
99
*/
100
108
void
but_init
() {
109
but_presses
= 0;
110
but_pendingPress
= 0;
111
but_currentState
=
BUT_S_IDLE
;
112
P1IE &= ~
BUT_SW0BIT
;
113
P1REN |=
BUT_SW0BIT
;
114
P1OUT |=
BUT_SW0BIT
;
115
P1IES |=
BUT_SW0BIT
;
116
P1IFG &= ~
BUT_SW0BIT
;
117
P1IE |=
BUT_SW0BIT
;
118
}
119
132
UInt16
but_proccess
() {
133
134
if
(
but_presses
& 0x80000000) {
135
// count bits set on but_press
136
//eliminate MSB, if there is far 1's, some loops are avoided.
137
but_presses
^= 0x80000000;
138
but_pendingPress
= 1;
139
while
(
but_presses
!= 0) {
140
while
(!(
but_presses
& 0xFF)) {
// eliminate some loops if next 1 is far
141
but_presses
>>= 8;
142
}
143
while
(!(
but_presses
& 0x0F)) {
// eliminate some loops if next 1 is far
144
but_presses
>>= 4;
145
}
146
while
(!(
but_presses
& 0x01)) {
147
but_presses
>>= 1;
148
}
149
but_pendingPress
++;
150
but_presses
>>= 1;
151
}
152
if
(
but_pendingPress
> 3) {
153
// reset device
154
WDTCTL = 0;
155
}
else
if
(
but_pendingPress
== 2) {
156
nwk_setDeviceType
(
NWK_DEVTYPE_ROUTER
);
157
}
else
if
(
but_pendingPress
== 1) {
158
nwk_setDeviceType
(
NWK_DEVTYPE_ENDDEVICE
);
159
}
160
}
161
162
switch
(
but_currentState
) {
163
case
BUT_S_IDLE
:
164
// delay prior presses
165
but_presses
<<= 1;
166
// check again in 100ms
167
return
100;
168
case
BUT_S_PRESSED
:
169
// delay prior presses
170
but_presses
<<= 1;
171
// button pressed, register and only enable interrupt after 95ms to avoid bounce
172
but_presses
|= 0x01;
173
but_currentState
=
BUT_S_DEBOUNCE
;
174
return
95;
175
case
BUT_S_DEBOUNCE
:
176
// this happens 95ms after previous call
177
// clear flag, enable interrupt and back to idle
178
P1IFG &= ~
BUT_SW0BIT
;
179
P1IE |=
BUT_SW0BIT
;
180
but_currentState
=
BUT_S_IDLE
;
181
return
5;
182
default
:
183
but_currentState
=
BUT_S_IDLE
;
184
return
1;
185
}
186
}
187
193
void
but_clearPending
() {
194
but_pendingPress
= 0;
195
}
196
204
UInt8
but_getPending
() {
205
return
but_pendingPress
;
206
}
207
216
void
BUT_ISR
() {
217
if
(
but_currentState
==
BUT_S_IDLE
) {
218
but_currentState
=
BUT_S_PRESSED
;
219
P1IE &= ~
BUT_SW0BIT
;
// disable interrupt to avoid bounce
220
}
221
}
nwk.h
Network functions interface.
but_getPending
UInt8 but_getPending()
This function get number of button presses on the time window.
Definition:
but.c:204
NWK_DEVTYPE_ROUTER
Router.
Definition:
nwk.h:218
but_currentState
but_state but_currentState
Current state of the button control.
Definition:
but.c:83
but_presses
UInt32 but_presses
Button presses over time, one bit for each press, shift left over time.
Definition:
but.c:87
BUT_ISR
void BUT_ISR()
Button Interrupt Service Request on port 1.
Definition:
but.c:216
nwk_setDeviceType
void nwk_setDeviceType(nwk_devType newType)
Set device type.
Definition:
nwk.c:516
but_pendingPress
UInt8 but_pendingPress
Number of button presses, it is cleared by but_clearPending.
Definition:
but.c:91
but_state
but_state
States for the button control.
Definition:
but.c:54
BUT_S_PRESSED
Button was pressed.
Definition:
but.c:56
BUT_S_DEBOUNCE
Timeout to avoid bounce counting.
Definition:
but.c:57
but_proccess
UInt16 but_proccess()
This function processes the tasks of the button control.
Definition:
but.c:132
NWK_DEVTYPE_ENDDEVICE
End device.
Definition:
nwk.h:219
BUT_SW0BIT
#define BUT_SW0BIT
Bit on with switch 0 is wired, port 1.2.
Definition:
but.c:45
but_init
void but_init()
This function initializes the button control variables.
Definition:
but.c:108
but_clearPending
void but_clearPending()
This function clear pending button presses.
Definition:
but.c:193
BUT_S_IDLE
Waiting for button press.
Definition:
but.c:55
but.h
Button interface.
Local:
Root
/
UPM
/
Master Tesis
/
SZS, PTP and Program
/
but.c