Skip to content
This repository was archived by the owner on Mar 31, 2023. It is now read-only.

Commit 3c29a11

Browse files
ridhaosWi6labsVVESTM
authored andcommitted
Add Wire library
Add from https://github.com/stm32duino/Arduino_Core_STM32 version 1.2.0.
1 parent 35037cb commit 3c29a11

File tree

12 files changed

+974
-0
lines changed

12 files changed

+974
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// I2C SRF10 or SRF08 Devantech Ultrasonic Ranger Finder
2+
// by Nicholas Zambetti <http://www.zambetti.com>
3+
// and James Tichenor <http://www.jamestichenor.net>
4+
5+
// Demonstrates use of the Wire library reading data from the
6+
// Devantech Utrasonic Rangers SFR08 and SFR10
7+
8+
// Created 29 April 2006
9+
10+
// This example code is in the public domain.
11+
12+
13+
#include <Wire.h>
14+
15+
void setup()
16+
{
17+
Wire.begin(); // join i2c bus (address optional for master)
18+
Serial.begin(9600); // start serial communication at 9600bps
19+
}
20+
21+
int reading = 0;
22+
23+
void loop()
24+
{
25+
// step 1: instruct sensor to read echoes
26+
Wire.beginTransmission(112); // transmit to device #112 (0x70)
27+
// the address specified in the datasheet is 224 (0xE0)
28+
// but i2c adressing uses the high 7 bits so it's 112
29+
Wire.write(byte(0x00)); // sets register pointer to the command register (0x00)
30+
Wire.write(byte(0x50)); // command sensor to measure in "inches" (0x50)
31+
// use 0x51 for centimeters
32+
// use 0x52 for ping microseconds
33+
Wire.endTransmission(); // stop transmitting
34+
35+
// step 2: wait for readings to happen
36+
delay(70); // datasheet suggests at least 65 milliseconds
37+
38+
// step 3: instruct sensor to return a particular echo reading
39+
Wire.beginTransmission(112); // transmit to device #112
40+
Wire.write(byte(0x02)); // sets register pointer to echo #1 register (0x02)
41+
Wire.endTransmission(); // stop transmitting
42+
43+
// step 4: request reading from sensor
44+
Wire.requestFrom(112, 2); // request 2 bytes from slave device #112
45+
46+
// step 5: receive reading from sensor
47+
if(2 <= Wire.available()) // if two bytes were received
48+
{
49+
reading = Wire.read(); // receive high byte (overwrites previous reading)
50+
reading = reading << 8; // shift high byte to be high 8 bits
51+
reading |= Wire.read(); // receive low byte as lower 8 bits
52+
Serial.println(reading); // print the reading
53+
}
54+
55+
delay(250); // wait a bit since people have to read the output :)
56+
}
57+
58+
59+
/*
60+
61+
// The following code changes the address of a Devantech Ultrasonic Range Finder (SRF10 or SRF08)
62+
// usage: changeAddress(0x70, 0xE6);
63+
64+
void changeAddress(byte oldAddress, byte newAddress)
65+
{
66+
Wire.beginTransmission(oldAddress);
67+
Wire.write(byte(0x00));
68+
Wire.write(byte(0xA0));
69+
Wire.endTransmission();
70+
71+
Wire.beginTransmission(oldAddress);
72+
Wire.write(byte(0x00));
73+
Wire.write(byte(0xAA));
74+
Wire.endTransmission();
75+
76+
Wire.beginTransmission(oldAddress);
77+
Wire.write(byte(0x00));
78+
Wire.write(byte(0xA5));
79+
Wire.endTransmission();
80+
81+
Wire.beginTransmission(oldAddress);
82+
Wire.write(byte(0x00));
83+
Wire.write(newAddress);
84+
Wire.endTransmission();
85+
}
86+
87+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// I2C Digital Potentiometer
2+
// by Nicholas Zambetti <http://www.zambetti.com>
3+
// and Shawn Bonkowski <http://people.interaction-ivrea.it/s.bonkowski/>
4+
5+
// Demonstrates use of the Wire library
6+
// Controls AD5171 digital potentiometer via I2C/TWI
7+
8+
// Created 31 March 2006
9+
10+
// This example code is in the public domain.
11+
12+
// This example code is in the public domain.
13+
14+
15+
#include <Wire.h>
16+
17+
void setup()
18+
{
19+
Wire.begin(); // join i2c bus (address optional for master)
20+
}
21+
22+
byte val = 0;
23+
24+
void loop()
25+
{
26+
Wire.beginTransmission(44); // transmit to device #44 (0x2c)
27+
// device address is specified in datasheet
28+
Wire.write(byte(0x00)); // sends instruction byte
29+
Wire.write(val); // sends potentiometer value byte
30+
Wire.endTransmission(); // stop transmitting
31+
32+
val++; // increment value
33+
if(val == 64) // if reached 64th position (max)
34+
{
35+
val = 0; // start over from lowest value
36+
}
37+
delay(500);
38+
}
39+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Wire Master Reader
2+
// by Nicholas Zambetti <http://www.zambetti.com>
3+
4+
// Demonstrates use of the Wire library
5+
// Reads data from an I2C/TWI slave device
6+
// Refer to the "Wire Slave Sender" example for use with this
7+
8+
// Created 29 March 2006
9+
10+
// This example code is in the public domain.
11+
12+
13+
#include <Wire.h>
14+
15+
void setup()
16+
{
17+
Wire.begin(); // join i2c bus (address optional for master)
18+
Serial.begin(9600); // start serial for output
19+
}
20+
21+
void loop()
22+
{
23+
Wire.requestFrom(2, 6); // request 6 bytes from slave device #2
24+
25+
while(Wire.available()) // slave may send less than requested
26+
{
27+
char c = Wire.read(); // receive a byte as character
28+
Serial.print(c); // print the character
29+
}
30+
31+
delay(500);
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* Wire Master Reader Writer
2+
by Wi6Labs
3+
4+
Demonstrates use of the Wire library.
5+
Reads/writes data from/to an I2C/TWI slave device.
6+
Refer to the "Wire Slave Sender Receiver" example for use with this.
7+
8+
Created 27 June 2017
9+
Updated 14 August 2017
10+
- this example is now common to all STM32 boards
11+
12+
This example code is in the public domain.
13+
*/
14+
15+
#include <Wire.h>
16+
17+
#define I2C_ADDR 2
18+
19+
byte x = 0;
20+
21+
void setup()
22+
{
23+
Wire.begin(); // join i2c bus (address optional for master)
24+
Serial.begin(9600); // start serial for output
25+
}
26+
27+
void loop()
28+
{
29+
Wire.requestFrom(I2C_ADDR, 6); // request 6 bytes from slave device
30+
31+
while(Wire.available()) // slave may send less than requested
32+
{
33+
char c = Wire.read(); // receive a byte as character
34+
Serial.print(c); // print the character
35+
}
36+
37+
delay(10);
38+
39+
Wire.beginTransmission(I2C_ADDR); // transmit to device
40+
Wire.write("x is "); // sends five bytes
41+
Wire.write(x); // sends one byte
42+
Wire.endTransmission(); // stop transmitting
43+
x++;
44+
45+
delay(500);
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Wire Master Writer
2+
// by Nicholas Zambetti <http://www.zambetti.com>
3+
4+
// Demonstrates use of the Wire library
5+
// Writes data to an I2C/TWI slave device
6+
// Refer to the "Wire Slave Receiver" example for use with this
7+
8+
// Created 29 March 2006
9+
10+
// This example code is in the public domain.
11+
12+
13+
#include <Wire.h>
14+
15+
void setup()
16+
{
17+
Wire.begin(); // join i2c bus (address optional for master)
18+
}
19+
20+
byte x = 0;
21+
22+
void loop()
23+
{
24+
Wire.beginTransmission(4); // transmit to device #4
25+
Wire.write("x is "); // sends five bytes
26+
Wire.write(x); // sends one byte
27+
Wire.endTransmission(); // stop transmitting
28+
29+
x++;
30+
delay(500);
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Wire Slave Receiver
2+
// by Nicholas Zambetti <http://www.zambetti.com>
3+
4+
// Demonstrates use of the Wire library
5+
// Receives data as an I2C/TWI slave device
6+
// Refer to the "Wire Master Writer" example for use with this
7+
8+
// Created 29 March 2006
9+
10+
// This example code is in the public domain.
11+
12+
13+
#include <Wire.h>
14+
15+
void setup()
16+
{
17+
Wire.begin(4); // join i2c bus with address #4
18+
Wire.onReceive(receiveEvent); // register event
19+
Serial.begin(9600); // start serial for output
20+
}
21+
22+
void loop()
23+
{
24+
delay(100);
25+
}
26+
27+
// function that executes whenever data is received from master
28+
// this function is registered as an event, see setup()
29+
void receiveEvent(int howMany)
30+
{
31+
while(1 < Wire.available()) // loop through all but the last
32+
{
33+
char c = Wire.read(); // receive byte as a character
34+
Serial.print(c); // print the character
35+
}
36+
int x = Wire.read(); // receive byte as an integer
37+
Serial.println(x); // print the integer
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Wire Slave Sender
2+
// by Nicholas Zambetti <http://www.zambetti.com>
3+
4+
// Demonstrates use of the Wire library
5+
// Sends data as an I2C/TWI slave device
6+
// Refer to the "Wire Master Reader" example for use with this
7+
8+
// Created 29 March 2006
9+
10+
// This example code is in the public domain.
11+
12+
13+
#include <Wire.h>
14+
15+
void setup()
16+
{
17+
Wire.begin(2); // join i2c bus with address #2
18+
Wire.onRequest(requestEvent); // register event
19+
}
20+
21+
void loop()
22+
{
23+
delay(100);
24+
}
25+
26+
// function that executes whenever data is requested by master
27+
// this function is registered as an event, see setup()
28+
void requestEvent()
29+
{
30+
Wire.write("hello "); // respond with message of 6 bytes
31+
// as expected by master
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* Wire Slave Receiver
2+
by Wi6Labs
3+
4+
Demonstrates use of the Wire library.
5+
Receives/sends data as an I2C/TWI slave device.
6+
Refer to the "Wire Master Reader Writer" example for use with this.
7+
8+
Created 27 June 2017
9+
Updated 14 August 2017
10+
- this example is now common to all STM32 boards
11+
12+
This example code is in the public domain.
13+
*/
14+
15+
#include <Wire.h>
16+
17+
#define I2C_ADDR 2
18+
19+
void setup()
20+
{
21+
Wire.begin(I2C_ADDR); // join i2c bus with address #4
22+
Wire.onRequest(requestEvent); // register event
23+
Wire.onReceive(receiveEvent); // register event
24+
Serial.begin(9600); // start serial for output
25+
}
26+
27+
void loop()
28+
{
29+
//empty loop
30+
}
31+
32+
// function that executes whenever data is received from master
33+
// this function is registered as an event, see setup()
34+
void receiveEvent(int howMany)
35+
{
36+
while(1 < Wire.available()) // loop through all but the last
37+
{
38+
char c = Wire.read(); // receive byte as a character
39+
Serial.print(c); // print the character
40+
}
41+
int x = Wire.read(); // receive byte as an integer
42+
Serial.println(x); // print the integer
43+
}
44+
45+
// function that executes whenever data is requested by master
46+
// this function is registered as an event, see setup()
47+
void requestEvent()
48+
{
49+
Wire.write("hello\n"); // respond with message of 6 bytes
50+
// as expected by master
51+
}

libraries/Wire/keywords.txt

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#######################################
2+
# Syntax Coloring Map For Wire
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
#######################################
10+
# Methods and Functions (KEYWORD2)
11+
#######################################
12+
13+
begin KEYWORD2
14+
setClock KEYWORD2
15+
beginTransmission KEYWORD2
16+
endTransmission KEYWORD2
17+
requestFrom KEYWORD2
18+
onReceive KEYWORD2
19+
onRequest KEYWORD2
20+
setSCL KEYWORD2
21+
setSDA KEYWORD2
22+
23+
#######################################
24+
# Instances (KEYWORD2)
25+
#######################################
26+
27+
Wire KEYWORD2
28+
Wire1 KEYWORD2
29+
30+
#######################################
31+
# Constants (LITERAL1)
32+
#######################################

0 commit comments

Comments
 (0)