Rev 1174 | Rev 1210 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1174 | Rev 1209 | ||
---|---|---|---|
Line 4... | Line 4... | ||
4 | #include "main.h" |
4 | #include "main.h" |
Line 5... | Line 5... | ||
5 | 5 | ||
6 | volatile unsigned char twi_state = 0; |
6 | volatile unsigned char twi_state = 0; |
7 | unsigned char motor = 0; |
7 | unsigned char motor = 0; |
8 | unsigned char motorread = 0; |
8 | unsigned char motorread = 0; |
Line 9... | Line 9... | ||
9 | unsigned char motor_rx[16]; |
9 | unsigned char motor_rx[16],motor_rx2[16]; |
10 | 10 | ||
11 | //############################################################################ |
11 | //############################################################################ |
12 | //Initzialisieren der I2C (TWI) Schnittstelle |
12 | //Initzialisieren der I2C (TWI) Schnittstelle |
Line 17... | Line 17... | ||
17 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
17 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
18 | } |
18 | } |
Line 19... | Line 19... | ||
19 | 19 | ||
20 | //############################################################################ |
20 | //############################################################################ |
21 | //Start I2C |
21 | //Start I2C |
22 | char i2c_start(void) |
22 | void i2c_start(void) |
23 | //############################################################################ |
23 | //############################################################################ |
24 | { |
24 | { |
25 | TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE); |
- | |
26 | return(0); |
25 | TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE); |
Line 27... | Line 26... | ||
27 | } |
26 | } |
28 | 27 | ||
29 | //############################################################################ |
28 | //############################################################################ |
Line 50... | Line 49... | ||
50 | i2c_start(); |
49 | i2c_start(); |
51 | i2c_write_byte(0); |
50 | i2c_write_byte(0); |
52 | } |
51 | } |
Line 53... | Line 52... | ||
53 | 52 | ||
54 | //############################################################################ |
53 | //############################################################################ |
55 | char i2c_write_byte(char byte) |
54 | void i2c_write_byte(char byte) |
56 | //############################################################################ |
55 | //############################################################################ |
57 | { |
56 | { |
58 | TWSR = 0x00; |
57 | TWSR = 0x00; |
59 | TWDR = byte; |
58 | TWDR = byte; |
60 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
- | |
61 | - | ||
62 | return(0); |
- | |
63 | 59 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
|
Line -... | Line 60... | ||
- | 60 | } |
|
- | 61 | ||
- | 62 | /****************************************/ |
|
- | 63 | /* Write to I2C */ |
|
- | 64 | /****************************************/ |
|
- | 65 | void I2C_WriteByte(int8_t byte) |
|
64 | } |
66 | { |
- | 67 | // move byte to send into TWI Data Register |
|
- | 68 | TWDR = byte; |
|
- | 69 | // clear interrupt flag (TWINT = 1) |
|
- | 70 | // enable i2c bus (TWEN = 1) |
|
- | 71 | // enable interrupt (TWIE = 1) |
|
- | 72 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
|
- | 73 | } |
|
- | 74 | ||
- | 75 | /****************************************/ |
|
- | 76 | /* Receive byte and send ACK */ |
|
- | 77 | /****************************************/ |
|
- | 78 | void I2C_ReceiveByte(void) |
|
- | 79 | { |
|
- | 80 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA); |
|
- | 81 | } |
|
- | 82 | ||
- | 83 | /****************************************/ |
|
- | 84 | /* I2C receive last byte and send no ACK*/ |
|
- | 85 | /****************************************/ |
|
- | 86 | void I2C_ReceiveLastByte(void) |
|
- | 87 | { |
|
- | 88 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
|
- | 89 | } |
|
- | 90 | ||
65 | 91 | ||
66 | #ifndef QUADRO |
92 | |
67 | //############################################################################ |
93 | //############################################################################ |
68 | SIGNAL (TWI_vect) |
94 | SIGNAL (TWI_vect) |
69 | //############################################################################ |
95 | //############################################################################ |
70 | { |
96 | { |
71 | switch (twi_state++) |
97 | switch (twi_state++) |
- | 98 | { |
|
- | 99 | case 0: |
|
- | 100 | J3High; |
|
72 | { |
101 | while(Mixer.Motor[motor][0] <= 0 && motor < MAX_MOTORS-1) motor++; // skip if not used |
73 | case 0: |
102 | if(motor == MAX_MOTORS) { motor = 0; twi_state = 3; i2c_start(); } // writing finished -> now read |
74 | i2c_write_byte(0x52+(motor*2)); |
103 | else i2c_write_byte(0x52+(motor*2)); |
75 | break; |
- | |
76 | case 1: |
- | |
77 | switch(motor++) |
- | |
78 | { |
104 | break; |
79 | case 0: |
- | |
80 | i2c_write_byte(Motor1); |
- | |
81 | break; |
- | |
82 | case 1: |
- | |
83 | i2c_write_byte(Motor2); |
- | |
84 | break; |
- | |
85 | case 2: |
- | |
86 | i2c_write_byte(Motor3); |
- | |
87 | break; |
- | |
88 | case 3: |
- | |
89 | i2c_write_byte(Motor4); |
- | |
90 | break; |
- | |
91 | case 4: |
- | |
92 | i2c_write_byte(Motor5); |
- | |
93 | break; |
- | |
94 | case 5: |
- | |
95 | i2c_write_byte(Motor6); |
- | |
96 | break; |
- | |
97 | case 6: |
- | |
98 | i2c_write_byte(Motor7); |
- | |
99 | break; |
- | |
100 | case 7: |
- | |
101 | i2c_write_byte(Motor8); |
- | |
102 | break; |
105 | case 1: |
103 | } |
106 | i2c_write_byte(Motor[motor++]); |
104 | break; |
107 | break; |
105 | case 2: |
- | |
106 | i2c_stop(); |
108 | case 2: |
107 | if (motor<8) twi_state = 0; |
109 | i2c_stop(); |
108 | else motor = 0; |
110 | twi_state = 0; |
109 | i2c_start(); |
- | |
110 | break; |
111 | i2c_start(); |
111 | 112 | break; |
|
- | 113 | //Liest Daten von Motor |
|
- | 114 | case 3: |
|
- | 115 | J5High; |
|
112 | //Liest Daten von Motor |
116 | while(Mixer.Motor[motorread][0] <= 0 && MAX_MOTORS-1) motorread++; |
113 | case 3: |
117 | if(motorread >= MAX_MOTORS) {motorread = 0;J4High;} |
114 | i2c_write_byte(0x53+(motorread*2)); |
118 | else i2c_write_byte(0x53+(motorread*2)); |
115 | break; |
119 | break; |
116 | case 4: |
- | |
117 | switch(motorread) |
- | |
118 | { |
- | |
119 | case 0: |
- | |
120 | i2c_write_byte(Motor1); |
- | |
121 | break; |
- | |
122 | case 1: |
- | |
123 | i2c_write_byte(Motor2); |
- | |
124 | break; |
- | |
125 | case 2: |
- | |
126 | i2c_write_byte(Motor3); |
- | |
127 | break; |
- | |
128 | case 3: |
120 | case 4: |
129 | i2c_write_byte(Motor4); |
- | |
130 | break; |
- | |
131 | case 4: |
- | |
132 | i2c_write_byte(Motor5); |
- | |
133 | break; |
- | |
134 | case 5: |
- | |
135 | i2c_write_byte(Motor6); |
- | |
136 | break; |
- | |
137 | case 6: |
- | |
138 | i2c_write_byte(Motor7); |
- | |
139 | break; |
- | |
140 | case 7: |
- | |
141 | i2c_write_byte(Motor8); |
- | |
142 | break; |
121 | //Transmit 1st byte for reading |
143 | } |
122 | I2C_ReceiveByte(); |
144 | break; |
123 | break; |
145 | case 5: //1 Byte vom Motor lesen |
- | |
- | 124 | case 5: //Read 1st byte and transmit 2nd Byte |
|
- | 125 | motor_rx[motorread] = TWDR; |
|
146 | motor_rx[motorread] = TWDR; |
126 | I2C_ReceiveLastByte(); //nack |
147 | - | ||
148 | case 6: |
- | |
149 | switch(motorread) |
- | |
150 | { |
- | |
151 | case 0: |
127 | break; |
152 | i2c_write_byte(Motor1); |
- | |
153 | break; |
- | |
154 | case 1: |
- | |
155 | i2c_write_byte(Motor2); |
- | |
156 | break; |
- | |
157 | case 2: |
- | |
158 | i2c_write_byte(Motor3); |
- | |
159 | break; |
- | |
160 | case 3: |
- | |
161 | i2c_write_byte(Motor4); |
- | |
162 | break; |
- | |
163 | case 4: |
- | |
164 | i2c_write_byte(Motor5); |
- | |
165 | break; |
- | |
166 | case 5: |
- | |
167 | i2c_write_byte(Motor6); |
- | |
168 | break; |
- | |
169 | case 6: |
- | |
170 | i2c_write_byte(Motor7); |
- | |
171 | break; |
- | |
172 | case 7: |
- | |
173 | i2c_write_byte(Motor8); |
- | |
174 | break; |
- | |
175 | } |
- | |
176 | break; |
128 | case 6: |
177 | case 7: //2 Byte vom Motor lesen |
- | |
178 | motor_rx[motorread+8] = TWDR; |
- | |
179 | motorread++; |
129 | //Read 2nd byte |
180 | if (motorread>7) motorread=0; |
- | |
181 | i2c_stop(); |
130 | motor_rx2[motorread++] = TWDR; |
- | 131 | i2c_stop(); |
|
182 | I2CTimeout = 10; |
132 | twi_state = 0; |
183 | twi_state = 0; |
133 | I2CTimeout = 10; |
184 | break; |
134 | break; |
185 | case 8: // Gyro-Offset |
135 | case 8: // Gyro-Offset |
186 | i2c_write_byte(0x98); // Address of the DAC |
136 | i2c_write_byte(0x98); // Address of the DAC |
Line 231... | Line 181... | ||
231 | case 22: |
181 | case 22: |
232 | i2c_stop(); |
182 | i2c_stop(); |
233 | I2CTimeout = 10; |
183 | I2CTimeout = 10; |
234 | twi_state = 0; |
184 | twi_state = 0; |
235 | break; |
185 | break; |
- | 186 | default: twi_state = 0; |
|
236 | } |
187 | break; |
- | 188 | } |
|
237 | TWCR |= 0x80; |
189 | TWCR |= 0x80; |
- | 190 | J3Low; |
|
- | 191 | J4Low; |
|
- | 192 | J5Low; |
|
238 | } |
193 | } |
239 | #else |
194 | /* |
240 | //############################################################################ |
195 | //############################################################################ |
241 | SIGNAL (TWI_vect) |
196 | SIGNAL (TWI_vect) |
242 | //############################################################################ |
197 | //############################################################################ |
243 | { |
198 | { |
244 | switch (twi_state++) |
199 | switch (twi_state++) |
Line 371... | Line 326... | ||
371 | twi_state = 0; |
326 | twi_state = 0; |
372 | break; |
327 | break; |
373 | } |
328 | } |
374 | TWCR |= 0x80; |
329 | TWCR |= 0x80; |
375 | } |
330 | } |
376 | #endif |
- | |
377 | 331 | */ |
|
- | 332 | ||
378 | 333 |