A) TransmitterTheory and Setting Asynchronous Mode of USART for TransmitterCircuit diagram for transmitter
In previous post, it is shown that the data pin of the transmitter module is connected to the TX pin of the microcontroller. Seem TX pin normally used as a digital I/O port, to enable the TX port as a serial port, SPEN which is bit 7 in RCSTA have to be set. Bit six in TRISC of PIC16F877A have to be clear in order to make the TX pin as an output pin. TXSTA is the transmit control register for the microcontroller. This register has to be initializing correctly in order to make the transmission work. By referring to the data sheet, the TXSTA is initialized as B'00100000' which mean that it transmit 8bit data in asynchronous low speed mode.
Representation of each bit in TXSTA Register
Next is to set the baud rate of the transmitter. Baud rate refers to the speed at which the serial data is transferred, in bits per second. In Asynchronous mode, the baud rate generator sets the baud rate using the value in the SPBRG register. The BRGH bit in TXSTA selects between high and low speed options for greater flexibility in setting the baud rate. From the initialization of TXSTA shown above, the BRGH is clear which mean that the baud rate is in low speed and the SPBRG register is set to 129 where the rate is 2.4K bit per second. The Baud rate for both transmitter and receiver must be the same in order for the data transmitted to receive in the receiver. The baud rate can be calculated with the formula shown below.
12Desired baud rate= Fosc64/(x+1)/Where, Fosc = frequency of crystal used
X= value that will be set in the SPBRG register
Example:
Taking the desired baud rate = 2.4K
2.4K=20M/64(x+1)
x=129
When a 1 byte digital data is being transmitted, it is transmit from the less significant bit to the most significant bit. This means that the transmitter transmits digital data bit by bit to the receiver.
The signal is high when no transmission (or reception) is in progress and goes low when the transmission starts. The receiving device uses this low-going transition to determine the timing for the bits that follow. The signal stays low for the duration of the START bit, and is followed by the data bits, Least Significant bit first. The USART can transmit and receive either eight or nine data bits. The STOP bit follows the last data bit and is always high. The transmission therefore ends with the pin high. After the STOP bit has completed, the START bit of the next transmission can occur.
Setup for transmitter
During transmitting data, the heart of the transmitter is the Transmit Shift Register (TSR). This register obtain the data from the transmit buffer, TXREG. Hence, to transmit a data to the receiver, first is to move the desire transmit data to the TXREG then it will load to TSR to be transmitted. To check whether the data in TXREG had been move to TSR, the flag bit TXIF which located in the PIR1 is checked. If TXREG is empty (means the data already load to TSR) the flag bit TXIF will be set. Hence new data can be load to TXREG to be transmitted next. The Bit TXEN in TXSTA 6 is always set so that all the data in TSR will be transmit.
The transmitter’s program runs
Programming for transmitter
Circuit diagram on breadboard