HT8 MCU Timer Module Application Note(3) – How to use
the TM Timer Mode to implement Software UART Function

HT8 MCU Timer Module Application Note (3) –Using the TM Timer Mode to implement aSoftware UART Function
D/N：AN0446EIntroduction
The Holtek HT8 MCUs provide various types of Time Modules, such as the Compact
Type TM, Standard Type TM and Periodic Type TM. In actual product applications,communication with peripheral devices is often implemented using the fully integrated
2SPI, I C and UART interface functions. This application note shows how to use the Timer
Module STM Timer/Counter Mode to implement UART transmitter and receiver functions
using software control. This specific application shows that MCUs that do not have an
integrated UART function can also implement UART transmitter and receiver functions byusing their TM functions.

Operating Principle
Standard Type TM Structure Description
The Standard Type TM has an external input pin and one or two external output pins andcan run in five operating modes which are Compare Match Output, Timer/Event Counter,
Capture Input, Single Pulse Output and PWM Output modes.
The core of the Standard TM is composed of a count-up counter, a Comparator A and aComparator P. The count-up counter is driven by an internal or external TCKn pin clock
source. The Comparator A and Comparator P will compare the counter value with theCCRA and CCRP registers respectively. The TPn output signal state is determined by theoperating mode. Taking the following figure as an example, the counter value iscompared with the 16-bit CCRA register and the high 8-bit CCRP register. The counter
will be cleared automatically by a counter overflow, changing the TnON bit from low tohigh or by a compare match with one of its associated comparators.

AN0446E

V1.00

1/7

December 11, 2016

HT8 MCU Timer Module Application Note(3) – How to use
the TM Timer Mode to implement Software UART Function
CCRP

fSYS/4
fSYS
fH/16
fH/64
fTBC
fH/8

8-bit Comparator P001

TnPF Interrupt
TnOC

b8~b15010
011

16-bit Count-up Counter

100
101110

TCKn

Comparator P Match000

TnON
TnPAU

TnCCLRb0~b1516-bit Comparator A

Polarity
Control

PinControl

TnM1, TnM0
TnIO1, TnIO0

TnPOL

TnCP

1111

TnCK2~TnCK0

OutputControlCounter Clear 0

Comparator A MatchTPn

TnAF InterruptTnIO1, TnIO0Edge
Detector

CCRAFig 1. TM-STM Structure
Note：The actual bit number of the count-up counter, comparator A and comparator P canbe found in the selected MCU datasheet.

Timer/Counter Mode Description
The Timer/Counter mode is the most widely used mode. To select this mode, bits TnM1
and TnM0 should be set to 11 respectively. When operating in this mode timing values
can be determined using the TnCCLR bit. When a compare match occurs fromComparator A with the TnCCLR bit set high or when a compare match occurs from theComparator P with the TnCCLR bit cleared low, the counter will be cleared after which itwill resume counting. At this point, the comparator A or comparator P interrupt request
flag, TnAF or TnPF, will be set. Whether the interval time is reached or not can bechecked by two methods which are polling the interrupt flags and the related interrupts.
Note: The TM interrupt request flags, TnAF and TnPF, have to be cleared by theapplication program.

Asynchronous Serial Port Operating Principle
Asynchronous communication transmits data in a frame format which is composed ofcharacters. The time interval between two characters is not fixed, however each bit withina character is transmitted at a fixed time interval, which means that the time interval
between characters is not necessarily an integral multiples of bit intervals, while the timeinterval between bits in the same character is an integral multiples of bit intervals.

AN0446E

V1.00

2/7

December 11, 2016

HT8 MCU Timer Module Application Note(3) – How to use
the TM Timer Mode to implement Software UART Function
The communication format is shown below.
A Frame DataIdleStart
BitParity StopBit
Bit

8 bits Data

LSB

Idle

Next
Character
Start
Bit

MSB

Fig. 2 Serial Port Communication Format
Note：MSB is the most significant bit and LSB is the least significant bit。

Application Circuit
GND

1234567891011121314

VSS&AVSS

VDD&AVDD

PC0/OSC1

PB0/INT0/XT1

PC1/OSC2PB1/INT1/XT2

PC2

PB2/TCK0/AN2

PA0/TP0/ICPDA

PA4/TCK1/AN3
PD3

PC3/SDO
PC4/SDI/SDA

PD2/TX

PC5/SCK/SCL

PD1/RX
PD0

PC6/SCS
PA1

PA5/AN4/VREFI

PA2/ICPCK

PA6/AN5/VREF

PA3/CX

PA7/TP1/AN6PB6/C+

PB3/TP2/AN7
PB4/CLO/AN8

PB5/C-

28

5V

27C1
1042625

GND

24
23
22Tx

21Rx

2019181716
15HT66F0185
Fig. 3 HT66F0185 Serial Port Circuit Simulation
The HT66F0185 MCU is used to implement the circuit in the above figure. The TX pin isused as the serial port transmitter pin and the RX pin is used as the serial port receiverpin. RX and TX can be changed to any location. Users can define RXD and TXD to theactual pins by modifying the stm.h file.

