forked from kenzanin/I2C-slave-lib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
91 lines (43 loc) · 1.12 KB
/
main.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
////////////////////////////////////////////////////////////////////////
// I2C SLAVE
////////////////////////////////////////////////////////////////////////
/*
I2C Slave MCU implementaiton:
(1) Master Transmitting to Slave:
A- Specify the Slave Address (Slave_Address)
=> receive the data on rxbuffer[i]
i is the index defined by the master transmitter
i range is 0-254
(2) Master Reading from slave
A- Specify the Slave Address (Slave_Address)
B- Specify the data you want to transmit in txbuffer[i]
i is chosen by the master
i range is 0-254
*/
#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "I2C_slave.h"
// buffer used to convert integer to string
char buffer[3];
#define Slave_Address 0x02
int main(void)
{
I2C_init(Slave_Address); // initalize as slave with address Slave_Address
txbuffer[0] = 0x55;
// allow interrupts
sei();
DDRB=0xFF;
while(1)
{
PORTB=0xFF;
_delay_ms(rxbuffer[0]);
PORTB=0x00;
_delay_ms(rxbuffer[1]);
txbuffer[1] = rxbuffer[1];
}
return 0;
}