Rev A - 13 March 2001 1 ANM079 Software UART using PCA 1. Intr oducti on For microcontroller applications which require more than one serial port, the Programmable Counter Array (PCA) can implement additional half-duplex serial ports. Several Atmel Wireless & Microcontrollers products include a PCA, among which: - TSC80C251A1 - TSC80C251G1D - TSC80C251G2D - TS80C51Rx2 - TS89C51RD2 If the on-chip UART is being used as an inter-processor link, the PCA can be used to interface the microcontrollerto additional asynchronous lines. This application note uses several different Compare/Capture modes available on the PCA to receive or transmit bytes of data. It is assumed the reader is familiar with the PCA and C programming language. For more information on the PCA refer to PCA chapter of the data sheet or design guide listed in Sec tion 5. 2. Des cri pti on The Fig ure 1 shows the format of a standard 10-bit asynchronous frame: 1 start bit (0), 8 data bits, and 1 stop bit (1). The start bit is used to synchronize the receiver to the transmitter; at the leading edge of the start bit the receiver must set up its timing logic to sample the incoming line in the center of each bit. Following the start bit are eight data bits which are transmitted least significant bit first. The stop bit is set to the opposite state of the start bit to guarantee that the leading edge of the start bit will cause a transition on the line. It also provides a dead time on the line so that the receiver can maintain its synchronization. The provided software can be easily adapted to support other data format ; for example 9 data bits can be supported by changing the constant NBR_OF_DATA_BITS. Figure 1. Data Frames Two of the Compare/Capture modes on the PCA are used in receiving and transmitting data bits. When receiving, the Negative-Edge Capture mode allows the PCA to detect the start bit. Then using the Software Timer mode, interrupts are generated to sample the incoming data bits. This same mode is used to clock out bits when transmitting. Two kinds of reception errors can be reported : overrun and frame error. Start D0 D1 D2 D3 D4 D5 D6 D7 8-bit data 10 bit frame Stop Mode 1
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
For microcontroller applications which require more than one serial port, the Programmable Counter Array (PCA)
can implement additional half-duplex serial ports. Several Atmel Wireless & Microcontrollers products include a
PCA, among which:- TSC80C251A1
- TSC80C251G1D
- TSC80C251G2D
- TS80C51Rx2
- TS89C51RD2
If the on-chip UART is being used as an inter-processor link, the PCA can be used to interface the microcontroller
to additional asynchronous lines.
This application note uses several different Compare/Capture modes available on the PCA to receive or transmit
bytes of data. It is assumed the reader is familiar with the PCA and C programming language. For more information
on the PCA refer to PCA chapter of the data sheet or design guide listed in Section 5.
2. Description
The Figure 1 shows the format of a standard 10-bit asynchronous frame: 1 start bit (0), 8 data bits, and 1 stop bit
(1). The start bit is used to synchronize the receiver to the transmitter; at the leading edge of the start bit the
receiver must set up its timing logic to sample the incoming line in the center of each bit. Following the start bit
are eight data bits which are transmitted least significant bit first. The stop bit is set to the opposite state of the
start bit to guarantee that the leading edge of the start bit will cause a transition on the line. It also provides a
dead time on the line so that the receiver can maintain its synchronization.
The provided software can be easily adapted to support other data format ; for example 9 data bits can be supported by changing the constant NBR_OF_DATA_BITS.
Figure 1. Data Frames
Two of the Compare/Capture modes on the PCA are used in receiving and transmitting data bits. When receiving,
the Negative-Edge Capture mode allows the PCA to detect the start bit. Then using the Software Timer mode,
interrupts are generated to sample the incoming data bits. This same mode is used to clock out bits when transmitting.
Two kinds of reception errors can be reported : overrun and frame error.
Interrupt is generated on:1. By the user at the beginning of an tranmission2. By a negative capture on channel 0 when a start bit arrives2. By the software Timer on channel 0 or 1 during transmission/reception
The interrupt routine uses register bank 2.
---------------------------------------------------------------------------*/void interrupt_pca (void) interrupt 6 using 2{
Performs reception of a byte on channel 0.receive_flag _0 is set to TRUE when reception is finished and the receivedbyte is stored in received_byte_0.If RXD_STATUS is enabled (see DEF.h) receive_status_0 will contain theerror information:
Bit 4 = O verRunBit 5 = FrameError
The user has to clear receive_flag_0 when a byte has been read. Othervisean overRun will be generated on the next received byte (If RXD_STATUS isused).
enum {WAITING_FOR_DATA_BITS, /* States used by the variable state */WAITING_FOR_MID_START_BIT,WAITING_FOR_STOP_BIT,IDLE};
static unsigned char state= IDLE;static unsigned char nbr_of_rxd_bits_left;/* Indicates number of bits left
to transmit */static data_type tmp_received_byte = 0; /* The received byte is stored here */union countertype /* tmp holds the CCAP0 counter value */{
unsigned int tmp;char byte[2];
};static union countertype counter;
switch (state){
case IDLE: /* State = IDLE, No reception is going on */counter.byte[1] = CCAP0L; /* Store the present counter value */counter.byte[0] = CCAP0H;CCAP0L= (counter.tmp+=HALF_BIT_TIME); /* Increase module 0 counter
with 1/2 bit time */CCAP0H= counter.byte[0];
CCAPM0 = S_W_TIMER; /* Set up the Module 0 for Software Timer mode */state = WAITING_FOR_MID_START_BIT; /* Wait for the start bit */break;
case WAITING_FOR_MID_START_BIT:if (!RXD_PIN0) /* Check if we have a correct start bit */{
CCAP0L= (counter.tmp+=ONE_BIT_TIME); /* Increase module0 counter with 1 bit time */
CCAP0H= counter.byte[0]; /*-" - */
nbr_of_rxd_bits_left = NBR_OF_DATA_BITS; /* Init variable */state = WAITING_FOR_DATA_BITS; /* Wait for the data bits */}else{
Transmits the byte stored in transmit_byte_1 on channel 1.transmit_flag_1 is set to TRUE before the START bit and FALSE aftertransmision of the STOP bit.