STC15W408AS library 0.12.0
Loading...
Searching...
No Matches
Macros | Enumerations
UART1 Mode 1 with Timer2 1T

Macros

#define uart1_mode1_timer2_1T_init(pins)
 Initialize UART1 in Mode 1 with Timer2 1T configuration.
 
#define uart1_mode1_timer2_1T_start(baudrate)
 Start UART1 communication with standart baudrate value.
 
#define uart1_mode1_timer2_1T_ticks(baudrate)   (65536 - (((MAIN_Fosc / baudrate) >> 2) >> get_frequency_divider_scale()))
 Get UART1 ticks for given baudrate and frequency divider scale.
 
#define uart1_mode1_timer2_1T_start_ext(baudrate)
 Start UART1 communication with baudrate value.
 
#define uart1_mode1_timer2_1T_stop   (bit_clr(AUXR, CBIT4))
 Stop UART1 communication and disable Timer2.
 
#define is_uart1_mode1_timer2_1T_started()   (test_if_bit_set(AUXR, SBIT4))
 Is UART1 started?
 

Enumerations

enum  uart1_mode1_timer2_1t_baudrate_t {
  baudrate_1200 = 0xF6FF , baudrate_2400 = 0xFB80 , baudrate_4800 = 0xFDC0 , baudrate_9600 = 0xFEE0 ,
  baudrate_19200 = 0xFF70 , baudrate_38400 = 0xFFB8 , baudrate_57600 = 0xFFD0 , baudrate_115200 = 0xFFE8
}
 UART1 precalculated baudrates. More...
 

Detailed Description

Functions and data structures for UART1 communication in Mode 1 with Timer2 1T as baudrate generator. Dont use Timer2 for other purposes.

This module implements UART1 point-to-point communication. Relay and broadcast features are not supported by this module.

UART1 Mode 1 provides 8-bit asynchronous communication with variable baud rate. Sent data is transmitted to the TxD pin, and received data is read from the RxD pin. The frame data includes one start bit, 8 data bits, and one stop bit. 8-N-1

UART1 baudrate is determined by the T2 overflow rate. T2 overflow rate is configured by setting T2H and T2L registers.

THTL = 65536 - Sysclk/(4 * baudrate). Sysclk = master clock frequency / frequency divder.

THTL value is precalulated for standard baudrates with none freq divider.

In UART1 Mode 1, three sets of RxD/TxD pins can be used: RxD on P3.0/TxD on P3.1, RxD on P3.6/TxD on P3.7 and RxD on P1.6/TxD on P1.7.

Typical usage:

for (;;)
{
// Send or receive data
}
@ baudrate_9600
9600 baudrate
Definition uart1_mode1_timer2_12T.h:76
#define uart1_mode1_timer2_1T_start(baudrate)
Start UART1 communication with standart baudrate value.
Definition uart1_mode1_timer2_1T.h:146
#define uart1_mode1_timer2_1T_init(pins)
Initialize UART1 in Mode 1 with Timer2 1T configuration.
Definition uart1_mode1_timer2_1T.h:105
@ RxD_P30_TxD_P31
RxD on P3.0 and TxD on P3.1 pins.
Definition uart1_shared.h:25
See also
uart1_send_byte()
uart1_receive_byte()
is_uart1_send_byte_complete()
is_uart1_receive_byte_complete()
Author
Michael Golovanov

Macro Definition Documentation

◆ is_uart1_mode1_timer2_1T_started

#define is_uart1_mode1_timer2_1T_started ( )    (test_if_bit_set(AUXR, SBIT4))

Is UART1 started?

Returns
bool returns true if UART1 started otherwise false.

◆ uart1_mode1_timer2_1T_init

