1 | #include <avr/io.h>
|
2 | #include "i2cmaster.h"
|
3 | #include <util/delay.h>
|
4 |
|
5 | //############# I2C/TWI Adressen
|
6 | #define Adr_AttinyI 0b00110100
|
7 |
|
8 |
|
9 | //############# Macro 2x 8Bit --> 16Bit
|
10 | #define uniq(LOW,HEIGHT) ((HEIGHT << 8)|LOW)
|
11 | //############# Macro 16Bit --> 2x 8Bit
|
12 | #define LOW_BYTE(LONG) (LONG & 0xff)
|
13 | #define HIGH_BYTE(LONG) ((LONG >> 8) & 0xff)
|
14 |
|
15 | //############# Functions
|
16 | uint8_t read_Attiny0(void);
|
17 | void read_Attiny1(void);
|
18 | void write_slave(uint8_t);
|
19 | uint8_t Byte0, Byte1, Byte2, Byte3 = 0; // Bytes
|
20 | uint8_t hig,low = 0; // hight und Low Variable
|
21 | uint16_t word1,word2 =0;
|
22 |
|
23 | //############# Variabels
|
24 | void InitUART (uint8_t baudrate);
|
25 | void TransmitByte (uint8_t data);
|
26 | uint8_t ReceiveByte (void);
|
27 |
|
28 |
|
29 | //############# Main
|
30 | int main(void)
|
31 | {
|
32 | InitUART (12);
|
33 | while (1)
|
34 | {
|
35 | write_slave(ReceiveByte());
|
36 | _delay_ms(1000);
|
37 | TransmitByte (read_Attiny0());
|
38 | _delay_ms(1000);
|
39 | read_Attiny1();
|
40 | _delay_ms(1000);
|
41 | }
|
42 |
|
43 | }
|
44 |
|
45 | //############# Abfrage einzelner Bytes.
|
46 | uint8_t read_Attiny0(void)
|
47 | {
|
48 | i2c_start_wait(Adr_AttinyI + I2C_WRITE); // Adr_AttinyI ansprechen
|
49 | i2c_write(0); // startadresse zum lesen
|
50 |
|
51 | i2c_rep_start (Adr_AttinyI + I2C_READ ); // Lesen beginnen
|
52 | Byte0 = i2c_readAck(); // Byte empfangen
|
53 | Byte1 = i2c_readAck(); // Byte empfangen
|
54 | Byte2 = i2c_readAck(); // Byte empfangen
|
55 | Byte3 = i2c_readNak(); // letes Byte empfangen
|
56 |
|
57 | i2c_stop(); // stopt I2C Verbindung
|
58 | return Byte0;
|
59 | }
|
60 |
|
61 |
|
62 | //############# Abfrage einzelner Bytes (8 Bits) und anschließend zusammenfügen zu einem Word (16 Bit)
|
63 | void read_Attiny1(void)
|
64 | {
|
65 | i2c_start_wait(Adr_AttinyI + I2C_WRITE); // Adr_AttinyI ansprechen
|
66 | i2c_write(0); // startadresse zum lesen
|
67 |
|
68 | i2c_rep_start (Adr_AttinyI + I2C_READ ); // Lesen beginnen
|
69 |
|
70 | low = i2c_readAck(); // Byte0 lesen
|
71 | hig = i2c_readAck(); // Byte1 lesen
|
72 | word1 = uniq(low,hig); // 2x 8Bit --> 16Bit
|
73 |
|
74 | low = i2c_readAck(); // Byte2 lesen
|
75 | hig = i2c_readAck(); // Byte3 lesen
|
76 | word2 = uniq(low,hig); // 2x 8Bit --> 16Bit
|
77 |
|
78 | i2c_stop(); // stopt I2C Verbindung
|
79 | }
|
80 |
|
81 |
|
82 | //############# Senden einzelner Bytes (8 Bits) und anschließend senden von einem Word (16 Bit), durch zerlegen in 2 Bytes
|
83 | void write_slave(uint8_t Data)
|
84 | {
|
85 | i2c_start_wait(Adr_AttinyI + I2C_WRITE);
|
86 | i2c_write(0); // Startadresse / Register
|
87 |
|
88 | i2c_write(Data); // 8bit Information - adresse 0
|
89 | i2c_write(0x42); // 8bit Information - adresse 1
|
90 |
|
91 | i2c_write(0x43); // 8bit Information - adresse 2
|
92 | i2c_write(0x44); // 8bit Information - adresse 3
|
93 |
|
94 | //i2c_write(LOW_BYTE(5678)); // 16bit --> 8bit - adresse 2
|
95 | //i2c_write(HIGH_BYTE(5678)); // 16bit --> 8bit - adresse 3
|
96 |
|
97 | i2c_stop(); // stopt I2C Verbindung
|
98 | }
|
99 |
|
100 |
|
101 |
|
102 | //############# Initialize UART
|
103 | void InitUART (uint8_t baudrate)
|
104 | {
|
105 | //Eigene Initialisierung WatchDog
|
106 | MCUSR =0;
|
107 |
|
108 | MCUCR = 0x80; //JTAG abschalten
|
109 | MCUCR = 0x80;
|
110 |
|
111 | //Eigene Initialisierung (Outputs)
|
112 | DDRD |= (1<<PD5);
|
113 |
|
114 | //Set the baud rate
|
115 | UBRRL = baudrate;
|
116 |
|
117 | // Enable UART receiver and transmitter
|
118 | UCSRB = (1 << RXEN) | (1 << TXEN);
|
119 |
|
120 | // 8 data bits, 1 stop bit
|
121 | UCSRC = (1 << UCSZ1) | (1 << UCSZ0);
|
122 |
|
123 |
|
124 | }
|
125 |
|
126 | //############# Read and write functions
|
127 | uint8_t ReceiveByte (void)
|
128 | {
|
129 | // Wait for incomming data
|
130 | while (!(UCSRA & (1 << RXC)));
|
131 |
|
132 |
|
133 | if(UDR == 0x41)
|
134 | {
|
135 | PORTD |= (1<<PD5); // Led on
|
136 | _delay_ms(1000);
|
137 | PORTD &= !(1<<PD5); // Led off
|
138 | _delay_ms(1000);
|
139 | }
|
140 | //Return the data
|
141 | return UDR;
|
142 | }
|
143 | void TransmitByte (uint8_t data)
|
144 | {
|
145 | // Wait for empty transmit buffer
|
146 | while (!(UCSRA & (1 << UDRE)));
|
147 |
|
148 | //Start transmittion
|
149 | UDR = data;
|
150 |
|
151 | PORTD |= (1<<PD5); // Led on
|
152 | _delay_ms(1000);
|
153 | PORTD &= !(1<<PD5); // Led off
|
154 | _delay_ms(1000);
|
155 | }
|