Subversion Repositories FlightCtrl

Rev

Rev 1 | Rev 918 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1 Rev 173
1
/*############################################################################
1
/*############################################################################
2
############################################################################*/
2
############################################################################*/
3
 
3
 
4
#include "main.h"
4
#include "main.h"
5
 
5
 
6
unsigned char twi_state = 0;
6
unsigned char twi_state = 0;
7
unsigned char motor = 0;
7
unsigned char motor = 0;
8
unsigned char motorread = 0;
8
unsigned char motorread = 0;
9
unsigned char motor_rx[8];
9
unsigned char motor_rx[8];
10
 
10
 
11
//############################################################################
11
//############################################################################
12
//Initzialisieren der I2C (TWI) Schnittstelle
12
//Initzialisieren der I2C (TWI) Schnittstelle
13
void i2c_init(void)
13
void i2c_init(void)
14
//############################################################################
14
//############################################################################
15
{
15
{
16
  TWSR = 0;
16
  TWSR = 0;
17
  TWBR = ((SYSCLK/SCL_CLOCK)-16)/2;
17
  TWBR = ((SYSCLK/SCL_CLOCK)-16)/2;
18
}
18
}
19
 
19
 
20
//############################################################################
20
//############################################################################
21
//Start I2C
21
//Start I2C
22
char i2c_start(void)
22
char i2c_start(void)
23
//############################################################################
23
//############################################################################
24
{
24
{
25
    TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE);
25
    TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE);
26
    return(0);
26
    return(0);
27
}
27
}
28
 
28
 
29
//############################################################################
29
//############################################################################
30
//Start I2C
30
//Start I2C
31
void i2c_stop(void)
31
void i2c_stop(void)
32
//############################################################################
32
//############################################################################
33
{
33
{
34
    TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT);
34
    TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT);
35
}
35
}
-
 
36
 
-
 
37
void i2c_reset(void)
-
 
38
//############################################################################
-
 
39
{
-
 
40
                 i2c_stop();                
-
 
41
                 twi_state = 0;
-
 
42
                 motor = TWDR;
-
 
43
                 motor = 0;
-
 
44
                 TWCR = 0x80;
-
 
45
                 TWAMR = 0;
-
 
46
                 TWAR = 0;
-
 
47
                 TWDR = 0;
-
 
48
                 TWSR = 0;
-
 
49
                 TWBR = 0;
-
 
50
                 i2c_init();
-
 
51
                 i2c_start();
-
 
52
                 i2c_write_byte(0);
-
 
53
}
36
 
54
 
37
//############################################################################
55
//############################################################################
38
//Start I2C
56
//Start I2C
39
char i2c_write_byte(char byte)
57
char i2c_write_byte(char byte)
40
//############################################################################
58
//############################################################################
41
{
59
{
42
    TWSR = 0x00;
60
    TWSR = 0x00;
43
    TWDR = byte;
61
    TWDR = byte;
44
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
62
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
45
   
63
   
46
    return(0);
64
    return(0);
47
   
65
   
48
}
66
}
49
 
67
 
