Subversion Repositories FlightCtrl

Rev

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

Rev 205 Rev 206
1
/*
1
/*
2
This program (files compass.c and compass.h) is free software; you can redistribute it and/or modify
2
This program (files compass.c and compass.h) is free software; you can redistribute it and/or modify
3
it under the terms of the GNU General Public License as published by the Free Software Foundation;
3
it under the terms of the GNU General Public License as published by the Free Software Foundation;
4
either version 3 of the License, or (at your option) any later version.
4
either version 3 of the License, or (at your option) any later version.
5
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
5
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
6
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7
GNU General Public License for more details. You should have received a copy of the GNU General Public License
7
GNU General Public License for more details. You should have received a copy of the GNU General Public License
8
along with this program. If not, see <http://www.gnu.org/licenses/>.
8
along with this program. If not, see <http://www.gnu.org/licenses/>.
9
 
9
 
10
Please note: All the other files for the project "Mikrokopter" by H.Buss are under the license (license_buss.txt) published by www.mikrokopter.de
10
Please note: All the other files for the project "Mikrokopter" by H.Buss are under the license (license_buss.txt) published by www.mikrokopter.de
11
*/
11
*/
12
 
12
 
13
#include "main.h"
13
#include "main.h"
14
 
14
 
15
MM3_struct MM3;
15
MM3_struct MM3;
16
 
16
 
17
 
17
 
18
//############################################################################
18
//############################################################################
19
//Initialisierung der SPI-Schnittstelle
19
//Initialisierung der SPI-Schnittstelle
20
void init_spi(void)
20
void init_spi(void)
21
//############################################################################
21
//############################################################################
22
{
22
{
23
        SPCR = (1<<SPIE)|(1<<SPE)|(1<<MSTR)|(1<<SPR1);
23
        SPCR = (1<<SPIE)|(1<<SPE)|(1<<MSTR)|(1<<SPR1);
24
        SPSR = (1<<SPI2X);
24
        SPSR = (1<<SPI2X);
25
        DDRB |= (1<<PB2)|(1<<PB7)|(1<<PB5); // J8, SCK, MOSI Ausgang (MicroMag3)
25
        DDRB |= (1<<PB2)|(1<<PB7)|(1<<PB5); // J8, SCK, MOSI Ausgang (MicroMag3)
26
        DDRD |= (1<PD3);
-
 
27
        PORTD &= ~(1<<PD3);
-
 
28
       
26
               
29
        MM3.AXIS = MM3_X;
27
        MM3.AXIS = MM3_X;
30
        MM3.STATE = MM3_RESET;
28
        MM3.STATE = MM3_RESET;
31
}
29
}
32
 
30
 
33
 
31
 
34
//############################################################################
32
//############################################################################
35
//Wird in der SIGNAL (SIG_OVERFLOW0) aufgerufen
33
//Wird in der SIGNAL (SIG_OVERFLOW0) aufgerufen
36
void MM3_timer0(void)
34
void MM3_timer0(void)
37
//############################################################################
35
//############################################################################
38
{
36
{
39
        switch (MM3.STATE)
37
        switch (MM3.STATE)
40
        {
38
        {
41
        case MM3_RESET:                        
39
        case MM3_RESET:                        
42
                PORTB |= (1<<PB2);      // J8 auf High, MM3 Reset
40
                PORTB |= (1<<PB2);      // J8 auf High, MM3 Reset
43
                MM3.STATE = MM3_START_TRANSFER;
41
                MM3.STATE = MM3_START_TRANSFER;
44
                break;
42
                break;
45
               
43
               
46
        case MM3_START_TRANSFER:
44
        case MM3_START_TRANSFER:
47
                PORTB &= ~(1<<PB2);     // J8 auf Low           
45
                PORTB &= ~(1<<PB2);     // J8 auf Low           
48
               
46
               
49
                if (MM3.AXIS == MM3_X) SPDR = 0x51;                     // Schreiben ins SPDR löst automatisch Übertragung (MOSI und MISO) aus
47
                if (MM3.AXIS == MM3_X) SPDR = 0x41;                     // Schreiben ins SPDR löst automatisch Übertragung (MOSI und MISO) aus
50
                else if (MM3.AXIS == MM3_Y) SPDR = 0x52;
48
                else if (MM3.AXIS == MM3_Y) SPDR = 0x42;
51
                else if (MM3.AXIS == MM3_Z) SPDR = 0x53;
49
                else if (MM3.AXIS == MM3_Z) SPDR = 0x43;
52
                else {MM3.STATE == MM3_IDLE;break;}
50
                else {MM3.STATE == MM3_IDLE;break;}
53
               
51
               
54
                MM3.DRDY = SetDelay(16);
52
                MM3.DRDY = SetDelay(8);
55
                MM3.STATE = MM3_WAIT_DRDY;
53
                MM3.STATE = MM3_WAIT_DRDY;
56
                break;
54
                break;
57
       
55
       
58
        case MM3_WAIT_DRDY:
56
        case MM3_WAIT_DRDY:
59
                if (CheckDelay(MM3.DRDY)) {SPDR = 0x00;MM3.STATE = MM3_DRDY;} // Irgendwas ins SPDR, damit Übertragung ausgelöst wird
57
                if (CheckDelay(MM3.DRDY)) {SPDR = 0x00;MM3.STATE = MM3_DRDY;} // Irgendwas ins SPDR, damit Übertragung ausgelöst wird
60
                break;
58
                break;
61
       
59
       
62
        case MM3_IDLE:         
60
        case MM3_IDLE:         
63
                break;
61
                break;
64
        }
62
        }
65
       
63
       
66
}
64
}
67
 
