Forum: Mikrocontroller und Digitale Elektronik Anbindung SHT71 an STM32F4Discovery


von (unknown) (Gast)


Lesenswert?

Hallo Leute,

Für ein Projekt habe ich die Aufgabe gefasst einen Feuchte-Temp Sensor 
von Sensirion (SHT71) an das STM32F4Discovery Board anzuhängen. Geht mal 
primär davon aus, dass ich ein totaler Noob bin und davon keinen blassen 
Schimmer habe.

Ich habe nun diverse Beispielprogramme laufen lassen und analysiert und 
danach auf Keil uVision 4 ein Programm geschrieben. Oh Wunder es tut 
nicht wie ich mir das vorstelle.

Code unten. Der Sensor wurde mit einem Pullupwiderstand an der 
Datenleitung angebunden. Ich kann kompilieren, Der Code läuft auch, 
bloss dass ich im Debugger keine Temperatur bekomme. Lustigerweise 
bekomme ich ein ACK vom Sensor (da habe ich eine rote LED leuchten 
lassen, wenn der die Datenleitung auf 0 zieht)

Für Tipps oder Hilfe wäre ich sehr dankbar, wenn mehr Infos nötig sind, 
dann liefere ich die gerne nach...
1
int main(void) {
2
  
3
  /*  Setup of the I2C Interface to SHT71 Sensor */
4
  setup_I2C();
5
  
6
   /* Initialize LEDs mounted on STM32F4-Discovery board */
7
  STM_EVAL_LEDInit(LED5);  
8
  
9
  while (1) {
10
    /*   Temperatur lesen */
11
    uint8_t mytemp[] = { 0, 0, 0};
12
    read_SHT71(TMP_ADRESS, mytemp);
13
    
14
    helptemp = mytemp[0]*256 + mytemp[1];
15
    temp = 0.01 * helptemp - 39.6;
16
    
17
    Delay(10000L);
18
    
19
  
20
    Delay(25000000L);
21
  }
22
}
23
24
void setup_I2C(void)
25
{
26
  /*  GPIO Setup  
27
      Initialise Pins PB6 and PB7*/
28
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
29
  GPIO_I2C.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
30
  GPIO_I2C.GPIO_Mode = GPIO_Mode_AF;
31
  GPIO_I2C.GPIO_OType = GPIO_OType_PP;
32
  GPIO_I2C.GPIO_Speed = GPIO_Speed_2MHz;
33
  GPIO_I2C.GPIO_PuPd = GPIO_PuPd_NOPULL;
34
  GPIO_Init(GPIOB, &GPIO_I2C);
35
}
36
37
void read_SHT71(uint8_t adr, uint8_t * data) {
38
  uint8_t i;
39
  uint8_t j;
40
  uint8_t helpdata[]= {0,0,0,0,0,0,0,0};
41
  
42
  /* Start Transmission
43
  *       ___     ___
44
  * SCL _/   \___/   \____
45
  *     ___        _____ 
46
  * SDA    \______/     \_
47
  */
48
  GPIO_SetBits(GPIOB, GPIO_Pin_6);   //SCL 1
49
  Delay(5000L);
50
  GPIO_ResetBits(GPIOB, GPIO_Pin_7); //SDA 0
51
  Delay(5000L);
52
  GPIO_ResetBits(GPIOB, GPIO_Pin_6); //SCL 0
53
  Delay(1000L);
54
  GPIO_SetBits(GPIOB, GPIO_Pin_6);   //SCL 1
55
  Delay(500L);
56
  GPIO_SetBits(GPIOB, GPIO_Pin_7);   //SDA 1
57
  Delay(500L);
58
  GPIO_ResetBits(GPIOB, GPIO_Pin_6); //SCL 0
59
  Delay(1000L);
60
  GPIO_ResetBits(GPIOB, GPIO_Pin_7); //SDA 0
61
  Delay(1000L);
62
  
63
  /* Send Adress
64
  *
65
  * Temperature = 000 0011
66
  * Humidity    = 000 0101
67
  *
68
  */
69
  GPIO_SetBits(GPIOB, GPIO_Pin_6);   //Clock
70
  Delay(125L);
71
  GPIO_ResetBits(GPIOB, GPIO_Pin_6); 
72
  Delay(125L);
73
  GPIO_SetBits(GPIOB, GPIO_Pin_6);   //Clock
74
  Delay(125L);
75
  GPIO_ResetBits(GPIOB, GPIO_Pin_6); 
76
  Delay(125L);
77
  GPIO_SetBits(GPIOB, GPIO_Pin_6);   //Clock
78
  Delay(125L);
79
  GPIO_ResetBits(GPIOB, GPIO_Pin_6); 
80
  Delay(125L);
81
  GPIO_SetBits(GPIOB, GPIO_Pin_6);   //Clock
82
  Delay(125L);
83
  GPIO_ResetBits(GPIOB, GPIO_Pin_6); 
84
  Delay(55L);
85
  
86
  //SDA 1 if Humidity measuring, 0 else
87
  if (adr == HUM_ADRESS) {
88
    GPIO_SetBits(GPIOB, GPIO_Pin_7);       
89
  }
90
  else {
91
    GPIO_ResetBits(GPIOB, GPIO_Pin_7); 
92
  }
93
  Delay(55L);
94
  
95
  GPIO_SetBits(GPIOB, GPIO_Pin_6);   //Clock
96
  Delay(125L);
97
  GPIO_ResetBits(GPIOB, GPIO_Pin_6); 
98
  Delay(55L);
99
  
100
  //SDA 1 if Temperature measuring, 0 else
101
  if (adr == TMP_ADRESS) {
102
    GPIO_SetBits(GPIOB, GPIO_Pin_7);       
103
  }
104
  else {
105
    GPIO_ResetBits(GPIOB, GPIO_Pin_7);
106
  }
107
  Delay(55L);
108
  
109
  GPIO_SetBits(GPIOB, GPIO_Pin_6);   //Clock
110
  Delay(125L);
111
  GPIO_ResetBits(GPIOB, GPIO_Pin_6);
112
  Delay(55L);
113
  
114
  //SDA 1 if Temperature or Humidity measuring, 0 else
115
  if (adr == HUM_ADRESS) {
116
    GPIO_SetBits(GPIOB, GPIO_Pin_7);   //SDA 1    
117
  }
118
  else if (adr == TMP_ADRESS) {
119
    GPIO_SetBits(GPIOB, GPIO_Pin_7);   //SDA 1    
120
  }
121
  else {
122
    GPIO_ResetBits(GPIOB, GPIO_Pin_7); //SDA 0
123
  }
124
  Delay(55L);
125
  
126
  GPIO_SetBits(GPIOB, GPIO_Pin_6);   //Clock
127
  Delay(125L);
128
  GPIO_ResetBits(GPIOB, GPIO_Pin_6);
129
  Delay(125L);
130
  
131
  // Read ACK = 0
132
  GPIO_SetBits(GPIOB, GPIO_Pin_6);   //Clock
133
  Delay(55L);
134
  if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7) == 0) {
135
    STM_EVAL_LEDOn(LED5); // Red LED if Ack, funktioniert
136
  }
