-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest.c
118 lines (100 loc) · 2.74 KB
/
test.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
/*
* test for TI LaunchPad
* @author: David Siroky ([email protected])
* @license: public domain
*/
#include <msp430.h>
#include <stdio.h>
#include <stdint.h>
#include "onewire.h"
#include "delay.h"
/***************************************************************/
int putchar(int c)
{
if (c == '\n') putchar('\r');
while (!(IFG2&UCA0TXIFG));
UCA0TXBUF = c;
return 0;
}
/***************************************************************/
void uart_setup()
{
P1SEL = BIT1 + BIT2;
P1SEL2 = BIT1 + BIT2;
P1DIR &= ~ BIT1;
P1DIR |= BIT2;
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 0x41; // 8MHz 9600
UCA0BR1 = 0x03; // 8MHz 9600
UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
UCA0CTL1 &= ~UCSWRST;
}
/***************************************************************/
void search(onewire_t *ow, uint8_t *id, int depth, int reset)
{
int i, b1, b2;
if (depth == 64)
{
// we have all 64 bit in this search branch
printf("found: ");
for (i = 0; i < 8; i++) printf("%02x", id[i]);
printf("\n");
return;
}
if (reset)
{
if (onewire_reset(ow) != 0) { printf("reset failed\n"); return; }
onewire_write_byte(ow, 0xF0); // search ROM command
// send currently recognized bits
for (i = 0; i < depth; i++)
{
b1 = onewire_read_bit(ow);
b2 = onewire_read_bit(ow);
onewire_write_bit(ow, id[i / 8] & (1 << (i % 8)));
}
}
// check another bit
b1 = onewire_read_bit(ow);
b2 = onewire_read_bit(ow);
if (b1 && b2) return; // no response to search
if (!b1 && !b2) // two devices with different bits on this position
{
// check devices with this bit = 0
onewire_write_bit(ow, 0);
id[depth / 8] &= ~(1 << (depth % 8));
search(ow, id, depth + 1, 0);
// check devices with this bit = 1
id[depth / 8] |= 1 << (depth % 8);
search(ow, id, depth + 1, 1); // different branch, reset must be issued
} else if (b1) {
// devices have 1 on this position
onewire_write_bit(ow, 1);
id[depth / 8] |= 1 << (depth % 8);
search(ow, id, depth + 1, 0);
} else if (b2) {
// devices have 0 on this position
onewire_write_bit(ow, 0);
id[depth / 8] &= ~(1 << (depth % 8));
search(ow, id, depth + 1, 0);
}
}
/***************************************************************/
int main()
{
onewire_t ow;
uint8_t id[8];
WDTCTL = WDTPW + WDTHOLD; //Stop watchdog timer
BCSCTL1 = CALBC1_8MHZ;
DCOCTL = CALDCO_8MHZ;
uart_setup();
ow.port_out = &P1OUT;
ow.port_in = &P1IN;
ow.port_ren = &P1REN;
ow.port_dir = &P1DIR;
ow.pin = BIT7;
printf("start\n");
search(&ow, id, 0, 1);
printf("done\n");
_BIS_SR(LPM0_bits + GIE);
return 0;
}