50
//############################################################################
68
//############################################################################
51
//Start I2C
69
//Start I2C
52
SIGNAL (TWI_vect)
70
SIGNAL (TWI_vect)
53
//############################################################################
71
//############################################################################
54
{
72
{
55
    switch (twi_state++)
73
    switch (twi_state++)
56
        {
74
        {
57
        case 0:
75
        case 0:
58
                i2c_write_byte(0x52+(motor*2));
76
                i2c_write_byte(0x52+(motor*2));
59
                break;
77
                break;
60
        case 1:
78
        case 1:
61
                switch(motor++)
79
                switch(motor++)
62
                    {
80
                    {
63
                    case 0:
81
                    case 0:
64
                            i2c_write_byte(Motor_Vorne);
82
                            i2c_write_byte(Motor_Vorne);
65
                            break;
83
                            break;
66
                    case 1:      
84
                    case 1:      
67
                            i2c_write_byte(Motor_Hinten);
85
                            i2c_write_byte(Motor_Hinten);
68
                            break;
86
                            break;
69
                    case 2:
87
                    case 2:
70
                            i2c_write_byte(Motor_Rechts);
88
                            i2c_write_byte(Motor_Rechts);
71
                            break;
89
                            break;
72
                    case 3:
90
                    case 3:
73
                            i2c_write_byte(Motor_Links);
91
                            i2c_write_byte(Motor_Links);
74
                            break;
92
                            break;
75
                    }
93
                    }
76
                break;
94
                break;
77
        case 2:
95
        case 2:
78
                i2c_stop();
96
                i2c_stop();
79
                if (motor<4) twi_state = 0;
97
                if (motor<4) twi_state = 0;
80
                else motor = 0;
98
                else motor = 0;
81
                i2c_start();  
99
                i2c_start();  
82
                break;
100
                break;
83
                   
101
                   
84
        //Liest Daten von Motor
102
        //Liest Daten von Motor
85
        case 3:
103
        case 3:
86
                i2c_write_byte(0x53+(motorread*2));
104
                i2c_write_byte(0x53+(motorread*2));
87
                break;
105
                break;
88
        case 4:
106
        case 4:
89
                switch(motorread)
107
                switch(motorread)
90
                    {
108
                    {
91
                    case 0:
109
                    case 0:
92
                        i2c_write_byte(Motor_Vorne);
110
                        i2c_write_byte(Motor_Vorne);
93
                        break;
111
                        break;
94
                    case 1:
112
                    case 1:
95
                        i2c_write_byte(Motor_Hinten);
113
                        i2c_write_byte(Motor_Hinten);
96
                        break;
114
                        break;
97
                    case 2:
115
                    case 2:
98
                        i2c_write_byte(Motor_Rechts);
116
                        i2c_write_byte(Motor_Rechts);
99
                        break;
117
                        break;
100
                    case 3:
118
                    case 3:
101
                        i2c_write_byte(Motor_Links);
119
                        i2c_write_byte(Motor_Links);
102
                        break;
120
                        break;
103
                    }
121
                    }
104
                break;
122
                break;
105
        case 5: //1 Byte vom Motor lesen       
123
        case 5: //1 Byte vom Motor lesen       
106
                motor_rx[motorread] = TWDR;
124
                motor_rx[motorread] = TWDR;
-
 
125
 
107
        case 6:
126
        case 6:
108
                switch(motorread)
127
                switch(motorread)
109
                    {
128
                    {
110
                    case 0:
129
                    case 0:
111
                        i2c_write_byte(Motor_Vorne);
130
                        i2c_write_byte(Motor_Vorne);
112
                        break;
131
                        break;
113
                    case 1:
132
                    case 1:
114
                        i2c_write_byte(Motor_Hinten);
133
                        i2c_write_byte(Motor_Hinten);
115
                        break;
134
                        break;
116
                    case 2:
135
                    case 2:
117
                        i2c_write_byte(Motor_Rechts);
136
                        i2c_write_byte(Motor_Rechts);
118
                        break;
137
                        break;
119
                    case 3:
138
                    case 3:
120
                        i2c_write_byte(Motor_Links);
139
                        i2c_write_byte(Motor_Links);
121
                        break;
140
                        break;
122
                    }
141
                    }
123
                break;  
142
                break;  
124
        case 7: //2 Byte vom Motor lesen       
143
        case 7: //2 Byte vom Motor lesen       
125
                motor_rx[motorread+4] = TWDR;
144
                motor_rx[motorread+4] = TWDR;
126
                motorread++;
145
                motorread++;
127
                if (motorread>3) motorread=0;
146
                if (motorread>3) motorread=0;
128
                i2c_stop();
147
                i2c_stop();
-
 
148
                I2CTimeout = 10;
129
                twi_state = 0;
149
                twi_state = 0;
130
        }
150
        }
-
 
151
 TWCR |= 0x80;
131
}
152
}
132
 
153