Subversion Repositories FlightCtrl

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
205 Nick666 1
/*
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;
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;
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
8
along with this program. If not, see <http://www.gnu.org/licenses/>.
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
11
*/
12
 
13
#include "main.h"
14
 
15
MM3_struct MM3;
16
 
17
 
18
//############################################################################
19
//Initialisierung der SPI-Schnittstelle
20
void init_spi(void)
21
//############################################################################
22
{
23
        SPCR = (1<<SPIE)|(1<<SPE)|(1<<MSTR)|(1<<SPR1);
24
        SPSR = (1<<SPI2X);
25
        DDRB |= (1<<PB2)|(1<<PB7)|(1<<PB5); // J8, SCK, MOSI Ausgang (MicroMag3)
26
        DDRD |= (1<PD3);
27
        PORTD &= ~(1<<PD3);
28
 
29
        MM3.AXIS = MM3_X;
30
        MM3.STATE = MM3_RESET;
31
}
32
 
33
 
34
//############################################################################
35
//Wird in der SIGNAL (SIG_OVERFLOW0) aufgerufen
36
void MM3_timer0(void)
37
//############################################################################
38
{
39
        switch (MM3.STATE)
40
        {
41
        case MM3_RESET:                        
42
                PORTB |= (1<<PB2);      // J8 auf High, MM3 Reset
43
                MM3.STATE = MM3_START_TRANSFER;
44
                break;
45
 
46
        case MM3_START_TRANSFER:
47
                PORTB &= ~(1<<PB2);     // J8 auf Low           
48
 
49
                if (MM3.AXIS == MM3_X) SPDR = 0x51;                     // Schreiben ins SPDR löst automatisch Übertragung (MOSI und MISO) aus
50
                else if (MM3.AXIS == MM3_Y) SPDR = 0x52;
51
                else if (MM3.AXIS == MM3_Z) SPDR = 0x53;
52
                else {MM3.STATE == MM3_IDLE;break;}
53
 
54
                MM3.DRDY = SetDelay(16);
55
                MM3.STATE = MM3_WAIT_DRDY;
56
                break;
57
 
58
        case MM3_WAIT_DRDY:
59
                if (CheckDelay(MM3.DRDY)) {SPDR = 0x00;MM3.STATE = MM3_DRDY;} // Irgendwas ins SPDR, damit Übertragung ausgelöst wird
60
                break;
61
 
62
        case MM3_IDLE:         
63
                break;
64
        }
65
 
66
}
67
 
68
//############################################################################
69
//SPI byte ready
70
SIGNAL (SIG_SPI)
71
//############################################################################
72
{
73
        if (!(SPCR & (1<<MSTR)))
74
        {              
75
                SPCR |= (1<<MSTR);
76
        }
77
 
78
        switch (MM3.STATE)
79
        {      
80
        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;}
82
                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;}
84
 
85
        case MM3_X_BYTE2:              
86
                MM3.x_axis |= SPDR;                            
87
                MM3.AXIS = MM3_Y;
88
                MM3.STATE = MM3_RESET;
89
                break;
90
 
91
        case MM3_Y_BYTE2:
92
                MM3.y_axis |= SPDR;                            
93
                MM3.AXIS = MM3_Z;
94
                MM3.STATE = MM3_RESET;
95
                break;
96
 
97
        case MM3_Z_BYTE2:
98
                MM3.z_axis |= SPDR;                    
99
                MM3.AXIS = MM3_X;
100
                MM3.STATE = MM3_RESET;
101
                break; 
102
        }
103
 
104
}