Chap 6. Serial port programming
Chap 6. Serial port programming
P/S S/P
RS232
RS232 MODEM MODEM
Telephone Line
MAX
232
RX TX
DB9
1. DTR (data terminal ready).
• When the terminal (or a PC COM port) is turned on, after going through
a self-test, it sends out signal DTR to indicate that it is ready for
communication.
• If there is something wrong with the COM port, this signal will not be
activated. This is an active-LOW signal and can be used to inform the
modem that the computer is working fine.
• This is an output pin from DTE (PC COM port) and an input to the
modem.
2. DSR (data set ready).
• When the DCE (modem) is turned on and has gone through the self-test, it
asserts DSR to indicate that it is ready to communicate. Thus, it is an
output from the modem (DCE) and an input to the PC (DTE).
• This is an active-LOW signal.
• If for any reason the modem cannot make a connection to the telephone,
this signal remains Inactive, indicating to the PC (or terminal) that it
cannot accept or send data.
3. RTS (request to send).
• When the DTE device (such as a PC) has a byte to transmit, it
asserts RTS to signal the modem that it has a byte of data to
transmit.
• RTS is an active-LOW output from the DTE and an input to the
modem.
UDR Register
Simplified USART Transmit Block diagram
USART Transmit operation
• Two operations can happen w.r.t UDR. Write
and Read
• Data to be transmitted serially is written to
UDR, via data bus by the sending
Microcontroller and it will be transferred to
the Transmit Data Buffer Register (TXB).
• On the other hand and when you read data
from UDR, it will return the contents of the
Receive Data Buffer Register (RXB).
UCSR registers and USART
configurations in the AVR
• UCSRs are 8-bit control registers used for
controlling serial communication in the AVR.
• There are three USART Control Status
Registers in the AVR. They are
– UCSRA
– UCSRB
– UCSRC.
UCSRA: USART control and status register A
1. The UCSRB register is loaded with the value 08H, enabling the USART transmitter. The
transmitter will override normal port operation for the TxD pin when enabled.
2. The UCSRC register is loaded with the value 86H, indicating asynchronous mode with 8-
bit data frame, no parity, and one stop bit.
3. The UBRR is loaded with appropriate value selected from the table discussed earlier to
set the baud rate for serial data transfer.
4. The character byte to be transmitted serially is written into the UDR register.
5. Monitor the UDRE bit of the UCSRA register to make sure UDR is ready for the next byte.
6. To transmit the next character, go to Step 4.
Programming the AVR to receive data serially
In programming the AVR to receive character bytes serially, the
following steps must be taken:
1. The UCSRB register is loaded with the value 10H, enabling the
USART receiver. The receiver will override normal port operation
for the RxD pin when enabled.
2. The UCSRC register is loaded with the value 86H, indicating
asynchronous mode with 8-bit data frame, no parity, and one
stop bit.
3. The UBRR is loaded with appropriate value selected from
the table discussed earlier to set the baud rate for serial
data transfer.
4. The RXC flag bit of the UCSRA register is monitored for a HIGH to
see if an entire character has been received yet.
5. When RXC is raised, the UDR register has the byte. Its contents
are moved into a safe place.
6. To receive the next character, go to Step 5.
a) What are the values of UCSRB and UCSRC needed to
configure USART for asynchronous operating mode, 8 data
bits(character size), no parity and 1 stop bit? Enable both
receive and transmit
b) Write a program for the AVR to set the values of UCSRB and
UCSRC for this configuration.
In the UCSRB Register make .INCLUDE "M32DEF.INC"
RXEN = 1 & TXEN = 1 to enable LDI R16, 0b00011000
receive and transmit
OUT UCSRB, R16
UCSZ2: 0 should be 011 for 8-
LDI R16, 0x86
bit data
OUT UCSRC, R16
UMSEL = 0 for Asynchronous
mode
UPM1:0 = 00 for no parity
USBS = 0 for 1 stop bit
In the above example set the baud rate to 1200 and write a program for the
AVR to set up the values of UCSRB, UCSRC and UBRR. (Fosc= 8MHz)
INCLUDE "M32DEF.INC"
LDI R16, 0b0011000
OUT UCSRB, R16
LDI R16, 0x86
OUT UCSRC, R16
LDI R16, 0x9F ; lower byte
OUT UBRRL, R16
LDI R16, 0x01 ; higher byte
OUT UBRRH, R16 ; baud rate is 1200
Write a program for the AVR to transfer the letter ‘G’ serially at 9600 baud,
continuously. Assume XTAL 8MHz.
.INCLUDE "M32DEF.INC"
LDI R16, (1<< TXEN) ;enable transmitter
OUT UCSRB, R16
LDI R16, (1<< UCSZ1)| (1<< UCSZ0)| (1<< URSEL)
;8-bit data, no parity, 1stop bit
OUT UCSRC, R16
LDI R16, 0x33 ; 9600 baud rate
OUT UBRRL, R16
AGAIN:
SBIS UCSRA, UDRE ; is UDR empty
RJMP AGAIN ; wait more
LDI R16, 'G' ; send G
OUT UDR,R16 ; to UDR
RJMP AGAIN
Write a program to transmit the message “YES” serially at 9600
Baud, 8-bit data and 1-stop bit. Do this forever.
.INCLUDE "M32DEF.INC"
AGAIN: LDI R17,'Y' ; move ‘Y’ to
LDI R20, HIGH (RAMEND) ;
R17
initialise high
CALL TRNSMT ; transmit R17
;byte of SP
to TxD
OUT SPH, R20
LDI R17,'E' ; move ‘E’ to
LDI R20, LOW (RAMEND) ;
R17
initialise low byte of SP
CALL TRNSMT ; transmit R17
OUT SPL, R20
to TxD
LDI R16, (1<< TXEN) ;
LDI R17,'S' ; move ‘Y’ to
enable
R17
;transmitter
CALL TRNSMT ; transmit R17
OUT UCSRB, R16
to TxD
LDI R16, (1<< UCSZ1)| (1<< UCSZ0)|
LDI R17,' ' ; move ' ' to
(1<< URSEL) ;8-bit data, no
R17
parity, 1
CALL TRNSMT ; transmit R17
;stop bit
to TxD
OUT UCSRC, R16
RJMP AGAIN ; do it again
LDI R16, 0x33 ; 9600 baud
rate TRNSMT: SBIS UCSRA, UDRE ; is UDR
empty
OUT UBRRL, R16
RJMP TRNSMT ; wait more
OUT UDR,R17 ; send R17 to
UDR
RET