Rev 463 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 463 | Rev 464 | ||
---|---|---|---|
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 | //############################################################################ |
30 | //Start I2C |
29 | //Stop I2C |
31 | void i2c_stop(void) |
30 | void i2c_stop(void) |
32 | //############################################################################ |
31 | //############################################################################ |
33 | { |
32 | { |
Line 34... | Line 33... | ||
34 | TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT); |
33 | TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT); |
35 | } |
34 | } |
36 | 35 | ||
37 | //############################################################################ |
36 | //############################################################################ |
38 | //Start I2C |
37 | //Write to I2C |
39 | char i2c_write_byte(char byte) |
- | |
40 | //############################################################################ |
38 | void i2c_write_byte(char byte) |
41 | { |
39 | //############################################################################ |
42 | TWSR = 0x00; |
- | |
43 | TWDR = byte; |
- | |
44 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
- | |
45 | 40 | { |
|
Line 46... | Line 41... | ||
46 | return(0); |
41 | TWDR = byte; |
47 | 42 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
|
48 | } |
43 | } |
Line 73... | Line 68... | ||
73 | i2c_write_byte(Motor_Links); |
68 | i2c_write_byte(Motor_Links); |
74 | break; |
69 | break; |
75 | } |
70 | } |
76 | break; |
71 | break; |
77 | case 2: |
72 | case 2: |
78 | i2c_stop(); |
- | |
79 | if (motor<4) twi_state = 0; |
73 | if (motor<4) twi_state = 0; |
80 | else motor = 0; |
- | |
81 | i2c_start(); |
74 | i2c_start(); |
82 | break; |
75 | break; |
Line 83... | Line 76... | ||
83 | 76 | ||
84 | //Liest Daten von Motor |
77 | //Liest Daten von Motor |
85 | case 3: |
78 | case 3: |
86 | i2c_write_byte(0x53+(motorread*2)); |
79 | i2c_write_byte(0x53+(motorread*2)); |
87 | break; |
80 | break; |
88 | case 4: |
- | |
89 | switch(motorread) |
- | |
90 | { |
- | |
91 | case 0: |
81 | case 4: |
92 | i2c_write_byte(Motor_Vorne); |
- | |
93 | break; |
- | |
94 | case 1: |
- | |
95 | i2c_write_byte(Motor_Hinten); |
- | |
96 | break; |
- | |
97 | case 2: |
- | |
98 | i2c_write_byte(Motor_Rechts); |
- | |
99 | break; |
- | |
100 | case 3: |
- | |
101 | i2c_write_byte(Motor_Links); |
- | |
102 | break; |
- | |
103 | } |
82 | i2c_write_byte(0xFF); |
104 | break; |
83 | break; |
105 | case 5: //1 Byte vom Motor lesen |
84 | case 5: //1. Byte vom Motor lesen |
106 | motor_rx[motorread] = TWDR; |
- | |
107 | case 6: |
- | |
108 | switch(motorread) |
- | |
109 | { |
- | |
110 | case 0: |
85 | motor_rx[motorread] = TWDR; |
111 | i2c_write_byte(Motor_Vorne); |
- | |
112 | break; |
- | |
113 | case 1: |
- | |
114 | i2c_write_byte(Motor_Hinten); |
- | |
115 | break; |
- | |
116 | case 2: |
- | |
117 | i2c_write_byte(Motor_Rechts); |
- | |
118 | break; |
- | |
119 | case 3: |
- | |
120 | i2c_write_byte(Motor_Links); |
- | |
121 | break; |
- | |
122 | } |
86 | i2c_write_byte(0xFF); |
123 | break; |
87 | break; |
124 | case 7: //2 Byte vom Motor lesen |
88 | case 6: //2. Byte vom Motor lesen |
125 | motor_rx[motorread+4] = TWDR; |
89 | motor_rx[motorread+4] = TWDR; |
126 | motorread++; |
90 | motorread++; |
- | 91 | if (motorread>3) motorread=0; |
|
127 | if (motorread>3) motorread=0; |
92 | default: |
128 | i2c_stop(); |
93 | i2c_stop(); |
- | 94 | twi_state = 0; |
|
129 | twi_state = 0; |
95 | motor = 0; |
130 | } |
96 | } |