STC15W408AS library 0.9.0
timer0_mode3.h
Go to the documentation of this file.
1#ifndef STC15_TIMER0_MODE3H
2#define STC15_TIMER0_MODE3H
3
27#include <sys.h>
28#include <bits.h>
29#include <interrupt.h>
30
31#include <stdint.h>
32#include <stdbool.h>
33
34#include <timer_structs.h>
35
41#define INT0 P32
42
43//============================== Timer0 mode3 declarations begin ==========================
47
53#define timer0_mode3_12T_init() \
54{ \
55 enable_mcu_interrupts(); \
56 enable_timer0_interrupt(); \
57 TMOD &= 0xf0; \
58 TMOD |= 0x03; \
59 bit_clr(AUXR, CBIT7); \
60}
61
67#define timer0_mode3_1T_init() \
68{ \
69 enable_mcu_interrupts(); \
70 enable_timer0_interrupt(); \
71 TMOD &= 0xf0; \
72 TMOD |= 0x03; \
73 bit_set(AUXR, SBIT7); \
74}
76//============================== Timer0 mode3 declarations end ============================
77
78
83
88#define timer0_mode3_enable_P35_output() (bit_set(INT_CLKO, SBIT0))
89
95#define timer0_mode3_disable_P35_output() (bit_clr(INT_CLKO, CBIT0))
96
104#define is_timer0_mode3_P35_output_enabled() (test_if_bit_set(INT_CLKO, SBIT0))
105
112#define timer0_mode3_close_gate() (bit_set(TMOD, SBIT3))
113
120#define timer0_mode3_open_gate() (bit_clr(TMOD, CBIT3))
121
129#define is_timer0_mode3_gate_opened() (test_if_bit_cleared(TMOD, SBIT3))
130
132//============================== Timer0 get mode, divider, pins declarations end =======
133
134
135//============================== Timer0 run/stop declarations start =====================
139
140
150#define timer0_mode3_reload(ticks) \
151{ \
152 uint16_t value = 0xffff - ticks; \
153 /* Load timer high and low bytes value */ \
154 TL0 = value & 0xff; \
155 TH0 = (value >> 8) & 0xff; \
156}
157
167#define timer0_mode3_direct_reload(th0, tl0) \
168{ \
169 TH0 = th0; \
170 TL0 = tl0; \
171}
172
189#define timer0_mode3_start(ticks) \
190{ \
191 timer0_mode3_reload(ticks); \
192 \
193 TF0 = 0; /* clear timer overload flag */ \
194 TR0 = 1; /* set run timer flag */ \
195}
196
215#define timer0_mode3_direct_start(th0, tl0) \
216{ \
217 timer0_mode3_direct_reload(th0, tl0); \
218 \
219 TF0 = 0; \
220 TR0 = 1; \
221}
222
231#define timer0_mode3_stop() \
232{ \
233 TR0 = 0; /* clear run timer flag */ \
234 TF0 = 0; /* clear timer overload flag */ \
235}
236
244#define is_timer0_mode3_started() (TR0 == 1 && (is_timer0_mode3_gate_opened() || INT0 == 1))
245
247//============================== Timer0 run/stop declarations end =======================
248
249//============================== Timer0 run once declarations start =====================
253
269#define timer0_mode3_delay(ticks) \
270{ \
271 bool is_gate_opened = is_timer0_mode3_gate_opened(); \
272 \
273 timer0_mode3_open_gate(); \
274 \
275 timer0_mode3_start(ticks); \
276 /* Waiting for timer overloaded (timer overload flag set to 1) */ \
277 while(!TF0) \
278 { \
279 } \
280 timer0_mode3_stop(); \
281 \
282 if (!is_gate_opened) \
283 { \
284 timer0_mode3_close_gate(); \
285 } \
286}
288//============================== Timer0 run once declarations end =========================
289
290#endif