Subversion Repositories idreammicro-avr

Rev

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

Rev Author Line No. Line
33 jlesech 1
/**************************************************************************//**
2
 * \brief ADC library.
3
 * \author Copyright (C) 2012  Julien Le Sech - www.idreammicro.com
4
 * \version 1.0
5
 * \date 20121029
6
 *
7
 * This file is part of the iDreamMicro library.
8
 *
9
 * This library is free software: you can redistribute it and/or modify it under
10
 * the terms of the GNU Lesser General Public License as published by the Free
11
 * Software Foundation, either version 3 of the License, or (at your option) any
12
 * later version.
13
 *
14
 * This library is distributed in the hope that it will be useful, but WITHOUT
15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
17
 * details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with this program. If not, see http://www.gnu.org/licenses/
21
 ******************************************************************************/
22
 
23
/**************************************************************************//**
24
 * \file adc.c
25
 ******************************************************************************/
26
 
27
/******************************************************************************
28
 * Header file inclusions.
29
 ******************************************************************************/
30
 
31
#include "../adc.h"
32
 
33
#include <useful/bits.h>
34
 
35
#include <avr/io.h>
36
 
37
#include <stdint.h>
38
 
39
/******************************************************************************
40
 * Public function definitions.
41
 ******************************************************************************/
42
 
43
/**************************************************************************//**
44
 * \fn void adc__single_channel_initialize(adc__channel_t channel)
45
 *
46
 * \brief Initialize ADC.
47
 *
48
 * \param channel   ADC channel.
49
 ******************************************************************************/
50
void
51
adc__single_channel_initialize
52
(
53
    adc__channel_t channel
54
){
55
    // Enable ADC.
56
    BIT__SET(ADCSRA, ADEN);
57
 
58
    // Clock prescaled by 16.
59
    // If Clock speed is 16 MHz, then ADC clock = 16 MHz / 16 = 1 MHz.
60
    ADCSRA |= 0b00000100;
61
 
62
    // Disable Digital Input on ADC Channel 0 to reduce power consumption
63
    DIDR0 = 0b00111111;
64
 
65
    // Disable Left-Adjust.
66
    ADMUX = 0b01000000;
67
 
68
    // Select ADC channel as input.
69
    ADMUX |= channel;
70
}
71
 
72
/**************************************************************************//**
73
 * \fn uint16_t adc__read(void)
74
 *
75
 * \brief Read ADC.
76
 *
77
 * \return Read value.
78
 ******************************************************************************/
79
uint16_t
80
adc__single_channel_read
81
(
82
    void
83
){
84
    // Start ADC Conversion.
85
    BIT__SET(ADCSRA, ADSC);
86
 
87
    // Wait till conversion is complete.
88
    //while ((ADCSRA & (1 << ADIF)) != 0x10);
89
    while (!BIT__TST(ADCSRA, ADIF));
90
 
91
    // Read the ADC Result.
92
    uint16_t value = ADC;
93
 
94
    // Clear ADC Conversion Interrupt Flag.
95
    BIT__RST(ADCSRA, ADIF);
96
 
97
    return value;
98
}