STC15W408AS library 0.16.0
Loading...
Searching...
No Matches
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() \
54do { \
55 enable_mcu_interrupts(); \
56 enable_timer0_interrupt(); \
57 TMOD &= 0xf0; \
58 TMOD |= 0x03; \
59 bit_clr(AUXR, CBIT7); \
60} while(0)
61
67#define timer0_mode3_1T_init() \
68do { \
69 enable_mcu_interrupts(); \
70 enable_timer0_interrupt(); \
71 TMOD &= 0xf0; \
72 TMOD |= 0x03; \
73 bit_set(AUXR, SBIT7); \
74} while(0)
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) \
151do { \
152 uint16_t value = 0xffff - ticks; \
153 /* Load timer high and low bytes value */ \
154 TL0 = value & 0xff; \
155 TH0 = (value >> 8) & 0xff; \
156} while(0)
157
167#define timer0_mode3_direct_reload(th0, tl0) \
168do { \
169 TH0 = th0; \
170 TL0 = tl0; \
171} while(0)
172
193#define timer0_mode3_start(ticks) \
194do { \
195 timer0_mode3_reload(ticks); \
196 \
197 TF0 = 0; /* clear timer overload flag */ \
198 TR0 = 1; /* set run timer flag */ \
199} while(0)
200
223#define timer0_mode3_direct_start(th0, tl0) \
224do { \
225 timer0_mode3_direct_reload(th0, tl0); \
226 \
227 TF0 = 0; \
228 TR0 = 1; \
229} while(0)
230
239#define timer0_mode3_stop() \
240do { \
241 TR0 = 0; /* clear run timer flag */ \
242 TF0 = 0; /* clear timer overload flag */ \
243} while(0)
244
252#define is_timer0_mode3_started() (TR0 == 1 && (is_timer0_mode3_gate_opened() || INT0 == 1))
253
255//============================== Timer0 run/stop declarations end =======================
256
257//============================== Timer0 run once declarations start =====================
261
277#define timer0_mode3_delay(ticks) \
278do { \
279 bool is_gate_opened = is_timer0_mode3_gate_opened(); \
280 \
281 timer0_mode3_open_gate(); \
282 \
283 timer0_mode3_start(ticks); \
284 /* Waiting for timer overloaded (timer overload flag set to 1) */ \
285 while(!TF0) \
286 { \
287 } \
288 timer0_mode3_stop(); \
289 \
290 if (!is_gate_opened) \
291 { \
292 timer0_mode3_close_gate(); \
293 } \
294} while(0)
296//============================== Timer0 run once declarations end =========================
297
298#endif