Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
584 | Nick666 | 1 | /*############################################################################ |
2 | ############################################################################*/ |
||
3 | |||
4 | #include "main.h" |
||
5 | |||
6 | unsigned char twi_state = 0; |
||
7 | unsigned char motor = 0; |
||
8 | unsigned char motor_rx[8]; |
||
9 | |||
10 | //############################################################################ |
||
11 | //Initzialisieren der I2C (TWI) Schnittstelle |
||
12 | void i2c_init(void) |
||
13 | //############################################################################ |
||
14 | { |
||
15 | TWSR = 0; |
||
16 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
||
17 | } |
||
18 | |||
19 | //############################################################################ |
||
20 | //Start I2C |
||
21 | void i2c_start(void) |
||
22 | //############################################################################ |
||
23 | { |
||
24 | TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE); |
||
25 | } |
||
26 | |||
27 | //############################################################################ |
||
28 | //Stop I2C |
||
29 | void i2c_stop(void) |
||
30 | //############################################################################ |
||
31 | { |
||
32 | TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT); |
||
33 | } |
||
34 | |||
35 | void i2c_reset(void) |
||
36 | //############################################################################ |
||
37 | { |
||
38 | i2c_stop(); |
||
39 | twi_state = 0; |
||
40 | motor = TWDR; |
||
41 | motor = 0; |
||
42 | TWCR = 0x80; |
||
43 | TWAMR = 0; |
||
44 | TWAR = 0; |
||
45 | TWDR = 0; |
||
46 | TWSR = 0; |
||
47 | TWBR = 0; |
||
48 | i2c_init(); |
||
49 | i2c_start(); |
||
50 | i2c_write_byte(0); |
||
51 | } |
||
52 | |||
53 | //############################################################################ |
||
54 | //Write to I2C |
||
55 | void i2c_write_byte(char byte) |
||
56 | //############################################################################ |
||
57 | { |
||
58 | TWDR = byte; |
||
59 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
||
60 | } |
||
61 | |||
62 | //############################################################################ |
||
63 | // I2C receive byte and send ACK |
||
64 | void i2c_receive_byte(void) |
||
65 | //############################################################################ |
||
66 | { |
||
67 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA); |
||
68 | } |
||
69 | |||
70 | //############################################################################ |
||
71 | // I2C receive last byte and send NOT ACK |
||
72 | void i2c_receive_last_byte(void) |
||
73 | //############################################################################ |
||
74 | { |
||
75 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
||
76 | } |
||
77 | |||
78 | //############################################################################ |
||
79 | //Start I2C |
||
80 | SIGNAL (TWI_vect) |
||
81 | //############################################################################ |
||
82 | { |
||
83 | static unsigned char motorread = 0; |
||
84 | |||
85 | switch (twi_state++) // First i2c_start from SendMotorData() |
||
86 | { |
||
87 | // Master Transmit |
||
88 | case 0: // Address Slave SL+W |
||
89 | i2c_write_byte(0x52+(motor*2)); |
||
90 | break; |
||
91 | case 1: // Send Data |
||
92 | switch(motor++) |
||
93 | { |
||
94 | case 0: |
||
95 | i2c_write_byte(Motor_Vorne); |
||
96 | break; |
||
97 | case 1: |
||
98 | i2c_write_byte(Motor_Hinten); |
||
99 | break; |
||
100 | case 2: |
||
101 | i2c_write_byte(Motor_Rechts); |
||
102 | break; |
||
103 | case 3: |
||
104 | i2c_write_byte(Motor_Links); |
||
105 | break; |
||
106 | } |
||
107 | break; |
||
108 | case 2: // Repeat case 0+1 for all Slaves |
||
109 | if (motor < 4) twi_state = 0; |
||
110 | i2c_start(); // Repeated start -> switch salve or switch Master Transmit -> Master Receive |
||
111 | break; |
||
112 | |||
113 | // Master Receive |
||
114 | case 3: // Address Slave SL+R |
||
115 | i2c_write_byte(0x53+(motorread*2)); |
||
116 | break; |
||
117 | case 4: //1. Byte vom Motor übertragen |
||
118 | i2c_receive_byte(); |
||
119 | break; |
||
120 | case 5: // 1. Byte lesen und 2. Byte übertragen |
||
121 | motor_rx[motorread] = TWDR; |
||
122 | i2c_receive_last_byte(); |
||
123 | break; |
||
124 | case 6: //2. Byte lesen |
||
125 | motor_rx[motorread+4] = TWDR; |
||
126 | motorread++; |
||
127 | if (motorread > 3) motorread=0; |
||
128 | default: |
||
129 | i2c_stop(); |
||
130 | twi_state = 0; |
||
131 | I2CTimeout = 10; |
||
132 | motor = 0; |
||
133 | } |
||
134 | } |