137
  Delay(55L);
138
  GPIO_ResetBits(GPIOB, GPIO_Pin_6); 
139
  Delay(100000L);
140
  
141
  /*wait for sensor to pull data line low after completion of measurement */
142
  while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7) != 0) {
143
  }
144
  Delay(125L);
145
  
146
  /*
147
  * Reading the Data 
148
  * High Byte, Ack, then low Byte
149
  */
150
  
151
  for (i = 0; i < 2; i++) {
152
    for (j = 0; j < 8; j++) {
153
      GPIO_SetBits(GPIOB, GPIO_Pin_6);   //Clock
154
      Delay(55L);
155
      if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7) == 0) {
156
        helpdata[j] = 0;
157
      }
158
      else {
159
        helpdata[j] = 1;
160
      }
161
      Delay(55L);
162
      GPIO_ResetBits(GPIOB, GPIO_Pin_6); 
163
      Delay(125L);
164
      }
165
    data[i] = helpdata[0]*128+helpdata[1]*64+helpdata[2]*32+helpdata[3]*16+helpdata[4]*8+helpdata[5]*4+helpdata[6]*2+helpdata[7];
166
    // Ack to Sensor (only first time)
167
    if (i == 0) {
168
      GPIO_ResetBits(GPIOB, GPIO_Pin_7); //SDA 0
169
      GPIO_SetBits(GPIOB, GPIO_Pin_6);   //Clock
170
      Delay(125L);
171
      GPIO_ResetBits(GPIOB, GPIO_Pin_6); 
172
      GPIO_SetBits(GPIOB, GPIO_Pin_7);   //SDA 1
173
      Delay(125L);
174
    }
175
  }
176
}

von (prx) A. K. (prx)


