STC15W408AS library 0.9.0
timer0_mode0.h
Go to the documentation of this file.
1#ifndef STC15_TIMER0_MODE0H
2#define STC15_TIMER0_MODE0H
3
26#include <sys.h>
27#include <bits.h>
28#include <interrupt.h>
29
30#include <stdint.h>
31#include <stdbool.h>
32
33#include <timer_structs.h>
34
40#define INTO P32
41
42//============================== Timer0 mode0 declarations begin ==========================
46
52#define timer0_mode0_12T_init() \
53{ \
54 enable_mcu_interrupts(); \
55 enable_timer0_interrupt(); \
56 TMOD &= 0xf0; \
57 bit_clr(AUXR, CBIT7); \
58}
59
65#define timer0_mode0_1T_init() \
66{ \
67 enable_mcu_interrupts(); \
68 enable_timer0_interrupt(); \
69 TMOD &= 0xf0; \
70 bit_set(AUXR, SBIT7); \
71}
73//============================== Timer0 mode0 declarations end ============================
74
75
80
85#define timer0_mode0_enable_P35_output() (bit_set(INT_CLKO, SBIT0))
86
92#define timer0_mode0_disable_P35_output() (bit_clr(INT_CLKO, CBIT0))
93
101#define is_timer0_mode0_P35_output_enabled() (test_if_bit_set(INT_CLKO, SBIT0))
102
109#define timer0_mode0_close_gate() (bit_set(TMOD, SBIT3))
110
117#define timer0_mode0_open_gate() (bit_clr(TMOD, CBIT3))
118
126#define is_timer0_mode0_gate_opened() (test_if_bit_cleared(TMOD, SBIT3))
127
129//============================== Timer0 get mode, divider, pins declarations end =======
130
131
132//============================== Timer0 run/stop declarations start =====================
136
137
147#define timer0_mode0_reload(ticks) \
148{ \
149 uint16_t value = 0xffff - ticks; \
150 /* Load timer high and low bytes value */ \
151 TL0 = value & 0xff; \
152 TH0 = (value >> 8) & 0xff; \
153}
154
164#define timer0_mode0_direct_reload(th0, tl0) \
165{ \
166 TL0 = tl0; \
167 TH0 = th0; \
168}
169
186#define timer0_mode0_start(ticks) \
187{ \
188 timer0_mode0_reload(ticks); \
189 \
190 TF0 = 0; /* clear timer overload flag */ \
191 TR0 = 1; /* set run timer flag */ \
192}
193
212#define timer0_mode0_direct_start(th0, tl0) \
213{ \
214 timer0_mode0_direct_reload(th0, tl0); \
215 \
216 TF0 = 0; \
217 TR0 = 1; \
218}
219
228#define timer0_mode0_stop() \
229{ \
230 TR0 = 0; /* clear run timer flag */ \
231 TF0 = 0; /* clear timer overload flag */ \
232}
233
241#define is_timer0_mode0_started() (TR0 == 1 && (is_timer0_mode0_gate_opened() || INT0 == 1))
242
244//============================== Timer0 run/stop declarations end =======================
245
246//============================== Timer0 run once declarations start =====================
250
266#define timer0_mode0_delay(ticks) \
267{ \
268 bool is_gate_opened = is_timer0_mode0_gate_opened(); \
269 \
270 timer0_mode0_open_gate(); \
271 \
272 timer0_mode0_start(ticks); \
273 /* Waiting for timer overloaded (timer overload flag set to 1) */ \
274 while(!TF0) \
275 { \
276 } \
277 timer0_mode0_stop(); \
278 \
279 if (!is_gate_opened) \
280 { \
281 timer0_mode0_close_gate(); \
282 } \
283}
285//============================== Timer0 run once declarations end =========================
286
287#endif