STC15W408AS library 0.15.0
Loading...
Searching...
No Matches
adc.h
Go to the documentation of this file.
1#ifndef STC15_ADCH
2#define STC15_ADCH
3
22#include <sys.h>
23#include <bits.h>
24#include <stdint.h>
25#include <stdbool.h>
26#include <gpio.h>
27
29#define ADC_START_BIT 3
31#define ADC_FLAG_BIT 4
33#define ADRJ_BIT 5
35#define ADC_POWER_ON_MSK 0x80
36
38#define ADC_LOW_BITS_COUNT 2
40#define ADC_LOW_BITS_MSK 0x03
41
49typedef enum
50{
52 ADC_SPEED_540 = 0b00000000,
54 ADC_SPEED_360 = 0b00100000,
56 ADC_SPEED_180 = 0b01000000,
58 ADC_SPEED_90 = 0b01100000
60
65
78#define adc_init_input_only(p1_pin, adrj_flag, speed) \
79do { \
80 /* Set input only pin mode */ \
81 pin_input_only_init(P1, p1_pin); \
82 \
83 /* Set P1ASF bit for using pin as ADC input */ \
84 bit_set(P1ASF, 1 << p1_pin); \
85 \
86 /* Set ADC power on, speed and source channel pin */\
87 ADC_CONTR = ADC_POWER_ON_MSK | p1_pin | speed; \
88 \
89 /* Set ADC_RES-ADC_RESL or ADC_RESL-ADC_RES */ \
90 /* result bits order */ \
91 adrj_flag ? \
92 bit_set(CLK_DIV, 1 << ADRJ_BIT) : \
93 bit_clr(CLK_DIV, ~(1 << ADRJ_BIT)); \
94} while(0)
95
109#define adc_init_open_drain(p1_pin, adrj_flag, speed) \
110do { \
111 /* Set input only pin mode */ \
112 pin_open_drain_init(P1, p1_pin); \
113 \
114 /* Set P1ASF bit for using pin as ADC input */ \
115 bit_set(P1ASF, 1 << p1_pin); \
116 \
117 /* Set ADC power on, speed and source channel pin */\
118 ADC_CONTR = ADC_POWER_ON_MSK | p1_pin | speed; \
119 \
120 /* Set ADC_RES-ADC_RESL or ADC_RESL-ADC_RES */ \
121 /* result bits order */ \
122 adrj_flag ? \
123 bit_set(CLK_DIV, 1 << ADRJ_BIT) : \
124 bit_clr(CLK_DIV, ~(1 << ADRJ_BIT)); \
125} while(0)
126
135#define adc_destroy(void) \
136do { \
137 /* Stop ADC */ \
138 bit_clr(ADC_CONTR, ~(1 << ADC_START_BIT)); \
139 /* Clear ADC result ready flag */ \
140 bit_clr(ADC_CONTR, ~(1 << ADC_FLAG_BIT)); \
141 \
142 /* Set all gpio P1 port pins */ \
143 /* as general purpose pins mode */ \
144 P1ASF = 0; \
145 \
146 /* ADC power off*/ \
147 bit_clr(ADC_CONTR, ~ADC_POWER_ON_MSK); \
148} while(0)
149
151
156
169#define adc_read_sync(value) \
170do { \
171 /* Clear ADC result ready flag */ \
172 bit_clr(ADC_CONTR, ~(1 << ADC_FLAG_BIT)); \
173 /* Set ADC power on */ \
174 bit_set(ADC_CONTR, 1 << ADC_START_BIT); \
175 \
176 /* Waiting for ADC result is ready */ \
177 while (test_if_bit_cleared(ADC_CONTR, 1 << ADC_FLAG_BIT)); \
178 \
179 /* Return ADC result value */ \
180 *value = test_if_bit_set(CLK_DIV, 1 << ADRJ_BIT) ? \
181 (ADC_RESL << ADC_LOW_BITS_COUNT) | (ADC_RES & ADC_LOW_BITS_MSK) \
182 : \
183 (ADC_RES << ADC_LOW_BITS_COUNT) | (ADC_RESL & ADC_LOW_BITS_MSK);\
184} while(0)
185
187
188
193
205#define adc_async_read_start() \
206do { \
207 if (!is_adc_async_read_started()) \
208 { \
209 bit_set(ADC_CONTR, 1 << ADC_START_BIT); \
210 } \
211} while(0);
212
220#define is_adc_async_read_started() (test_if_bit_set(ADC_CONTR, 1 << ADC_START_BIT) && (ADC_CONTR & ADC_POWER_ON_MSK))
221
229#define adc_async_read_finish() (bit_clr(ADC_CONTR, ~(1 << ADC_FLAG_BIT)))
230
244#define adc_async_get_result() (test_if_bit_set(CLK_DIV, 1 << ADRJ_BIT) ? (ADC_RESL << ADC_LOW_BITS_COUNT) | (ADC_RES & ADC_LOW_BITS_MSK) : (ADC_RES << ADC_LOW_BITS_COUNT) | (ADC_RESL & ADC_LOW_BITS_MSK))
246
247#endif
adc_speed_t
ADC speed enum.
Definition adc.h:50
@ ADC_SPEED_360
Definition adc.h:54
@ ADC_SPEED_540
Definition adc.h:52
@ ADC_SPEED_180
Definition adc.h:56
@ ADC_SPEED_90
Definition adc.h:58