Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
375 | osiair | 1 | /*############################################################################ |
2 | ############################################################################*/ |
||
3 | |||
4 | #include "main.h" |
||
5 | |||
6 | 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 | } |
||
151 | TWCR |= 0x80; |
||
152 | } |