Subversion Repositories HomeAutomation

Rev

Rev 796 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 796 Rev 853
Line 6... Line 6...
6
#include <stdio.h>
6
#include <stdio.h>
7
#include <stdlib.h>
7
#include <stdlib.h>
8
 
8
 
9
#include <config.h>
9
#include <config.h>
10
#include "adc.h"
10
#include "adc.h"
-
 
11
 
-
 
12
#define BV(x) (1<<(x))
-
 
13
 
11
/*----------------------------------------------
14
/*----------------------------------------------
12
 * Functions
15
 * Functions
13
 * --------------------------------------------*/
16
 * --------------------------------------------*/
14
 
17
 
15
 static uint8_t currentChannel; //Used to keep the channel of the last measurement. This is used to determine if the channel needs to be changed.
18
 static uint8_t currentChannel; //Used to keep the channel of the last measurement. This is used to determine if the channel needs to be changed.
Line 40... Line 43...
40
{
43
{
41
    uint16_t adcdata;
44
    uint16_t adcdata;
42
   
45
   
43
    if (currentChannel != channel) //Did we use this channel the last time?
46
    if (currentChannel != channel) //Did we use this channel the last time?
44
    {
47
    {
45
        switch(channel)//If not, let's switch the admux
-
 
46
        {
-
 
47
#if ADCHAN0!=0
-
 
48
        case 0:
-
 
49
            /* Enable ADC0 */
48
        /* Set to 0 all mux registers */
50
            ADMUX &= ~((1<<MUX0)|(1<<MUX1)|(1<<MUX2)|(1<<MUX3));
49
        ADMUX &= ~( BV(MUX3) | BV(MUX2) | BV(MUX1) | BV(MUX0) );
51
            break;
-
 
52
#endif
-
 
53
#if ADCHAN1!=0
-
 
54
        case 1:
-
 
55
            /* Enable ADC1 */
-
 
56
            ADMUX |= (1<<MUX0);
-
 
57
            ADMUX &= ~((1<<MUX1)|(1<<MUX2)|(1<<MUX3));
-
 
58
            break;
-
 
59
#endif
-
 
60
#if ADCHAN2!=0
-
 
61
        case 2:
-
 
62
            /* Enable ADC2 */
-
 
63
            ADMUX |= (1<<MUX1);
-
 
64
            ADMUX &= ~((1<<MUX0)|(1<<MUX2)|(1<<MUX3));
-
 
65
            break;
-
 
66
#endif
-
 
67
#if ADCHAN3!=0
-
 
68
        case 3:
-
 
69
            /* Enable ADC3 */
-
 
70
            ADMUX |= (1<<MUX0)|(1<<MUX1);
-
 
71
            ADMUX &= ~((1<<MUX2)|(1<<MUX3));
-
 
72
            break;
-
 
73
#endif
-
 
74
#if ADCHAN4!=0
-
 
75
        case 4:
-
 
76
            /* Enable ADC4 */
-
 
77
            ADMUX |= (1<<MUX2);
-
 
78
            ADMUX &= ~((1<<MUX0)|(1<<MUX1)|(1<<MUX3));
-
 
79
            break;
-
 
80
#endif
-
 
81
#if ADCHAN5!=0
-
 
82
        case 5:
-
 
83
            /* Enable ADC5 */
-
 
84
            ADMUX |= (1<<MUX0)|(1<<MUX2);
-
 
85
            ADMUX &= ~((1<<MUX1)|(1<<MUX3));
-
 
86
            break;
-
 
87
#endif
-
 
88
#if ADCHAN6!=0
-
 
89
        case 6:
-
 
90
            /* Enable ADC6 (only for TQFP&MLF package) */
50
        /* Select channel, only first 8 channel modes are supported for now */
91
            ADMUX |= (1<<MUX1)|(1<<MUX2);
51
        ADMUX |= (channel & 0x07);
92
            ADMUX &= ~((1<<MUX0)|(1<<MUX3));
-
 
93
            break;
-
 
94
#endif
-
 
95
#if ADCHAN7!=0
-
 
96
        case 7:
-
 
97
            /* Enable ADC7 (only for TQFP&MLF package) */
-
 
98
            ADMUX |= (1<<MUX0)|(1<<MUX1)|(1<<MUX2);
-
 
99
            ADMUX &= ~(1<<MUX3);
-
 
100
            break;
-
 
101
#endif
-
 
102
        default:
-
 
103
            break;
-
 
104
            //Some kind of error message?
-
 
105
        }
-
 
106
       
52
       
107
        currentChannel = channel;   //Update the channel for the last measurement to the one we want this time
53
        currentChannel = channel;   //Update the channel for the last measurement to the one we want this time
108
        ADC_Get(channel);   //And do a dummy-read
54
        ADC_Get(channel);   //And do a dummy-read
109
    }
55
    }
110
 
56