65
 
68
//############################################################################
66
//############################################################################
69
//SPI byte ready
67
//SPI byte ready
70
SIGNAL (SIG_SPI)
68
SIGNAL (SIG_SPI)
71
//############################################################################
69
//############################################################################
72
{
70
{      
73
        if (!(SPCR & (1<<MSTR)))
-
 
74
        {              
-
 
75
                SPCR |= (1<<MSTR);
-
 
76
        }
-
 
77
       
-
 
78
        switch (MM3.STATE)
71
        switch (MM3.STATE)
79
        {      
72
        {      
80
        case MM3_DRDY:         
73
        case MM3_DRDY:         
81
                if (MM3.AXIS == MM3_X) {MM3.x_axis=SPDR; MM3.x_axis<<=8; SPDR=0x00; MM3.STATE=MM3_X_BYTE2; break;}
74
                if (MM3.AXIS == MM3_X) {MM3.x_axis=SPDR; MM3.x_axis<<=8; SPDR=0x00; MM3.STATE=MM3_X_BYTE2; break;}
82
                if (MM3.AXIS == MM3_Y) {MM3.y_axis=SPDR; MM3.y_axis<<=8; SPDR=0x00; MM3.STATE=MM3_Y_BYTE2; break;}
75
                if (MM3.AXIS == MM3_Y) {MM3.y_axis=SPDR; MM3.y_axis<<=8; SPDR=0x00; MM3.STATE=MM3_Y_BYTE2; break;}
83
                if (MM3.AXIS == MM3_Z) {MM3.z_axis=SPDR; MM3.z_axis<<=8; SPDR=0x00; MM3.STATE=MM3_Z_BYTE2; break;}
76
                if (MM3.AXIS == MM3_Z) {MM3.z_axis=SPDR; MM3.z_axis<<=8; SPDR=0x00; MM3.STATE=MM3_Z_BYTE2; break;}
84
       
77
       
85
        case MM3_X_BYTE2:              
78
        case MM3_X_BYTE2:              
86
                MM3.x_axis |= SPDR;                            
79
                MM3.x_axis |= SPDR;                            
87
                MM3.AXIS = MM3_Y;
80
                MM3.AXIS = MM3_Y;
88
                MM3.STATE = MM3_RESET;
81
                MM3.STATE = MM3_RESET;
89
                break;
82
                break;
90
       
83
       
91
        case MM3_Y_BYTE2:
84
        case MM3_Y_BYTE2:
92
                MM3.y_axis |= SPDR;                            
85
                MM3.y_axis |= SPDR;                            
93
                MM3.AXIS = MM3_Z;
86
                MM3.AXIS = MM3_Z;
94
                MM3.STATE = MM3_RESET;
87
                MM3.STATE = MM3_RESET;
95
                break;
88
                break;
96
       
89
       
97
        case MM3_Z_BYTE2:
90
        case MM3_Z_BYTE2:
98
                MM3.z_axis |= SPDR;                    
91
                MM3.z_axis |= SPDR;                    
99
                MM3.AXIS = MM3_X;
92
                MM3.AXIS = MM3_X;
100
                MM3.STATE = MM3_RESET;
93
                MM3.STATE = MM3_RESET;
101
                break; 
94
                break; 
102
        }
95
        }
103
 
96
 
104
}
97
}
105
 
98