Subversion Repositories FlightCtrl

Rev

Rev 683 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 683 Rev 685
Line 1... Line 1...
1
/*############################################################################
1
/*############################################################################
2
############################################################################*/
2
############################################################################*/
Line 3... Line 3...
3
 
3
 
-
 
4
#include "main.h"
-
 
5
#include "twimaster.h"
Line 4... Line 6...
4
#include "main.h"
6
#include "fc.h"
5
 
7
 
6
unsigned char twi_state = 0;
8
unsigned char twi_state = 0;
7
unsigned char motor = 0;
9
unsigned char motor = 0;
Line 12... Line 14...
12
//Initzialisieren der I2C (TWI) Schnittstelle
14
//Initzialisieren der I2C (TWI) Schnittstelle
13
void i2c_init(void)
15
void i2c_init(void)
14
//############################################################################
16
//############################################################################
15
{
17
{
16
  TWSR = 0;
18
  TWSR = 0;
17
  TWBR = ((SYSCLK/SCL_CLOCK)-16)/2;
19
  TWBR = ((SYSCLK/SCL_CLOCK)-16)/2;
18
}
20
}
Line 19... Line 21...
19
 
21
 
20
//############################################################################
22
//############################################################################
21
//Start I2C
23
//Start I2C
22
char i2c_start(void)
24
char i2c_start(void)
23
//############################################################################
25
//############################################################################
24
{
26
{
25
    TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE);
27
    TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE);
26
    return(0);
28
    return(0);
Line 35... Line 37...
35
}
37
}
Line 36... Line 38...
36
 
38
 
37
void i2c_reset(void)
39
void i2c_reset(void)
38
//############################################################################
40
//############################################################################
39
{
41
{
40
                 i2c_stop();                
42
                 i2c_stop();
41
                 twi_state = 0;
43
                 twi_state = 0;
42
                 motor = TWDR;
44
                 motor = TWDR;
43
                 motor = 0;
45
                 motor = 0;
44
                 TWCR = 0x80;
46
                 TWCR = 0x80;
Line 54... Line 56...
54
 
56
 
55
//############################################################################
57
//############################################################################
56
//Start I2C
58
//Start I2C
57
char i2c_write_byte(char byte)
59
char i2c_write_byte(char byte)
58
//############################################################################
60
//############################################################################
59
{
61
{
60
    TWSR = 0x00;
62
    TWSR = 0x00;
61
    TWDR = byte;
63
    TWDR = byte;
62
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
64
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
63
   
65
 
64
    return(0);
66
    return(0);
65
   
67
 
Line 66... Line 68...
66
}
68
}
67
 
69
 
68
//############################################################################
70
//############################################################################
Line 79... Line 81...
79
                switch(motor++)
81
                switch(motor++)
80
                    {
82
                    {
81
                    case 0:
83
                    case 0:
82
                            i2c_write_byte(Motor_Vorne);
84
                            i2c_write_byte(Motor_Vorne);
83
                            break;
85
                            break;
84
                    case 1:      
86
                    case 1:
85
                            i2c_write_byte(Motor_Hinten);
87
                            i2c_write_byte(Motor_Hinten);
86
                            break;
88
                            break;
87
                    case 2:
89
                    case 2:
88
                            i2c_write_byte(Motor_Rechts);
90
                            i2c_write_byte(Motor_Rechts);
89
                            break;
91
                            break;
Line 94... Line 96...
94
                break;
96
                break;
95
        case 2:
97
        case 2:
96
                i2c_stop();
98
                i2c_stop();
97
                if (motor<4) twi_state = 0;
99
                if (motor<4) twi_state = 0;
98
                else motor = 0;
100
                else motor = 0;
99
                i2c_start();  
101
                i2c_start();
100
                break;
102
                break;
101
                   
103
 
102
        //Liest Daten von Motor
104
        //Liest Daten von Motor
103
        case 3:
105
        case 3:
104
                i2c_write_byte(0x53+(motorread*2));
106
                i2c_write_byte(0x53+(motorread*2));
105
                break;
107
                break;
106
        case 4:
108
        case 4:
Line 118... Line 120...
118
                    case 3:
120
                    case 3:
119
                        i2c_write_byte(Motor_Links);
121
                        i2c_write_byte(Motor_Links);
120
                        break;
122
                        break;
121
                    }
123
                    }
122
                break;
124
                break;
123
        case 5: //1 Byte vom Motor lesen       
125
        case 5: //1 Byte vom Motor lesen
124
                motor_rx[motorread] = TWDR;
126
                motor_rx[motorread] = TWDR;
Line 125... Line 127...
125
 
127
 
126
        case 6:
128
        case 6:
127
                switch(motorread)
129
                switch(motorread)
Line 137... Line 139...
137
                        break;
139
                        break;
138
                    case 3:
140
                    case 3:
139
                        i2c_write_byte(Motor_Links);
141
                        i2c_write_byte(Motor_Links);
140
                        break;
142
                        break;
141
                    }
143
                    }
142
                break;  
144
                break;
143
        case 7: //2 Byte vom Motor lesen       
145
        case 7: //2 Byte vom Motor lesen
144
                motor_rx[motorread+4] = TWDR;
146
                motor_rx[motorread+4] = TWDR;
145
                motorread++;
147
                motorread++;
146
                if (motorread>3) motorread=0;
148
                if (motorread>3) motorread=0;
147
                i2c_stop();
149
                i2c_stop();
148
                I2CTimeout = 10;
150
                I2CTimeout = 10;