-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmsp430fr413x_pinosc_02.c
More file actions
270 lines (247 loc) · 11.1 KB
/
msp430fr413x_pinosc_02.c
File metadata and controls
270 lines (247 loc) · 11.1 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* --COPYRIGHT--,BSD_EX
* Copyright (c) 2014, Texas Instruments Incorporated
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Texas Instruments Incorporated nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*******************************************************************************
*
* MSP430 CODE EXAMPLE DISCLAIMER
*
* MSP430 code examples are self-contained low-level programs that typically
* demonstrate a single peripheral function or device feature in a highly
* concise manner. For this the code may rely on the device's power-on default
* register values and settings such as the clock configuration and care must
* be taken when combining code from several examples to avoid potential side
* effects. Also see www.ti.com/grace for a GUI- and www.ti.com/msp430ware
* for an API functional library-approach to peripheral configuration.
*
* --/COPYRIGHT--*/
//******************************************************************************
// MSP430FR413x Demo - Capacitive Touch, Pin Oscillator Method, 4-buttons
//
// Description: Basic 4-button input using the built-in pin oscillation feature
// on GPIO input structure. PinOsc signal feed into INCLK. WDT interval is used
// to gate the measurements. Difference in measurements indicate button touch.
// LED flash if input is touched.
//
// Input 1: LED1 (LED2 off)
// Input 2: LED2 (LED1 off)
// Input 3: Both LEDs on
// Input 4: Both LEDs flash on/off
//
// ACLK = VLO = 32kHz, MCLK = SMCLK = 1MHz DCO
//
// MSP430FR4133
// -----------------
// /|\| XIN|-
// | | |
// --|RST XOUT|-
// | |
// | P5.0|<--Capacitive Touch Input 1
// | |
// LED 2 <--|P1.2 P5.1|<--Capacitive Touch Input 2
// | |
// LED 1 <--|P1.0 P5.2|<--Capacitive Touch Input 3
// | |
// | P5.3|<--Capacitive Touch Input 4
//
// Zhu Ling
// Texas Instruments Inc.
// January 2014
// Built with IAR Embedded Workbench v5.60 & Code Composer Studio v5.5
//******************************************************************************
#include <msp430.h>
/* Define User Configuration values */
/*----------------------------------*/
/* Defines WDT SMCLK interval for sensor measurements*/
#define WDT_meas_setting (DIV_SMCLK_512)
/* Defines WDT ACLK interval for delay between measurement cycles*/
#define WDT_delay_setting (DIV_ACLK_512)
/* Sensor settings*/
#define NUM_SEN 4 // Defines number of sensors
#define KEY_LVL 220 // Defines threshold for a key press
/*Set to ~ half the max delta expected*/
/* Definitions for use with the WDT settings*/
#define DIV_ACLK_32768 (WDT_ADLY_1000) // ACLK/32768
#define DIV_ACLK_8192 (WDT_ADLY_250) // ACLK/8192
#define DIV_ACLK_512 (WDT_ADLY_16) // ACLK/512
#define DIV_ACLK_64 (WDT_ADLY_1_9) // ACLK/64
#define DIV_SMCLK_32768 (WDT_MDLY_32) // SMCLK/32768
#define DIV_SMCLK_8192 (WDT_MDLY_8) // SMCLK/8192
#define DIV_SMCLK_512 (WDT_MDLY_0_5) // SMCLK/512
#define DIV_SMCLK_64 (WDT_MDLY_0_064) // SMCLK/64
#define LED_1 (BIT0) // P1.0 LED output
#define LED_2 (BIT2) // P1.2 LED output
// Global variables for sensing
unsigned int base_cnt[NUM_SEN];
unsigned int meas_cnt[NUM_SEN];
int delta_cnt[NUM_SEN];
unsigned char key_press[NUM_SEN];
char key_pressed;
const unsigned char electrode_bit[NUM_SEN]={BIT0, BIT1, BIT2, BIT3};
/* System Routines*/
void measure_count(void); // Measures each capacitive sensor
void pulse_LED(void); // LED gradient routine
void pinosc_channel_select(unsigned char PORT, unsigned char PIN);
int main( void )
{
unsigned int i,j;
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
PM5CTL0 &= ~LOCKLPM5;
SFRIE1 |= WDTIE; // enable WDT interrupt
P1DIR = LED_1 + LED_2; // P1.0 & P1.2 = LEDs
P1OUT = LED_1 + LED_2;
P1OUT = 0x00;
__bis_SR_register(GIE); // Enable interrupts
measure_count(); // Establish baseline capacitance
for (i = 0; i<NUM_SEN; i++)
base_cnt[i] = meas_cnt[i];
for(i=15; i>0; i--) // Repeat and avg base measurement
{
measure_count();
for (j = 0; j<NUM_SEN; j++)
base_cnt[j] = (meas_cnt[j]+base_cnt[j])/2;
}
/* Main loop starts here*/
while (1)
{
j = KEY_LVL;
key_pressed = 0; // Assume no keys are pressed
measure_count(); // Measure all sensors
for (i = 0; i<NUM_SEN; i++)
{
delta_cnt[i] = base_cnt[i] - meas_cnt[i]; // Calculate delta: c_change
/* Handle baseline measurment for a base C decrease*/
if (delta_cnt[i] < 0) // If negative: result increased
{ // beyond baseline, i.e. cap dec
base_cnt[i] = (base_cnt[i]+meas_cnt[i]) >> 1; // Re-average quickly
delta_cnt[i] = 0; // Zero out for pos determination
}
if (delta_cnt[i] > j) // Determine if each key is pressed
{ // per a preset threshold
key_press[i] = 1; // Specific key pressed
j = delta_cnt[i];
key_pressed = i+1; // key pressed
}
else
key_press[i] = 0;
}
/* Delay to next sample */
WDTCTL = WDT_delay_setting; // WDT, ACLK, interval timer
/* Handle baseline measurment for a base C increase*/
if (!key_pressed) // Only adjust baseline down
{ // if no keys are touched
for (i = 0; i<NUM_SEN; i++)
base_cnt[i] = base_cnt[i] - 1; // Adjust baseline down, should be
} // slow to accomodate for genuine
pulse_LED(); // changes in sensor C
__bis_SR_register(LPM3_bits);
}
} // End Main
/* Measure count result (capacitance) of each sensor*/
/* Routine setup for four sensors, not dependent on NUM_SEN value!*/
void measure_count(void)
{
unsigned char i;
TA0CTL = TASSEL_3 + MC_2; // TACLK, cont mode
TA0CCTL1 = CM_3 + CCIS_2 + CAP; // Pos&Neg,GND,Cap
for (i = 0; i<NUM_SEN; i++)
{
/*Configure Ports for relaxation oscillator*/
pinosc_channel_select(5, electrode_bit[i]);
/*Setup Gate Timer*/
WDTCTL = WDT_meas_setting; // WDT, ACLK, interval timer
TA0CTL |= TACLR; // Clear Timer_A TAR
__bis_SR_register(LPM0_bits+GIE); // Wait for WDT interrupt
TA0CCTL1 ^= CCIS0; // Create SW capture of CCR1
meas_cnt[i] = TA0CCR1; // Save result
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
CAPSIO0CTL = 0;
}
}
void pinosc_channel_select(unsigned char PORT, unsigned char PIN)
{
CAPSIO0CTL = 0;
switch(PORT)
{
case 1: CAPSIO0CTL |= CAPSIOPOSEL0; break;
case 2: CAPSIO0CTL |= CAPSIOPOSEL1; break;
case 3: CAPSIO0CTL |= CAPSIOPOSEL0 + CAPSIOPOSEL1; break;
case 4: CAPSIO0CTL |= CAPSIOPOSEL2; break;
case 5: CAPSIO0CTL |= CAPSIOPOSEL0 + CAPSIOPOSEL2; break;
case 6: CAPSIO0CTL |= CAPSIOPOSEL1 + CAPSIOPOSEL2; break;
case 7: CAPSIO0CTL |= CAPSIOPOSEL0 + CAPSIOPOSEL1 + CAPSIOPOSEL2; break;
case 8: CAPSIO0CTL |= CAPSIOPOSEL3; break;
case 9: CAPSIO0CTL |= CAPSIOPOSEL0 + CAPSIOPOSEL3; break;
case 10: CAPSIO0CTL |= CAPSIOPOSEL1 + CAPSIOPOSEL3; break;
case 11: CAPSIO0CTL |= CAPSIOPOSEL0 + CAPSIOPOSEL1 + CAPSIOPOSEL3; break;
case 12: CAPSIO0CTL |= CAPSIOPOSEL2 + CAPSIOPOSEL3; break;
case 13: CAPSIO0CTL |= CAPSIOPOSEL0 + CAPSIOPOSEL2 + CAPSIOPOSEL3; break;
case 14: CAPSIO0CTL |= CAPSIOPOSEL1 + CAPSIOPOSEL2 + CAPSIOPOSEL3; break;
case 15: CAPSIO0CTL |= CAPSIOPOSEL0 +CAPSIOPOSEL1 + CAPSIOPOSEL2 + CAPSIOPOSEL3; break;
default: break;
}
switch(PIN)
{
case BIT0: break;
case BIT1: CAPSIO0CTL |= CAPSIOPISEL0; break;
case BIT2: CAPSIO0CTL |= CAPSIOPISEL1; break;
case BIT3: CAPSIO0CTL |= CAPSIOPISEL0 + CAPSIOPISEL1; break;
case BIT4: CAPSIO0CTL |= CAPSIOPISEL2; break;
case BIT5: CAPSIO0CTL |= CAPSIOPISEL0 + CAPSIOPISEL2; break;
case BIT6: CAPSIO0CTL |= CAPSIOPISEL1 + CAPSIOPISEL2; break;
case BIT7: CAPSIO0CTL |= CAPSIOPISEL0 + CAPSIOPISEL1 + CAPSIOPISEL2; break;
default: break;
}
CAPSIO0CTL |= CAPTIOEN;
}
void pulse_LED(void)
{
switch (key_pressed)
{
case 0: P1OUT &= ~(LED_1 + LED_2); break;
case 1: P1OUT = LED_1; break;
case 2: P1OUT = LED_2; break;
case 3: P1OUT = LED_1 + LED_2; break;
case 4: P1OUT ^= LED_1 + LED_2; break;
}
}
/* Watchdog Timer interrupt service routine*/
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=WDT_VECTOR
__interrupt void watchdog_timer(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(WDT_VECTOR))) watchdog_timer (void)
#else
#error Compiler not supported!
#endif
{
TA0CCTL1 ^= CCIS0; // Create SW capture of CCR1
__bic_SR_register_on_exit(LPM3_bits); // Exit LPM3 on reti
}