Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
440 | Nick666 | 1 | /*############################################################################ |
2 | ############################################################################*/ |
||
3 | |||
4 | #include "main.h" |
||
5 | |||
6 | unsigned char motor_rx[8]; |
||
7 | |||
8 | //############################################################################ |
||
9 | //Initzialisieren der I2C (TWI) Schnittstelle |
||
10 | void i2c_init(void) |
||
11 | //############################################################################ |
||
12 | { |
||
13 | TWSR = 0; |
||
14 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
||
15 | } |
||
16 | |||
17 | //############################################################################ |
||
18 | //Start I2C |
||
441 | Nick666 | 19 | void i2c_start(void) |
440 | Nick666 | 20 | //############################################################################ |
21 | { |
||
22 | TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE); |
||
23 | } |
||
24 | |||
25 | //############################################################################ |
||
441 | Nick666 | 26 | //Stop I2C |
440 | Nick666 | 27 | void i2c_stop(void) |
28 | //############################################################################ |
||
29 | { |
||
30 | TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT); |
||
31 | } |
||
32 | |||
33 | //############################################################################ |
||
441 | Nick666 | 34 | //Write to I2C |
35 | void i2c_write_byte(char byte) |
||
440 | Nick666 | 36 | //############################################################################ |
441 | Nick666 | 37 | { |
440 | Nick666 | 38 | TWDR = byte; |
39 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
||
40 | } |
||
41 | |||
42 | //############################################################################ |
||
441 | Nick666 | 43 | //ISR |
440 | Nick666 | 44 | SIGNAL (TWI_vect) |
45 | //############################################################################ |
||
46 | { |
||
441 | Nick666 | 47 | static unsigned char twi_state = 0; |
48 | static unsigned char motor = 0; |
||
49 | static unsigned char motorread = 0; |
||
50 | |||
440 | Nick666 | 51 | switch (twi_state++) |
52 | { |
||
53 | case 0: |
||
54 | i2c_write_byte(0x52+(motor*2)); |
||
55 | break; |
||
56 | case 1: |
||
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; |
||
73 | case 2: |
||
74 | if (motor<4) twi_state = 0; |
||
75 | i2c_start(); |
||
76 | break; |
||
77 | |||
78 | //Liest Daten von Motor |
||
79 | case 3: |
||
80 | i2c_write_byte(0x53+(motorread*2)); |
||
81 | break; |
||
82 | case 4: |
||
441 | Nick666 | 83 | i2c_write_byte(0xFF); |
440 | Nick666 | 84 | break; |
441 | Nick666 | 85 | case 5: // 1. Byte vom Motor lesen |
440 | Nick666 | 86 | motor_rx[motorread] = TWDR; |
441 | Nick666 | 87 | i2c_write_byte(0xFF); |
88 | break; |
||
89 | case 6: // 2. Byte vom Motor lesen |
||
440 | Nick666 | 90 | motor_rx[motorread+4] = TWDR; |
91 | motorread++; |
||
92 | if (motorread>3) motorread=0; |
||
441 | Nick666 | 93 | default: |
440 | Nick666 | 94 | i2c_stop(); |
95 | twi_state = 0; |
||
441 | Nick666 | 96 | motor = 0; |
440 | Nick666 | 97 | } |
98 | } |