Subversion Repositories MK3Mag

Rev

Rev 18 | Rev 22 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 18 Rev 19
Line 1... Line 1...
1
/*#######################################################################################
1
/*#######################################################################################
2
MK3Mag 3D-Magnet sensor
2
MK3Mag 3D-Magnet sensor
3
!!! THIS IS NOT FREE SOFTWARE !!!                                                      
3
!!! THIS IS NOT FREE SOFTWARE !!!
4
#######################################################################################*/
4
#######################################################################################*/
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// + Copyright (c) 05.2008 Holger Buss
6
// + Copyright (c) 05.2008 Holger Buss
7
// + Thanks to Ilja Fähnrich (P_Latzhalter)
7
// + Thanks to Ilja Fähnrich (P_Latzhalter)
8
// + Nur für den privaten Gebrauch
8
// + Nur für den privaten Gebrauch
9
// + www.MikroKopter.com
9
// + www.MikroKopter.com
10
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
11
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur 
11
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
12
// + mit unserer Zustimmung zulässig
12
// + mit unserer Zustimmung zulässig
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
14
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), 
14
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
15
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. 
15
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
16
// + AUSNAHME: Ein bei www.mikrokopter.de erworbener vorbestückter MK3Mag darf als Baugruppe auch in kommerziellen Systemen verbaut werden
16
// + AUSNAHME: Ein bei www.mikrokopter.de erworbener vorbestückter MK3Mag darf als Baugruppe auch in kommerziellen Systemen verbaut werden
17
// + Im Zweifelsfall bitte anfragen bei: info@mikrokopter.de
17
// + Im Zweifelsfall bitte anfragen bei: info@mikrokopter.de
18
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
18
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, 
19
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
20
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
20
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
21
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
22
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
23
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
23
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
24
// + eindeutig als Ursprung verlinkt werden
24
// + eindeutig als Ursprung verlinkt werden
Line 27... Line 27...
27
// + Benutzung auf eigene Gefahr
27
// + Benutzung auf eigene Gefahr
28
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
28
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
29
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
30
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// + Redistributions of source code (with or without modifications) must retain the above copyright notice, 
32
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
33
// + this list of conditions and the following disclaimer.
33
// + this list of conditions and the following disclaimer.
34
// +   * PORTING this software (or parts of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed
34
// +   * PORTING this software (or parts of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
36
// +     from this software without specific prior written permission.
36
// +     from this software without specific prior written permission.
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet 
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
38
// +     for non-commercial use (directly or indirectly)
38
// +     for non-commercial use (directly or indirectly)
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted 
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
40
// +     with our written permission
40
// +     with our written permission
41
// +     Exception: A preassembled MK3Mag, purchased from www.mikrokopter.de may be used as a part of commercial systems
41
// +     Exception: A preassembled MK3Mag, purchased from www.mikrokopter.de may be used as a part of commercial systems
42
// +     In case of doubt please contact: info@MikroKopter.de
42
// +     In case of doubt please contact: info@MikroKopter.de
43
// +   * If sources or documentations are redistributet on other webpages, our webpage (http://www.MikroKopter.de) must be 
43
// +   * If sources or documentations are redistributet on other webpages, our webpage (http://www.MikroKopter.de) must be
44
// +     clearly linked as origin 
44
// +     clearly linked as origin
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55
// +  POSSIBILITY OF SUCH DAMAGE. 
55
// +  POSSIBILITY OF SUCH DAMAGE.
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57
 
-
 
58
#include <avr/io.h>
57
#include <avr/io.h>
-
 
58
#include <avr/interrupt.h>
59
#include <util/twi.h>
59
#include <util/twi.h>
-
 
60
#include "twislave.h"
-
 
61
#include "uart.h"
60
#include "main.h"
62
#include "main.h"
-
 
63
#include "timer0.h"
Line 61... Line -...
61
 
-
 
