Rev 440 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 440 | Rev 441 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | /*############################################################################ |
1 | /*############################################################################ |
2 | ############################################################################*/ |
2 | ############################################################################*/ |
Line 3... | Line 3... | ||
3 | 3 | ||
Line 4... | Line -... | ||
4 | #include "main.h" |
- | |
5 | - | ||
6 | unsigned char twi_state = 0; |
- | |
7 | unsigned char motor = 0; |
4 | #include "main.h" |
Line 8... | Line 5... | ||
8 | unsigned char motorread = 0; |
5 | |
9 | unsigned char motor_rx[8]; |
6 | unsigned char motor_rx[8]; |
10 | 7 | ||
Line 17... | Line 14... | ||
17 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
14 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
18 | } |
15 | } |
Line 19... | Line 16... | ||
19 | 16 | ||
20 | //############################################################################ |
17 | //############################################################################ |
21 | //Start I2C |
18 | //Start I2C |
22 | char i2c_start(void) |
19 | void i2c_start(void) |
23 | //############################################################################ |
20 | //############################################################################ |
24 | { |
21 | { |
25 | TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE); |
- | |
26 | return(0); |
22 | TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE); |
Line 27... | Line 23... | ||
27 | } |
23 | } |
28 | 24 | ||
29 | //############################################################################ |
25 | //############################################################################ |
30 | //Start I2C |
26 | //Stop I2C |
31 | void i2c_stop(void) |
27 | void i2c_stop(void) |
32 | //############################################################################ |
28 | //############################################################################ |
33 | { |
29 | { |
Line 34... | Line 30... | ||
34 | TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT); |
30 | TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT); |
35 | } |
31 | } |
36 | 32 | ||
37 | //############################################################################ |
33 | //############################################################################ |
38 | //Start I2C |
34 | //Write to I2C |
39 | char i2c_write_byte(char byte) |
- | |
40 | //############################################################################ |
35 | void i2c_write_byte(char byte) |
41 | { |
36 | //############################################################################ |
42 | TWSR = 0x00; |
- | |
43 | TWDR = byte; |
- | |
44 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
- | |
45 | 37 | { |
|
Line 46... | Line 38... | ||
46 | return(0); |
38 | TWDR = byte; |
47 | 39 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
|
48 | } |
40 | } |
49 | 41 | ||
50 | //############################################################################ |
42 | //############################################################################ |
- | 43 | //ISR |
|
- | 44 | SIGNAL (TWI_vect) |
|
- | 45 | //############################################################################ |
|
- | 46 | { |
|
51 | //Start I2C |
47 | static unsigned char twi_state = 0; |
52 | SIGNAL (TWI_vect) |
48 | static unsigned char motor = 0; |
53 | //############################################################################ |
49 | static unsigned char motorread = 0; |
54 | { |
50 | |
55 | switch (twi_state++) |
51 | switch (twi_state++) |
Line 73... | Line 69... | ||
73 | i2c_write_byte(Motor_Links); |
69 | i2c_write_byte(Motor_Links); |
74 | break; |
70 | break; |
75 | } |
71 | } |
76 | break; |
72 | break; |
77 | case 2: |
73 | case 2: |
78 | i2c_stop(); |
- | |
79 | if (motor<4) twi_state = 0; |
74 | if (motor<4) twi_state = 0; |
80 | else motor = 0; |
- | |
81 | i2c_start(); |
75 | i2c_start(); |
82 | break; |
76 | break; |
Line 83... | Line 77... | ||
83 | 77 | ||
84 | //Liest Daten von Motor |
78 | //Liest Daten von Motor |
85 | case 3: |
79 | case 3: |
86 | i2c_write_byte(0x53+(motorread*2)); |
80 | i2c_write_byte(0x53+(motorread*2)); |
87 | break; |
81 | break; |
88 | case 4: |
- | |
89 | switch(motorread) |
- | |
90 | { |
- | |
91 | case 0: |
82 | 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 | } |
83 | i2c_write_byte(0xFF); |
104 | break; |
84 | break; |
105 | case 5: //1 Byte vom Motor lesen |
85 | case 5: // 1. Byte vom Motor lesen |
106 | motor_rx[motorread] = TWDR; |
- | |
107 | case 6: |
- | |
108 | switch(motorread) |
- | |
109 | { |
- | |
110 | case 0: |
86 | 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 | } |
87 | i2c_write_byte(0xFF); |
123 | break; |
88 | break; |
124 | case 7: //2 Byte vom Motor lesen |
89 | case 6: // 2. Byte vom Motor lesen |
125 | motor_rx[motorread+4] = TWDR; |
90 | motor_rx[motorread+4] = TWDR; |
126 | motorread++; |
91 | motorread++; |
- | 92 | if (motorread>3) motorread=0; |
|
127 | if (motorread>3) motorread=0; |
93 | default: |
128 | i2c_stop(); |
94 | i2c_stop(); |
- | 95 | twi_state = 0; |
|
129 | twi_state = 0; |
96 | motor = 0; |
130 | } |
97 | } |