MS5534A Kommunikation Beispiel MSP430

OP #764661
Lesenswert?

Hi,

hat jemand ein Beispiel in C für eine Kommunikation vom MSP430 mit einem 
MS5534A Sensor?
Hab mich schon etwas damit beschäftigt, bräuchte aber ein Beispiel ohne 
SPI.
Das kann ich auf der vorhandenen Platine nicht nutzen.

Bis jetzt habe ich das hier, etwas angepasst aus einem Beispiel mit SPI 
hier aus dem Board.
1
//******************************************************************************
2
//* wait (Warteschleife, Zyklen)
3
//******************************************************************************
4
void wait(unsigned int i)
5
{
6
    unsigned int j;
7
    for (j = 0; j < i; j++);
8
}
9

10
//******************************************************************************
11
//* SerialSendLsbFirst (Sendet Daten zum MS5534A)
12
//******************************************************************************
13
void SerialSendLsbFirst(int pattern, int nbr_clock)
14
{
15
    int i;
16
    P2OUT &= ~SCK;
17
    wait(10);
18

19
    for (i = 0; i < nbr_clock; i++) {
20
        if (pattern & 0x01)
21
            P2OUT |= SDO;
22
        else
23
            P2OUT &= ~SDO;
24
        wait(10);
25
        P2OUT |= SCK;
26
        wait(10);
27
        P2OUT &= ~SCK;
28
        pattern = pattern >> 1;
29
    }
30
}
31

32
//******************************************************************************
33
//* reset_MS5534A (Sendet Resetsignal zum MS5534A)
34
//******************************************************************************
35
void reset_MS5534A(void)
36
{
37
    SerialSendLsbFirst(0x55, 8);        //  21-bit reset sequence 0x55 0x55 0x00
38
    SerialSendLsbFirst(0x55, 8);
39
    SerialSendLsbFirst(0x00, 5);
40
}
41

42
//******************************************************************************
43
//* SerialGet16 (MS5534A auslesen)
44
//******************************************************************************
45
long SerialGet16(void)
46
{
47
    int j;
48
    long v;
49
    v = 0;
50
    P2OUT &= ~SCK;
51
    wait(20);
52

53
    for (j = 0; j < 16; j++) {
54
        P2OUT |= SCK;
55
        wait(20);
56
        P2OUT &= ~SCK;
57
        v = v << 1;
58
        if (P2IN & SDI)
59
            v = v | 1;
60
        wait(20);
61
    }
62

63
    return (v);
64
}
65

66
//******************************************************************************
67
//* getW (MS5534A CalibrationWords auslesen)
68
//******************************************************************************
69
long getW(int index)
70
{
71
    int cmd1 = 0, cmd2 = 0;
72
    long wdata;
73
    wdata = 0;
74

75
    switch (index) {
76
    case 0:
77
        cmd1 = 0x57;
78
        cmd2 = 0x01;
79
        break;
80
    case 1:
81
        cmd1 = 0xD7;
82
        break;
83
    case 2:
84
        cmd1 = 0x37;
85
        cmd2 = 0x01;
86
        break;
87
    case 3:
88
        cmd1 = 0xB7;
89
        break;
90
    }
91
    SerialSendLsbFirst(cmd1, 8);
92
    SerialSendLsbFirst(cmd2, 5);
93
    wdata = SerialGet16();
94
    SerialSendLsbFirst(0x00, 1);
95

96
    return (wdata);
97
}
98

99
//******************************************************************************
100
//* WaitOnDoutFall (auf MS5534A ADC warten)
101
//******************************************************************************
102
long waitOnDoutFall(void)
103
{
104
    int cnt = 0;
105
    short working = 1;
106
    long error = 0;
107

108
    while (working) {
109
        working = (P2IN & SDI);
110
        cnt++;
111
        wait(10);
112
        if (cnt >= 50) {
113
            working = 0;
114
            error = 1;
115
        }
116
    }
117

118
    return (error);
119
}
120

121
//******************************************************************************
122
//* getData (Druck und Temperatur auslesen (MS5534A))
123
//******************************************************************************
124
long get_data(int reg_sel)
125
{
126
    long d1;
127
    int error, pulse = 2;
128

129
    if (reg_sel == PRESSURE) {
130
        SerialSendLsbFirst(0x2F, 8);
131
        SerialSendLsbFirst(0x00, 2);
132
    } else {
133
        SerialSendLsbFirst(0x4F, 8);
134
        SerialSendLsbFirst(0x00, 3);
135
        pulse = 1;
136
    }
137

138
    error = 0;
139
    if (!(P2IN & SDI))
140
        error = 1;
141

142
    SerialSendLsbFirst(0x00, pulse);
143

144
    if (!error)
145
        error = waitOnDoutFall();
146

147
    if (!error)
148
        d1 = SerialGet16();
149
    else
150
        d1 = 0;
151

152
    if (reg_sel == PRESSURE)
153
        SerialSendLsbFirst(0x00, 1);
154

155
    //if (error_pt != 0)
156
    //  *error_pt = error;
157

158
    return (d1);
159
}
160

161
//******************************************************************************
162
//* ConvertWtoC (4 Words => 6 CalibrationParams)
163
//******************************************************************************
164
long ConvertWtoC5534(int ix, long W1, long W2, long W3, long W4)
165
{
166
    long c = 0;
167
    long x, y;
168

169
    switch (ix) {
170
    case 0:
171
        c = (W1 >> 1) & 0x7FFF;
172
        break;
173
    case 1:
174
        x = (W3 << 6) & 0x0FC0;
175
        y = W4 & 0x003F;
176
        c = x | y;
177
        break;
178
    case 2:
179
        c = (W4 >> 6) & 0x03FF;
180
        break;
181
    case 3:
182
        c = (W3 >> 6) & 0x03FF;
183
        break;
184
    case 4:
185
        x = (W1 << 10) & 0x0400;
186
        y = (W2 >> 6) & 0x03FF;
187
        c = x | y;
188
        break;
189
    case 5:
190
        c = W2 & 0x003F;
191
        break;
192
    }
193

194
    return (c);
195
}
196

197
//******************************************************************************
198
//* read_absdruck (Liest Sensor aus)
199
//******************************************************************************
200
void read_absdruck()
201
{
202
    int i;
203

204
    reset_MS5534A();            // Initialize MS5534A
205

206
    for (i = 0; i < 4; i++)     // Get MS5534 calibration words
207
        cw[i] = getW(i);
208

209

210
    for (i = 0; i < 6; i++)     // Extract cal coefficients
211
    {
212
        fc[i] = ConvertWtoC5534(i, cw[0], cw[1], cw[2], cw[3]);
213
        // printf("%lu ", fc[i]);
214
    }
215

216
    if ((cw[0] != 0) & (cw[0] != 0xFFFF))       // Check for valid sensor
217
    {                           // otherwise return error defaults
218
        d1 = get_data(PRESSURE);        // read pressure value
219
        d2 = get_data(TEMPERATURE);     // read temperature value
220
    }
221

222
}

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren