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