Ich sende von einem Mikrocontroller zum anderen eine Befehl über I2C und
sende diesen dann per UART weiter.
Das funktioniert eigentlich recht gut, wenn ich bei empfangen.c in der
while Schlaufe eine Pause von mindestens 1ms einbaue. Ohne diese habe
ich mit dem ersten Befehl das Problem, dass nicht alle Bytes einen Wert
enthalten.
Hat jemand eine Idee?
1 | //senden.c
|
2 | void write_slave1(void)
|
3 | {
|
4 | unsigned char temp = ((0x21^1^0x35^1^1)|0x01);
|
5 | i2c_start_wait(0x50 + I2C_WRITE);
|
6 | i2c_write(1); // Startadresse
|
7 | i2c_write(0x00); // Temp auf 0x00 bis alle Daten geschrieben sind
|
8 | i2c_write(1); // Adresse
|
9 | i2c_write(0x35); // Rot
|
10 | i2c_write(1); // Grün
|
11 | i2c_write(1); // Blau
|
12 | i2c_write(temp); // CRC
|
13 | i2c_stop();
|
14 | i2c_start_wait(0x50 + I2C_WRITE);
|
15 | i2c_write(1); // Startadresse
|
16 | i2c_write(0x21); // Befehl 0x21
|
17 | i2c_stop(); // stopt I2C Verbindung
|
18 | }
|
19 |
|
20 | int main(void)
|
21 | {
|
22 | i2c_init(); // initialize I2C library
|
23 |
|
24 | //write_slave();
|
25 | while(1)
|
26 | {
|
27 | write_slave1();
|
28 | _delay_ms(1000);
|
29 | write_slave2();
|
30 | _delay_ms(1000);
|
31 | write_slave3();
|
32 | _delay_ms(1000);
|
33 |
|
34 | }
|
35 | return 0;
|
36 | }
|
37 |
|
38 |
|
39 | //empfangen.c
|
40 | while(ieCntS < READINS && ieCntN < 252/6) //Recive max x Instructions or check Recive Buffer one times
|
41 | {
|
42 | delay_ms(1);
|
43 | if(rxbuffer[ie] != 0x00) //Something in receive buffer
|
44 | {
|
45 | if(((rxbuffer[ie]^rxbuffer[ie+1]^rxbuffer[ie+2]^rxbuffer[ie+3]^rxbuffer[ie+4]) | 0x01) != rxbuffer[ie+5]) //Check CRC
|
46 | {
|
47 | rxbuffer[ie]=0; //Clear recive instruction
|
48 | }
|
49 | else
|
50 | {
|
51 | for(int i=0; i<SENDBUFFER; i++) //Search free space in send buffer
|
52 | {
|
53 | if(!sendPufLED[is][1])
|
54 | {
|
55 | sendPufLED[is][0] = 0; //Counter for age
|
56 | sendPufLED[is][1] = rxbuffer[ie]; //Instruction
|
57 | rxbuffer[ie]=0; //Clear recive instruction
|
58 | sendPufLED[is][2] = rxbuffer[ie+1]; //Address
|
59 | sendPufLED[is][3] = rxbuffer[ie+2]; //Data1
|
60 | sendPufLED[is][4] = rxbuffer[ie+3]; //Data2
|
61 | sendPufLED[is][5] = rxbuffer[ie+4]; //Data3
|
62 | if(is++ == SENDBUFFER)
|
63 | is = 1; //Index send buffer to next row
|
64 | ieCntS++; //Sucessfull read of instruction
|
65 | break;
|
66 | }
|
67 | else
|
68 | {
|
69 | if(is++ == SENDBUFFER)
|
70 | is = 1; //Index send buffer to next row
|
71 | if(i == SENDBUFFER-1)
|
72 | {
|
73 | uart_puts_p(PSTR("! Send buffer overloadet\n"));
|
74 | ieCntN = SENDBUFFER; //Not possible to recive instruction at the moment, leave
|
75 | }
|
76 | }
|
77 | }
|
78 | }
|
79 | }
|
80 | ie = ie+6; //Index recive buffer to next instruction
|
81 | if(ie==253)
|
82 | ie=1;
|
83 | ieCntN++;
|
84 | }
|