/
blink.c
163 lines (140 loc) · 4.61 KB
/
blink.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
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
//******************************************************************************
// MSP430x2xx Demo - Read button status and togle LED
//
// Description; Toggle LEDs according to button commands inside a software loop.
// ACLK = n/a, MCLK = SMCLK = default DCO
//
// MSP430x2xx
// -----------------
// /|\| XIN|-
// | | |
// --|RST XOUT|-
// | |
// | P1.0|--> RED LED
// | P1.1|--> GREEN LED
// | P1.2|--> Button switch S1
// (push=GND=logic 0!!)
//
//
// Original source: A. Dannenberg Texas Instruments, Inc January 2006
// Modified for eZ430_RF2500 LED blink demo: Gregory Doumenis,
// TEIEP, Oct. 2011, Nov. 2012
// Built with CCE for MSP430 Version: 4.2.4
//******************************************************************************
#include "msp430f2101.h"
// Exercise 1: Write delay function delay_i( unsigned int x )
void delay_i( unsigned int x ) // delay function
{
volatile unsigned int j=x; // without volatile doesn't work
do j--; // decrease j
while(j != 0); //
}
void freeze() // freeze function
{
// do{
// P1OUT ^= 0x03; // xor leds
// }while((P1IN & 0x04) != 0);
}
void blinkfast()
{
P1OUT ^= 0x03;
delay_i(25000);
}
void blinkleds(volatile unsigned int v) // blinkleds function
{
if(v==0) // if value is 0 which it is as tmp starts from 0
{
// do{
P1OUT ^= 0x03; // xor both leds
delay_i(50000); // and wait ~1 second
// }while((P1IN & 0x04) != 0);
}
else // if value is 1
{
// do{
P1OUT ^= 0x03; // xor both leds
delay_i(25000); // wait for ~half second
P1OUT ^= 0x02; // xor green led
delay_i(25000); // and wait another half second
// }while((P1IN & 0x04) != 0);
}
}
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1DIR |= 0x03; // Set P1.0 & P1.1 to output direction
P1OUT = 0x04; // Dummy, to set P1.2 pullup (S1 grounds P1.2!!)
P1REN |= 0x04; // sets P1.2 pullup
volatile unsigned int B_pressed=0; // volatile to prevent optimization -
volatile unsigned int Led_Blink_on=0;
volatile unsigned int B_sense=0;
volatile unsigned int i=0;
volatile unsigned int tmp=0;
volatile unsigned int k=25000; // ~half second time period for exercise 4
volatile unsigned int r=0; // used for button counter
// Exercise 2: Detect the presence of button and blink RED LED for ~1 sec
// Exercise 3: Start and stop (freeze) LED blink sequence
// according to button pressed. When unfreezed, Green LED blinks
// twice as fast as RED
// Exercise 4: Start and stop (freeze) RED LED blink sequence
// according to button pressed. If button is pressed twice
//(momentarily), then double the blink frequency.
for (;;)
{
B_sense = P1IN & 0x04;
/* do{ // check how many times button pressed in half second
if(B_sense == 0) // detect press of button
{
do
B_sense = P1IN & 0x04;
while(B_sense == 0);
r++; // if pressed add r + 1
}
k--;
}while((k != 0) & (r != 2)); // while half second not passed and not pressed 2 times
k=25000; // give back ~half second
if (r == 2) // if button pressed 2 times
{
do{ // not yet sure
blinkfast();
B_sense = P1IN & 0x04; // continuously check if button is pressed
}while(B_sense != 0);
r=0; // make r 0 for next counting
}*/
if (B_sense==0) //debounce
{
do
{
B_sense = P1IN & 0x04; // continuously check if button is pressed
}while(B_sense == 0); // and exit the loop when button unpressed
B_pressed ^= 1; // change the B_pressed by xoring it
if(B_pressed == 1) // if it is 1
{
Led_Blink_on = 1; // change variable value to 1
//tmp = 0;
}
else // if not
{
Led_Blink_on = 0; // change variable value to 0
tmp ^= 1; // by xoring tmp we tell blinkleds function to what frequency to blink them
}
}
else // if b_sense = 0 which it is after button is unpressed
{
if(Led_Blink_on == 1) // if led blink is on
{
do{
B_sense = P1IN & 0x04;
blinkleds(tmp); // blink leds according to tmp
}while(B_sense != 0);
}
else // or else
{
do{
B_sense = P1IN & 0x04;
freeze(); // freeze leds
}while(B_sense != 0);
}
}
}
}