Lesenswert?

Die Initialisierungssequenz aus 10 Takten fehlt.

Beide Pins werden als PushPull Ausgänge definiert. Die Datenleitung ist 
aber bidirektional und muss als Open Drain definiert werden.

Die Pins werden als AF definiert, aber korrekterweise als programmierte 
I/Os genutzt. AF verbindet statt dessen mit dem I2C Controller, folglich 
passiert überhaupt nichts.

Siehe Beitrag "Sensirion SHT11 Code".

von erhardd (Gast)


Lesenswert?

...du setzt und resetzt die Pins als GPIO's:
wäre es nicht günstiger, sie auch so zu config. ?
void I2C_Configuration(void)
{

  GPIO_InitTypeDef  GPIO_InitStructure;
  /* Configure SCCB pins: PE4->SCL and PE5->SDA */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_4 | GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  //     4  50
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_Init(GPIOE, &GPIO_InitStructure);
}

von erhardd (Gast)


Lesenswert?

...ups, er war schneller..

von erhardd (Gast)


Lesenswert?

...und es ist SCCB;

von (unknown) (Gast)


Lesenswert?

Danke erhardd und A.K. Ich bin dabei, den Code anzupassen und eure 
Inputs einzupflegen.

Kurze Rückfrage:

Ich definiere die GPIO Pins PB6 und PB7 als OUT, Open Drain und NoPull. 
Wenn ich die Startsequenz im Debugger laufen lasse kann ich schön sehen, 
wie die Spannung rauf und runtergeht an den zwei PINS. (jaja KO wär 
schön)

Muss ich den Datenpin PB7 umdefinieren (zu IN), wenn ich vom Sensor die 
Messwerte empfangen will (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7)), 
oder geht das auch bei einem auf GPIO_Mode_OUT gesetzten Pin?

Danke echt nochmals, ihr rettet mir hier wirklich den Tag.

von (prx) A. K. (prx)


Lesenswert?

Reto Lüchinger schrieb:
> Muss ich den Datenpin PB7 umdefinieren (zu IN), wenn ich vom Sensor die
> Messwerte empfangen will

Nein. es reicht aus, den Ausgang auf high zu setzen, damit der Open 
Drain Treiber inaktiv ist.

von erhardd (Gast)


Lesenswert?

Reto Lüchinger schrieb:
> Muss ich den Datenpin PB7 umdefinieren (zu IN), wenn ich vom Sensor die
> Messwerte empfangen will (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7)),
> oder geht das auch bei einem auf GPIO_Mode_OUT gesetzten Pin?

...aus meiner Kameraanwendung für das F4-Discovery:
static int I2C_ReceiveByte(void)
{
    uint8_t i=8;
    uint8_t ReceiveByte=0;

    SDA_H;
    while(i--)
    {
      ReceiveByte<<=1;
      SCL_L;
      I2C_delay();
    SCL_H;
      I2C_delay();
      if(SDA_read)
      {
        ReceiveByte|=0x01;
      }
    }
    SCL_L;
    return ReceiveByte;
}
- und aus der SCCB.h:
/* Private define 
------------------------------------------------------------*/
#define SCL_H         GPIO_SetBits(GPIOE , GPIO_Pin_4)   /* 
GPIOB->BSRRL = GPIO_Pin_8  */
#define SCL_L         GPIO_ResetBits(GPIOE , GPIO_Pin_4)  /* 
GPIO_ResetBits(GPIOB , GPIO_Pin_10) */

#define SDA_H         GPIO_SetBits(GPIOE , GPIO_Pin_5) /* 
GPIO_SetBits(GPIOB , GPIO_Pin_11)   */
#define SDA_L         GPIO_ResetBits(GPIOE , GPIO_Pin_5)   /* 
GPIO_ResetBits(GPIOB , GPIO_Pin_11) */

#define SCL_read      GPIOE->IDR  & GPIO_Pin_4   /* 
GPIO_ReadInputDataBit(GPIOB , GPIO_Pin_10) */
#define SDA_read      GPIOE->IDR  & GPIO_Pin_5 /* 
GPIO_ReadInputDataBit(GPIOB , GPIO_Pin_11) */

#define ADDR_OV7725   0x42 // 2a

von erhardd (Gast)


Lesenswert?

...lies mal nach RM0090 6.3.10 Output configuration:

