-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHL_I2C_Tester.ino
95 lines (80 loc) · 2.07 KB
/
HL_I2C_Tester.ino
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
/**
*
* Sample Multi Master I2C implementation. Sends a button state over I2C to another
* Arduino, which flashes an LED correspinding to button state.
*
* Connections: Arduino analog pins 4 and 5 are connected between the two Arduinos,
* with a 1k pullup resistor connected to each line. Connect a push button between
* digital pin 10 and ground, and an LED (with a resistor) to digital pin 9.
*
*/
#include <Wire.h>
#define LED 13
#define BUTTON 10
#define THIS_ADDRESS 0x8
#define OTHER_ADDRESS 0x20
boolean last_state = HIGH;
uint8_t incomingByte = 0;
void setup() {
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
pinMode(BUTTON, INPUT);
digitalWrite(BUTTON, HIGH);
digitalWrite(A4, HIGH);
digitalWrite(A5, HIGH);
Wire.begin(THIS_ADDRESS);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
delay(500);
Serial.println("Arduino Hermes-Lite I2C tester");
Serial.println("copyright (c) Graeme Jury ZL2APV");
Serial.println();
}
void loop() {
if (Serial.available() > 0){
incomingByte = Serial.read();
Serial.print("Data received = 0b");
printBinaryByte(incomingByte);
Wire.beginTransmission(OTHER_ADDRESS);
Wire.write(0x0A);
Wire.write(incomingByte);
Wire.write(0x00);
if(Wire.endTransmission() == 0){
digitalWrite (LED, HIGH);
} else {
digitalWrite (LED, LOW);
Serial.println("Hermes-Lite not connected");
}
// setOutputPins(incomingByte);
}
}
void printBinaryByte(uint8_t readValue)
{
// Print the value of the variable "readValue" as an 8 bit binary number
for(uint8_t mask = 0x80; mask; mask >>= 1){
if(mask & readValue)
Serial.print('1');
else
Serial.print('0');
}
Serial.println();
}
void receiveEvent(int howMany){
while (Wire.available() > 0){
boolean b = Wire.read();
Serial.print(b, DEC);
digitalWrite(LED, !b);
}
Serial.println();
}
void setOutputPins(uint8_t cmd)
{
uint8_t rxValue;
uint8_t cmdValue;
uint8_t txValue;
uint8_t moxValue;
rxValue = cmd & 0b00000111;
cmdValue = ((cmd & 0b00001000) >> 3);
txValue = ((cmd & 0b01110000) >> 4);
moxValue = ((cmd & 0b10000000) >> 7);
}