8051-ELL库  beta 1.1.6
8051-ELL library API Document Center
STC8x_ADC.h
浏览该文件的文档.
1 /*-----------------------------------------------------------------------
2 | FILE DESCRIPTION |
3 -----------------------------------------------------------------------*/
4 /*----------------------------------------------------------------------
5  - File name : STC8x_ADC.h
6  - Author : zeweni
7  - Update date : 2020.04.23
8  - Copyright(C) : 2020-2021 zeweni. All rights reserved.
9 -----------------------------------------------------------------------*/
10 /*------------------------------------------------------------------------
11 | COPYRIGHT NOTICE |
12 ------------------------------------------------------------------------*/
13 /*
14  * Copyright (C) 2021, zeweni (17870070675@163.com)
15 
16  * This file is part of 8051 ELL low-layer libraries.
17 
18  * 8051 ELL low-layer libraries is free software: you can redistribute
19  * it and/or modify it under the terms of the Apache-2.0 License.
20 
21  * 8051 ELL low-layer libraries is distributed in the hope that it will
22  * be useful,but WITHOUT ANY WARRANTY; without even the implied warranty
23  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * Apache-2.0 License for more details.
25 
26  * You should have received a copy of the Apache-2.0 License.8051 ELL
27  * low-layer libraries. If not, see <http://www.apache.org/licenses/>.
28 **/
29 /*-----------------------------------------------------------------------
30 | INCLUDES |
31 -----------------------------------------------------------------------*/
32 #ifndef __STC8x_ADC_H_
33 #define __STC8x_ADC_H_
34 
35 /*--------------------------------------------------------
36 | @Description: ELL library core |
37 --------------------------------------------------------*/
38 #include "ELL_CORE.h"
39 
40 /*--------------------------------------------------------
41 | @Description: STC8x MCU Register |
42 --------------------------------------------------------*/
43 
44 #if (PER_LIB_MCU_MUODEL == STC8Ax)
45  #include "STC8Ax_REG.h"
46 #elif (PER_LIB_MCU_MUODEL == STC8Cx)
47  #include "STC8Cx_REG.h"
48 #elif (PER_LIB_MCU_MUODEL == STC8Fx)
49  #include "STC8Fx_REG.h"
50 #elif (PER_LIB_MCU_MUODEL == STC8Gx)
51  #include "STC8Gx_REG.h"
52 #elif (PER_LIB_MCU_MUODEL == STC8Hx)
53  #include "STC8Hx_REG.h"
54 #endif
55 
56 /*-----------------------------------------------------------------------
57 | DATA |
58 -----------------------------------------------------------------------*/
59 
60 #ifndef PER_LIB_MCU_MUODEL
63  #define PER_LIB_MCU_MUODEL STC8Ax
64 #endif
65 
66 
67 #ifndef PER_LIB_ADC_CTRL
70  #define PER_LIB_ADC_CTRL 1
71 #endif
72 
73 
74 #ifndef PER_LIB_ADC_INIT_CTRL
77  #define PER_LIB_ADC_INIT_CTRL 1
78 #endif
79 
80 
81 #ifndef PER_LIB_ADC_NVIC_CTRL
84  #define PER_LIB_ADC_NVIC_CTRL 1
85 #endif
86 
87 
88 #ifndef PER_LIB_ADC_WORK_CTRL
91  #define PER_LIB_ADC_WORK_CTRL 1
92 #endif
93 
94 
96 #define ADC_Channel_REV 0x0F
97 
98 /* STC8Ax */
99 #define ADC_Channel_P10_8Ax 0x00
100 #define ADC_Channel_P11_8Ax 0x01
101 #define ADC_Channel_P12_8Ax 0x02
102 #define ADC_Channel_P13_8Ax 0x03
103 #define ADC_Channel_P14_8Ax 0x04
104 #define ADC_Channel_P15_8Ax 0x05
105 #define ADC_Channel_P16_8Ax 0x06
106 #define ADC_Channel_P17_8Ax 0x07
107 #define ADC_Channel_P00_8Ax 0x08
108 #define ADC_Channel_P01_8Ax 0x09
109 #define ADC_Channel_P02_8Ax 0x0A
110 #define ADC_Channel_P03_8Ax 0x0B
111 #define ADC_Channel_P04_8Ax 0x0C
112 #define ADC_Channel_P05_8Ax 0x0D
113 #define ADC_Channel_P06_8Ax 0x0E
115 /* STC8G1K08A 8Pin */
116 #define ADC_Channel_P30_8G1K08A 0x00
117 #define ADC_Channel_P31_8G1K08A 0x01
118 #define ADC_Channel_P32_8G1K08A 0x02
119 #define ADC_Channel_P33_8G1K08A 0x03
120 #define ADC_Channel_P54_8G1K08A 0x04
121 #define ADC_Channel_P55_8G1K08A 0x05
123 /* STC8G1K08 16Pin and 20Pin / STC8G1K08T 20Pin */
124 #define ADC_Channel_P10_8G1K08_T 0x00
125 #define ADC_Channel_P11_8G1K08_T 0x01
126 #define ADC_Channel_P12_8G1K08_T 0x02
127 #define ADC_Channel_P13_8G1K08_T 0x03
128 #define ADC_Channel_P14_8G1K08_T 0x04
129 #define ADC_Channel_P15_8G1K08_T 0x05
130 #define ADC_Channel_P16_8G1K08_T 0x06
131 #define ADC_Channel_P17_8G1K08_T 0x07
132 #define ADC_Channel_P30_8G1K08_T 0x08
133 #define ADC_Channel_P31_8G1K08_T 0x09
134 #define ADC_Channel_P32_8G1K08_T 0x0A
135 #define ADC_Channel_P33_8G1K08_T 0x0B
136 #define ADC_Channel_P34_8G1K08_T 0x0C
137 #define ADC_Channel_P35_8G1K08_T 0x0D
138 #define ADC_Channel_P36_8G1K08_T 0x0E
140 /* STC8G2K64S4 16Pin / STC8G2K64S2 */
141 #define ADC_Channel_P10_8G2K64S2_S4 0x00
142 #define ADC_Channel_P11_8G2K64S2_S4 0x01
143 #define ADC_Channel_P12_8G2K64S2_S4 0x02
144 #define ADC_Channel_P13_8G2K64S2_S4 0x03
145 #define ADC_Channel_P14_8G2K64S2_S4 0x04
146 #define ADC_Channel_P15_8G2K64S2_S4 0x05
147 #define ADC_Channel_P16_8G2K64S2_S4 0x06
148 #define ADC_Channel_P17_8G2K64S2_S4 0x07
149 #define ADC_Channel_P00_8G2K64S2_S4 0x08
150 #define ADC_Channel_P01_8G2K64S2_S4 0x09
151 #define ADC_Channel_P02_8G2K64S2_S4 0x0A
152 #define ADC_Channel_P03_8G2K64S2_S4 0x0B
153 #define ADC_Channel_P04_8G2K64S2_S4 0x0C
154 #define ADC_Channel_P05_8G2K64S2_S4 0x0D
155 #define ADC_Channel_P06_8G2K64S2_S4 0x0E
157 /* STC8H1K28 */
158 #define ADC_Channel_P10_8H1K28 0x00
159 #define ADC_Channel_P11_8H1K28 0x01
160 #define ADC_Channel_P12_8H1K28 0x02
161 #define ADC_Channel_P13_8H1K28 0x03
162 #define ADC_Channel_P14_8H1K28 0x04
163 #define ADC_Channel_P15_8H1K28 0x05
164 #define ADC_Channel_P16_8H1K28 0x06
165 #define ADC_Channel_P17_8H1K28 0x07
166 #define ADC_Channel_P00_8H1K28 0x08
167 #define ADC_Channel_P01_8H1K28 0x09
168 #define ADC_Channel_P02_8H1K28 0x0A
169 #define ADC_Channel_P03_8H1K28 0x0B
171 /* STC8H1K08 */
172 #define ADC_Channel_P10_8H1K08 0x00
173 #define ADC_Channel_P11_8H1K08 0x01
174 #define ADC_Channel_P30_8H1K08 0x08
175 #define ADC_Channel_P31_8H1K08 0x09
176 #define ADC_Channel_P32_8H1K08 0x0A
177 #define ADC_Channel_P33_8H1K08 0x0B
178 #define ADC_Channel_P34_8H1K08 0x0C
179 #define ADC_Channel_P35_8H1K08 0x0D
180 #define ADC_Channel_P36_8H1K08 0x0E
182 /* STC8H3K64S2 / STC8H3K64S4 */
183 #define ADC_Channel_P10_8H3K64S2_S4 0x00
184 #define ADC_Channel_P11_8H3K64S2_S4 0x01
185 #define ADC_Channel_P12_8H3K64S2_S4 0x02
186 #define ADC_Channel_P16_8H3K64S2_S4 0x06
187 #define ADC_Channel_P17_8H3K64S2_S4 0x07
188 #define ADC_Channel_P00_8H3K64S2_S4 0x08
189 #define ADC_Channel_P01_8H3K64S2_S4 0x09
190 #define ADC_Channel_P02_8H3K64S2_S4 0x0A
191 #define ADC_Channel_P03_8H3K64S2_S4 0x0B
192 #define ADC_Channel_P04_8H3K64S2_S4 0x0C
193 #define ADC_Channel_P05_8H3K64S2_S4 0x0D
194 #define ADC_Channel_P06_8H3K64S2_S4 0x0E
196 /* STC8H8K64U / STC8H2K64T */
197 #define ADC_Channel_P10_8H8K64U_2K64T 0x00
198 #define ADC_Channel_P11_8H8K64U_2K64T 0x01
199 #define ADC_Channel_P54_8H8K64U_2K64T 0x02
200 #define ADC_Channel_P13_8H8K64U_2K64T 0x03
201 #define ADC_Channel_P14_8H8K64U_2K64T 0x04
202 #define ADC_Channel_P15_8H8K64U_2K64T 0x05
203 #define ADC_Channel_P16_8H8K64U_2K64T 0x06
204 #define ADC_Channel_P17_8H8K64U_2K64T 0x07
205 #define ADC_Channel_P00_8H8K64U_2K64T 0x08
206 #define ADC_Channel_P01_8H8K64U_2K64T 0x09
207 #define ADC_Channel_P02_8H8K64U_2K64T 0x0A
208 #define ADC_Channel_P03_8H8K64U_2K64T 0x0B
209 #define ADC_Channel_P04_8H8K64U_2K64T 0x0C
210 #define ADC_Channel_P05_8H8K64U_2K64T 0x0D
211 #define ADC_Channel_P06_8H8K64U_2K64T 0x0E
218 typedef enum
219 {
220  #if (PER_LIB_MCU_MUODEL == STC8Ax || PER_LIB_MCU_MUODEL == STC8Hx )
221  ADC_Acc_12Bits = 0x00,
222  ADC_Acc_11Bits = 0x01,
223  ADC_Acc_10Bits = 0x02,
224  ADC_Acc_9Bits = 0x03,
225  ADC_Acc_8Bits = 0x04
226  #elif (PER_LIB_MCU_MUODEL == STC8Gx )
227  ADC_Acc_10Bits = 0x00,
228  ADC_Acc_09Bits = 0x01,
229  ADC_Acc_08Bits = 0x02
230  #endif
232 
233 
238 typedef enum
239 {
240  ADC_Left = 0x00,
241  ADC_Right = 0x01
243 
244 
250 typedef struct
251 {
258 } ADC_InitType;
259 
260 /*-----------------------------------------------------------------------
261 | API FUNCTION |
262 -----------------------------------------------------------------------*/
263 
264 #if (PER_LIB_ADC_CTRL == 1)
265 
266  #if (PER_LIB_MCU_MUODEL == STC8Ax || PER_LIB_MCU_MUODEL == STC8Gx || PER_LIB_MCU_MUODEL == STC8Hx)
267 
268  #if (PER_LIB_ADC_INIT_CTRL == 1)
269 
281 
282  #endif
283 
284  #if (PER_LIB_ADC_WORK_CTRL == 1)
285 
301 
302 
314 
320  #define ADC_GET_CHANNEL() (ADC_CONTR & 0x0F)
321 
322 
332  #define ADC_CHANGE_CHANNEL(channel) \
333  do{ \
334  ADC_CONTR = (ADC_CONTR & 0xF0) | (channel); \
335  }while(0)
336 
337  #endif
338 
339  #if (PER_LIB_ADC_NVIC_CTRL == 1)
340 
350 
351 
358  #define ADC_GET_FLAG() (ADC_CONTR & 0x20)
359 
360 
366  #define ADC_CLEAR_FLAG() do{ADC_CONTR &= 0xDF;}while(0)
367 
368 
374  #define NVIC_ADC_CTRL(run) do{EADC = run;}while(0)
375 
376 
382  #define NVIC_ADC_PRI(pri) \
383  do{ \
384  IPH = (IPH & 0xDF) | ((pri & 0x02) << 4); \
385  IP = (IP & 0xDF) | ((pri & 0x01) << 5); \
386  }while(0)
387 
388  #endif
389 
390  #endif
391 
392 #endif
393 
394 
395 /*-----------------------------------------------------------------------
396 | END OF FLIE. (C) COPYRIGHT zeweni |
397 -----------------------------------------------------------------------*/
398 #endif
NVICPri_Type
Definition: ELL_CORE.h:99
BOOL
Definition: ELL_CORE.h:50
FSCSTATE
Definition: ELL_CORE.h:65
unsigned int uint16_t
Definition: ELL_TYPE.h:73
unsigned char uint8_t
Definition: ELL_TYPE.h:72
ADCAlign_Type
ADC输出结果对齐方向枚举体。
Definition: STC8x_ADC.h:239
@ ADC_Right
Definition: STC8x_ADC.h:241
@ ADC_Left
Definition: STC8x_ADC.h:240
uint16_t ADC_Get_Sample(uint8_t channel, ADCAcc_Type acc)
ADC获取采集结果函数(查询法)。
FSCSTATE ADC_Init(const ADC_InitType *adcx)
ADC外设初始化函数。
uint16_t ADC_Get_Sample_Interrupt(ADCAcc_Type acc)
ADC获取采集结果函数(中断法)。
FSCSTATE NVIC_ADC_Init(NVICPri_Type pri, BOOL run)
ADC中断初始化函数。
ADCAcc_Type
ADC采集精度枚举体。
Definition: STC8x_ADC.h:219
@ ADC_Acc_8Bits
Definition: STC8x_ADC.h:225
@ ADC_Acc_11Bits
Definition: STC8x_ADC.h:222
@ ADC_Acc_9Bits
Definition: STC8x_ADC.h:224
@ ADC_Acc_12Bits
Definition: STC8x_ADC.h:221
@ ADC_Acc_10Bits
Definition: STC8x_ADC.h:223
ADC初始化结构体句柄,初始化时请定义该句柄,并用其地址来传参。
Definition: STC8x_ADC.h:251
uint8_t Channel
Definition: STC8x_ADC.h:253
BOOL Power
Definition: STC8x_ADC.h:252
BOOL Run
Definition: STC8x_ADC.h:257
uint8_t Speed
Definition: STC8x_ADC.h:254
ADCAlign_Type Align
Definition: STC8x_ADC.h:256