This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
i2cscan.sb
109 lines (96 loc) · 4.08 KB
/
i2cscan.sb
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
// Copyright (c) 2016, Laird
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// SPDX-License-Identifier:ISC
//
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++ ++
// +++++ When UwTerminal downloads the app it will store it as a filenname ++
// +++++ which consists of all characters up to the first . and excluding it ++
// +++++ ++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// This is a simple I2C scanning application which will scan for I2C devices,
// and display the addresses of detected devices to the UART.
//
// - Version 1.00, Updated 20/09/2016
//
//******************************************************************************
//******************************************************************************
// Global Variable Declarations
//******************************************************************************
DIM I2CHandle //Handle of I2C interface
DIM rc //Result code
//******************************************************************************
// Function and Subroutine definitions
//******************************************************************************
sub I2CScan()
//Open I2C at 100KHz
rc = I2COpen(100000, 0, I2CHandle)
if (rc == 0x5207) then
//I2C already open
print "\nI2C already open."
elseif (rc == 0) THEN
//I2C opened
print "\nI2C opened successfully.";
else
//I2C failed to open
print "\nFailed to open I2C, error code: ";INTEGER.h'rc;
exitsub
endif
//Variables for detection loop
dim I2CAddr //Current I2C address
dim I2CVal //Holds the value of the register
dim I2CDevFound //Set to 1 if a device has been detected
//Initialise variables
I2CAddr = 0
I2CDevFound = 0
//Detect devices
print "\nDetecting devices..."
//127 possible addresses (7-bit address)
while (I2CAddr <= 127)
//Read from first register of device
rc = I2CReadReg8(I2CAddr, 0, I2CVal)
if (rc == 0) then
//No error
if (I2CDevFound == 0) then
//First device detected, output header
print "\nDetected Device Addresses:"
I2CDevFound = 1
endif
//Output device address as hex (removing leading 0s)
dim OutStr$
sprint #OutStr$, integer.h'I2CAddr
OutStr$ = right$(OutStr$, 2)
print "\n\t0x";OutStr$;""
endif
//Increment to next address
I2CAddr = I2CAddr + 1
endwhile
if (I2CDevFound == 0) then
//No I2C devices were found
print "\nFailed to detect any I2C devices:"
print "\n > Check that the I2C devices are properly connected to the SCL and SDA pins."
print "\n > Note that external modules/boards may need SCL/SDA pull-up resistors,"
print "\n consult your I2C module/board documentation for further information."
endif
//Close I2C interface
I2CClose(I2CHandle)
print "\nI2C closed.";
endsub
//******************************************************************************
// Equivalent to main() in C
//******************************************************************************
//Call the detection subroutine
I2CScan()