Zitat:"When the I/O port is programmed as output:
● The output buffer is enabled:
– Open drain mode: A “0” in the Output register activates the N-MOS 
whereas a “1”
in the Output register leaves the port in Hi-Z (the P-MOS is never 
activated)
...
...
● The data present on the I/O pin are sampled into the input data 
register every AHB1
clock cycle
● A read access to the input data register gets the I/O state
" /Zitat

von Andreas (Gast)


Lesenswert?


von Daniel B. (dbuergin)


Lesenswert?

So wie ich das sehe, verwendest Du das I2C subsystem oder ?

Bin nicht sicher, ob das wirklich gut kommt. Die älteren SHT Sensoren
sht11, sht15, sht71 und sht75 sind nicht wirklich I2C Sensoren.
Erst die neuen Dinger sht21 und sht25 haben offizielle I2C 
Schnittstellen.

Ich würde ganz normale Pins nehmen, und eines der vorhandenen Beispiele
hier im Forum anpassen.

z.B. Beitrag "Tempertur/Feuchte Display/Logger mit ATMega128  SHT75  SD-Karte" ;-)

Daniel

von (unknown) (Gast)


Lesenswert?

Danke Leute,

Ich habs hinbekommen. Muss mal echt festhalten, dass das hier ein 
verd***t geiles Forum ist. Alle eure Hinweise waren nützlich für mich! 
Falls jemand mit einem ähnlichen Problem durchkommt, häng ich mal noch 
den Code der main Routine an.

Teile darin sind aus dem Blinky- Beispiel, ein Teil aus dem EXTI - 
Beispiel, welches mit dem STM32F4Discovery Board geliefert wird. Ich 
benötige die Standard Peripheral Libraries (v.a. rcc, exti und gpio) und 
stm32f4_discovery.c.
1
/**
2
  ******************************************************************************
3
  * @file    main.c 
4
  * @author  Reto Lüchinger
5
  * @version V0.0.0
6
  * @date    30-May-2012
7
  * @brief   Main Programm, Initialisieren des STM32F4Discovery Boards
8
  ******************************************************************************
9
* Dieses Programm liest einen SHT71 FT Sensor aus und lässt LED blinken,       
10
* Am Pushbutton hängt ein Interrupt
11
*
12
*
13
******************************************************************************  
14
  */ 
15
16
/* Includes ------------------------------------------------------------------*/
17
#include "stm32f4xx.h"
18
#include "stm32f4xx_conf.h"
19
#include "stm32f4_discovery.h"
20
/* Private typedef -----------------------------------------------------------*/
21
GPIO_InitTypeDef  GPIO_InitStructure, GPIO_SHT;
22
EXTI_InitTypeDef   EXTI_InitStructure;
23
/* Private define ------------------------------------------------------------*/
24
#define TMP_ADRESS 0x03
25
#define HUM_ADRESS 0x05
26
#define SDA_read      GPIOB->IDR  & GPIO_Pin_7 /*GPIO_ReadInputDataBit(GPIOB , GPIO_Pin_7) */
27
/* Private macro -------------------------------------------------------------*/
28
/* Private variables ---------------------------------------------------------*/
29
uint8_t i;
30
/* Private function prototypes -----------------------------------------------*/
31
void setup_I2C(void);
32
void EXTILine0_Config(void);
33
int16_t read_SHT71(uint8_t adr);
34
void Delay(__IO uint32_t nCount);
35
void nop1(void);
36
static void scl_pulse(void);
37
static void  scl_hi(void);
38
static void  scl_lo(void);
39
static void  sda_hi(void); 
40
static void  sda_lo(void);
41
static uint8_t  sda_val(void);
42
static uint8_t crc_value;
43
static uint8_t send(uint16_t b);
44
static uint8_t recv_data(void);
45
static uint8_t recv_crc(void);
46
static void start_SHT(void);
47
uint8_t sht71_start_temp(void);
48
uint8_t sht71_start_humid(void);
49
uint8_t sht71_ready(void);
50
static int16_t result(void);
51
52
/* Private functions ---------------------------------------------------------*/
53
54
55
void Delay(__IO uint32_t nCount) { while(nCount--) {  } }
56
void nop1(void){ Delay(5000L); }
57
58
/**
59
  * @brief  Main program
60
  * @param  None
61
  * @retval None
62
  */
