Subversion Repositories FlightCtrl

Rev

Rev 465 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 465 Rev 476
Line 1... Line 1...
1
/*############################################################################
1
/*############################################################################
2
############################################################################*/
2
############################################################################*/
Line 3... Line 3...
3
 
3
 
Line 4... Line 4...
4
#include "main.h"
4
#include "main.h"
5
 
5
 
6
unsigned char twi_state = 0;
-
 
7
unsigned char motor = 0;
6
volatile unsigned char twi_state = 0;
Line 8... Line 7...
8
unsigned char motorread = 0;
7
volatile unsigned char motor = 0;
9
unsigned char motor_rx[8];
8
volatile unsigned char motor_rx[8];
10
 
9
 
11
//############################################################################
10
//############################################################################
Line 41... Line 40...
41
    TWDR = byte;
40
    TWDR = byte;
42
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
41
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
43
}
42
}
Line 44... Line 43...
44
 
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
 
45
//############################################################################
60
//############################################################################
46
//Start I2C
61
//Start I2C
47
SIGNAL (TWI_vect)
62
SIGNAL (TWI_vect)
48
//############################################################################
63
//############################################################################
-
 
64
{
-
 
65
    static unsigned char motorread = 0;
49
{
66
 
50
    switch (twi_state++)        // First i2c_start from SendMotorData()
67
    switch (twi_state++) // First i2c_start from SendMotorData()
51
        {
68
    {
52
                // Master Transmit
69
        // Master Transmit
53
        case 0: // Address Slave SL+W
70
        case 0: // Address Slave SL+W
Line 70... Line 87...
70
                            break;
87
                            break;
71
                    }
88
                }
72
                break;
89
                break;
73
        case 2: // Repeat case 0+1 for all Slaves
90
        case 2: // Repeat case 0+1 for all Slaves
74
                if (motor < 4) twi_state = 0;
91
                if (motor < 4) twi_state = 0;
75
                i2c_start();    // Repeated start -> switch salve and switch Master Transmit <-> Master Receive respectively
92
                i2c_start(); // Repeated start -> switch salve or switch Master Transmit -> Master Receive
76
                break;
93
                break;
Line 77... Line 94...
77
                 
94
 
78
        // Master Receive
95
        // Master Receive
79
        case 3: // Address Slave SL+R
96
        case 3: // Address Slave SL+R
80
                i2c_write_byte(0x53+(motorread*2));
97
                i2c_write_byte(0x53+(motorread*2));
81
                break;
98
                break;
-
 
99
        case 4: //1. Byte vom Motor übertragen
-
 
100
                i2c_receive_byte();
-
 
101
                break;
82
        case 4: //1. Byte vom Motor lesen       
102
        case 5: // 1. Byte lesen und 2. Byte übertragen
83
                motor_rx[motorread] = TWDR;
103
                motor_rx[motorread] = TWDR;
84
                                TWCR |= (1<<TWINT);
104
                i2c_receive_last_byte();
85
                                break;
105
                break;
86
        case 5: //2. Byte vom Motor lesen       
106
        case 6: //2. Byte lesen
87
                motor_rx[motorread+4] = TWDR;
107
                motor_rx[motorread+4] = TWDR;
88
                motorread++;
108
                motorread++;
89
                                if (motorread > 3) motorread=0;
109
                if (motorread > 3) motorread=0;
90
                default:
110
        default: