Friday, 30 December 2011

Serial communication

Serial communication

Data transfer within a system is generally in parallel. All the bits of the data word are transferred in parallel at the same instant. In some cases, particularly  in transferring data over long distances, it is preferred to transfer the data in serial form.

The data word from a transmitting system is converted to stream of bits by parallel to serial conversion, and one bit at a time is transferred on a single line to a receiving system. At the receiving end, the word is reconstructed by serial to parallel conversion.

The speed of data transfer in serial communication is specified by baud. The baud unit is named after Jean Maurice Emile Baudot, who was an officer in the French Telegraph Service. He is credited with devising the first uniform- length 5-bit code for characters of the alphabet in the late 19th century. What baud really refers to is modulation rate or the number of times per second that a line changes state.
This is not always the same as bits per second (bps). If we connect two serial devices together using direct cables then baudand bps are, in fact, the same. But when modems are in question, this isn’t the case.

Synchronous data transfer [1]www.sena.com/download/tutorial/tech_Serial_v1r0c0
In program-to-program communication, synchronous communication requires that each end of an exchange of communication respond in turn without initiating a new communication. A typical activity that might use a synchronous protocol would be a transmission of files from one point to another. As each transmission is received, a response is returned indicating success or the need to resend.

Asynchronous data transfer [2]www.sena.com/download/tutorial/tech_Serial_v1r0c0
The term asynchronous is usually used to describe communications in which data can be transmitted intermittently rather than in a steady stream. For example, a telephone conversation is asynchronous because both parties can talk whenever they like. If the communication were synchronous, each party would be required to wait a specified interval before speaking. The difficulty with asynchronous communications is that the receiver must have a way to distinguish between valid data and noise. In computer communications, this is usually accomplished through a special start bit and stop bit at the beginning and end of each piece of data. For this reason, asynchronous communication is sometimes called start-stop transmission.

Serial Vs Parallel [3]http://www.eeherald.com/section/design-guide/esmod7.html
Let us now try to have a comparative study on parallel and serial communications to understand the differences and advantages & disadvantages of both in detail.
We know that parallel ports are typically used to connect a PC to a printer and are rarely used for other connections. A parallel port sends and receives data eight bits at a time over eight separate wires or lines. This allows data to be transferred very quickly. However, the setup looks more bulky because of the number of individual wires it must contain. But, in the case of a serial communication, as stated earlier, a serial port sends and receives data, one bit at a time over one wire. While it takes eight times as long to transfer each byte of data this way, only a few wires are required. Although this is slower than parallel communication, which allows the transmission of an entire byte at once, it is simpler and can be used over longer distances. For example, the IEEE 488 specifications for parallel communication state that the cabling between equipment can be no more than 20 meters total, with no more than 2 meters between any two devices; serial, however, can extend as much as1200meters (with high-quality cable).
So, at first sight it would seem that a serial link must be inferior to a parallel one, because it can transmit less data on each clock tick. However, it is often the case that, in modern technology, serial links can be clocked considerably faster than parallel links, and achieve a higher data rate. 
Even in shorter distance communications, serial computer buses are becoming more common because of a tipping point where the disadvantages of parallel busses (clock skew, interconnect density) outweigh their advantage of simplicity (no need for serializer and deserializer).
The serial port on your PC is a full-duplex device meaning that it can send and receive data at the same time. In order to be able to do this, it uses separate lines for transmitting and receiving data.
From the above discussion we could understand that serial communications have many advantages over parallel one like:
a) Requires fewer interconnecting cables and hence occupies less space.
b) "Cross talk" is less of an issue, because there are fewer conductors compared to that of parallel communication cables.

c) Many IC s and peripheral devices have serial interfaces.
d) Clock skew between different channels is not an issue.
e)No need of (SERDES).
f) Cheaper to implement.
How to program the Serial Port using C/C++?
[4 http://electrosofts.com/serial/]
          There are two popular methods of sending data to or from the serial port in Turbo C. One is using outportb(PORT_ID, DATA)  or outport(PORT_ID,DATA) defined in “dos.h”. Another method is using bioscom() function defined in “bios.h”.
         Using outportb() :
           The function outportb () sends a data byte to the port ‘PORT_ID’. The function outport() sends a data word. These functions can be used for any port including serial port, parallel ports. Similarly to receive data these are used.
·         inport reads a word from a hardware port
·         inportb reads a byte from a hardware port
·         outport outputs a word to a hardware port
·         outportb outputs a byte to a hardware port
 Declaration:
·            int inport(int portid);
·            unsigned char inportb(int portid);
·            void outport(int portid, int value);
·            void outportb(int portid, unsigned char value);
 Remarks:
·          inport works just like the 80x86 instruction IN. It reads the low byte of a word from portid, the high byte from portid + 2.
·          inportb is a macro that reads a byte
·            outport works just like the 80x86 instruction OUT. It writes the low byte of value to portid, the high byte to portid + 1.
·            outportb is a macro that writes value Argument
portid:
·          Inport- port that inport and inportb read from;
·         Outport- port that outport and outportb write to
value:
·         Word that outport writes to portid;
·         Byte- that outportb writes to portid.

      If you call inportb or outportb when dos.h has been included, they are treated as macros that expand to inline code.
      If you don't include dos.h, or if you do include dos.h and #undef the macro(s), you get the function(s) of the same name.
  Return Value:
#    inport and inportb return the value read
#    outport and outportb do not return
 For more details of these functions read article from beondlogic.com




 
Using bioscom:
         The macro bioscom () and function _bios_serialcom() are used in this method in the serial communication using RS-232 connecter. First we have to set the port with the settings depending on our need and availability. In this method, same function is used to make the settings using control word, to send data to the port and check the status of the port. These actions are distinguished using the first parameter of the function. Along with that we are sending data and the port to be used to communicate.
Here are the deatails of the Turbo C Functions for communication ports.         
Declaration:
       bioscom(int cmd, char abyte, int  port)
      _bios_serialcom(int cmd ,int port, char abyte)
         bioscom() and _bios_serialcom() uses the bios  interrupt 0x14 to perform various communicate the serial communication over the I/O ports given in  port.
        cmd: The I/O operation to be performed.  
portid: port to which data is to be sent or from which data is to be read.
0:COM1
1:COM2
2:COM3
abyte:
When cmd =2 or 3 (_COM_SEND or _COM_RECEIVE) parameter abyte is ignored.



#include <bios.h>
#include <conio.h>
#define COM1       0
#define DATA_READY 0x100
#define SETTINGS ( 0x80 | 0x02 | 0x00 | 0x00)
int main(void)
{
   int in, out, status;
   bioscom(0, SETTINGS, COM1); /*initialize the port*/
   cprintf("Data sent to you:  ");
   while (1)
   {
      status = bioscom(3, 0, COM1); /*wait until get a data*/
      if (status & DATA_READY)
           if ((out = bioscom(2, 0, COM1) & 0x7F) != 0)  /*input a data*/
              putch(out);
           if (kbhit())
           {
              if ((in = getch()) == 27)   /* ASCII of Esc*/
                 break;
              bioscom(1, in, COM1);   /*output a data*/
           }
   }
   return 0;
}


When cmd = 0 (_COM_INIT), abyte is an OR combination of the following bits (One from each group): 

For example, if 
 abyte = 0x8B = (0x80 | 0x08 | 0x00 | 0x03) = (_COM_1200 | _COM_ODDPARITY | _COM_STOP1 | _COM_CHR8)
  the communications port is set to
  1200 baud    (0x80 = _COM_1200)
  Odd parity   (0x08 = _COM_ODDPARITY)
 
1 stop bit   (0x00 = _COM_STOP1)
  8 data bits (0x03 = _COM_CHR8)


To initialise the port with above settings we have to write,
                 bioscom(0, 0x8B, 0);
 To send a data to COM1, the format of the function will be bioscom(1, data, 0). Similarly bioscom(1, 0, 0 ) will read a data byte from the port.        
         The following example illustrate how to serial port programs. When a data is available in the port, it inputs the data and displays onto the screen and if a key is pressed the ASCII value will be sent to the port

         When you compile and run the above program in both the computers, The characters typed in one computer should appear on the other computer screen and vice versa. Initially, we set the port to desired settings as defined in macro settings. Then we waited in an idle loop until a  key is pressed or a data is available on the port. If any key is pressed, then kbhit() function returns non zero value. So will go to getch function where we are finding out which key is pressed. Then we are sending it to the com port. Similarly, if any data is available on the port, we are receiving it from the port and displaying it on the screen.


          To check the port, If you have a single computer, you can use loop-back connection as follows. This is most commonly used method for developing communication programs. Here, data is transmitted to that port itself. Loop-back plug connection is as follows. 
Fig 2. Loop-back plug connection
If you run the above program with the connection as in this diagram, the character entered in the keyboard should be displayed on the screen. This method is helpful in writing serial port program with single computer. Also you can make changes in the port id if your computer has 2 rs232ports. You can connect the com1 port to com2 of the same computer and change the port id in the program. The data sent to the port com1 should come to port com2. then also whatever you type in the keyboard should appear on the screen.


The program given below is an example source code for serial communication programmers. It is a PC to PC communication using RS232. Download the code, unzip and run to chat in dos mode between two computers. Use the program to get more idea about serial port programming.


[5] PC-to-PC communication – by VARUN JINDAL */
#include<stdio.h>
#include<conio.h>
#include<bios.h>

#define SETTINGS (_COM_9600 | _COM_CHR8 | _COM_NOPARITY |_COM_STOP1)
/* baud rate = 9600, 8 data bits, no parity bit, 1 stop bit */

void main(void)
{
unsigned in,out,status;
int port;
clrscr();
printf(“Select Port(Enter ‘0’ for COM1 and ‘1’ for COM2):”);
scanf(“%d”,&port);
printf(“Press ESC to exit”);
textcolor(YELLOW);
cprintf(“\n\rData Received:”);
_bios_serialcom(_COM_INIT,port,SETTINGS);
for(;;)
{
          status=_bios_serialcom(_COM_STATUS,port,0);
          if (status & 512)
               printf(“\n\t\a Overrun Error”);
         if (status & 1024)
              printf(“\n\t\a Parity Error”);
         if (status & 2048)
              printf(“\n\t\a Framing Error”);
         if(status & (512|1024|2048)) /* if any error */
              break;
         if(status & 256) /* if data ready */
         {
              if((out=_bios_serialcom(_COM_RECEIVE,port,0) & 255)!=0)
                       putch(out);
         }
         if(kbhit()) /* if a keystroke is currently available */
         {
                in=getch(); /* get a character without echoing onto the screen */
         if(in==27) /* if ESC */
                break;
         _bios_serialcom(_COM_SEND,port,in);
         }
}

}

Tuesday, 20 December 2011

LM35 interfacing with 8051

Temperature Measuring
A temperature sensor LM35 is interfaced to the 8051 by an ADC0804
The output voltage from the LM34/LM35 is linearly proportional to the measuring temperature
The ADC0804 converts the output voltages from the LM35 into digital signals, which correspond to the measured temperature. Handled by the 8051
Interfacing with the LM35 (Temperature Sensor)

i
The ADC0804 converts the output voltages from the LM35 into digital signals, which correspond to the measured temperature.
Step size of the ADC0804 = (1.28)x(2)/255 = 0.01004V
Clock input to the ADC0804 = clock frequency / 4
If the following data of LM35 are given
the temperature range of the temperature sensor LM35 is -55C to 150C and its output scale is 10mV/C, and the output value of the ADC0804 is 00H when the LM35 senses -55C
then the value output from the ADC0804 for a measuring temperature 100 C is:
the LM35 (Temperature
  Sensor)

GSM Module interfacing with 8051

This the project report of car controlling system. Project done in 5th semester in course of microcontroller. GSM module DGS100 Used for this project Available in Communication LAB of ciit wah(for ciit students).

















Please extract the require data And improve this project. Future work may done by someone gps based car controlling system.

SPI AND I2C PROTOCOL

AoA
this post give the concept of SPI and I2C protcol

 






















 
Reference
lecture by
Dr. Royan Ong