Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | ingob | 1 | /*############################################################################ |
2 | ############################################################################*/ |
||
3 | |||
4 | #include "main.h" |
||
685 | killagreg | 5 | #include "twimaster.h" |
6 | #include "fc.h" |
||
1 | ingob | 7 | |
8 | unsigned char twi_state = 0; |
||
9 | unsigned char motor = 0; |
||
10 | unsigned char motorread = 0; |
||
11 | unsigned char motor_rx[8]; |
||
12 | |||
13 | //############################################################################ |
||
14 | //Initzialisieren der I2C (TWI) Schnittstelle |
||
15 | void i2c_init(void) |
||
16 | //############################################################################ |
||
17 | { |
||
18 | TWSR = 0; |
||
685 | killagreg | 19 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
1 | ingob | 20 | } |
21 | |||
22 | //############################################################################ |
||
23 | //Start I2C |
||
685 | killagreg | 24 | char i2c_start(void) |
1 | ingob | 25 | //############################################################################ |
26 | { |
||
27 | TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE); |
||
28 | return(0); |
||
29 | } |
||
30 | |||
31 | //############################################################################ |
||
32 | //Start I2C |
||
33 | void i2c_stop(void) |
||
34 | //############################################################################ |
||
35 | { |
||
36 | TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT); |
||
37 | } |
||
38 | |||
173 | holgerb | 39 | void i2c_reset(void) |
1 | ingob | 40 | //############################################################################ |
173 | holgerb | 41 | { |
685 | killagreg | 42 | i2c_stop(); |
173 | holgerb | 43 | twi_state = 0; |
44 | motor = TWDR; |
||
45 | motor = 0; |
||
46 | TWCR = 0x80; |
||
47 | TWAMR = 0; |
||
48 | TWAR = 0; |
||
49 | TWDR = 0; |
||
50 | TWSR = 0; |
||
51 | TWBR = 0; |
||
52 | i2c_init(); |
||
53 | i2c_start(); |
||
54 | i2c_write_byte(0); |
||
55 | } |
||
56 | |||
57 | //############################################################################ |
||
1 | ingob | 58 | //Start I2C |
59 | char i2c_write_byte(char byte) |
||
60 | //############################################################################ |
||
685 | killagreg | 61 | { |
1 | ingob | 62 | TWSR = 0x00; |
63 | TWDR = byte; |
||
64 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
||
685 | killagreg | 65 | |
1 | ingob | 66 | return(0); |
685 | killagreg | 67 | |
1 | ingob | 68 | } |
69 | |||
70 | //############################################################################ |
||
71 | //Start I2C |
||
72 | SIGNAL (TWI_vect) |
||
73 | //############################################################################ |
||
74 | { |
||
75 | switch (twi_state++) |
||
76 | { |
||
77 | case 0: |
||
78 | i2c_write_byte(0x52+(motor*2)); |
||
79 | break; |
||
80 | case 1: |
||
81 | switch(motor++) |
||
82 | { |
||
83 | case 0: |
||
84 | i2c_write_byte(Motor_Vorne); |
||
85 | break; |
||
685 | killagreg | 86 | case 1: |
1 | ingob | 87 | i2c_write_byte(Motor_Hinten); |
88 | break; |
||
89 | case 2: |
||
90 | i2c_write_byte(Motor_Rechts); |
||
91 | break; |
||
92 | case 3: |
||
93 | i2c_write_byte(Motor_Links); |
||
94 | break; |
||
95 | } |
||
96 | break; |
||
97 | case 2: |
||
98 | i2c_stop(); |
||
99 | if (motor<4) twi_state = 0; |
||
100 | else motor = 0; |
||
685 | killagreg | 101 | i2c_start(); |
102 | break; |
||
103 | |||
1 | ingob | 104 | //Liest Daten von Motor |
105 | case 3: |
||
106 | i2c_write_byte(0x53+(motorread*2)); |
||
107 | break; |
||
108 | case 4: |
||
109 | switch(motorread) |
||
110 | { |
||
111 | case 0: |
||
112 | i2c_write_byte(Motor_Vorne); |
||
113 | break; |
||
114 | case 1: |
||
115 | i2c_write_byte(Motor_Hinten); |
||
116 | break; |
||
117 | case 2: |
||
118 | i2c_write_byte(Motor_Rechts); |
||
119 | break; |
||
120 | case 3: |
||
121 | i2c_write_byte(Motor_Links); |
||
122 | break; |
||
123 | } |
||
124 | break; |
||
685 | killagreg | 125 | case 5: //1 Byte vom Motor lesen |
1 | ingob | 126 | motor_rx[motorread] = TWDR; |
173 | holgerb | 127 | |
1 | ingob | 128 | case 6: |
129 | switch(motorread) |
||
130 | { |
||
131 | case 0: |
||
132 | i2c_write_byte(Motor_Vorne); |
||
133 | break; |
||
134 | case 1: |
||
135 | i2c_write_byte(Motor_Hinten); |
||
136 | break; |
||
137 | case 2: |
||
138 | i2c_write_byte(Motor_Rechts); |
||
139 | break; |
||
140 | case 3: |
||
141 | i2c_write_byte(Motor_Links); |
||
142 | break; |
||
143 | } |
||
685 | killagreg | 144 | break; |
145 | case 7: //2 Byte vom Motor lesen |
||
1 | ingob | 146 | motor_rx[motorread+4] = TWDR; |
147 | motorread++; |
||
148 | if (motorread>3) motorread=0; |
||
149 | i2c_stop(); |
||
173 | holgerb | 150 | I2CTimeout = 10; |
1 | ingob | 151 | twi_state = 0; |
152 | } |
||
173 | holgerb | 153 | TWCR |= 0x80; |
1 | ingob | 154 | } |