7 8051 Serial Interface New

50
8051 Serial Interface

description

cv

Transcript of 7 8051 Serial Interface New

  • 8051 Serial Interface

  • 8051 Serial InterfaceAn introduction to serial communicationBit by Bit Transfer/receipt of message between two units.Parallel communication preferred over short distance (PC to Printer)Few meters.Larger distance Signal distortion , Bit skewing.

  • Simplex, Half Duplex and Full Duplex communication.

    Simplex

    Half Duplex

    Full Duplex

  • Synchronous communication

    Asynchronous Communication

    - Transmission and Reception are not synchronized. - A device can send at its on one other devices may receive it.

  • Both devices must decide. Data frame i.e how data is packed. i.e Start bit Stop bit Parity bit Set of rules for sending and receiving - protocol.Synchronous Communication- Large block of data transferAsynchronous Communication Character by character transfer.Short Distance Communication Signals are directly transferred without any conversion.

  • Long Distance Communication - Signals are transferred on telephone lines - Other media Coaxial cable, Open space - Data signals must be converted (modulated) before transmission to the media. - Received signals must be converted back to data signals - called demodulation.

    Modem Modulator Demodulator.Performs the task of signal conversion from one form to other form.- Carrier frequency is required.

    Device 1Modem 2Modem 1Device 2

  • Telephone modem- Audio frequency Device 1, Device 2 Computer or PC (DTE) Data Terminal Equipment Modem1, Modem2 DCE (Data Communication Equipment) DTE DCE -------- DCE DTE Communication

  • To communicate over telephone linesModem will convert the transmitting binary data (0,1) to Audio tone.On receipt at other end, modem will convert the Audio signal back to digital data i.e 0,1Device(Computer) needs a circuit/device to convert parallel data i.e byte form in to bit form, both in Asynchronous as well as Synchronous mode.

  • Such devices working in Asynchronous mode are called Universal Asynchronous Receiver Transmitter or UART.Many computer have UART chips.8051 has on chip UART circuit.

  • USART - Universal Synchronous Asynchronous Receiver Transmitter - circuit/devices may work in both synchronous/Asynchronous mode.We shall concentrate on UART resident on 8051. Overall communication scenario.

    Data Transfer RateBPS Bits per secondBaud Signal events per secNow used interchangeably.Device 1

    Device 2

    Modem 2Modem 1UARTUART

  • RS 232 standardTo bring compatibility between data communication equipments of different manufactures. RS 232 interfacing standards was set by EIA (Electronic Industries Association) in 1963.Standard was modified to RS 232C in 1969 referred as RS 232 only now a days.Serial communication standard between computers (PCs) and other equipments.Voltage levels in RS 232 is different from TTL voltage levels. RS 232C - Logic 1 - -3 to -25 V Logic 0 - +3 to +25V -3 to +3V undefined

  • Voltage converter chips/devices called line drivers needed to convert TTL level signals to RS 232C level signals and vice versa.MAX 232 and MAX 233 chips are currently used for thin purpose. RS 232C pins and signals Original RS 232C has 25 pins in 25 pin connector (DB -25)

    RS 232C DB 25 connectorHowever for IBM PC only 9 pins are used in PC cable. Thus DB 9 was introduced. The pins are- 1312

  • Pin Description Data Carrier Detect (DCD) Received Data (RD) Transmitted Data (TD) Data Terminal Ready (DTR) Signal Ground (GND) Data Set Ready (DSR) Request to Send (RTS) Clear to Send (CTS) Ring Indicator (RI)

  • However simplest connection between two devices for RS 232 C is with RD , TD and GND pins.In 8051 UART communication using RD, TD and GND pins is supported.

    In IBM PC normally 2 com ports (com1 & com2) both have RS 232C type connectors.PC8051TDRDGNDGNDRDTD

  • Many PCs use one DB -25 and one DB-9 type connector.Present PCs use COM1 for mouse and COM2 is available for connection to other devices.

  • 8051 Serial Interface

    Bit by bit transmission/receipt of message between two units

    Bytes by bytes Parallel Communication preferred over Short distance (few meters) - PC to printer - Between chips on motherboard or between diff. boards in a system. Larger distance - Signal distortion - Bit skewing.- Simplex , Half duplex and Full duplex communication Simplex Only Transmit or Receive Half duplex Transmit or Receive both but one role at a time. Full duplex Transmit and Receive both simultaneously.

    Serial Communication

  • Synchronous Communication

    Transmission from one device and Receipt at other device synchronized - by a common clock - by two clocks which are synchronized Cumbersome - Used for large block of data transfer.Asynchronous Communication

    Transmission and reception are not synchronized.A device can send at its own and other device may receive it.

  • Both devices must decide on Data frame i.e. how data is packed. i.e Start bit Stop bit Parity bit and baud rate.Set of rules for sending and receiving - protocol.Device (Computer) needs a circuit or device to convert parallel data i.e byte form (in memory) in to bit form (i.e serial data) in both Asynchronous as well as Synchronous communication.

  • Such devices working in Asynchronous mode are called UART(Universal Asynchronous Receiver- Transmitter).They convert parallel to serial ( for transmit) as well as serial to parallel (on receipt).Many computers have UART chip.USART (Universal Synchronous Asynchronous Receiver Transmitter) performs the above function both in Synchronous and Asynchronous mode.

  • 8051 Serial I/O8051 has on chip UART for serial communication.Full Duplex Communication.Two pins - TD (P3.1) - RD (P3.0)SFR SBUF used for both transmitting and receiving the byte.SCON and PCON SFR used for programming and controlling the serial communication.TI Transmit Interrupt Flag.RI- Receive interrupt Flag.

  • MODESSerial port can work in one of the four modes.Mode 0- RD pin used for receiving and transmitting the data. - TD pin used to output shift clock. - 8 bits of data is transmitted or received at one time.TI set by hardware at end of 8th bit transmitted. Causes Interrupt.RI Set by hardware at the end of 8th bit received Causes Interrupt. - TI and RI are two bits of SCON register.Baud Rate 1/12 of oscillator frequency.

  • Mode 1 8051 transmits 10 bits (data frame) (using TD) and receives 10 bits data frame (using RD ).Start bit 0---------- bit7 Stop bit data frameStart bit low , Stop bit highThus clear demarcation between end of one data frame and start of another data frame.Reception of data frame complete on receipt of stop bit.Stop bit loaded to RB8 bit in SCON register.Start and stop bits added by 8051 UART automatically.TI and RI are set.Baud rate variable and set by overflow rate of Timer 1. By loading preset value in timer register, overflow rate may be set and varied.

  • Mode 2 11 bits are transmitted or received -11 bit frame is

    9th bit is programmable. While transmitting TB8 bit of SCON is loaded in 9th bit.Thus 9th bit can be used as parity bit.On receipt of frame 9th bit is loaded to RB8 in SCON register. Stop bit is ignored.Baud rate is programmable to 1/32 of oscillator frequency (if SMOD = 1) or 1/64 of oscillator frequency (if SMOD =0).SMOD is 7th bit of SFR PCON.

    Start bitStop bitProgrammable bit bit 0 - - - - - - - - - -bit 7

  • -For 12 MHz FrequencySMOD=0, 12/64=3/10=0.18MbpsSMOD=1, 12/13=5/8=0.37 Mbps

  • To program the 9th bit to represent parity of data byte. MOV C , P ;- Parity of ACC loaded to CY. MOV TB8 , C MOV SBUF,A Note:- Bit movement in 8051 is through CY only. - To program SMOD as 0 or 1. MOV A, PCON SETB A.7 MOV PCON, ANote:- PCON is not bit addressable.

    SCON.3PSW.7PSW.0ORL A, #80H

  • Mode 3 Same as Mode2 The difference is that baud rate can be varied using Timer1.

    SCON Serial Control Register.

    SM0 SM1 SM2 REN TB8 RB8 TIRI

    SM0 SM1 00 Mode 0, 01 - Mode1 10 Mode 2, 11 - Mode 3.SM2 used for multiprocessor communication in mode 2 and mode 3.If SM2 = 1 (in mode 2 and mode 3) RI is not activated unless 9th bit is received. i.e. RB8 =1.7 6 5 4 3 2 1 0

  • In mode 1, if SM2 = 1, RI is not activated unless valid stop bit in received.In mode 0, SM2 should be reset.REN If REN=1, Enables serial reception.If REN=0, Reception is disabled.TB8 9th bit of data that is to be transmitted. Set or Reset by software.RB8- In mode 2 and mode 3 9th bit received. In mode 1 if SM2 =0 RB8= stop bit that is received. In mode 0 RB8 is not used.

  • TI (Transmit Interrupt Flag) Set by hardware.In mode 0 set at the end of 8th bit transmitted.In mode1,2,3- set at the beginning of stop bit.Cleared by software.RI Receive Interrupt Flag.Set by hardware. In mode 0, set at the end of reception of 8th bit.In mode 1,2,3 set at the end of reception of stop bit.To be cleared by software.

  • PCON Power Control Register Used for Power Control Function only bit 7 in available for user 7 0

    used for doubling the baud rate.Baud Rate CalculationMode = 0 Baud Rate = 1/12 of oscillator frequencyMode = 2 Baud Rate = 1/32 of oscillator frequency if SMOD =1 = 1/64 of oscillator frequency if SMOD =0 SMOD

  • In mode 1 and mode 3Baud rate depends on timer 1 overflow rate and SMOD.Baud Rate = 1/32 ( Timer 1 overflow rate)if SMOD =0= 1/16 (Timer 1 overflow rate)if SMOD =1Timer 1 must be kept in mode 2 i.e. 8 bit auto reload mode.Overflow rate will depend on reload value in TH1

    Overflow rate = Count rate 256 TH1

  • For C/T = 0 i.e. timer modeCount rate = 1/12 oscillator frequencyFor C/T = 1 i.e. counter modeCount rate depends on external input signal.

  • Mode 2OSC12TL 1TH 1TF 1TR 1T 1PinGateINT 1ControlInterruptC / T = 0C / T = 1Gate = 0 , TR 1 should be made 1_/ _

  • Mode 0-13 bit Timer/CounterMode 1-16 bit Timer/CounterMode 2-8 bit Auto reload of Preset valueMode 3-Two split 8 bit operation for Timer 0.Calculate baud rate if reload value = FFH, oscillator frequency = 12 MHz and SMOD = 1. Overflow rate = 1/12 (12 MHz) 1x106 256 FFH 1Baud rate = 1/16 (Overflow rate) = 1/16 (106) = = 625 x 100 = 62.5 K

  • If SMOD = 0 then Baud rate = 1/32 (overflow rate)= (62.5K) = 31.25 KThus if baud rate is known then reload value can be calculated or vice versa. 8051 PC Serial Interface Baud rate of 8051 must match with baud rate of PC.Some of the baud rates supported by PC BIOS are-100,150,300,600,1200,2400,4800,9600 and 19200. The crystal frequency of 11.0592MHz make 8051 work properly in some of the above baud rates.

  • Case 1 SMOD = 0 Baud rate = 1/32 (overflow rate) 2400 = 1/32 (overflow rate) 2400= 1/32

    256 xx =

    Let us calculate the reload value for 2400 baud with 11.0592 MHz frequency 12 clock m/c cycle.

  • xx= 256

    = 256 12

    = 244 7 6 5 4 3 2 1 0=

    = F4H

    11110100

  • Case -2SMOD = 1xx= 256 24 = 232= 1 1 1 0 1 0 0 0 = E8HFor SMOD = 0 and clock frequency = 11.0592MHz, 12 clock m/c cycle the reload values for different baud rates are as follows

  • Baud RateTH1(decimal)TH1 (Hex) -3FDH -6FAH -12F4H -24 E8HConnecting 8051 to PC for Serial Communication The COM 2 port of PC has 9 pin connecter. The RxD, TxD, and GND pins of the connecter may be connected to RxD,TxD and GND of 8051 and other pins of COM 2 (DB9 connecter ) may be left unconnected.

  • COM 2 port (DB-9 connecter) in PC works on RS232C signal standardLogic 0-+ 3 to +25VLogic 1-- 3 to 25V RxD, TxD pins work on TTL signal. Thus voltage converter chip (called line driver) is required to interface 8051 to PC for serial communication.MAX 232 and MAX 233 chips are normally used.These chips perform same functions. They are not pin compatable.MAX 232 require capacitors to be connected MAX 233 doesnt require that.MAX 233 is costlier then MAX232.

  • Programming 8051 to transfer the data serially to PCDetermine TMOD value and load Timer1 in mode2 Auto reload (8 bit) Timer 1TMOD- = 20H

    b) Based on baud rate selected, determine the TH1 reload value and load the value in TH1.Determine SCON register value and load.

    Gate C/T M1 M0

    00100 0 0 0

    SM0SM1SM2RENTB8RB8TIRI

  • SM1 = 1, SM0 = 0 Mode 1 9 bit UART

    PC uses 1 start bit and 1stop bit for RS 232C REN = 1 To enable Reception of data or REN = 0 Other bits SM2 , TB8 , RB8 , T1 , R1 =0 =50H

    76543210

    TC0N-

    Start bit bit0----------------------bit7 Stop bit

    01010 0 0 0

    TF1TR1TF0TR0IE1IT1IE0IT0

  • Set TR1 (TCON.6) to start timer.Clear TI (SCON.1) i.e. transmit interrupt flag. The byte to be transmitted is loaded to SBUF register.TI bit is monitored if TI = 1, Byte has been transmitted. h). Go to e. for next byte transmission.Note Start and Stop bits are added by 8051 UART for mode 02 - Timer 1 is used to adjust the transmission rate or Reception rate of bits serially.

  • Example :A block of 20 bytes to be transmitted serially.Baud rate = 24008051 frequency = 11.0592 MHz12 clock m/c cycle.For 2400 baud, Reload value for Timer 1 = -12(or F4H) SMOD = 0

    TMOD = 0010 0000 = 20H (8 bits Auto reload) MOV R2, # 00; Initialize Counter MOV TMOD, # 20H

  • (b) MOV TH1, # -12 [or MOV TH1, # F4H](c) MOV SCON, # 50H(d) SETB TCON.6 [ or SETB TR1](e) LOOP1: CLR SCON.1[or CLR TI](f) MOV DPTR, # BT_CODE MOV A, R2 MOVC A,@A+DPTR MOV SBUF, A(g) JNB SCON.1, $[or JNB TI, $](h) INC R2 CJNE R2, #20, LOOP1 SJMP $

    BT-CODE is Part of program.

  • BT_CODE DB A,B,C,D, -------- END.

  • Programming 8051 to receive the data serially from PCTMOD register is loaded with 20H Timer1 Auto reload 8 bits mode Mode=2b. TH1 loaded with reload value based on baud rate.SCON register loaded with 50H SM0=0 SM1=1, REN= 1 Other bits =0, -9 bit UART moded. TR1 i.e TCON.6 is set to start Timer1e. RI(SCON.0) is cleared i.e Receive Interrupt Flag.RI bit is monitored if RI=1, byte has been received.

  • h. Store the byte received from SBUF to memory/register.i. Goto e. to receive next character.Example:A block of 50 bytes to be received serially and stored in IDR memory locations 30H onwards.Baud rate = 9600, 8051 frequency=11.0592 MHz12 clock m/c cycle.TH1 Reload value = -3(or FDH)MOV R2, #00H; Counter initializationMOV R1, #30H; Memory address for storage.MOV TMOD, #20HMOV TH1, #-3[or MOV TH1, #FDH]

  • MOV SCON, #50HSETB TCON.6 [ or SETB TR1]LOOP1: CLR SCON.0 [or CLR RI]JNB SCON.0, $ [or JNB RI, $]; Calculate memory address for data storage.MOV A, R1,ADD A, R2 MOV R0, A ; R0 contains the addressMOV A, SBUF MOV @R0, A ; store.INC R2CJNE R2, # 50, LOOP1-SJMP $-END.

    **