63
int main(void) {
64
  float acttemp, acthum, humlin;
65
  int16_t mytemp;
66
  int16_t myhum;
67
  /*  Setup of the I2C Interface to SHT71 Sensor */
68
  setup_I2C();
69
  
70
   /* Initialize LEDs mounted on STM32F4-Discovery board */
71
  STM_EVAL_LEDInit(LED3);
72
  STM_EVAL_LEDInit(LED4);
73
  STM_EVAL_LEDInit(LED5);
74
  STM_EVAL_LEDInit(LED6);
75
  
76
  /* Configure EXTI Line0 (connected to PA0 pin = Pushbutton) in interrupt mode */
77
  EXTILine0_Config();
78
79
  /* Generate software interrupt: simulate a rising edge applied on EXTI0 line */
80
  EXTI_GenerateSWInterrupt(EXTI_Line0);
81
 
82
  
83
  
84
  while (1) {
85
    /*   Feuchte und Temperatur lesen */
86
    mytemp = read_SHT71(TMP_ADRESS);
87
    
88
    acttemp = 0.01 * mytemp - 39.6; 
89
    
90
    myhum =  read_SHT71(HUM_ADRESS);
91
    
92
    humlin = 0.0367 * myhum - 2.0468 - 0.0000015955*myhum*myhum;
93
    acthum = (acttemp-25)*(0.01+0.00008*myhum)+humlin;
94
    acthum = acthum + 1 -1; // Debugger zeigt Wert nur an, wenn er verwendet wird
95
    
96
    
97
    
98
    
99
    /* lässt LEDs rotieren
100
    *
101
    * Dies soll Basis werden für Ampelsteuerung
102
    */
103
    
104
    /* Set Green LED */
105
    STM_EVAL_LEDOn(LED4);
106
    /* Reset Orange, Red, and Blue LED*/
107
    STM_EVAL_LEDOff(LED3);
108
    STM_EVAL_LEDOff(LED5);
109
    STM_EVAL_LEDOff(LED6);
110
    Delay(25000000L);
111
 
112
    /* Set Orange LED */
113
    STM_EVAL_LEDOn(LED3);
114
    /* Reset Green, Red, and Blue LED*/
115
    STM_EVAL_LEDOff(LED4);
116
    STM_EVAL_LEDOff(LED5);
117
    STM_EVAL_LEDOff(LED6);
118
    Delay(25000000L);
119
 
120
    /* Set Red LED */
121
    STM_EVAL_LEDOn(LED5);
122
    /* Reset Green, Orange, and Blue LED*/
123
    STM_EVAL_LEDOff(LED4);
124
    STM_EVAL_LEDOff(LED3);
125
    STM_EVAL_LEDOff(LED6);
126
    Delay(25000000L);
127
 
128
    /* Set Blue LED */
129
    STM_EVAL_LEDOn(LED6);
130
    /* Reset Green, Orange and Red LED*/
131
    STM_EVAL_LEDOff(LED4);
132
    STM_EVAL_LEDOff(LED3);
133
    STM_EVAL_LEDOff(LED5);
134
    Delay(25000000L);
135
  }
136
}
137
138
void setup_I2C(void)
139
{
140
  /*  GPIO Setup  
141
      Initialise Pins PB6 and PB7*/
142
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
143
  GPIO_SHT.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
144
  GPIO_SHT.GPIO_Mode = GPIO_Mode_OUT;
145
  GPIO_SHT.GPIO_OType = GPIO_OType_OD;
146
  GPIO_SHT.GPIO_Speed = GPIO_Speed_2MHz;
147
  GPIO_SHT.GPIO_PuPd = GPIO_PuPd_NOPULL;
148
  GPIO_Init(GPIOB, &GPIO_SHT);
149
  
150
  sda_hi(); scl_lo(); // Initial conditions
151
}
152
153
static void scl_pulse(void)    { scl_hi();  nop1(); scl_lo();}
154
static void  scl_hi(void)      { GPIO_SetBits(GPIOB, GPIO_Pin_6); }
155
static void  scl_lo(void)      { GPIO_ResetBits(GPIOB, GPIO_Pin_6); }
156
static void  sda_hi(void)      { GPIO_SetBits(GPIOB, GPIO_Pin_7); }
157
static void  sda_lo(void)      { GPIO_ResetBits(GPIOB, GPIO_Pin_7); }
158
static uint8_t  sda_val(void) { 
159
  uint8_t v = 0x00;
160
  if(SDA_read) {v|=0x01;}
161
  return v;
162
  }
