search

Saturday, August 14, 2010

8051 serial port

The only one communication hardware present in atmel 89S52 version of 8051 microcontroller is the serial port. The serial port or UART transfers data bit by bit. In mode 1, 8 bits can be sent with one start and stop bit in a frame.
Communicating with the PC:
We can communicate with PC by using the serial port available in 8051. In order to communicate with the PC, we need a level converter because the serial port of the pc uses RS232 voltage levels which is much higher voltage than the TTL logic used by the microcontroller (+12v for logic 0 and -12v for logic 1). A dedicated IC from MAXIM i.e. Max232 is readily available in the market. It converts the TTL signals from the microcontroller to RS232 level and the other way round. Max232 contains 2 channels, works with single 5V supply and uses the principle of charge pump to convert the voltage levels.
http://www.shree-electronics.com/images/rs232_level_conv.gif  http://www.shree-electronics.com/images/rs232_level_conv.gif
The schematic :
http://www.shree-electronics.com/images/max232_pins.gif  http://www.shree-electronics.com/images/max232_pins.gif
http://www.shree-electronics.com/images/max232_ckt.gif  http://www.shree-electronics.com/images/max232_ckt.gif
Configuring HyperTerminal:
HyperTerminal is the tool usually used to test and communicate with the PC’s serial port. To set up HyperTerminal follow the steps given below.
1. Open HyperTerminal.
Start > All programs > Accessories > Communications > Hyper Terminal
2.Specify a name for the connection
Click OK
http://www.shree-electronics.com/images/hyper2.gif  http://www.shree-electronics.com/images/hyper2.gif
3.Configure the connection parameters
Select the port of your PC i.e.COM1, COM2 etc., click OK
http://www.shree-electronics.com/images/hyper3.gif  http://www.shree-electronics.com/images/hyper3.gif
Set baud rate as required, and change Flow control to none
http://www.shree-electronics.com/images/hyper4.gif  http://www.shree-electronics.com/images/hyper4.gif
The connected in the bottom shows the status of the connection.
http://www.shree-electronics.com/images/hyper6.gif  http://www.shree-electronics.com/images/hyper6.gif
Now your HyperTerminal is ready to communicate. You can disconnect or connect the connection by clicking on the icons shown in the image.
http://www.shree-electronics.com/images/hyper5.gif  http://www.shree-electronics.com/images/hyper5.gif
HyperTerminal displays only the character or data received, not the one you typed to send.
Testing the Level converter:
You are ready with level converter hardware and HyperTerminal, now its time for testing your level converter hardware. For testing purpose, just interconnect the Tx and Rx pins of the level converter, and type something in the HyperTerminal, you will see the echo.
Programming the 8051:
8051 USART uses dedicated buffer register SBUF, the mode and other settings of the serial port such as number of stop bits etc. is determined by the contents of SCON register. Timer 1 should be initialized to 8-bit auto reload, and its content determines the Baud rate of the 8051 serial port. Read datasheet to know more about different modes of UART.
Connecting the Microcontroller:
Connect the Tx and Rx pins of the 8051 (pin no. 10 and 11 i.e. P3.0 and P3.1 in 89S52)microcontroller to the level converter. Provide power for the 8051 microcontroller target board.

;*************************************************
;
;Program: UART_test
;Author: Srikanth
;Website: http://shree-electronics.com/
;Description: Prints string on the hyperterminal window
;
;*************************************************
org 0000h
ljmp main

main:
mov tmod,#20h      ;Timer 1 configured to 8 bit
;auto reload
mov th1,#-6        ;Baud rate set to 9600
mov scon,#50h      ;UART configure as 8 data bits
setb tr1           ;with 1 start and stop bit
up:
mov sbuf,#’s'      ;Store the char to be
;transmitted in SBUF refister
acall transmit     ;call transmit subroutine
mov sbuf,#’h’
acall transmit
mov sbuf,#’r’
acall transmit
mov sbuf,#’e’
acall transmit
mov sbuf,#’e’
acall transmit
mov sbuf,#’-’
acall transmit
mov sbuf,#’e’
acall transmit
mov sbuf,#’l’
acall transmit
mov sbuf,#’e’
acall transmit
mov sbuf,#’c’
acall transmit
mov sbuf,#’t’
acall transmit
mov sbuf,#’r’
acall transmit
mov sbuf,#’o’
acall transmit
mov sbuf,#’n’
acall transmit
mov sbuf,#’i’
acall transmit
mov sbuf,#’c’
acall transmit
mov sbuf,#’s’
acall transmit
mov sbuf,#’.’
acall transmit
mov sbuf,#’c’
acall transmit
mov sbuf,#’o’
acall transmit
mov sbuf,#’m’
acall transmit

up1:
ajmp up1           ;Loop here

transmit:            ;Transmit subroutine
jnb ti, transmit   ;Wait for completion of
;transmission
clr ti             ;Clear transmit flag
ret

end

;*************************************************
;
;Program: UART_test_1
;Author: Srikanth
;Website: http://shree-electronics.com/
;Description: Prints the next char of the typed
; character on the hyperterminal window
;
;*************************************************
org 0000h
ljmp main

main:
mov tmod,#20h      ;Timer 1 configured to 8 bit
;auto reload
mov th1,#-6       ;Baud rate set to 9600
mov scon,#50h     ;UART configure as 8 data bits
setb tr1          ;with 1 start and stop bit
up:
acall recieve     ;First recieve the char typed on
;hyperterminal
cjne a,#’z',next  ;If typed char is not Z, then
mov a,#’a'-1      ;send next char of the typed one
next:
inc a
acall transmit
sjmp up           ;Loop

transmit:           ;Transmit subroutine
mov sbuf,a        ;Move the char to be transmitted
;into SBUF reg
jnb ti,$          ;Wait for transmission to be
;complete
clr ti            ;clear transmit flag
ret

recieve:            ;Recieve subroutine
jnb ri,$          ;wait for a char to be recieved
mov a,sbuf        ;save the recieved char
clr ri            ;clear recieve flag
ret

end

No comments:

Post a Comment