62
unsigned char I2C_RxBufferSize, I2C_TxBufferSize;
-
 
63
unsigned char *I2C_TxBuffer, *I2C_RxBuffer;
-
 
64
unsigned char Tx_Idx=0, Rx_Idx=0, I2C_Direction;
64
 
65
unsigned char I2C_Command;
65
#define TWI_BUS_ERR_1   0x00
66
unsigned char Tx_Idx, Rx_Idx;
-
 
67
 
-
 
68
struct str_I2C_Heading       I2C_Heading;
-
 
69
struct str_I2C_WriteNickRoll I2C_WriteNickRoll;
-
 
70
struct str_I2C_Mag           I2C_Mag;
-
 
71
struct str_I2C_EEPROM        I2C_ReadEEPROM, I2C_WriteEEPROM;
-
 
72
struct str_I2C_Version       I2C_Version;
-
 
73
struct str_I2C_WriteCal      I2C_WriteCal;
-
 
74
 
-
 
Line -... Line 66...
-
 
66
#define TWI_BUS_ERR_2   0xF8
-
 
67
 
-
 
68
// Status Slave RX Mode
-
 
69
#define SR_SLA_ACK      0x60
-
 
70
#define SR_LOST_ACK     0x68
-
 
71
#define SR_GEN_CALL_ACK 0x70
-
 
72
#define GEN_LOST_ACK    0x78
-
 
73
#define SR_PREV_ACK     0x80
-
 
74
#define SR_PREV_NACK    0x88
-
 
75
#define GEN_PREV_ACK    0x90
-
 
76
#define GEN_PREV_NACK   0x98
-
 
77
#define STOP_CONDITION  0xA0
-
 
78
#define REPEATED_START  0xA0
-
 
79
 
-
 
80
// Status Slave TX mode
-
 
81
#define SW_SLA_ACK      0xA8
-
 
82
#define SW_LOST_ACK     0xB0
-
 
83
#define SW_DATA_ACK     0xB8
-
 
84
#define SW_DATA_NACK    0xC0
-
 
85
#define SW_LAST_ACK     0xC8
-
 
86
 
-
 
87
 
-
 
88
uint8_t I2C_RxBufferSize, I2C_TxBufferSize;
-
 
89
uint8_t *I2C_TxBuffer, *I2C_RxBuffer;
-
 
90
uint8_t Tx_Idx = 0, Rx_Idx = 0, I2C_Direction;
-
 
91
uint8_t I2C_Command;
-
 
92
 
-
 
93
 
-
 
94
I2C_Heading_t       I2C_Heading;
75
 
95
I2C_WriteAttitude_t I2C_WriteAttitude;
76
 
96
I2C_Mag_t           I2C_Mag;
-
 
97
I2C_EEPROM_t        I2C_ReadEEPROM, I2C_WriteEEPROM;
-
 
98
I2C_Version_t       I2C_Version;
-
 
99
I2C_WriteCal_t      I2C_WriteCal;
77
//############################################################################
100
 
-
 
101
 
-
 
102
void I2C_Init(void)
-
 
