Subversion Repositories FlightCtrl

Rev

Rev 205 | Go to most recent revision | Details | Compare with Previous | 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;
29
}
30
 
31
 
32
//############################################################################
33
//Wird in der SIGNAL (SIG_OVERFLOW0) aufgerufen
34
void MM3_timer0(void)
35
//############################################################################
36
{
37
        switch (MM3.STATE)
38
        {
39
        case MM3_RESET:                        
40
                PORTB |= (1<<PB2);      // J8 auf High, MM3 Reset
41
                MM3.STATE = MM3_START_TRANSFER;
42
                break;
43
 
44
        case MM3_START_TRANSFER:
45
                PORTB &= ~(1<<PB2);     // J8 auf Low           
46
 
206 Nick666 47
                if (MM3.AXIS == MM3_X) SPDR = 0x41;                     // Schreiben ins SPDR löst automatisch Übertragung (MOSI und MISO) aus
48
                else if (MM3.AXIS == MM3_Y) SPDR = 0x42;
49
                else if (MM3.AXIS == MM3_Z) SPDR = 0x43;
205 Nick666 50
                else {MM3.STATE == MM3_IDLE;break;}
51
 
206 Nick666 52
                MM3.DRDY = SetDelay(8);
205 Nick666 53
                MM3.STATE = MM3_WAIT_DRDY;
54
                break;
55
 
56
        case MM3_WAIT_DRDY:
57
                if (CheckDelay(MM3.DRDY)) {SPDR = 0x00;MM3.STATE = MM3_DRDY;} // Irgendwas ins SPDR, damit Übertragung ausgelöst wird
58
                break;
59
 
60
        case MM3_IDLE:         
61
                break;
62
        }
63
 
64
}
65
 
66
//############################################################################
67
//SPI byte ready
68
SIGNAL (SIG_SPI)
69
//############################################################################
206 Nick666 70
{      
205 Nick666 71
        switch (MM3.STATE)
72
        {      
73
        case MM3_DRDY:         
74
                if (MM3.AXIS == MM3_X) {MM3.x_axis=SPDR; MM3.x_axis<<=8; SPDR=0x00; MM3.STATE=MM3_X_BYTE2; break;}
75
                if (MM3.AXIS == MM3_Y) {MM3.y_axis=SPDR; MM3.y_axis<<=8; SPDR=0x00; MM3.STATE=MM3_Y_BYTE2; break;}
76
                if (MM3.AXIS == MM3_Z) {MM3.z_axis=SPDR; MM3.z_axis<<=8; SPDR=0x00; MM3.STATE=MM3_Z_BYTE2; break;}
77
 
78
        case MM3_X_BYTE2:              
79
                MM3.x_axis |= SPDR;                            
80
                MM3.AXIS = MM3_Y;
81
                MM3.STATE = MM3_RESET;
82
                break;
83
 
84
        case MM3_Y_BYTE2:
85
                MM3.y_axis |= SPDR;                            
86
                MM3.AXIS = MM3_Z;
87
                MM3.STATE = MM3_RESET;
88
                break;
89
 
90
        case MM3_Z_BYTE2:
91
                MM3.z_axis |= SPDR;                    
92
                MM3.AXIS = MM3_X;
93
                MM3.STATE = MM3_RESET;
94
                break; 
95
        }
96
 
97
}