Hallo!
In meiner Schule haben wir derzeit ein Projekt mit unseren
Mikrocontrollern (STM32F4 Discovery) zu machen. Wir sollen die
Temperatur eines ADT7420-Temperatursensors einlesen und mit
selbsteingegebenen Schwellenwerten vergleichen.
Wir sind damit ins kalte Wasser geworfen worden, da wir noch nie mit
I2C-Komponenten gearbeitet haben. Auch aus den Datasheets o.Ä. bin ich
nicht schlau geworden.
Mein Problem ist, dass ich den Temperaturwert, den der Sensor liefert,
nicht auslesen kann. Ich habe mir im Internet viele Beispielcodes
angesehen, bekomme aber keinen davon zum Laufen.
Meine Idee wäre, den STM32 als Master und den I2C-Sensor als Slave zu
benutzen, aber auch mit dem Master/Slave-Prinzip bin ich noch nicht 100%
vertraut.
Meine Initialisierungschaut im Moment wie folgt aus:
1 | #include <stm32f4xx.h>
|
2 | #include <stm32f4xx_i2c.h>
|
3 |
|
4 | void init_I2C1(void){
|
5 |
|
6 | GPIO_InitTypeDef GPIO_InitStruct;
|
7 | I2C_InitTypeDef I2C_InitStruct;
|
8 |
|
9 | // enable APB1 peripheral clock for I2C1
|
10 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
|
11 | // enable clock for SCL and SDA pins
|
12 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
|
13 |
|
14 |
|
15 | //1. SCL on PB6 and SDA on PB7
|
16 |
|
17 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
|
18 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
|
19 | GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
20 | GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
|
21 | GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
|
22 | GPIO_Init(GPIOB,GPIO_InitStruct);
|
23 |
|
24 | // Connect I2C1 pins to AF
|
25 | GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1); // SCL
|
26 | GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1); // SDA
|
27 |
|
28 | // configure I2C1
|
29 | I2C_InitStruct.I2C_ClockSpeed = 100000; // 100kHz
|
30 | I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
|
31 | I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;
|
32 | I2C_InitStruct.I2C_OwnAddress1 = 0x00;
|
33 | I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
|
34 | I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
|
35 | I2C_Init(I2C1, &I2C_InitStruct);
|
36 |
|
37 | // enable I2C1
|
38 | I2C_Cmd(I2C1, ENABLE);
|
39 | }
|
Ich würde mich über einen kleinen Denkanstoß oder Lösungsansatz sehr
freuen.
MfG
Christoph