Sunday, February 3, 2013

Simple UART protocol simulation using proteus (Transmitting data from microcontroller to PC)


The Universal Asynchronous Receiver/Transmitter (UART) controller is the essential component of the serial communications subsystem of a computer. The UART takes bytes of data and transmits the individual bits in a sequential manner. At the destination, a second UART re-assembles the bits into complete bytes. Serial transmission is commonly used with modems and for non-networked communication between computers, terminals and other devices.

Asynchronous transmission allows data to be transmitted without the sender having to send a clock signal to the receiver. Instead, the sender and receiver must agree on timing parameters in advance and special bits are added to each word which is used to synchronize the sending and receiving units. 

When a word is given to the UART for Asynchronous transmissions, a bit called the "Start Bit" is added to the beginning of each word that is to be transmitted. The Start Bit is used to alert the receiver that a word of data is about to be sent, and to force the clock in the receiver into synchronization with the clock in the transmitter. These two clocks must be accurate enough to not have the frequency drift by more than 10% during the transmission of the remaining bits in the word. After the Start Bit, the individual bits of the word of data are sent, with the  Least Significant Bit (LSB) being sent first. Each bit in the transmission is transmitted for exactly the same amount of time as all of the other bits, and the receiver “looks” at the wire at approximately halfway through the period assigned to each bit to determine if the bit is a 1 or a 0. 

For example, if it takes two seconds to send each bit, the receiver will examine the signal to determine if it is a 1 or a 0 after one second has passed, then it will wait two seconds and then examine the value of the next bit, and so on. (Frank Durda, 1996)


For the full duplex UART serial communication protocol we will show the basic hardware interfacing block diagram with the according to the requirements mentioned above. The interfacing block diagram is as shown in following:


From the figure shown above the MAX-232 is a dual driver/receiver and typically converts the Reception (RX) and Transmitting (TX) signals. The drivers provide RS-232 voltage level outputs (approx. ± 7.5V) from a single + 5V supply via on-chip charge pumps and external capacitors. It is used to convert the TTL logic levels to the RS-232 voltage levels and vice versa. This makes it useful for implementing RS-232 in devices that otherwise do not need any voltages outside the 0V to + 5V range, as power supply design does not need to be made more complicated just for driving the RS-232 in this case.

The SCON (Serial Port Control Register) is responsible for all serial communication related setting in AT89C51 microcontroller. Thus we have to set the SCON register to mode1 and enable the reception, where mode 1 is an 8 bit UART with a user defined baud rate. In mode 1 the baud rate is determined by how frequently timer 1 overflows. The more frequently timer 1 overflows, the higher the baud rate. There are many ways one can cause timer 1 to overflow at a rate that determines a baud rate, but the most common method is to put timer 1 in 8-bit auto-reload mode (timer mode 2) and set a reload value (TH1) that causes Timer 1 to overflow at a frequency appropriate to generate a baud rate. As we know that 8051 microcontrollers takes 12 clock cycles to complete one machine cycle and then divide by 32 to generate its baud rate.

(The Following Calculations is used to find the loaded number in the timer register while writing the code)

To determine the value that must be placed in TH1 to generate a given baud rate, we may use the following equation (assuming PCON.7 is clear).

TH1 = 256 - ((Crystal frequency / 12×32) / Baud rate)

If PCON.7 is set then the baud rate is effectively doubled, thus the equation becomes:

TH1 = 256 - ((Crystal frequency / 6×32) / Baud rate)

For our design we have an 11.0592 MHz crystal and we want to configure the serial port to 9600 baud we try plugging it in the first equation:

TH1 = 256 - ((Crystal / 12×32) / Baud rate)

TH1 = 256 - ((11059000 / 384) / 9600)

TH1 = 256 - ((28800) / 9600)

TH1 = 256 - 3 = 253

As you can see, to obtain 9600 baud on a 11.0592Mhz crystal, we have to set TH1 to 253. Finally, we have to loaded the calculated number of cycles to let the microcontroller timer start counting from that value till it reaches the overflow and stop counting, but the timer register is apple to be loaded by hexadecimal number only, so we have to convert the calculated number of cycles into hexadecimal number and then loaded to the timer register.

Loaded number in the timer register in hexadecimal is "FD".

The sender and the receiver devices must have the same baud rate to achieve the serial communication. The circuit diagram of the system is as shown below in (Figure 3.2). For transferring data we have to use SBUF, where SBUF is an 8-bit register used for serial communication specific programs. 

For a byte to be transferred via Tx line, it must be placed in the SBUF register first. Similarly SBUF holds the byte of data when it is received by 8051's receiver line Rx. SBUF can be accessed similar to any other register in 8051, but it is not bit addressable.

Circuit diagram (For simulation ignore MAX232 as we just have to use virtual terminal or its called hyper-terminal "The black box") The virtual terminal is considered as PC.



By using Keil uVision software we will develop a C code to transmit some data from microcontroller to PC.

(You can ignore the LCD, MAX232 and RS232 in simulation) You just need 8051 MCU, push button and virtual terminal, that's work in simulation only not in real life.



