Subversion Repositories HomeAutomation

Rev

Rev 339 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 339 Rev 353
1
/*********************************************************************
1
/*********************************************************************
2
 *
2
 *
3
 *   Functions for reading ADC
3
 *   Functions for reading ADC
4
 *
4
 *
5
 *********************************************************************
5
 *********************************************************************
6
 * FileName:        $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/Include/adc.c $
6
 * FileName:        $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/Include/adc.c $
7
 * Last changed:    $LastChangedDate: 2007-02-25 20:51:08 +0100 (Sun, 25 Feb 2007) $
7
 * Last changed:    $LastChangedDate: 2007-03-03 19:07:14 +0100 (Sat, 03 Mar 2007) $
8
 * By:              $LastChangedBy: johboh $
8
 * By:              $LastChangedBy: johboh $
9
 * Revision:        $Revision: 339 $
9
 * Revision:        $Revision: 353 $
10
 ********************************************************************/
10
 ********************************************************************/
11
#include <adc.h>
11
#include <adc.h>
12
 
12
 
13
#ifdef USE_ADC
13
#ifdef USE_ADC
14
 
14
 
15
static unsigned long TEMP_VALUE;
15
unsigned long TEMP_VALUE;
16
static unsigned int TEMP_COUNT;
16
WORD TEMP_COUNT;
17
 
17
 
18
BOOL TEMP_DONE = FALSE;
18
BOOL TEMP_DONE = FALSE;
19
 
19
 
20
/*
20
/*
21
*   Function: adcInit
21
*   Function: adcInit
22
*
22
*
23
*   Input:  True or false to use external voltage reference or not. portCfg is portconfiguration.
23
*   Input:  True or false to use external voltage reference or not. portCfg is portconfiguration.
24
*   Output: none.
24
*   Output: none.
25
*   Pre-conditions: none
25
*   Pre-conditions: none
26
*   Affects: none.
26
*   Affects: none.
27
*   Depends: none.
27
*   Depends: none.
28
*/
28
*/
29
void adcInit(BOOL useExtRef,BYTE portCfg)
29
void adcInit(BOOL useExtRef,BYTE portCfg)
30
{
30
{
31
    // Setup ADC to use low interrupt prior and external ref or not.
31
    // Setup ADC to use low interrupt prior and external ref or not.
32
 
32
 
33
    ADCON1 = 0b00000000 | ((1 & useExtRef)<<4) | (0xF & portCfg);
33
    ADCON1 = 0b00000000 | ((1 & useExtRef)<<4) | (0xF & portCfg);
34
    ADCON0 = 0b00000000;
34
    ADCON0 = 0b00000000;
35
    ADCON2 = 0b10101011;
35
    ADCON2 = 0b10101011;
36
    //useExtRef
36
    //useExtRef
37
    ADCON0bits.ADON = 1;
37
    ADCON0bits.ADON = 1;
38
    PIR1bits.ADIF = 0;
38
    PIR1bits.ADIF = 0;
39
    PIE1bits.ADIE = 1;
39
    PIE1bits.ADIE = 1;
40
    IPR1bits.ADIP = 0; // Low priority
40
    IPR1bits.ADIP = 1; // high priority
41
}
41
}
42
 
42
 
43
 
43
 
44
/*
44
/*
45
*   Function: adcISR
45
*   Function: adcISR
46
*
46
*
47
*   Input:  ADC interrupt rotune. Need to be called from LOW ISR.
47
*   Input:  ADC interrupt rotune. Need to be called from LOW ISR.
48
*   Output: none.
48
*   Output: none.
49
*   Pre-conditions: none
49
*   Pre-conditions: none
50
*   Affects: none.
50
*   Affects: none.
51
*   Depends: none.
51
*   Depends: none.
52
*/
52
*/
53
void adcISR(void)
53
void adcISR(void)
54
{
54
{
55
    if (PIE1bits.ADIE && PIR1bits.ADIF)
55
    if (PIE1bits.ADIE==1 && PIR1bits.ADIF==1)
56
    {
56
    {
57
        PIR1bits.ADIF = 0;
57
        PIR1bits.ADIF = 0;
58
 
58
 
59
        TEMP_VALUE+=((int)ADRESH)<<8 | ADRESL;
59
        TEMP_VALUE+=((WORD)ADRESH<<8) | ADRESL;
60
        TEMP_COUNT++;
60
        TEMP_COUNT++;
61
 
61
 
62
        if (TEMP_COUNT>=TEMP_SAMPLES)
62
        if (TEMP_COUNT>=TEMP_SAMPLES)
63
        {
63
        {
64
            TEMP_VALUE = TEMP_VALUE/TEMP_COUNT;
64
            TEMP_VALUE = TEMP_VALUE/TEMP_COUNT;
65
            TEMP_DONE = TRUE;
65
            TEMP_DONE = TRUE;
66
        }
66
        }
67
        else
67
        else
68
        {
68
        {
69
            Delay10TCYx(100);
69
            Delay10TCYx(100);
70
            ADCON0bits.GO = 1;
70
            ADCON0bits.GO = 1;
71
        }
71
        }
72
 
72
 
73
    }
73
    }
74
}
74
}
75
 