#define uart1_mode1_timer2_1T_init (   pins)
Value:
do { \
\
enable_mcu_interrupts(); \
enable_uart1_interrupt(); \
disable_timer2_interrupt(); \
\
PCON &= 0x3F; \
SCON = 0x50; \
\
/* The clock source of Timer 2 is SYSclk/12. AUXR.T2x12 = 0 */ \
/* AUXR.UART_M0x6 = 0 */ \
/* Timer2 is not started. AUXR.T2R = 0 */ \
/* Timer2 are used as timer. AUXR.T2_C/T = 0 */ \
AUXR &= 0xC2; \
\
/* Select Timer2 as UART1 baud rate generator. AUXR.S1ST2 = 1; */ \
bit_set(AUXR, SBIT0); \
/* The clock source of Timer 2 is SYSclk. AUXR.T2x12 = 1 */ \
bit_set(AUXR, SBIT2); \
\
/* Point-to-point mode */ \
bit_clr(CLK_DIV, CBIT4); \
\
/* Set AUXR1 bits 6, 7 to select RxD/TxD pins */ \
AUXR1 &= 0x3F; \
AUXR1 |= pins; \
} while(0)

Initialize UART1 in Mode 1 with Timer2 1T configuration.

The clock source of Timer2 is SYSclk. AUXR.T2x12 = 1. AUXR.UART_M0x6 = 0. Timer2 are used as timer. AUXR.T2_C/T = 0. Timer2 is not started. AUXR.T2R = 0.

See also
uart1_pins_t
Parameters
pinsPin configuration from uart1_pins_t enum
Note
Must be called before starting UART communication

◆ uart1_mode1_timer2_1T_start

#define uart1_mode1_timer2_1T_start (   baudrate)
Value:
do { \
/* Set TH TL values */ \
T2L = baudrate & 0xFF; \
T2H = baudrate >> 8; \
\
/* Start Timer2 */ \
bit_set(AUXR, SBIT4); \
} while(0)

Start UART1 communication with standart baudrate value.

Parameters
baudrateconst uart1_mode1_timer2_1t_baudrate_t Baudrate selection from uart1_mode1_timer2_1t_baudrate_t enum
Note
Before calling this function, uart1_mode1_timer2_1T_init() must be called.
Enum contains standard baudrates with precalculated THTL values for 1T mode
Warning
UART1 mode1 in this routine is biased by frequency divider.

◆ uart1_mode1_timer2_1T_start_ext

#define uart1_mode1_timer2_1T_start_ext (   baudrate)
Value:
do { \
const uint16_t ticks = uart1_mode1_timer2_1T_ticks(baudrate);\
\
/* Set TH TL values */ \
T2L = ticks & 0xFF; \
T2H = ticks >> 8; \
\
/* Start Timer2 */ \
bit_set(AUXR, SBIT4); \
} while (0)
#define uart1_mode1_timer2_1T_ticks(baudrate)
Get UART1 ticks for given baudrate and frequency divider scale.
Definition uart1_mode1_timer2_1T.h:171

Start UART1 communication with baudrate value.

Parameters
baudrateconst uint32_t Baudrate value. Value may be non standard (not in 1200, 9600 etc).
Note
Before calling this function, uart1_mode1_timer2_1T_init() must be called.
Attention
This function is not baudrate overflow safe.

◆ uart1_mode1_timer2_1T_stop

#define uart1_mode1_timer2_1T_stop   (bit_clr(AUXR, CBIT4))

Stop UART1 communication and disable Timer2.

Warning
Calling this function will terminate any ongoing communication

◆ uart1_mode1_timer2_1T_ticks

#define uart1_mode1_timer2_1T_ticks (   baudrate)    (65536 - (((MAIN_Fosc / baudrate) >> 2) >> get_frequency_divider_scale()))

Get UART1 ticks for given baudrate and frequency divider scale.

Ticks used as input parameter for uart1_mode1_timer2_1T_start_ext()

Parameters
baudrateuint32_t baudrate
Returns
uint16_t baudrate ticks
Attention
This function is not overflow safe. The value of (((MAIN_Fosc / baudrate) >> 2) >> get_frequency_divider_scale()) should be less than 65535 (uint16_t).

Enumeration Type Documentation

◆ uart1_mode1_timer2_1t_baudrate_t

UART1 precalculated baudrates.

Enumerator
baudrate_1200 

1200 baudrate

baudrate_2400 

2400 baudrate

baudrate_4800 

4800 baudrate

baudrate_9600 

9600 baudrate

baudrate_19200 

19200 baudrate

baudrate_38400 

38400 baudrate

baudrate_57600 

57600 baudrate

baudrate_115200 

115200 baudrate