Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

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