103
{
-
 
104
        uint8_t sreg = SREG;
78
//I2C (TWI) Interface Init
105
        cli();
-
 
106
 
-
 
107
        //SPI SCK/SCL and  MISO/SDA are at put together on the same connector pin in the schematic
-
 
108
 
-
 
109
        // set PB4 (SCK) and PB5 (MISO) as input pull up
-
 
110
        DDRB &= ~((1<<DDB4)|(1<<DDB5));
-
 
111
        PORTB |= ((1<<PORTB4)|(1<<PORTB5));
-
 
112
 
-
 
113
        // set PC4 (SDA) and PC5 (SCL) as input pull up
79
void InitIC2_Slave(void)
114
        DDRC &= ~((1<<DDC4)|(1<<DDC5));
-
 
115
        PORTC |= ((1<<PORTC4)|(1<<PORTC5));
80
//############################################################################
116
 
-
 
117
         // set own address
-
 
118
    TWAR = I2C_SLAVE_ADDRESS; // set own address
-
 
119
        // TWI Control Register
-
 
120
        // clear TWI interrupt flag (TWINT=1)
-
 
121
        // enable TWI Acknowledge Bit (TWEA = 1)
-
 
122
        // disable TWI START Condition Bit (TWSTA = 0), SLAVE
-
 
123
        // disable TWI STOP Condition Bit (TWSTO = 0), SLAVE
-
 
124
        // disable TWI Write Collision Flag (TWWC = 0)
81
{
125
        // enable i2c (TWEN = 1)
82
    TWAR = I2C_SLAVE_ADDRESS; // Eigene Adresse setzen 
-
 
83
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA);
-
 
-
 
126
        // enable TWI Interrupt (TWIE = 1)
84
       
127
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA);
85
       
128
        // update version info
86
        I2C_Version.Hauptversion = VersionInfo.Hauptversion;
129
        I2C_Version.Major = VERSION_MAJOR;
87
        I2C_Version.Nebenversion = VersionInfo.Nebenversion;
130
        I2C_Version.Minor = VERSION_MINOR;
-
 
131
        I2C_Version.Compatible = I2C_PROTOCOL_COMP;
88
        I2C_Version.Comp = 1;
132
 
-
 
133
        SREG = sreg;
-
 
134
}
-
 
135
 
89
       
136
 
90
}
-
 
91
#define TWCR_ACK TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC)
-
 
92
//############################################################################
-
 
-
 
137
 
93
//ISR, die bei einem Ereignis auf dem Bus ausgelöst wird. Im Register TWSR befindet
138
#define TWCR_ACK TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC)
94
//sich dann ein Statuscode, anhand dessen die Situation festgestellt werden kann.
-
 
95
ISR (TWI_vect)
139
 
-
 
