Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
463 | Nick666 | 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 |
||
464 | Nick666 | 22 | void i2c_start(void) |
463 | Nick666 | 23 | //############################################################################ |
24 | { |
||
25 | TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE); |
||
26 | } |
||
27 | |||
28 | //############################################################################ |
||
464 | Nick666 | 29 | //Stop I2C |
463 | Nick666 | 30 | void i2c_stop(void) |
31 | //############################################################################ |
||
32 | { |
||
33 | TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT); |
||
34 | } |
||
35 | |||
36 | //############################################################################ |
||
464 | Nick666 | 37 | //Write to I2C |
38 | void i2c_write_byte(char byte) |
||
463 | Nick666 | 39 | //############################################################################ |
464 | Nick666 | 40 | { |
463 | Nick666 | 41 | TWDR = byte; |
42 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
||
43 | } |
||
44 | |||
45 | //############################################################################ |
||
46 | //Start I2C |
||
47 | SIGNAL (TWI_vect) |
||
48 | //############################################################################ |
||
49 | { |
||
465 | Nick666 | 50 | switch (twi_state++) // First i2c_start from SendMotorData() |
463 | Nick666 | 51 | { |
465 | Nick666 | 52 | // Master Transmit |
53 | case 0: // Address Slave SL+W |
||
463 | Nick666 | 54 | i2c_write_byte(0x52+(motor*2)); |
55 | break; |
||
465 | Nick666 | 56 | case 1: // Send Data |
463 | Nick666 | 57 | switch(motor++) |
58 | { |
||
59 | case 0: |
||
60 | i2c_write_byte(Motor_Vorne); |
||
61 | break; |
||
62 | case 1: |
||
63 | i2c_write_byte(Motor_Hinten); |
||
64 | break; |
||
65 | case 2: |
||
66 | i2c_write_byte(Motor_Rechts); |
||
67 | break; |
||
68 | case 3: |
||
69 | i2c_write_byte(Motor_Links); |
||
70 | break; |
||
71 | } |
||
72 | break; |
||
465 | Nick666 | 73 | case 2: // Repeat case 0+1 for all Slaves |
74 | if (motor < 4) twi_state = 0; |
||
75 | i2c_start(); // Repeated start -> switch salve and switch Master Transmit <-> Master Receive respectively |
||
76 | break; |
||
77 | |||
78 | // Master Receive |
||
79 | case 3: // Address Slave SL+R |
||
463 | Nick666 | 80 | i2c_write_byte(0x53+(motorread*2)); |
81 | break; |
||
465 | Nick666 | 82 | case 4: //1. Byte vom Motor lesen |
463 | Nick666 | 83 | motor_rx[motorread] = TWDR; |
465 | Nick666 | 84 | TWCR |= (1<<TWINT); |
464 | Nick666 | 85 | break; |
465 | Nick666 | 86 | case 5: //2. Byte vom Motor lesen |
463 | Nick666 | 87 | motor_rx[motorread+4] = TWDR; |
88 | motorread++; |
||
465 | Nick666 | 89 | if (motorread > 3) motorread=0; |
464 | Nick666 | 90 | default: |
463 | Nick666 | 91 | i2c_stop(); |
92 | twi_state = 0; |
||
464 | Nick666 | 93 | motor = 0; |
463 | Nick666 | 94 | } |
95 | } |