STC15W408AS library
0.18.0
Loading...
Searching...
No Matches
include
adc.h
Go to the documentation of this file.
1
#ifndef STC15_ADCH
2
#define STC15_ADCH
3
22
#include <bits.h>
23
#include <
gpio.h
>
24
26
#define ADC_START_BIT_POS 3
28
#define ADC_START_BIT_MASK bit_mask(ADC_START_BIT_POS)
30
#define ADC_START_BIT (ADC_START_BIT_MASK)
31
33
#define ADC_FLAG_BIT_POS 4
35
#define ADC_FLAG_BIT_MASK bit_mask(ADC_FLAG_BIT_POS)
37
#define ADC_FLAG_BIT (ADC_FLAG_BIT_MASK)
38
40
#define ADRJ_BIT_POS 5
42
#define ADRJ_BIT_MASK bit_mask(ADRJ_BIT_POS)
44
#define ADRJ_BIT (ADRJ_BIT_MASK)
45
47
#define ADC_POWER_ON_MASK 0x80
48
50
#define ADC_LOW_BITS_COUNT 2
52
#define ADC_LOW_BITS_MASK 0x03
53
61
typedef
enum
62
{
64
ADC_SPEED_540
= 0b00000000,
66
ADC_SPEED_360
= 0b00100000,
68
ADC_SPEED_180
= 0b01000000,
70
ADC_SPEED_90
= 0b01100000
71
}
adc_speed_t
;
72
77
90
#define adc_init_input_only(p1_pin, adrj_flag, speed) \
91
do { \
92
/* Set input only pin mode */
\
93
pin_input_only_init(P1, p1_pin); \
94
\
95
/* Set P1ASF bit for using pin as ADC input */
\
96
bit_set(P1ASF, bit_mask(p1_pin)); \
97
\
98
/* Set ADC power on, speed and source channel pin */
\
99
ADC_CONTR = ADC_POWER_ON_MASK | p1_pin | speed; \
100
\
101
/* Set ADC_RES-ADC_RESL or ADC_RESL-ADC_RES */
\
102
/* result bits order */
\
103
adrj_flag ? \
104
bit_set(CLK_DIV, ADRJ_BIT) : \
105
bit_clr(CLK_DIV, ADRJ_BIT); \
106
} while(0)
107
121
#define adc_init_open_drain(p1_pin, adrj_flag, speed) \
122
do { \
123
/* Set input only pin mode */
\
124
pin_open_drain_init(P1, p1_pin); \
125
\
126
/* Set P1ASF bit for using pin as ADC input */
\
127
bit_set(P1ASF, 1 << p1_pin); \
128
\
129
/* Set ADC power on, speed and source channel pin */
\
130
ADC_CONTR = ADC_POWER_ON_MASK | p1_pin | speed; \
131
\
132
/* Set ADC_RES-ADC_RESL or ADC_RESL-ADC_RES */
\
133
/* result bits order */
\
134
adrj_flag ? \
135
bit_set(CLK_DIV, ADRJ_BIT) : \
136
bit_clr(CLK_DIV, ADRJ_BIT); \
137
} while(0)
138
147
#define adc_destroy(void) \
148
do { \
149
/* Stop ADC and Clear ADC result ready flag */
\
150
bit_clr(ADC_CONTR, ADC_START_BIT | ADC_FLAG_BIT); \
151
\
152
/* Set all gpio P1 port pins */
\
153
/* as general purpose pins mode */
\
154
P1ASF = 0; \
155
\
156
/* ADC power off*/
\
157
bit_clr(ADC_CONTR, ADC_POWER_ON_MASK); \
158
} while(0)
159
161
166
179
#define adc_read_sync(value) \
180
do { \
181
/* Clear ADC result ready flag */
\
182
bit_clr(ADC_CONTR, ADC_FLAG_BIT); \
183
/* Set ADC power on */
\
184
bit_set(ADC_CONTR, ADC_START_BIT); \
185
\
186
/* Waiting for ADC result is ready */
\
187
while (test_if_bit_cleared(ADC_CONTR, ADC_FLAG_BIT)); \
188
\
189
/* Return ADC result value */
\
190
*value = test_if_bit_set(CLK_DIV, ADRJ_BIT) ? \
191
(ADC_RESL << ADC_LOW_BITS_COUNT) | (ADC_RES & ADC_LOW_BITS_MASK) \
192
: \
193
(ADC_RES << ADC_LOW_BITS_COUNT) | (ADC_RESL & ADC_LOW_BITS_MASK);\
194
} while(0)
195
197
198
203
215
#define adc_async_read_start() \
216
do { \
217
if (!is_adc_async_read_started()) \
218
{ \
219
bit_set(ADC_CONTR, ADC_START_BIT); \
220
} \
221
} while(0);
222
230
#define is_adc_async_read_started() (test_if_bit_set(ADC_CONTR, ADC_START_BIT) && (ADC_CONTR & ADC_POWER_ON_MASK))
231
239
#define adc_async_read_finish() (bit_clr(ADC_CONTR, ADC_FLAG_BIT))
240
254
#define adc_async_get_result() (test_if_bit_set(CLK_DIV, ADRJ_BIT) ? (ADC_RESL << ADC_LOW_BITS_COUNT) | (ADC_RES & ADC_LOW_BITS_MASK) : (ADC_RES << ADC_LOW_BITS_COUNT) | (ADC_RESL & ADC_LOW_BITS_MASK))
256
257
#endif
gpio.h
adc_speed_t
adc_speed_t
ADC speed enum.
Definition
adc.h:62
ADC_SPEED_360
@ ADC_SPEED_360
Definition
adc.h:66
ADC_SPEED_540
@ ADC_SPEED_540
Definition
adc.h:64
ADC_SPEED_180
@ ADC_SPEED_180
Definition
adc.h:68
ADC_SPEED_90
@ ADC_SPEED_90
Definition
adc.h:70
Generated by
1.9.8