Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
21 | user | 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 | //############################################################################ |
||
38 | //Start I2C |
||
39 | char i2c_write_byte(char byte) |
||
40 | //############################################################################ |
||
41 | { |
||
42 | TWSR = 0x00; |
||
43 | TWDR = byte; |
||
44 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
||
45 | |||
46 | return(0); |
||
47 | |||
48 | } |
||
49 | |||
50 | //############################################################################ |
||
51 | //Start I2C |
||
52 | SIGNAL (TWI_vect) |
||
53 | //############################################################################ |
||
54 | { |
||
55 | switch (twi_state++) |
||
56 | { |
||
57 | case 0: |
||
58 | i2c_write_byte(0x52+(motor*2)); |
||
59 | break; |
||
60 | case 1: |
||
61 | switch(motor++) |
||
62 | { |
||
63 | case 0: |
||
64 | i2c_write_byte(Motor_Vorne); |
||
65 | break; |
||
66 | case 1: |
||
67 | i2c_write_byte(Motor_Hinten); |
||
68 | break; |
||
69 | case 2: |
||
70 | i2c_write_byte(Motor_Rechts); |
||
71 | break; |
||
72 | case 3: |
||
73 | i2c_write_byte(Motor_Links); |
||
74 | break; |
||
75 | } |
||
76 | break; |
||
77 | case 2: |
||
78 | i2c_stop(); |
||
79 | if (motor<4) twi_state = 0; |
||
80 | else motor = 0; |
||
81 | i2c_start(); |
||
82 | break; |
||
83 | |||
84 | //Liest Daten von Motor |
||
85 | case 3: |
||
86 | i2c_write_byte(0x53+(motorread*2)); |
||
87 | break; |
||
88 | case 4: |
||
89 | switch(motorread) |
||
90 | { |
||
91 | case 0: |
||
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 | } |
||
104 | break; |
||
105 | case 5: //1 Byte vom Motor lesen |
||
106 | motor_rx[motorread] = TWDR; |
||
107 | case 6: |
||
108 | switch(motorread) |
||
109 | { |
||
110 | case 0: |
||
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 | } |
||
123 | break; |
||
124 | case 7: //2 Byte vom Motor lesen |
||
125 | motor_rx[motorread+4] = TWDR; |
||
126 | motorread++; |
||
127 | if (motorread>3) motorread=0; |
||
128 | i2c_stop(); |
||
129 | twi_state = 0; |
||
130 | } |
||
131 | } |