Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
463 | Nick666 | 1 | /*############################################################################ |
2 | ############################################################################*/ |
||
3 | |||
4 | #include "main.h" |
||
5 | |||
476 | Nick666 | 6 | volatile unsigned char twi_state = 0; |
7 | volatile unsigned char motor = 0; |
||
8 | volatile unsigned char motor_rx[8]; |
||
463 | Nick666 | 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 |
||
464 | Nick666 | 21 | void i2c_start(void) |
463 | Nick666 | 22 | //############################################################################ |
23 | { |
||
24 | TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE); |
||
25 | } |
||
26 | |||
27 | //############################################################################ |
||
464 | Nick666 | 28 | //Stop I2C |
463 | Nick666 | 29 | void i2c_stop(void) |
30 | //############################################################################ |
||
31 | { |
||
32 | TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT); |
||
33 | } |
||
34 | |||
35 | //############################################################################ |
||
464 | Nick666 | 36 | //Write to I2C |
37 | void i2c_write_byte(char byte) |
||
463 | Nick666 | 38 | //############################################################################ |
464 | Nick666 | 39 | { |
463 | Nick666 | 40 | TWDR = byte; |
41 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
||
42 | } |
||
43 | |||
44 | //############################################################################ |
||
476 | Nick666 | 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 | //############################################################################ |
||
463 | Nick666 | 61 | //Start I2C |
62 | SIGNAL (TWI_vect) |
||
63 | //############################################################################ |
||
64 | { |
||
476 | Nick666 | 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 |
||
463 | Nick666 | 71 | i2c_write_byte(0x52+(motor*2)); |
72 | break; |
||
476 | Nick666 | 73 | case 1: // Send Data |
463 | Nick666 | 74 | switch(motor++) |
476 | Nick666 | 75 | { |
463 | Nick666 | 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; |
||
476 | Nick666 | 88 | } |
463 | Nick666 | 89 | break; |
476 | Nick666 | 90 | case 2: // Repeat case 0+1 for all Slaves |
465 | Nick666 | 91 | if (motor < 4) twi_state = 0; |
476 | Nick666 | 92 | i2c_start(); // Repeated start -> switch salve or switch Master Transmit -> Master Receive |
465 | Nick666 | 93 | break; |
476 | Nick666 | 94 | |
465 | Nick666 | 95 | // Master Receive |
96 | case 3: // Address Slave SL+R |
||
463 | Nick666 | 97 | i2c_write_byte(0x53+(motorread*2)); |
98 | break; |
||
476 | Nick666 | 99 | case 4: //1. Byte vom Motor übertragen |
100 | i2c_receive_byte(); |
||
101 | break; |
||
102 | case 5: // 1. Byte lesen und 2. Byte übertragen |
||
463 | Nick666 | 103 | motor_rx[motorread] = TWDR; |
476 | Nick666 | 104 | i2c_receive_last_byte(); |
105 | break; |
||
106 | case 6: //2. Byte lesen |
||
463 | Nick666 | 107 | motor_rx[motorread+4] = TWDR; |
108 | motorread++; |
||
476 | Nick666 | 109 | if (motorread > 3) motorread=0; |
110 | default: |
||
463 | Nick666 | 111 | i2c_stop(); |
112 | twi_state = 0; |
||
476 | Nick666 | 113 | motor = 0; |
114 | } |
||
463 | Nick666 | 115 | } |