Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
715 | Nick666 | 1 | /*############################################################################ |
2 | ############################################################################*/ |
||
3 | |||
4 | #include "main.h" |
||
5 | |||
6 | volatile unsigned char twi_state = 0; |
||
7 | volatile unsigned char motor = 0; |
||
8 | volatile 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 | //############################################################################ |
||
36 | //Write to I2C |
||
37 | void i2c_write_byte(char byte) |
||
38 | //############################################################################ |
||
39 | { |
||
40 | TWDR = byte; |
||
41 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
||
42 | } |
||
43 | |||
44 | //############################################################################ |
||
45 | // I2C receive byte and send ACK |
||
46 | void i2c_receive_byte(void) |
||
47 | //############################################################################ |
||
48 | { |
||
49 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA); |
||
50 | } |
||
51 | |||
52 | //############################################################################ |
||
53 | // I2C receive last byte and send NOT ACK |
||
54 | void i2c_receive_last_byte(void) |
||
55 | //############################################################################ |
||
56 | { |
||
57 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
||
58 | } |
||
59 | |||
60 | //############################################################################ |
||
61 | //Start I2C |
||
62 | SIGNAL (TWI_vect) |
||
63 | //############################################################################ |
||
64 | { |
||
65 | static unsigned char motorread = 0; |
||
66 | |||
67 | switch (twi_state++) // First i2c_start from SendMotorData() |
||
68 | { |
||
69 | // Master Transmit |
||
70 | case 0: // Address Slave SL+W |
||
71 | i2c_write_byte(0x52+(motor*2)); |
||
72 | break; |
||
73 | case 1: // Send Data |
||
74 | switch(motor++) |
||
75 | { |
||
76 | case 0: |
||
77 | i2c_write_byte(Motor_Vorne); |
||
78 | break; |
||
79 | case 1: |
||
80 | i2c_write_byte(Motor_Hinten); |
||
81 | break; |
||
82 | case 2: |
||
83 | i2c_write_byte(Motor_Rechts); |
||
84 | break; |
||
85 | case 3: |
||
86 | i2c_write_byte(Motor_Links); |
||
87 | break; |
||
88 | } |
||
89 | break; |
||
90 | case 2: // Repeat case 0+1 for all Slaves |
||
91 | if (motor < 4) twi_state = 0; |
||
92 | i2c_start(); // Repeated start -> switch salve or switch Master Transmit -> Master Receive |
||
93 | break; |
||
94 | |||
95 | // Master Receive |
||
96 | case 3: // Address Slave SL+R |
||
97 | i2c_write_byte(0x53+(motorread*2)); |
||
98 | break; |
||
99 | case 4: //1. Byte vom Motor übertragen |
||
100 | i2c_receive_byte(); |
||
101 | break; |
||
102 | case 5: // 1. Byte lesen und 2. Byte übertragen |
||
103 | motor_rx[motorread] = TWDR; |
||
104 | i2c_receive_last_byte(); |
||
105 | break; |
||
106 | case 6: //2. Byte lesen |
||
107 | motor_rx[motorread+4] = TWDR; |
||
108 | motorread++; |
||
109 | if (motorread > 3) motorread=0; |
||
110 | default: |
||
111 | i2c_stop(); |
||
112 | twi_state = 0; |
||
113 | motor = 0; |
||
114 | } |
||
115 | } |