Program to establish a serial communication link between the PC and the 8051
Description:
Serial communication is often used either to control or to receive data from an embedded microprocessor. Serial communication is a form of I/O in which the bits of a byte begin transferred appear one after the other in a timed sequence on a single wire. Serial communication has become the standard for intercomputer communication. In this Project, we'll try to build a serial link between 8051 and PC using RS232.
Using the Serial Port
8051 provides a transmit channel and a receive channel of serial communication. The transmit data pin (TXD) is specified at P3.1, and the receive data pin (RXD) is at P3.0. The serial signals provided on these pins are TTL signal levels and must be boosted and inverted through a suitable converter(Max232) to comply with RS232 standard.
All modes are controlled through SCON, the Serial CONtrol register. The SCON bits are defined as SM0, SM1, SM2, REN, TB8, RB8, TI, RI from MSB to LSB. The timers are controlled using TMOD, the Timer MODe register, and TCON, the Timer CONtrol register.
Use Hyper Terminal at PC side to send/receive Data
Code (Text):
#pragma SMALL DB OE #include <reg51.h> unsigned char ReceiveSerial() { unsigned char c; TMOD = 0x20; /* configure timer for the correct baud rate */ TH1 = 0xe6; /* 1200 bps for 12 MHz clock */ TCON = 0x00; /* Set timer to not running */ SCON = 0x50; /* Set Serial IO to receive and normal mode */ TR1 = 1; /* start timer to Receive */ while( (SCON & 0x01) == 0 ) /* wait for receive data */; c = SBUF; return c; } void SendSerial(unsigned char c) { /* initialize..set values for TMOD, TH1 and TCON */ /* set the Tx interrupt in SCON to indicate sending data */ /* start timer */ /* write character to SBUF */ /* wait for completion of sent data */ } void main(void) { unsigned char c; while( 1 ) { /* Use ReceiveSerial to read in a character 'c' */ /* Do some computation on 'c' */ /* Send the result using SendSerial() */ } }
Interfacing Serial Port (RS232) with 8051 microcontroller 1.0
Interfacing Serial Port with 8051 microcontroller