Rev 463 | Go to most recent revision | Details | Compare with Previous | 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 | { |
||
50 | switch (twi_state++) |
||
51 | { |
||
52 | case 0: |
||
53 | i2c_write_byte(0x52+(motor*2)); |
||
54 | break; |
||
55 | case 1: |
||
56 | switch(motor++) |
||
57 | { |
||
58 | case 0: |
||
59 | i2c_write_byte(Motor_Vorne); |
||
60 | break; |
||
61 | case 1: |
||
62 | i2c_write_byte(Motor_Hinten); |
||
63 | break; |
||
64 | case 2: |
||
65 | i2c_write_byte(Motor_Rechts); |
||
66 | break; |
||
67 | case 3: |
||
68 | i2c_write_byte(Motor_Links); |
||
69 | break; |
||
70 | } |
||
71 | break; |
||
72 | case 2: |
||
73 | if (motor<4) twi_state = 0; |
||
74 | i2c_start(); |
||
75 | break; |
||
76 | |||
77 | //Liest Daten von Motor |
||
78 | case 3: |
||
79 | i2c_write_byte(0x53+(motorread*2)); |
||
80 | break; |
||
81 | case 4: |
||
464 | Nick666 | 82 | i2c_write_byte(0xFF); |
463 | Nick666 | 83 | break; |
464 | Nick666 | 84 | case 5: //1. Byte vom Motor lesen |
463 | Nick666 | 85 | motor_rx[motorread] = TWDR; |
464 | Nick666 | 86 | i2c_write_byte(0xFF); |
87 | break; |
||
88 | case 6: //2. Byte vom Motor lesen |
||
463 | Nick666 | 89 | motor_rx[motorread+4] = TWDR; |
90 | motorread++; |
||
464 | Nick666 | 91 | if (motorread>3) motorread=0; |
92 | default: |
||
463 | Nick666 | 93 | i2c_stop(); |
94 | twi_state = 0; |
||
464 | Nick666 | 95 | motor = 0; |
463 | Nick666 | 96 | } |
97 | } |