Upload
hadieu
View
268
Download
2
Embed Size (px)
Citation preview
I/O Emulated UART Baud Rate Calibration Application Note
AN0475E 1 / 13 January 11, 2018
I/O Emulated UART Baud Rate Calibration Application Note
D/N: AN0475E
Introduction
Not every HOLTEK MCU contains a Universal Asynchronous Receiver/Transmitter
function, otherwise known as a UART. If this function is required, it can be emulated using
software. However, the software accuracy will change according to the system frequency
error. This application note will illustrate how to use a software method to calibrate the
baud rate and how to avoid errors between the two communicating parties. It will also
show how to readjust to the same transmission frequency of each other when
transmission errors occur.
Taking the HT66F4540 as an example, this application note describes the UART baud
rate calibration method.
Functional Description
1. UART data transmission mode: The UART transmission is implemented in a
time-sharing way. The transmitter sends a data bit to the receiver at fixed time
intervals, until all 8 data bits have been transmitted. Note that the receiving speed of
the receiver should be the same as the transmitter; otherwise the received data will be
incorrect.
UART1 UART2
GND
RX
TX
VDD
GND
TX
RX
VDD
2. During data transmission, the UART will send a START (low) signal first to
synchronise with the receiver, after which it will begin to transfer data from the LSB
until the entire byte has been transmitted. After this, it will send a STOP (high) signal
to inform the receiver that the data transmission has finished.
UART data transmission format: 1 START Bit + 8 Data Bits + 1 STOP Bit, as shown
below:
I/O Emulated UART Baud Rate Calibration Application Note
AN0475E 2 / 13 January 11, 2018
START Bit 0 Bit 1 Bit 2 Bit 3 Bit 4 Bit 5 Bit 6 Bit 7 STOP
LSB MSB
3. UART transmission speed: This is measured in the number of transmitting bits per
second, with a unit of bit/sec, commonly known as baud rate or bit rate.
The common baud rates are 2400, 9600, 19200 and 38400 in bit/sec, which can
convert to a 1 bit transmission time as shown in the following table:
Baud Rate 1 Bit Transmission Time (µs)
2400 416.67
9600 104.16
19200 52.08
38400 26.04
Operating Principle
When the UART is waiting for data to be received or transmitted, the RX and TX pins will
remain at a high level. When data reception or transmission begins, these pins are pulled
low for a period of 1 bit, which is used to synchronise with each other as a START bit.
After this the data transmission will start from bit 0. After the completion of an entire data
transmission, the RX and TX pins will go high. Therefore, when the I/O emulated UART
function is used, the RX pin function should be emulated using an external interrupt pin.
This will start to receive data as a falling edge incoming signal. The TX pin function can
be emulated using general I/O pins. However, the RX pin function can use general I/O
pins for their emulation for baud rate detection.
I/O Emulated TX Data Transmission Steps
1. When preparing to transmit, TX is at a high level.
2. When data transmission begins, TX will be pulled low for a period of 1 bit. (START Bit)
3. The data is transferred in sequence, with the least significant bit (Bit 0) first, followed
by Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6 and Bit 7.
4. After the data transmission has completed, TX will be pulled high for a period of 1 bit.
(STOP Bit)
5. Then the following data can be transferred.
START Bit 0 Bit 1 Bit 2 Bit 3 Bit 4 Bit 5 Bit 6 Bit 7 STOP
LSB MSBWaiting or receiving
data bits
I/O Emulated UART Baud Rate Calibration Application Note
AN0475E 3 / 13 January 11, 2018
I/O Emulated RX Data Reception Steps
1. When preparing to receive, RX is at a high level.
2. When the RX receives a falling edge signal, it begins to count for about a 0.4 bit
transmission time to determine whether the signal is low. If it is, increase the ZERO
count by 1, otherwise increase the ONE count by 1. After three consecutive decisions,
check whether the ZERO count is greater than or equal to 2. If it is, indicate that this
signal refers to the correct data, otherwise to other signals.
3. If the correct data has been confirmed, the data will be received in sequence after a 1 bit
transmission time delay, with the least significant bit (Bit 0) first, followed by Bit 1, Bit 2,
Bit 3, Bit 4, Bit 5, Bit 6 and Bit 7. A certain delay time, which includes about 0.5~0.6 bit
time at the beginning and a 1 bit time at the end, is allowed for to capture data at the
center and to avoid fetching data before the present data or after.
4. After the data reception has completed, RX will be pulled high.
5. After this the following data is ready to be received.
START Bit 0 Bit 1 Bit 2 Bit 3 Bit 4 Bit 5 Bit 6 Bit 7 STOP
LSB MSBWaiting or receiving
data bits
1 2 3
RX Receiving Baud Rate Steps
Method 1
1. When preparing to receive, RX is at a high level.
2. When RX receives a low signal, start counting and record the count as COUNT.
3. When RX receives a high signal, stop counting.
4. After the data reception has completed, calculate the data transmission or reception
time using this counting value.
Note: a. This method has to ensure that Bit 0 is high, so that the received data is correct.
b. The counting loop is a group of four instructions.
START Bit 0 Bit 1 Bit 2 Bit 3 Bit 4 Bit 5 Bit 6 Bit 7 STOP
COUNT
I/O Emulated UART Baud Rate Calibration Application Note
AN0475E 4 / 13 January 11, 2018
Method 2
1. When preparing to receive, RX is at a high level.
2. When RX receives a low signal, which stands for an incoming START signal, prepare
to start counting.
3. When RX receives a high signal, start counting and record the count as COUNT0.
4. When RX receives a low signal, start counting and record the count as COUNT1.
5. When RX receives a high signal, start counting and record the count as COUNT2.
6. When RX receives a low signal, start counting and record the count as COUNT3.
7. When RX receives a high signal, start counting and record the count as COUNT4.
8. When RX receives a low signal, start counting and record the count as COUNT5.
9. When RX receives a high signal, start counting and record the count as COUNT6.
10. When RX receives a low signal, start counting and record the count as COUNT7.
11. When RX receives a high signal, stop counting.
12. After the data reception has completed, calculate the data transmission or reception
time using the summation and average values of these counting values
COUNT0~COUNT7.
Note: a. This method must ensure that the data is 55H, so that the received data is correct.
b. The counting loop is a group of four instructions.
START Bit 0 Bit 1 Bit 2 Bit 3 Bit 4 Bit 5 Bit 6 Bit 7 STOP
COUNT1 COUNT3 COUNT5 COUNT7
COUNT0 COUNT2 COUNT4 COUNT6
Method 3
Use the Time Base or the TM to count the 1 bit transmission time, and calculate the data
transmission or reception time.
Note: The following sample program takes Method 2 as an example. Other methods are
not further explained and demonstrated here.
I/O Emulated UART Baud Rate Calibration Application Note
AN0475E 5 / 13 January 11, 2018
Hardware Description The MCU1 VDD and GND pins are connected to the MCU2 VDD and GND pins.
The MCU1 PB5 and PA4 pins, known as the TX and RX pin functions, are connected to
the MCU2 RX and TX pins.
MCU1 MCU2
GND
PA4/INT0
PB5
VDD
GND
TX
RX
VDD
Software Description
The MCU initial setups are shown in the table:
Step Operation Content Register Setup Bits Functional Description
1 Setup HIRC operating frequency
SCC CKS[2:0] System clock selection: fH CKS[2:0] = 000
SCC FHS High frequency clock selection: HIRC FHS = 0
HIRCC HIRC[1:0] HIRC frequency selection: 8MHz HIRC[1:0] = 10
HIRCC HIRCEN HIRC oscillator control: enable HIRCEN = 1
2 Setup pins
PAS1 PAS1[1:0] PA4 pin-shared function selection: INT0/PA4 PAS1[1:0] = 00
PBS1 PBS1[3:2] PB5 pin-shared function selection: PB5 PBS1[3:2] = 00
PAC PAC4 PA4 pin type control: input PAC4 = 1
PBC PBC5 PB5 pin type control: output PBC5 = 0
PAPU PAPU4 PA4 pin pull-high resistor control: enable pull-high resistor PAPU4 = 1
PBPU PBPU5 PB5 pin pull-high resistor control: enable pull-high resistor PBPU5 = 1
3 Setup WDT WDTC WE[4:0] WDT control: enable
WE[4:0] = 01010
WS[2:0] WDT time-out period selection: 218/fLIRC WS[2:0] = 111
4 Setup interrupts
INTC0 INT0E External interrupt 0 control: enable INT0E = 1
INTC0 INT0F External interrupt 0 request flag: 0 INT0F = 0
INTC0 EMI Global interrupt control: enable EMI = 1
INTEG INT0S[1:0] External interrupt 0 edge selection: falling edge INT0S[1:0] = 01
I/O Emulated UART Baud Rate Calibration Application Note
AN0475E 6 / 13 January 11, 2018
The user defined registers are described below:
Item Register Bit Number Functional description Note 1 RX_DATA 8 The received data 2 RX_DATA_ITEMS 8 The number of the received data 3 TX_DATA 8 The transmitted data 4 TX_DATA_ITEMS 8 The number of the transmitted data 5 ONE 8 The counting value of keeping "1" for half cycle 6 ZERO 8 The counting value of keeping "0" for half cycle 7 RX_FG 1 Reception completion flag 8 COUNT0 8 Bit 0 counting width 9 COUNT1 8 Bit 1 counting width
10 COUNT2 8 Bit 2 counting width 11 COUNT3 8 Bit 3 counting width 12 COUNT4 8 Bit 4 counting width 13 COUNT5 8 Bit 5 counting width 14 COUNT6 8 Bit 6 counting width 15 COUNT7 8 Bit 7 counting width
16 COUNT 16 Summation and average of Bit 0 ~ Bit 7 in counting width
COUNT_L COUNT_H
17 BR_ONE_BIT 8 One cycle width 18 BR_HALF_BIT 8 Half cycle width
Points to consider:
1. When using this program, special attention must be made to the system frequency
and baud rate choices. In the software design only one user defined register is used to
store the baud rate parameters and in the calculations the execution of one LOOP
takes four instruction cycles. As a result, the maximum allowable instruction cycles are
1020. Additionally, in the receiver and transmitter programs, 20 instructions are
required, thus special care must be taken. The reference selections are as follows:
System Frequency
System Period
1 Instruction Cycle Time Available Instruction Cycles for Different Baud Rates
fsys tSYS(µs) Instruction Cycle (µs)(4T)
2400 (416.67µs)
9600 (104.17µs)
19200 (52.08µs)
38400 (26.04µs)
115200 (8.68µs)
2M 0.5 2 208 52 26 13* 4* 4M 0.25 1 416 104 52 26 8* 8M 0.125 0.5 833 208 104 52 17* 16M 0.0625 0.25 1666** 416 208 104 34
Note: * is limited to 20 instruction cycles and thus cannot be used.
** is limited to 1020 instruction cycles and thus cannot be used.
2. When receiving data, a low state bit, known as START, will first be received. However,
there may be noise on the receiver input, so that this low signal is not a START bit. For
this reason, it is necessary to make three decisions in the START bit reception
process. If two or more ZEROs are detected, determine if this signal is correct, and
continue to receive the subsequent signals. But if only one ZERO is detected,
determine that the signal is incorrect and then stop reception. Then jump out and wait
for the next signal. The diagram is shown below:
(1) Correct Signals:
Start Bit 01
2
3
4 6
5 7
8
9 11
10
13
1412
15
Start Bit 01
2
3
4 6
5 7
8
9 11
10
13
1412
15
I/O Emulated UART Baud Rate Calibration Application Note
AN0475E 7 / 13 January 11, 2018
Start Bit 01
2
3
4 6
5 7
8
9 11
10
13
1412
15
Start Bit 01 3 5 7 9 11 13 15
(2) Error Signals:
Start Bit 01
2
3
4 6
5 7
8
9 11
10
13
1412
15
Start Bit 01
2
3
4 6
5 7
8
9 11
10
13
1412
15
Start Bit 01
2
3
4 6
5 7
8
9 11
10
13
1412
15
Start Bit 01
2
3
4 6
5 7
8
9 11
10
13
1412
15
Flowchart
Global Flowchart
Step 1. Initialisation: Clear the RAM data, setup the system frequency and the Watchdog
Timer, initialise the I/O and then go to Step 2.
Step 2. Read the data width and then go to Step 3.
Step 3. Calculate the baud rate and then go to Step 4.
Step 4. Setup the external interrupt 0 enable control bit and the global interrupt enable
control bit, then go to Step 5.
Step 5. Setup the UART related parameters and go to Step 6.
Step 6. Determine whether the RX_FG is set high.
If yes, then go to Step 7.
If no, then go to Step 6.
Step 7. Send back the received data as well as additional data of 0xAA, then go to Step 6.
INT0 ISR: When a falling edge on the INT0 pin occurs, determine whether the received
START bit is correct. If it is, start to receive the data, store the data into RX_DATA and set
RX_FG high. Otherwise, jump out from the interrupt.
I/O Emulated UART Baud Rate Calibration Application Note
AN0475E 8 / 13 January 11, 2018
START
INIT RAM
INIT SYSTEM
INIT IO
Measure_BR
Calaulate_BR
INIT INT
MAIN
INIT UART
INT0 ISR
Read data from RX
RET
Read Data Width Flowchart
Step 1. Determine whether the UART_RX_PIN is "1".
If yes, indicate that the UART_RX_PIN is ready and then go to Step 2.
If no, indicate that the UART_RX_PIN is not ready and then repeat Step 1.
Step 2. Determine whether the UART_RX_PIN is "0".
If yes, indicate that the UART_RX_PIN is ready and then go to Step 3.
If no, then repeat Step 2.
Step 3. Determine whether the UART_RX_PIN is "1".
If yes, then go to Step 4.
If no, indicate that the START bit has not transmitted completely and then repeat
Step 3.
Step 4. Increase COUNT0 by 1 and then go to Step 5.
Step 5. Determine whether the UART_RX_PIN is "0".
If yes, then go to Step 6.
If no, indicate that Bit 0 has not transmitted completely and then go to Step 4.
Step 6. Increase COUNT1 by 1 and then go to Step 7.
Step 7. Determine whether the UART_RX_PIN is "1".
If yes, then go to Step 8.
If no, indicate that Bit 1 has not transmitted completely and then go to Step 6.
Step 8. Increase COUNT2 by 1 and then go to Step 9.
I/O Emulated UART Baud Rate Calibration Application Note
AN0475E 9 / 13 January 11, 2018
Step 9. Determine whether the UART_RX_PIN is "0".
If yes, then go to Step 10.
If no, indicate that Bit 2 has not transmitted completely and then go to Step 8.
Step 10. Increase COUNT3 by 1 and then go to Step 11.
Step 11. Determine whether the UART_RX_PIN is "1".
If yes, then go to Step 12.
If no, indicate that Bit 3 has not transmitted completely and then go to Step 10.
Step 12. Increase COUNT4 by 1 and then go to Step 13.
Step 13. Determine whether the UART_RX_PIN is "0".
If yes, then go to Step 14.
If no, indicate that Bit 4 has not transmitted completely and then go to Step 12.
Step 14. Increase COUNT5 by 1 and then go to Step 15.
Step 15. Determine whether the UART_RX_PIN is "1".
If yes, then go to Step 16.
If no, indicate that Bit 5 has not transmitted completely and then go to Step 14.
Step 16. Increase COUNT6 by 1 and then go to Step 17.
Step 17. Determine whether the UART_RX_PIN is "0".
If yes, then go to Step 18.
If no, indicate that Bit 6 has not transmitted completely and then go to Step 16.
Step 18. Increase COUNT7 by 1 and then go to Step 19.
Step 19. Determine whether the UART_RX_PIN is "1".
If yes, then go to Step 20.
If no, indicate that Bit 7 has not transmitted completely and then go to Step 18.
Step 20. Return to the main program.
I/O Emulated UART Baud Rate Calibration Application Note
AN0475E 10 / 13 January 11, 2018
UART_RX_PIN = = 1?
UART_RX_PIN = = 0?
YES
NO
Measure_BR
YES
NO
UART_RX_PIN = = 1?
UART_RX_PIN = = 0?
YES
NO
YES
NO
COUNT0 = COUNT0 + 1
UART_RX_PIN = = 1?
YES
NO
COUNT1 = COUNT1 + 1
UART_RX_PIN = = 0?
YES
NO
COUNT2 = COUNT2 + 1
UART_RX_PIN = = 1?
YES
NO
COUNT3 = COUNT3 + 1
UART_RX_PIN = = 0?
YES
NO
COUNT4 = COUNT4 + 1
UART_RX_PIN = = 1?
YES
NO
COUNT5 = COUNT5 + 1
UART_RX_PIN = = 0?
YES
NO
COUNT6 = COUNT6 + 1
UART_RX_PIN = = 1?
YES
NO
COUNT7 = COUNT7 + 1
RET
I/O Emulated UART Baud Rate Calibration Application Note
AN0475E 11 / 13 January 11, 2018
Calculate Baud Rate Flowchart
Step 1. Add up all the COUNT0 ~ COUNT7, store the result in COUNT, and then go to
Step 2.
Step 2. Average the COUNT, store the result in COUNT, and then go to Step 3.
Step 3. Subtract 5 from the COUNT, store the result in BR_ONE_BIT, and then go to Step 4.
Step 4. Subtract 8 from the COUNT and divide by 2, store the result in BR_HALF_BIT,
and then go to Step 5.
Step 5. Return to the main program.
Note:
1. During data transmission or reception, 20 instruction cycles are required, and in turn
the conversion time is 5. Therefore, the BR_ONE_BIT value will be correct when 5 is
subtracted from COUNT.
2. During a half cycle, three decisions will made to determine whether the START bit is
correct. This will take 32 instruction cycles, and in turn the conversion time is 8.
Therefore, the BR_HALF_BIT value will be correct when 8 is subtracted from COUNT.
Calaulate_BR
COUNT = COUNT0 + COUNT1 + COUNT2 + COUNT3 + COUNT4 +
COUNT5 + COUNT6 + COUNT7
COUNT = COUNT / 8
BR_ONE_BIT = COUNT - 05H
BR_HALF_BIT =( COUNT - 08H) / 2
RET
I/O Emulated UART Baud Rate Calibration Application Note
AN0475E 12 / 13 January 11, 2018
Conclusion
This application note has mainly introduced how to calibrate the baud rate to ensure
correct data transmission when using I/O to emulate the UART function. When UART
data transmission begins, the signal will first be pulled low as a START signal and thus
used as the data transmission width. However, problems will occur if the detection of the
LSB is not high. Therefore, two methods are provided to compensate, one is to set the
LSB high; the other is to set the data bits to 55H. Both of these have advantages and
disadvantages. The first method will create a large error. The second method has a
smaller error due to the detection of eight data bits, but because there is no fixed baud
rate, the other communicating party must first send data to settle its transmission speed
when in use.
References
Reference document: HT66F45x0 Data Sheet.
For more information, refer to the Holtek’s official website: http://www.holtek.com/en/.
Versions and Modification Information
Date Author Issue Release and Modification
2017.12.01 陳淑娟 (Chen, Shu-Juan) 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 of
this website by any party. There may be links in this area, which allow you to visit the
websites of other companies. These websites are not controlled by Holtek. Holtek will
bear no responsibility and no guarantee to whatsoever Information displayed at such sites.
Hyperlinks to other websites are at your own risk.
I/O Emulated UART Baud Rate Calibration Application Note
AN0475E 13 / 13 January 11, 2018
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 jurisdiction
of 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.