Subversion Repositories FlightCtrl

Rev

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)
206 Nick666 26
 
205 Nick666 27
        MM3.AXIS = MM3_X;
28
        MM3.STATE = MM3_RESET;
218 Nick666 29
 
30
        PORTD &= ~(1<<PD3);     // J5 einmalig auf Low -> SSNOT
205 Nick666 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
 
206 Nick666 49
                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 = 0x42;
51
                else if (MM3.AXIS == MM3_Z) SPDR = 0x43;
205 Nick666 52
                else {MM3.STATE == MM3_IDLE;break;}
53
 
206 Nick666 54
                MM3.DRDY = SetDelay(8);
205 Nick666 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
//############################################################################
206 Nick666 72
{      
205 Nick666 73
        switch (MM3.STATE)
74
        {      
75
        case MM3_DRDY:         
76
                if (MM3.AXIS == MM3_X) {MM3.x_axis=SPDR; MM3.x_axis<<=8; SPDR=0x00; MM3.STATE=MM3_X_BYTE2; break;}
77
                if (MM3.AXIS == MM3_Y) {MM3.y_axis=SPDR; MM3.y_axis<<=8; SPDR=0x00; MM3.STATE=MM3_Y_BYTE2; break;}
78
                if (MM3.AXIS == MM3_Z) {MM3.z_axis=SPDR; MM3.z_axis<<=8; SPDR=0x00; MM3.STATE=MM3_Z_BYTE2; break;}
79
 
80
        case MM3_X_BYTE2:              
81
                MM3.x_axis |= SPDR;                            
82
                MM3.AXIS = MM3_Y;
83
                MM3.STATE = MM3_RESET;
84
                break;
85
 
86
        case MM3_Y_BYTE2:
87
                MM3.y_axis |= SPDR;                            
88
                MM3.AXIS = MM3_Z;
89
                MM3.STATE = MM3_RESET;
90
                break;
91
 
92
        case MM3_Z_BYTE2:
93
                MM3.z_axis |= SPDR;                    
94
                MM3.AXIS = MM3_X;
95
                MM3.STATE = MM3_RESET;
96
                break; 
97
        }
98
 
99
}