75
 
76
 
76
 
77
/*
77
/*
78
*   Function: adcConvert
78
*   Function: adcConvert
79
*
79
*
80
*   Input:  Channel to start convert. Between 0 and 15
80
*   Input:  Channel to start convert. Between 0 and 15
81
*   Output: none.
81
*   Output: none.
82
*   Pre-conditions: none
82
*   Pre-conditions: none
83
*   Affects: none.
83
*   Affects: none.
84
*   Depends: none.
84
*   Depends: none.
85
*/
85
*/
86
BOOL adcConvert(BYTE channel)
86
BOOL adcConvert(BYTE channel)
87
{
87
{
88
    if (channel>15) return FALSE;
88
    if (channel>15) return FALSE;
89
 
89
 
90
    TEMP_VALUE = 0;
90
    TEMP_VALUE = 0;
91
    TEMP_COUNT = 0;
91
    TEMP_COUNT = 0;
92
 
92
 
93
    ADCON0 = (channel << 2)  | (ADCON0 & 0b11000011);
93
    ADCON0 = (channel << 2)  | (ADCON0 & 0b11000011);
94
 
94
 
95
    TEMP_DONE = FALSE;
95
    TEMP_DONE = FALSE;
96
 
96
 
97
    ADCON0bits.GO = 1;
97
    ADCON0bits.GO = 1;
98
 
98
 
99
    return TRUE;
99
    return TRUE;
100
}
100
}
101
 
101
 
102
 
102
 
103
/*
103
/*
104
*   Function: adcRead
104
*   Function: adcRead
105
*
105
*
106
*   Input:  none.
106
*   Input:  none.
107
*   Output: Raw read data.
107
*   Output: Raw read data.
108
*   Pre-conditions: none
108
*   Pre-conditions: none
109
*   Affects: none.
109
*   Affects: none.
110
*   Depends: none.
110
*   Depends: none.
111
*/
111
*/
112
int adcRead(void)
112
int adcRead(void)
113
{
113
{
114
    TEMP_DONE=FALSE;
114
    TEMP_DONE=FALSE;
115
    return TEMP_VALUE;
115
    return TEMP_VALUE;
116
}
116
}
117
 
117
 
118
 
118
 
119
/*
119
/*
120
*   Function: temperatureRead
120
*   Function: temperatureRead
121
*
121
*
122
*   Input:  none.
122
*   Input:  none.
123
*   Output: XX.Y C, where tenth is XX and decimal is Y
123
*   Output: XX.Y C, where tenth is XX and decimal is Y
124
*   Pre-conditions: none
124
*   Pre-conditions: none
125
*   Affects: none.
125
*   Affects: none.
126
*   Depends: none.
126
*   Depends: none.
127
*/
127
*/
128
void temperatureRead(signed char *tenth, BYTE *decimal)
128
void temperatureRead(signed char *tenth, BYTE *decimal)
129
{
129
{
130
    // assuming 2.5V ref and TC47
130
    // assuming 2.5V ref and TC47
131
    signed int i = 0, v11,v01,tfloat;
131
    signed int i = 0, v11,v01,tfloat;
132
 
132
 
133
    tfloat =((TEMP_VALUE * 39)>>4) - 499;
133
    tfloat =((TEMP_VALUE * 39)>>4) - 499;
134
   
134
   
135
    v11 = tfloat/10;
135
    v11 = tfloat/10;
136
    v01 = tfloat-v11*10;
136
    v01 = tfloat-v11*10;
137
 
137
 
138
         if (v01>=0 && v01<=2) v01 = 0;
138
         if (v01>=0 && v01<=2) v01 = 0;
139
    else if (v01>=3 && v01<=7) v01 = 5;
139
    else if (v01>=3 && v01<=7) v01 = 5;
140
    else  { v01 = 0; v11++;}
140
    else  { v01 = 0; v11++;}
141
 
141
 
142
    *tenth = (signed char)v11;
142
    *tenth = (signed char)v11;
143
    *decimal = (BYTE)v01;
143
    *decimal = (BYTE)v01;
144
 
144
 
145
        TEMP_DONE=FALSE;
145
        TEMP_DONE=FALSE;
146
}
146
}
147
 
147
 
148
unsigned long adcGetRaw()
148
unsigned long adcGetRaw()
149
{
149
{
150
    return TEMP_VALUE;
150
    return TEMP_VALUE;
151
}
151
}
152
 
152
 
153
 
153
 
154
/*
154
/*
155
*   Function: adcDone
155
*   Function: adcDone
156
*
156
*
157
*   Input:  none.
157
*   Input:  none.
158
*   Output: If adc convertion is done or not.
158
*   Output: If adc convertion is done or not.
159
*   Pre-conditions: none
159
*   Pre-conditions: none
160
*   Affects: none.
160
*   Affects: none.
161
*   Depends: none.
161
*   Depends: none.
162
*/
162
*/
163
BOOL adcDone(void)
163
BOOL adcDone(void)
164
{
164
{
165
    return TEMP_DONE;
165
    return TEMP_DONE;
166
}
166
}
167
 
167
 
168
#endif
168
#endif
169
 
169