Subversion Repositories MK3Mag

Rev

Rev 19 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 19 Rev 29
Line 52... Line 52...
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55
// +  POSSIBILITY OF SUCH DAMAGE.
55
// +  POSSIBILITY OF SUCH DAMAGE.
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57
//#include <stdlib.h>
57
#include <util/delay.h>
58
#include "analog.h"
58
#include "analog.h"
Line -... Line 59...
-
 
59
 
-
 
60
uint8_t AccPresent = 0;
59
 
61
 
60
void ADC_Init(void)
62
void ADC_Init(void)
-
 
63
{
-
 
64
        // The analog inputs have a VREF of 5V and a resolution of 10 bit (0...1024 counts)
-
 
65
        // i.e. 4.88mV/Count
61
{
66
 
62
        // set PortC 0,1,2,3 as input
67
        // set PortC 0,1,2,3 as input
63
        DDRC  &= ~((1<<DDC3)|(1<<DDC2)|(1<<DDC1)|(1<<DDC0));
68
        DDRC  &= ~((1<<DDC3)|(1<<DDC2)|(1<<DDC1)|(1<<DDC0));
64
        // set PortC 0,1,2,3 and 6,7 as tri state
69
        // set PortC 0,1,2,3 as tri state
Line 65... Line 70...
65
    PORTC &= ~((1<<PORTC3)|(1<<PORTC2)|(1<<PORTC1)|(1<<PORTC0));
70
    PORTC &= ~((1<<PORTC3)|(1<<PORTC2)|(1<<PORTC1)|(1<<PORTC0));
66
 
71
 
67
        // port PD5 and PD6 control the current direction of the test coils of the sensors
72
        // port PD5 and PD6 control the current direction of the test coils of the sensors
Line 76... Line 81...
76
        ADCSRA &= ~((1<<ADSC)|(1<<ADATE)|(1<<ADIE));
81
        ADCSRA &= ~((1<<ADSC)|(1<<ADATE)|(1<<ADIE));
77
        // Enable ADC (ADEN = 1) with SYSCLK/128 (ADPS2 = 1, ADPS1 = 1, ADPS0 = 1) and clear ready flag (ADIF = 1)
82
        // Enable ADC (ADEN = 1) with SYSCLK/128 (ADPS2 = 1, ADPS1 = 1, ADPS0 = 1) and clear ready flag (ADIF = 1)
78
        ADCSRA |= ((1<<ADEN)|(1<<ADIF)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0));
83
        ADCSRA |= ((1<<ADEN)|(1<<ADIF)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0));
79
        ADMUX = 0x00; // select ADC0
84
        ADMUX = 0x00; // select ADC0
80
        ADCSRA |= (1<<ADSC); // start conversion
85
        ADCSRA |= (1<<ADSC); // start conversion
-
 
86
 
-
 
87
 
-
 
88
        // Check if acceleration sensor is present
-
 
89
        // The output of the LIS3L02AL (MK3MAG V1.0) and the LIS344ALH (MK3MAG V1.1) is Vdd/5 +/- 10% per 1g.
-
 
90
        // The Vdd is 3.0V at this board therefore the sensitivity is 0.6V/g +/-10% that corresponds to 123 counts.
-
 
91
        // The offsets at 0g is VDD/2 (1.5V) that is 307 counts.
-
 
92
        // that yields to an ADC range of 184 to 430 counts.
-
 
93
 
-
 
94
        // pullup PC2(AccX) and PC3 (AccY)
-
 
95
        PORTC |= ((1<<PORTC3)|(1<<PORTC2));
-
 
96
        _delay_ms(10.0);
-
 
97
        // if ADC2 and ADC 3 is larger than 1000 counts (4.88V) no load is at the pins
-
 
98
        if((ADC_GetValue(ADC2) > 1000) && (ADC_GetValue(ADC3) > 1000)) AccPresent = 0;
-
 
99
        else AccPresent = 1;
-
 
100
        // set port back to tristate
-
 
101
        PORTC &= ~((1<<PORTC3)|(1<<PORTC2));
81
}
102
}
Line 82... Line 103...
82
 
103
 
83
 
104
 
84
uint16_t ADC_GetValue(ADChannel_t channel)
105
uint16_t ADC_GetValue(ADChannel_t channel)
85
{
106
{
86
        uint16_t value = 0;
107
        uint16_t value = 0;
87
        ADMUX = channel; // set muxer bits
108
        ADMUX = channel;                // set muxer bits
88
        ADCSRA |= (1<<ADIF); // clear ready-flag
109
        ADCSRA |= (1<<ADIF);    // clear ready-flag
89
        ADCSRA |= (1<<ADSC); // start conversion
110
        ADCSRA |= (1<<ADSC);    // start conversion
90
        while (((ADCSRA & (1<<ADIF)) == 0)); // wait for end of conversion
111
        while (((ADCSRA & (1<<ADIF)) == 0)); // wait for end of conversion
91
        value =  ADCW; // read adc result
112
        value =  ADCW;                  // read adc result