Software Description
This application uses the HT66F0185 to describe how to use the STM to simulate serialports.
T : Timer interval time; Tbit : Time for sending a bit
Relationship between them: 4T = Tbit
Baud Rate : Baudrate = 1/Tbit
The simulation serial port transmits the data to the variable buffer first, and then transmits
the buffer data bit out one by one every Tbit seconds.

AN0446E

V1.00

3/7

December 11, 2016

HT8 MCU Timer Module Application Note(3) – How to use
the TM Timer Mode to implement Software UART Function
When the simulation serial port is receiving data the MCU will enter the interrupt program
every T seconds to detect whether the RXD pin is at a low level or not. This is to detect
whether the start bit has been received or not. If the RXD pin is low, a middle sampling
method can be used to receive a data bit one every Tbit seconds. The data bits can thenbe stored in the input variables.
As an example, use the HT66F0185 STM to setup T=52µs and use a baud rate of 4800.
The register setup steps are as follows:
Step1

2

3
4
5Setup steps for STM operating in the Timer/Counter modeRegister
Bit
Function Description
T0CK2~T0CK0：000：fSYS/4001：fSYS
010：fH/16
Setup clock source
TM0C0 011：fH/64
Select STM clock source
100：fTBC
101：fH/8
110：TCKn rising edge clock
111：TCKn falling edge clock
T0M1~T0M0：
00：Compare Match Output
Mode
Setup operating mode TM0C1 01：Capture Input Mode
Select STM operating mode
10：PWM Output Mode orSingle Pulse Output Mode
11：Timer/Counter Mode
Setup counter0: Comparator P match
Select STM counter clear
T0CCLRclear condition
1: Comparator A matchcondition
Setup CCRA
TM0AHAll bits
Write the timer period
register value
TM0AL
0：Off
Setup count control bit TM0C0
Enable STM counter to run
1：On
Operating Content

AN0446E

V1.00

4/7

Software Selection

T0CK[2:0]=001=fSYS=8M

T0M[1:0]=11=
Timer/Counter Mode

T0CCLR=1
TM0AH=0x01
TM0AL=0xa0
T0ON=1

December 11, 2016

HT8 MCU Timer Module Application Note(3) – How to use
the TM Timer Mode to implement Software UART Function
Software Design Flowchart
Start

Send_flag==1?

Yes

NoYes

Send a bit

Rxd==0?
No

Receive a bit

End

Fig. 4 STM Interrupt Service Subroutine Flowchart

StartSetup the RX as an input
and TX as an output
Configure STM registers
Enable the multi-function
interrupt 0 control bit
Enable the Comparator Ainterrupt control bit

Loop jump

Fig. 5 Main Program Flowchart

AN0446E

V1.00

5/7

December 11, 2016

HT8 MCU Timer Module Application Note(3) – How to use
the TM Timer Mode to implement Software UART Function‟U” Type Character Output Waveform

4800 BuadRate

Program Example
C Language Example

HT8 MCU Timer Module Application Note (3).zip

Program Example Instruction
 <stm.h>：Used to modify the simulated I/O pins and to change the baud rate.
 void Init_Timer_Uart()：Serial port simulation initialisation function, can also be used after initialisation.
 uchar Send_Byte(uchar output)：
This parameter is used for the transmitted data. If the data is successfully transmitted,return a 1 value. If not return a 0 value.
 This program uses input variables to receive data. These variables can be processedat the location that the ISR function has marked.

Conclusion
This application note should assist users to understand how to use the Standard TM tosimulate serial ports.

AN0446E

V1.00

6/7

December 11, 2016

HT8 MCU Timer Module Application Note(3) – How to use
the TM Timer Mode to implement Software UART Function

Reference Files
Reference File: HT66F0185 DataSheet.
For more information, refer to the Holtek official website http://www.holtek.com.

Versions and Modification Information
DateAuthor

Issue Release andModification

2016.07.10

鐘傳猛(Mounch)

First Version

Disclaimer
All information, trademarks, logos, graphics, videos, audio clips, links and other items
appearing on this website ('Information') are for reference only and is subject to change at
any time without prior notice and at the discretion of Holtek Semiconductor Inc.
(hereinafter 'Holtek', 'the company', 'us', 'we' or 'our'). Whilst Holtek endeavors to ensure
the accuracy of the Information on this website, no express or implied warranty is given
by Holtek to the accuracy of the Information. Holtek shall bear no responsibility for any
incorrectness or leakage.
Holtek shall not be liable for any damages (including but not limited to computer virus,
system problems or data loss) whatsoever arising in using or in connection with the use ofthis website by any party. There may be links in this area, which allow you to visit thewebsites of other companies. These websites are not controlled by Holtek. Holtek willbear no responsibility and no guarantee to whatsoever Information displayed at such sites.
Hyperlinks to other websites are at your own risk.

Limitation of Liability
In any case, the Company has no need to take responsibility for any loss or damage
caused when anyone visits the website directly or indirectly and uses the contents,
information or service on the website.

Governing Law
This disclaimer is subjected to the laws of the Republic of China and under the jurisdictionof the Court of the Republic of China.

Update of Disclaimer
Holtek reserves the right to update the Disclaimer at any time with or without prior notice,
all changes are effective immediately upon posting to the website.

AN0446E

V1.00

7/7

December 11, 2016