Skip to content

Commit 7f51b5a

Browse files
committed
first commit
0 parents  commit 7f51b5a

File tree

6 files changed

+176
-0
lines changed

6 files changed

+176
-0
lines changed

Makefile

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CC=msp430-gcc
2+
CFLAGS=-O0 -Wall -g -mmcu=msp430x2012
3+
4+
#OBJS=main.o
5+
6+
7+
all: $(OBJS)
8+
$(CC) $(CFLAGS) -o main.elf $(OBJS)
9+
10+
%.o: %.c
11+
$(CC) $(CFLAGS) -c $<
12+
13+
clean:
14+
rm -fr main.elf *.o

README

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
samples for msp430 lauchpad
2+
Code licensed under GPLv2

blink-leds.c

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <msp430x20x2.h>
2+
3+
#define REDLED BIT0
4+
#define GREENLED BIT6
5+
#define DELAY 32000
6+
7+
int main(void) {
8+
volatile unsigned int i = DELAY; // Avoid compiler optimization to
9+
// allow using counter as busy
10+
// wait loop
11+
12+
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
13+
P1DIR |= REDLED; // Set P1.0 to output direction
14+
P1DIR |= GREENLED; // Set P1.6 to output direction
15+
16+
P1OUT = REDLED; // Turn on REDLED
17+
18+
for(;;) {
19+
i = DELAY;
20+
21+
P1OUT ^= REDLED; // Switch state of REDLED
22+
P1OUT ^= GREENLED; // Switch state of GREENLED
23+
24+
do // Busy wait loop for delay
25+
i--;
26+
while (i > 0);
27+
}
28+
}

blink-leds2.c

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <msp430x20x2.h>
2+
3+
#define REDLED BIT0
4+
#define GREENLED BIT6
5+
#define DELAY 32000
6+
7+
void delay(void);
8+
void init_port1(void);
9+
void switch_state(void);
10+
11+
int main(void) {
12+
13+
init_port1();
14+
15+
P1OUT = REDLED; // Turn on REDLED
16+
17+
for(;;) {
18+
switch_state();
19+
delay();
20+
}
21+
}
22+
23+
void delay(void)
24+
{
25+
volatile unsigned int i = 0; // Avoid compiler optimization to
26+
// allow using counter as busy
27+
// wait loop
28+
i = DELAY;
29+
30+
do // Busy wait loop for delay
31+
i--;
32+
while (i > 0);
33+
}
34+
35+
void init_port1(void)
36+
{
37+
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
38+
P1DIR |= REDLED; // Set P1.0 to output direction
39+
P1DIR |= GREENLED; // Set P1.6 to output direction
40+
}
41+
42+
void switch_state(void)
43+
{
44+
P1OUT ^= REDLED; // Switch state of REDLED
45+
P1OUT ^= GREENLED; // Switch state of GREENLED
46+
}

button.c

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <msp430x20x2.h>
2+
3+
#define REDLED BIT0
4+
#define GREENLED BIT6
5+
#define BUTTON BIT3
6+
7+
int main(void) {
8+
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
9+
P1DIR |= REDLED; // Set P1.0 to output direction
10+
P1DIR |= GREENLED; // Set P1.6 to output direction
11+
12+
P1OUT = 0x00; // Turn off both LEDS
13+
14+
for(;;) {
15+
if ((P1IN & BUTTON) == BUTTON)
16+
P1OUT = REDLED; // BUTTON RELEASED
17+
else
18+
P1OUT = GREENLED; // BUTTON PRESSED
19+
}
20+
}

button2.c

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <msp430x20x2.h>
2+
3+
#define REDLED BIT0
4+
#define GREENLED BIT6
5+
#define BUTTON BIT3
6+
7+
//Delay is DELAY * DELAX
8+
#define DELAY 1
9+
#define DELAX 7500
10+
11+
void delay(void);
12+
void green(void);
13+
void init_port1(void);
14+
void red(void);
15+
16+
int main(void) {
17+
int button_status = 0x00; // 0x00 - released
18+
// 0x01 - pressed
19+
init_port1();
20+
21+
P1OUT = REDLED; // Turn on only REDLED
22+
23+
for(;;) {
24+
// If button is pressed
25+
if (!((P1IN & BUTTON) == BUTTON))
26+
button_status ^= 0x01; // Switch state
27+
28+
if (button_status == 0x01)
29+
green();
30+
else
31+
red();
32+
delay();
33+
}
34+
}
35+
36+
void delay(void)
37+
{
38+
volatile unsigned int i, j; // Avoid compiler optimization to
39+
// allow using counter as busy
40+
// wait loop
41+
42+
for (i = 0; i <= DELAY; i++) // Busy wait loop DELAY * DELAX
43+
for (j = 0; j <= DELAX; j++) {}
44+
}
45+
46+
void green(void)
47+
{
48+
P1OUT = GREENLED;
49+
}
50+
51+
void init_port1(void)
52+
{
53+
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
54+
55+
P1DIR = 0x00; // Set all ports to input direction
56+
// This is not needed
57+
58+
P1DIR |= REDLED; // Set P1.0 to output direction
59+
P1DIR |= GREENLED; // Set P1.6 to output direction
60+
}
61+
62+
void red(void)
63+
{
64+
P1OUT = REDLED;
65+
}
66+

0 commit comments

Comments
 (0)