#include<reg51.h>
sfr lcd_data_pin=0xA0; //P2 port
sbit rs=P3^5;
sbit rw=P3^6;
sbit en=P3^7;

void delay(unsigned int count) //Function to provide time delay
{
int i,j;
for(i=0;i<count;i++)
for(j=0;j<1275;j++);
}

void lcd_command(unsigned char comm) //Lcd command funtion
{
lcd_data_pin=comm;
en=1;
rs=0;
rw=0;
delay(1);
en=0;
}

void lcd_data(unsigned char disp) //Lcd data function
{
lcd_data_pin=disp;
en=1;
rs=1;
rw=0;
delay(1);
en=0;
}

lcd_string(unsigned char *disp) //Lcd string function
{
int x;
for(x=0;disp[x]!=0;x++)
{
lcd_data(disp[x]);
}
}

void lcd_ini() //Function to initialize the LCD
{
lcd_command(0x38);
delay(5);
lcd_command(0x0F);
delay(5);
lcd_command(0x80);
delay(5);
}

sbit Transmit_Switch=P1^0;
unsigned char Txbuffer[86]={"http://mardak-life-side.blogspot.com/"}; //Details to be displayed in PC
int i;

void main(void)
{
lcd_ini();
TMOD = 0x20; //Timer 1, mode 2 (auto-reload)
TH1 = 0xFD; //load TH with -3 or FDh 9600 (Baud Rate)
SCON = 0x50; //UART mode 1, receive enabled 
TR1 = 1; //start Timer1
TI = 0; //clear TI
Transmit_Switch=1;
lcd_string("Welcome");
lcd_command(0x01);
lcd_string("Push to Transmit");
delay(50);
while(1)
{
if(Transmit_Switch==0)
{
while(Transmit_Switch==0); // Transmit when the push button is released 
lcd_command(0xC0);
lcd_string("Transmitting...");
delay(30);
lcd_command(0xC0);
lcd_string(" ");
for(i=0;i<86;i++)
{
SBUF =Txbuffer[i]; //send the buffer array contents one by one
while(TI==0); //monitor if TI is set
TI = 0; //clear TI once TI is set
}}}}



The transmitting occurs when the push button is pressed.




Tips

1. If you built the circuit and you want to connect it to PC, you have to download a serial port tools such as Comm Operator or use windows hyper-terminal.

2. You have to set the baud rate of the serial port tools, which is same to the microcontroller baud rate 9600 baud.





Things you should keep in mind before buying a laptop

1. Set your budget amount


2. Choose the processor that satisfy your needs (Always choose Intel processors)


i. Light users (i3)


Intel® Core™ i3 Processor

Visibly Smart performance starts here
Responsive performance Four-way multitask processing Built-in visual enhancements 3M cache 
Intel® HD Graphics

ii. Mid users (i5) *Most popular*


Intel® Core™ i5 Processor

Visibly Smart performance with a boost
More responsive performance Four-way multitask processing Automatic burst of speed when you need it Built-in visual enhancements 3M cache 
Intel® HD Graphics

iii. Heavy users (i7)


Intel® Core™ i7 Processor

Visibly Smart performance at its best
Top-of-the-line performance Eight-way multitask processing Automatic burst of speed when you need it Built-in visual enhancements 8M cache 
Intel® HD3000 Graphics


3. 32bit or 64bit 


32-bit Windows has an address space of 4GB. Part of that is used by system BIOS and 
graphics memory.

64-bit Windows has an address space of 16GB. Part of that is used by system BIOS and 
graphics memory.

64-bit OS can emulate 32-bit programs.



4. CPU series


M series = (More power Less battery life)


U series (used in ultrabooks) = (Less Power More battery life)


Difference in power about 14% - 20%



5. RAM (Most laptops have 2 x SO-DIMM socket for expansion up) At least get DDR3 SDRAM 4GB 


Most medium to high end RAM runs at high enough frequencies to handle most applications 
with ease so as long as you have a high enough amount for your usage then you should be fine. Highest RAM frequency for laptops is DDR3 SDRAM 1600 MHz.


6. Graphics card


If you are not going to use your laptop for heavy gaming then you are good with the integrated 
graphic card.

Fast game action and impressive 3D visuals have gone mainstream. 3rd generation Intel®
Core™ processors integrated with powerful graphics engines by Intel® HD Graphics now give you responsive gaming performance and realistic visuals, WITHOUT THE COST of a separate graphics card.

Laptops with dedicated graphics card are more expensive.


High end dedicated graphics cards LIST



7. Hard disk drive


A 5400 rpm drive spins 25% slower than a 7200 rpm drive, increasing bearing life by at least 
that amount.

(5400 rpm 500-750 GB + 24 GB SSD for OS) is preferable


OR


(5400 rpm 500-750 GB)

OR


(256 SSD)


But SSD hard drives are expensive.



8. Display


14" - 15.6" LED back-lit with minimum resolution of 1366 x 768