-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adc.c
222 lines (218 loc) · 4.85 KB
/
adc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "main.h"
void _KhoiTao_ADC(void)
{
_ThietLapKenh_ADC(AN4_AN3_AN2_AN1_AN0);
ADFM = 1; // dinh dang 10 bit ket qua chuyen doi AD canh phai -> LSB
ADCON0 = 0; // khoi dong thanh ghi ADCON0
ADCS0 = 1; // chon xung chuyen doi AD tu mach RC trong chip
ADCS1 = 1;
}
unsigned int _Read_ADC(char Kenh)
{
_ChonKenh(Kenh);
__delay_us(5); // cho ADC thuc hien lay mau
GODONE = 1; // bat dau thuc hien chuyen doi AD
while (GODONE); // cho ADC thuc hien chuyen doi xong
// tra ve ket qua ADC sau khi chuyen doi xong
return (((unsigned int)ADRESH) << 8) | ADRESL; // ghep byte thap va byte cao cua gia tri ADC
}
void _ChonKenh(char Kenh)
{
switch (Kenh)
{
case 0:
{
CHS0 = 0; // chon kenh chuyen doi AD la AN0;
CHS1 = 0;
CHS2 = 0;
ADON = 1; // cho phep ADC hoat dong
break;
}
case 1:
{
CHS0 = 1; // chon kenh chuyen doi AD la AN1;
CHS1 = 0;
CHS2 = 0;
ADON = 1; // cho phep ADC hoat dong
break;
}
case 2:
{
CHS0 = 0; // chon kenh chuyen doi AD la AN2;
CHS1 = 1;
CHS2 = 0;
ADON = 1; // cho phep ADC hoat dong
break;
}
case 3:
{
CHS0 = 1; // chon kenh chuyen doi AD la AN3;
CHS1 = 1;
CHS2 = 0;
ADON = 1; // cho phep ADC hoat dong
break;
}
case 4:
{
CHS0 = 0; // chon kenh chuyen doi AD la AN4;
CHS1 = 0;
CHS2 = 1;
ADON = 1; // cho phep ADC hoat dong
break;
}
case 5:
{
CHS0 = 1; // chon kenh chuyen doi AD la AN5;
CHS1 = 0;
CHS2 = 1;
ADON = 1; // cho phep ADC hoat dong
break;
}
case 6:
{
CHS0 = 0; // chon kenh chuyen doi AD la AN6;
CHS1 = 1;
CHS2 = 1;
ADON = 1; // cho phep ADC hoat dong
break;
}
case 7:
{
CHS0 = 1; // chon kenh chuyen doi AD la AN7;
CHS1 = 1;
CHS2 = 1;
ADON = 1; // cho phep ADC hoat dong
break;
}
default:
break;
}
}
void _ThietLapKenh_ADC(char config)
{
switch (config)
{
case All_Analog:
{
PCFG3 = 0;
PCFG2 = 0;
PCFG1 = 0;
PCFG0 = 0;
break;
}
case All_Analog_Vref_AN3:
{
PCFG3 = 0;
PCFG2 = 0;
PCFG1 = 0;
PCFG0 = 1;
break;
}
case AN4_AN3_AN2_AN1_AN0:
{
PCFG3 = 0;
PCFG2 = 0;
PCFG1 = 1;
PCFG0 = 0;
break;
}
case AN4_AN2_AN1_AN0_Vref_AN3:
{
PCFG3 = 0;
PCFG2 = 0;
PCFG1 = 1;
PCFG0 = 1;
break;
}
case AN3_AN2_AN1_AN0:
{
PCFG3 = 0;
PCFG2 = 1;
PCFG1 = 0;
PCFG0 = 0;
break;
}
case AN1_AN0_Vref_AN3:
{
PCFG3 = 0;
PCFG2 = 1;
PCFG1 = 0;
PCFG0 = 1;
break;
}
case No_All_Analog:
{
PCFG3 = 0;
PCFG2 = 1;
PCFG1 = 1;
PCFG0 = 1;
break;
}
case All_Analog_Vref_AN3_AN2:
{
PCFG3 = 1;
PCFG2 = 0;
PCFG1 = 0;
PCFG0 = 0;
break;
}
case AN5_AN4_AN3_AN2_AN1_AN0:
{
PCFG3 = 1;
PCFG2 = 0;
PCFG1 = 0;
PCFG0 = 1;
break;
}
case AN5_AN4_AN2_AN1_AN0_Vref_AN3:
{
PCFG3 = 1;
PCFG2 = 0;
PCFG1 = 1;
PCFG0 = 0;
break;
}
case AN5_AN4_AN1_AN0_Vref_AN3_AN2:
{
PCFG3 = 1;
PCFG2 = 0;
PCFG1 = 1;
PCFG0 = 1;
break;
}
case AN4_AN1_AN0_Vref_AN3_AN2:
{
PCFG3 = 1;
PCFG2 = 1;
PCFG1 = 0;
PCFG0 = 0;
break;
}
case AN1_AN0_Vref_AN3_AN2:
{
PCFG3 = 1;
PCFG2 = 1;
PCFG1 = 0;
PCFG0 = 1;
break;
}
case AN0:
{
PCFG3 = 1;
PCFG2 = 1;
PCFG1 = 1;
PCFG0 = 0;
break;
}
case AN0_Vref_AN3_AN2:
{
PCFG3 = 1;
PCFG2 = 1;
PCFG1 = 1;
PCFG0 = 1;
break;
}
default:
break;
}
}