Programming 8051 SPORT (Assembly Language) serial communication • The data is sent one bit at a time. • serial communication uses single data line making it much cheaper. serial communication • 2 methods, asynchronous and synchronous • synchronous method transfers a block of data (characters) at a time • asynchronous method transfers a single byte at a time • Uses special IC chips called UART (universal asynchronous receiver-transmitter) and USART (universal synchronousasynchronous receiver- transmitter) • 8051 chip has a built-in UART Simple and Duplex transmission Simple and Duplex transmission • Half- and full-duplex transmission – simplex (one way) communication allows data to be transmitted in only one direction – duplex(two way) communication allows data to be transmitted in two directions. – duplex transmissions can be half or full duplex – If one way at a time, it is half duplex – If can go both ways at the same time, it is full duplex – full duplex requires two wire conductors for the data lines (in addition to the signal ground) Asynchronous serial communication and data framing – data coming in 0s and 1s – to make sense of the data sender and receiver agree on a set of rules – Protocol determines • how the data is packed • how many bits/character • when the data begins and ends Asynchronous serial communication and data framing cont… • Start and stop bits – asynchronous method, each character is placed between start and stop bits – called framing – start bit is always one bit – stop bit can be one or two bits – start bit is always a 0 (low) – stop bit(s) is 1 (high) – LSB is sent out first Framing ASCII “A” When there is no data transfer the signal is high Transmission begins with a start (low) bit LSB first Finally 1 stop bit (high) Data transfer rate (baud rate) is stated in bps Baud rate • Data transfer rate – rate of data transfer is stated in bps (bits per second) – widely used terminology for bps is baud rate – baud and bps rates are not necessarily equal – The baud rate is the number of times per second a serial communication signal changes states; a state being either a voltage level, a frequency, or a frequency phase angle. RS232 Standard • This standard is used in PCs • Logic 1 : -3 to -25 volt • Logic 0 : 3 to 25 volt • To Connect TXD to RXD and RXD to TXD from pc to 8051 you must use max232 to convert signal from TTL level to RS232 level • The baud rate of the 8051 must matched the baud rate of the pc • PC standard baud rate (see hyper terminal configuration) – 2400-4800-9600-14400-19200-28800-3360057600 1 2 3 4 5 6 7 8 9 DCD RD TD DTR GND DSR RTS CTS RI 8051 Serial Port • 8051 has an internal UART – P3.0 (pin 11) is assigned to TxD – P3.1 (pin 10) is assigned to Rxd – Baud rate is set by Timer1 – these pins are TTL compatible – require a line driver to make them RS232 compatible – driver is the MAX232 chip • SBUF : Serial Buffer Register – There are physically two SBUF registers having same address i.e 99 H. – Data moved to SBUF is Transmitted serially – Serial data Rx-ed is stored by CPU in SBUF Serial control (SCON) Register 7 6 SM0 SM1 5 SM2 SM0 : mode specifier SM1 : mode specifier 4 REN 3 TB8 2 RB8 SM0 SM1 MODE operation 0 0 0 shift register 0 1 1 8 bit UART 1 0 2 9 bit UART 1 1 3 9 bit UART 1 0 TI RI transmit rate fixed (xtal/12) variable (timer1) fixed (xtal/32 or xtal/64) variable (timer1) SM2 : used for multi processor communication (Make it 0) REN : receive enable (by software enable/disable) TB8 : Not widely used RB8 : Not widely used TI : transmit interrupt flag set by HW after send , clear by SW RI : receive interrupt flag set by HW after received ,clear by SW Mode of operation • Mode 1 – Ten bits are transmitted (through TxD) or received (through RxD) (A start bit (0), 8 data bits (LSB first), and a stop bit (1) ) – On receive, the stop bit goes into RB8 in SCON – the baud rate is determined by the Timer 1 overflow rate (not by TF1). – Timer1 clock is (1/32) machine cycle (MC=1/12 XTAL) To timer1 to set baudrate Finding timer1 registers values 11.0592MHz Xtal frequency allows the 8051 system to communicate with the PC without any error. Machine cycle frequency = 11.0592 MHz/ 12 = 921.6 kHz Timer1 clock = 921.6 kHz / 32 = 28,800 Hz Initial value in TH1 = 256 – (Timer1 clock / Required baudrate) Example: For 9600 baud rate TH1 = 256 – ( 28800/9600) = 256 – 3 = 253 = FDh For 2400 baud rate or -3 TH1 = 256 – ( 28800/2400) = 256 – 12 = 253 = F4h or -12 Common XTAL and timer settings for 8051 UARTs Baud Rate XTAL Frequency SMOD Timer 1 mode Timer1 reload value 1.2K 11.0592MHz 0 2 E8h 2.4K 11.0592MHz 0 2 F4h 4.8K 11.0592MHz 0 2 FAh 9.6K 11.0592MHz 0 2 FDh 19.2K 11.0592MHz 1 2 FDh 110K 6MHz 0 2 72h 110K 12MHz 0 1 FEEBh 137.5K 11.0592MHz 0 2 1Dh Doubling Baud Rate Ways of Increasing the baud rate • increase the crystal frequency • set SMOD bit in the PCON register (baud rate will doubled) PCON Register (SFR address 87) How to set SMOD Mov a, pcon Setb acc.7 Mov pcon, a Doubling baud rate cont... Programming the 8051 to transfer data serially 1. Load TMOD with value 20h. Timer1 mode 2 ( 8bit auto reload ) 2. Load calculated initial value into TH1 register 3. Write 40h into SCON (mode 1, REN =0) 4. Start timer by SETB TR1 5. Clear TI by CLR TI 6. Write data to be transmitted into SBUF 7. Keep monitoring TI using the instruction JNB TI, xx TI is set while transmitting the Stop bit 8. Go to step 5 to transfer the next byte Program for sending data ( in mode 1 ) Transfer letter ‘A’ serially at 9600 baudrate org 0 ljmp main org 030h main: MOV TMOD,#20H ;TIMER 1 MODE 2 MOV TH1,#-3 ;9600 BAUD MOV TL1,#-3 ;9600 BAUD MOV SCON,#40H ;mode 1, REN = 0 SETB TR1 ;start timer1 again: CLR TI ; Clear TI to transfer the next ; character MOV SBUF, #”‘A” ;SBUF = 41H wait: JNB TI, wait ; wait for the last bit SJMP again ; keep sending A end Programming the 8051 to receive data serially 1. Load TMOD with value 20h. Timer1 mode 2 ( 8bit auto reload ) 2. Load calculated initial value into TH1 register 3. Write 50h into SCON (mode 1, REN = 1) 4. Start timer by SETB TR1 5. Keep monitoring RI using the instruction JNB RI, xx RI is set while receiving the Stop bit 6. Save received data 7. Clear RI get ready to receive next byte 8. Go to step 5 to receive next byte Program for receiving data ( in mode 1 ) Receive serial data with 9600 baud rate, 1 start bit, 1 stop bit and 8 data bit and write it into port 1 main: WAIT: org 0 ljmp main org 030h MOV TMOD,#20H MOV TH1,#-3 MOV SCON,#50H SETB TR1 JNB RI, WAIT MOV A,SBUF MOV P1, A CLR RI SJMP WAIT end ;Timer1 mode 2 ;9600 baud rate ;mode 1 & Rx Enable ;start timer1 ;wait for char to come in ; save incoming byte in A ; send to port 1 ; get ready to receive next byte ; keep getting data Thank you