Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2751 | - | 1 | /*############################################################################ |
2 | ############################################################################*/ |
||
3 | |||
4 | #include "main.h" |
||
5 | |||
6 | volatile unsigned char twi_state = 0; |
||
7 | unsigned char motor = 0; |
||
8 | unsigned char motorread = 0; |
||
9 | unsigned char motor_rx[8]; |
||
10 | |||
11 | //############################################################################ |
||
12 | //Initzialisieren der I2C (TWI) Schnittstelle |
||
13 | void i2c_init(void) |
||
14 | //############################################################################ |
||
15 | { |
||
16 | TWSR = 0; |
||
17 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
||
18 | } |
||
19 | |||
20 | //############################################################################ |
||
21 | //Start I2C |
||
22 | char i2c_start(void) |
||
23 | //############################################################################ |
||
24 | { |
||
25 | TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE); |
||
26 | return(0); |
||
27 | } |
||
28 | |||
29 | //############################################################################ |
||
30 | //Start I2C |
||
31 | void i2c_stop(void) |
||
32 | //############################################################################ |
||
33 | { |
||
34 | TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT); |
||
35 | } |
||
36 | |||
37 | void i2c_reset(void) |
||
38 | //############################################################################ |
||
39 | { |
||
40 | i2c_stop(); |
||
41 | twi_state = 0; |
||
42 | motor = TWDR; |
||
43 | motor = 0; |
||
44 | TWCR = 0x80; |
||
45 | TWAMR = 0; |
||
46 | TWAR = 0; |
||
47 | TWDR = 0; |
||
48 | TWSR = 0; |
||
49 | TWBR = 0; |
||
50 | i2c_init(); |
||
51 | i2c_start(); |
||
52 | i2c_write_byte(0); |
||
53 | } |
||
54 | |||
55 | //############################################################################ |
||
56 | //Start I2C |
||
57 | char i2c_write_byte(char byte) |
||
58 | //############################################################################ |
||
59 | { |
||
60 | TWSR = 0x00; |
||
61 | TWDR = byte; |
||
62 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
||
63 | |||
64 | return(0); |
||
65 | |||
66 | } |
||
67 | |||
68 | //############################################################################ |
||
69 | //Start I2C |
||
70 | SIGNAL (TWI_vect) |
||
71 | //############################################################################ |
||
72 | { |
||
73 | switch (twi_state++) |
||
74 | { |
||
75 | case 0: |
||
76 | i2c_write_byte(0x52+(motor*2)); |
||
77 | break; |
||
78 | case 1: |
||
79 | switch(motor++) |
||
80 | { |
||
81 | case 0: |
||
82 | i2c_write_byte(Motor_Vorne); |
||
83 | break; |
||
84 | case 1: |
||
85 | i2c_write_byte(Motor_Hinten); |
||
86 | break; |
||
87 | case 2: |
||
88 | i2c_write_byte(Motor_Rechts); |
||
89 | break; |
||
90 | case 3: |
||
91 | i2c_write_byte(Motor_Links); |
||
92 | break; |
||
93 | } |
||
94 | break; |
||
95 | case 2: |
||
96 | i2c_stop(); |
||
97 | if (motor<4) twi_state = 0; |
||
98 | else motor = 0; |
||
99 | i2c_start(); |
||
100 | break; |
||
101 | |||
102 | //Liest Daten von Motor |
||
103 | case 3: |
||
104 | i2c_write_byte(0x53+(motorread*2)); |
||
105 | break; |
||
106 | case 4: |
||
107 | switch(motorread) |
||
108 | { |
||
109 | case 0: |
||
110 | i2c_write_byte(Motor_Vorne); |
||
111 | break; |
||
112 | case 1: |
||
113 | i2c_write_byte(Motor_Hinten); |
||
114 | break; |
||
115 | case 2: |
||
116 | i2c_write_byte(Motor_Rechts); |
||
117 | break; |
||
118 | case 3: |
||
119 | i2c_write_byte(Motor_Links); |
||
120 | break; |
||
121 | } |
||
122 | break; |
||
123 | case 5: //1 Byte vom Motor lesen |
||
124 | motor_rx[motorread] = TWDR; |
||
125 | |||
126 | case 6: |
||
127 | switch(motorread) |
||
128 | { |
||
129 | case 0: |
||
130 | i2c_write_byte(Motor_Vorne); |
||
131 | break; |
||
132 | case 1: |
||
133 | i2c_write_byte(Motor_Hinten); |
||
134 | break; |
||
135 | case 2: |
||
136 | i2c_write_byte(Motor_Rechts); |
||
137 | break; |
||
138 | case 3: |
||
139 | i2c_write_byte(Motor_Links); |
||
140 | break; |
||
141 | } |
||
142 | break; |
||
143 | case 7: //2 Byte vom Motor lesen |
||
144 | motor_rx[motorread+4] = TWDR; |
||
145 | motorread++; |
||
146 | if (motorread>3) motorread=0; |
||
147 | i2c_stop(); |
||
148 | I2CTimeout = 10; |
||
149 | twi_state = 0; |
||
150 | break; |
||
151 | case 8: // Gyro-Offset |
||
152 | i2c_write_byte(0x98); // Address of the DAC |
||
153 | break; |
||
154 | case 9: |
||
155 | i2c_write_byte(0x10); // Update Channel A |
||
156 | break; |
||
157 | case 10: |
||
158 | i2c_write_byte(AnalogOffsetNick); // Value |
||
159 | break; |
||
160 | case 11: |
||
161 | i2c_write_byte(0x80); // Value |
||
162 | break; |
||
163 | case 12: |
||
164 | i2c_stop(); |
||
165 | I2CTimeout = 10; |
||
166 | i2c_start(); |
||
167 | break; |
||
168 | case 13: |
||
169 | i2c_write_byte(0x98); // Address of the DAC |
||
170 | break; |
||
171 | case 14: |
||
172 | i2c_write_byte(0x12); // Update Channel B |
||
173 | break; |
||
174 | case 15: |
||
175 | i2c_write_byte(AnalogOffsetRoll); // Value |
||
176 | break; |
||
177 | case 16: |
||
178 | i2c_write_byte(0x80); // Value |
||
179 | break; |
||
180 | case 17: |
||
181 | i2c_stop(); |
||
182 | I2CTimeout = 10; |
||
183 | i2c_start(); |
||
184 | break; |
||
185 | case 18: |
||
186 | i2c_write_byte(0x98); // Address of the DAC |
||
187 | break; |
||
188 | case 19: |
||
189 | i2c_write_byte(0x14); // Update Channel C |
||
190 | break; |
||
191 | case 20: |
||
192 | i2c_write_byte(AnalogOffsetGier); // Value |
||
193 | break; |
||
194 | case 21: |
||
195 | i2c_write_byte(0x80); // Value |
||
196 | break; |
||
197 | case 22: |
||
198 | i2c_stop(); |
||
199 | I2CTimeout = 10; |
||
200 | twi_state = 0; |
||
201 | break; |
||
202 | } |
||
203 | TWCR |= 0x80; |
||
204 | } |