Daily Archives: March 1, 2019

I2C scanner is fairly simple, yet fast and effective way to find whatever device you put onto I2C bus. Some devices (boards) comes with clear designation on the PCB, some with misleading designation – address is shifted to the left, so for example OLED display typical address is 0x3C/0x3D (depends of address selector jumper), but shifted to the left by 1 bit is 0x78 or 0x7A respectively. They made it as if zero bit is inserted at LSB place so that you ‘just’ send it as-is. But that is bad practice. Rather make your own shifting routine and insert 1 or 0, whether you want to read or write at this address.

Here is how looks whole packet with six addresses found. After each address found, there is 5.5 mS delay until timeout.:

Almost standard wire.c library

Since I am beginner, I just begin to make few more or less standard libraries for STM32f10x series. Right now there are three functions in wire.c; twiScan(), twiSend(), and twiReceive(). So far I did not found need for anything else, but that may change, so stay tuned. Since I have none of other MCU that has different set of registers with also different syntax for those. Here is complete thing with example of main.c code that does nothing except scan.:

main.c

C++

1

2

3

4

5

6

7

8

9

10

11

#include "stm32f10x.h"

#include "printMsg.h"

#include "wire.h"

#include "delayUs.h"

intmain()

{

usart_1_enable();//enabling printMsg();

twiEnable();//enabling two wire interface, IIC or I2C

twiScan();//before anything else, lets check which devices are on the bus,

}

Implemented in wire.c with wireSend() and wireReceive() functions:

wire.c

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

119

120

121

122

123

124

125

126

127

/* Two Wire Interface, I2C (or IIC), here will be called 'twi', and we have

only twiEnable(), twiSend() and twiReceive(). The twiSend() function is

fairly simple, we just send address of the device shifted to the left by