140
ISR (TWI_vect)
96
//############################################################################
141
{
97
{  
142
        // check event
98
    switch (TWSR & 0xF8)
143
        switch (TWSR & 0xF8)
99
        {  
-
 
100
        case SR_SLA_ACK:  
144
        {
101
            //TWCR |= (1<<TWINT);
-
 
102
            Rx_Idx = 0;
145
        case SR_SLA_ACK:
103
                       
146
            Rx_Idx = 0;
104
                        TWCR_ACK;
-
 
-
 
147
                        TWCR_ACK;
105
            return;
148
            return;
106
        // Daten Empfangen
149
 
107
        case SR_PREV_ACK:
150
        case SR_PREV_ACK: // data byte received
-
 
151
 
108
           
152
                        if (Rx_Idx  == 0)
109
                        if (Rx_Idx  == 0)
153
                        {
110
                        { I2C_Command = TWDR;
154
                                I2C_Command = TWDR;
111
                         
155
 
112
                          switch(I2C_Command)
156
                                switch(I2C_Command)
113
                          {
157
                                {
114
                             case I2C_CMD_VERSION:
158
                                        case I2C_CMD_VERSION:
115
                                                I2C_TxBuffer = (unsigned char *)&I2C_Version;
159
                                                I2C_TxBuffer = (uint8_t *)&I2C_Version;
116
                                                I2C_TxBufferSize = sizeof(I2C_Version);
160
                                                I2C_TxBufferSize = sizeof(I2C_Version);
-
 
161
                                                I2C_RxBufferSize = 0;
117
                                                I2C_RxBufferSize = 0;
162
                                                break;
118
                                break;
163
 
119
                                case I2C_CMD_WRITE_EEPROM:
164
                                        case I2C_CMD_WRITE_EEPROM:
120
                                                I2C_TxBufferSize = 0;
165
                                                I2C_TxBufferSize = 0;
121
                                                I2C_RxBuffer = (unsigned char *)&I2C_WriteEEPROM;
166
                                                I2C_RxBuffer = (uint8_t *)&I2C_WriteEEPROM;
Line 122... Line 167...
122
                                                I2C_RxBufferSize = sizeof(I2C_WriteEEPROM);
167
                                                I2C_RxBufferSize = sizeof(I2C_WriteEEPROM);
123
                                break;
168
                                                break;
124
 
169
 
125
                                case I2C_CMD_WRITE_CAL:
170
                                        case I2C_CMD_WRITE_CAL:
126
                                                I2C_TxBufferSize = 0;
171
                                                I2C_TxBufferSize = 0;
Line 127... Line 172...
127
                                                I2C_RxBuffer = (unsigned char *)&I2C_WriteCal;
172
                                                I2C_RxBuffer = (uint8_t *)&I2C_WriteCal;
128
                                                I2C_RxBufferSize = sizeof(I2C_WriteCal);
173
                                                I2C_RxBufferSize = sizeof(I2C_WriteCal);
129
                                break;
174
                                                break;
130
 
175
 
131
                                case I2C_CMD_READ_EEPROM:
176
                                        case I2C_CMD_READ_EEPROM:
132
                                                I2C_TxBuffer = (unsigned char *)&I2C_ReadEEPROM.Inhalt;
177
                                                I2C_TxBuffer = (uint8_t *)&I2C_ReadEEPROM.Content;
-
 
178
                                                I2C_TxBufferSize = 2;
133
                                                I2C_TxBufferSize = 2;
179
                                                I2C_RxBuffer = (uint8_t *)&I2C_ReadEEPROM;
134
                                                I2C_RxBuffer = (unsigned char *)&I2C_ReadEEPROM;
180
                                                I2C_RxBufferSize = 1;
135
                                                I2C_RxBufferSize = 1;
181
                                                break;
136
                                break;
182
 
137
                                case I2C_CMD_READ_MAG:
183
                                        case I2C_CMD_READ_MAG:
138
                                                I2C_TxBuffer = (unsigned char *)&I2C_Mag;
184
                                                I2C_TxBuffer = (uint8_t *)&I2C_Mag;
139
                                                I2C_TxBufferSize = sizeof(I2C_Mag);
185
                                                I2C_TxBufferSize = sizeof(I2C_Mag);
140
                                                I2C_RxBufferSize = 0;
186
                                                I2C_RxBufferSize = 0;
-
 
187
 
141
                                               
188
                                                I2C_Mag.MagX = MagnetX;
142
                                                I2C_Mag.MagX = MagnetN;
-
 
143
                                                I2C_Mag.MagY = MagnetR;
189
                                                I2C_Mag.MagY = MagnetY;
144
                                                I2C_Mag.MagZ = MagnetZ;
190
                                                I2C_Mag.MagZ = MagnetZ;
145
                                               
191
                                                break;
146
                                break;
192
 
147
                                case I2C_CMD_READ_HEADING:
193
                                        case I2C_CMD_READ_HEADING:
148
                                                I2C_TxBuffer = (unsigned char *)&I2C_Heading;
-
 
-
 
194
                                                I2C_TxBuffer = (uint8_t *)&I2C_Heading;
149
                                                I2C_TxBufferSize = sizeof(I2C_Heading);
195
                                                I2C_TxBufferSize = sizeof(I2C_Heading);
-
 
196
                                                I2C_RxBuffer =  (uint8_t *)&I2C_WriteAttitude;
150
                                                I2C_RxBuffer =  (unsigned char *)&I2C_WriteNickRoll;
197
                                                I2C_RxBufferSize = sizeof(I2C_WriteAttitude);
151
                                                I2C_RxBufferSize = sizeof(I2C_WriteNickRoll);
198
                                                // update heading from global variable
152
                                               
199
                                                I2C_Heading.Heading = Heading;
153
                                                I2C_Heading.Heading = Heading;
200
                                                // copy current attitude from I2C rx buffer to uart rx buffer (this is used for the calulation of the heading)
154
                                                ExternData.Winkel[NICK] = I2C_WriteNickRoll.Nick;
201
                                                ExternData.Attitude[NICK] = I2C_WriteAttitude.Nick;
155
                                                ExternData.Winkel[ROLL] = I2C_WriteNickRoll.Roll;
202
                                                ExternData.Attitude[ROLL] = I2C_WriteAttitude.Roll;
156
                                break;
203
                                                break;
-
 
204
                                }
157
                          }
205
                        }
158
                        }
206
                        else // Rx_Idx  != 0
159
                        else
207
                        {
160
                        {
208
                                // fill receiver buffer
161
                          if ((Rx_Idx-1) < I2C_RxBufferSize) I2C_RxBuffer[Rx_Idx-1] = TWDR;
-
 
162
                        }
209
                                if ((Rx_Idx - 1) < I2C_RxBufferSize) I2C_RxBuffer[Rx_Idx - 1] = TWDR;
163
                       
210
                        }
164
                        Rx_Idx++;
-
 
165
                       
211
                        // next byte
166
                       
212
                        Rx_Idx++;
167
            I2C_Timeout = 500;
-
 
168
            //TWCR |= (1<<TWINT);
213
 
169
                        TWCR_ACK;
214
            I2C_Timeout = 500;
-
 
215
                        TWCR_ACK; // send acknowledge
170
            return;
216
            return;
171
        // Daten Senden
-
 
-
 
217
 
172
               
218
        case SW_SLA_ACK: // slave transmitter selected
173
        case SW_SLA_ACK:  
-
 
174
            Tx_Idx = 0;
219
                // reset position in tx buffer
175
                       
220
            Tx_Idx = 0;
176
                        if (I2C_TxBufferSize > 0) TWDR = I2C_TxBuffer[Tx_Idx++];
221
            // write first bte o tx buffer to the twi data register
177
                       
-
 
-
 
222
                        if (I2C_TxBufferSize > 0) TWDR = I2C_TxBuffer[Tx_Idx++];
178
//            TWCR |= (1<<TWINT) | (1<< TWEA);
223
                        // send acknowledge
-
 
224
            TWCR_ACK;
179
             TWCR_ACK;
225
            return;
180
            return;
226
 
181
        // Daten Senden
227
                case SW_DATA_ACK: // send data byte
182
                case SW_DATA_ACK:
-
 
183
            if (Tx_Idx < I2C_TxBufferSize) TWDR = I2C_TxBuffer[Tx_Idx++];
228
                        // put next byte from tx buffer to twi data register
-
 
229
            if (Tx_Idx < I2C_TxBufferSize) TWDR = I2C_TxBuffer[Tx_Idx++];
184
                         else TWDR = 0x00;
230
                        else TWDR = 0x00;
185
                        //TWCR |= (1<<TWINT) | (1<< TWEA);
231
                        TWCR_ACK; // send acknowledge
186
                        TWCR_ACK;
232
            return;
187
            return;
233
 
188
        // Bus-Fehler zurücksetzen
234
        // clear Bus-Error
189
        case TWI_BUS_ERR_2:
235
        case TWI_BUS_ERR_2:
-
 
236
            TWCR |=(1<<TWSTO) | (1<<TWINT);
-
 
237
        // clear Bus-Error
-
 
238
        case TWI_BUS_ERR_1:
190
            TWCR |=(1<<TWSTO) | (1<<TWINT);
239
            TWCR |=(1<<TWSTO) | (1<<TWINT);
-
 
240
        }
191
        // Bus-Fehler zurücksetzen   
241
        // clear interrupt flag (TWINT = 1)
192
        case TWI_BUS_ERR_1:
242
        // enable TWI Acknowledge Bit (TWEA = 1)