-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduinoPicInterface.ino
64 lines (56 loc) · 1.45 KB
/
ArduinoPicInterface.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
#include <Wire.h>
constexpr int i2cBufferSize = 2;
volatile byte i2cRegister[i2cBufferSize];
size_t counter=0;
void setup() {
// put your setup code here, to run once:
Wire.begin(B1100110); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
Wire.onReceive(i2cReceive); // stores address of program counter
Wire.onRequest(requestEvent); // responds with opcode at program counter
}
void i2cReceive(int bytesReceived)
{
i2cRegister[counter++] = Wire.read();
if(counter>=i2cBufferSize)
{
counter=0;
}
// onReceive will not be invoked unless rxBuffer is empty.
while (Wire.available())
{
i2cRegister[counter++] = Wire.read();
if(counter>=i2cBufferSize)
{
counter=0;
}
}
}
void requestEvent()
{
uint16_t address = i2cRegister[0]<<8;
address += i2cRegister[1];
//if(address == 127)
{
// test code - 0x546 - BSF GPIO, 2 (the LED)
// - 0x446 - BCF GPIO, 2 (the LED)
// This code will blink the LED
static int flipflop = 0;
byte value = 0x5;
flipflop++;
if(flipflop<=500)
{
value = 4;
}
else if( flipflop ==1000)
{
flipflop=0;
}
Wire.write(value);
Wire.write(0x46);
}
}
void loop()
{
// do nothing
}