163
164
/* calculate CRC value*/
165
static void crc8(uint8_t b) {
166
  for (i = 0; i < 8; ++i) {
167
    if ((crc_value ^ b) & 0x80) {
168
      crc_value <<= 1;
169
      crc_value ^= 0x31;
170
    } 
171
    else crc_value <<= 1;
172
    b <<= 1;
173
  }
174
}
175
176
static uint8_t send(uint16_t b) {
177
  uint8_t ack;
178
  crc8(b);  
179
  // data
180
  for (i = 0; i < 8; ++i) {
181
    if (b & 0x80) sda_hi();
182
    else sda_lo();
183
    b <<= 1;
184
    nop1();
185
    scl_pulse();
186
  }
187
188
  // read acknowledge (low)
189
  nop1();
190
  scl_hi();  nop1();
191
  ack = sda_val();
192
  scl_lo();
193
  return ack;
194
}
195
196
static uint8_t recv_data(void) {
197
  // data
198
  uint8_t b;
199
  b  = 0;
200
  for (i = 0; i < 8; ++i) {
201
    // data is transmitted MSB first
202
    b <<= 1;
203
    if (sda_val()) b |= 1;
204
    scl_pulse();
205
    nop1();
206
  }
207
208
  // lo acknowledge
209
  sda_lo();
210
  nop1();
211
  scl_pulse();
212
  sda_hi();
213
  nop1();
214
215
  crc8(b);
216
  return b;
217
}
218
219
static uint8_t recv_crc(void) {
220
  // data
221
  uint8_t b;
222
  b = 0;
223
  for (i = 0; i < 8; ++i) {
224
    // CRC is transmitted LSB first
225
    b >>= 1;
226
    if (sda_val()) b |= 0x80;
227
    scl_pulse();
228
    nop1();
229
  }
230
231
  // hi acknowledge
232
  sda_hi();
233
  nop1();
234
  scl_pulse();
235
  nop1();
236
  return b;
237
}
238
239
static void start_SHT(void) {
240
  sda_hi(); scl_lo(); // Initial conditions
241
  crc_value = 0x00;
242
  nop1();
243
  
244
  // reset communication, 10 clock pulses
245
  for (i = 0; i < 10; ++i) { scl_pulse(); nop1(); }
246
247
  /* "Start" Sequence
248
  *       __   __
249
  * SCL _/  \_/  \_
250
  *     ___     ___ 
251
  * SDA    \___/  
252
  */
253
  scl_hi();  nop1();  sda_lo(); nop1(); 
254
  scl_lo();  nop1(); scl_hi();  nop1();
255
  sda_hi();  nop1();  scl_lo();  nop1(); 
256
}
257
258
uint8_t sht71_start_temp(void)  { start_SHT(); return send(TMP_ADRESS) == 0; }
259
uint8_t sht71_start_humid(void) { start_SHT(); return send(HUM_ADRESS) == 0; }
260
261
static int16_t result(void) {
262
  int16_t v;
263
  uint8_t crc;
264
  // if (!sht71_ready()) return 0;
265
  // read senor data high byte, low byte
266
  v = recv_data() << 8; v |= recv_data();
267
  crc = recv_crc();
268
  if (crc != crc_value) return 0;
269
  return v;
270
}
271
272
int16_t read_SHT71(uint8_t adr) {
273
  int16_t data;
274
  
275
  // Start Transmission
276
  start_SHT();
277
  
278
  /* Send Adress
279
  * Temperature = 000 00011
280
  * Humidity    = 000 00101
281
  */
282
  if (send(adr) != 0x00) return 0xFFFF;
283
  while(SDA_read) {     }
284
  data = result();
285
  return data;
286
}
287
288
void EXTILine0_Config(void)
289
{
290
  
291
  GPIO_InitTypeDef   GPIO_InitStructure;
292
  NVIC_InitTypeDef   NVIC_InitStructure;
293
294
  /* Enable GPIOA clock */
295
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
296
  /* Enable SYSCFG clock */
297
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
298
  
299
  /* Configure PA0 pin as input floating */
300
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
301
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
302
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
303
  GPIO_Init(GPIOA, &GPIO_InitStructure);
304
305
  /* Connect EXTI Line0 to PA0 pin */
306
  SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
307
308
  /* Configure EXTI Line0 */
309
  EXTI_InitStructure.EXTI_Line = EXTI_Line0;
310
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
311
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;  
312
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
313
  EXTI_Init(&EXTI_InitStructure);
314
315
  /* Enable and set EXTI Line0 Interrupt to the lowest priority */
316
  NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
317
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
318
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
319
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
320
  NVIC_Init(&NVIC_InitStructure);
321
}
322
323
/****END OF FILE****/

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.