Microcontroller based velocity-sensing system of a moving object

 Abstract of Microcontroller based velocity Meter Project

The notion behind the implementation of Velocity Measurement System is to learn by doing, the application and functionality of microcontroller. Velocity Measurement has a wide variety of applications in industry and microcontroller is the most suitable device that can be dedicated for the task. This document discusses the circuitry and the programming involve in the implementation of Velocity Measurement System. The Microcontroller will be programmed using C- language and will familiarize us with hardware and programming of an 8051 Microcontroller. We will learn how to design and implement both hardware and software components.

Project Overview

Our project is to implement the velocity measurement system of a moving object using 8051 microcontrollers. We have used a ball rolling inside a channel which passes through two sensors (micro limit switches in this case) and the time is calculated between the two using microcontroller and by usage of a simple formula, the velocity is calculated.

Measurement of Linear Velocity

As the ball i.e. the moving object passes over the first sensor an interrupt signal is generated and the software starts counting the time interval until the second interrupt is received when the ball passes over the second sensor. The length between the sensors is fixed i.e. 30cm, which is known to us by dividing that length by the time interval between two interrupts the velocity is obtained, the measured velocity is displayed on the LCD and also on the monitor of PC. We insure to have small displacement by keeping sensors not too far from each other so that time interval is very small and average velocity measured approaches to instantaneous velocity.  

Average speed is

                            Vavg = (y2 - y1) / (t2 - t1)                 

Where,

             y2 - y1= The length between two sensors.

             t2 - t1=  the time interval between two interrupts.

Basic Block Diagram of Velocity Meter

First figure is about the flowchart of the project. It describes the flow of data through out the system.

 

Block diagram for the velocity measurement using microcontroller
Basic Block Diagram of a Project

            

Input from Velocity Sensor side

This module is responsible for the data input. It includes object detect sensors. These sensors could be contact less like optical or proximity sensors or contact sensors like limit switches. We used micro limit switches for that purpose. The reason behind this is they are inexpensive, simple and reliable. Moreover, they are available at our lab.

sensor board diagram for the velocity measurement using microcontroller
sensor board diagram for the velocity measurement using microcontroller


Data processing and Manipulation

For the data processing some processor is required. The main processing is carried out within the controller. Device used for the processing is 89C51 microcontroller.

Output on LCD and also on PC through RS-232

These modules are responsible for the data output. They include interfacing with computers Interfacing with other Devices.  Following is the flow of data within the output module.

 Modules describing Interfacing

The Serial Port

 Introduction to Serial Port

Advantages of using Serial data transfer rather than Parallel port are summarized as. Serial Cables are usually more longer than the Parallel port communication cables. The serial port transmits a '1' as -3 to -25 volts and a '0' as +3 to +25 volts where as a parallel port transmits a '0' as 0v and a '1' as 5v. The number of wires are at lesser side than parallel transmission cable which have many wires. Micro controller’s have also proven to be quite popular recently. The microcontrollers have in built Serial Communications Interfaces in the chips. The built in hardware is used to talk to the outside world. Serial port Communication uses the less number of the ios pins of the MPU's or microcontrollers. The number of IOs used in null modem configuration only two pins named Transmit Data (TXD) and Receive Data (RXD).

Interfacing the Serial / RS232 Port

Frequently, a voltage standard (and often times a handshaking standard) such as RS-232 is used to allow the 8051-based system to communicate with a wide range of common systems such as PCs.

Serial Port's Registers (PC's)

The port terminology and addresses are shown below in table below:

Table  Standard Port Addresses

Name

Address

COM 1

3F8

COM 2

2F8

COM 3

3E8

COM 4

2E8

 

The address at which the Communications (COM) ports are Addressed in the BIOS Data Area are as under. Each port address is consisting of 2 bytes.

Table-2 COM Port Addresses in the BIOS Data Area

Start Address

Function

0000:0400

COM1's Base Address

0000:0402

COM2's Base Address

0000:0404

COM3's Base Address

0000:0406

COM4's Base Address

 Pins and Wires

Table3: Serial Pinouts (D9 Connectors)

D-Type-9 Pin No.

Abbreviation

Full Name

Pin 3

TD

Transmit Data

Pin 2

RD

Receive Data

Pin 7

RTS

Request To Send

Pin 8

CTS

Clear To Send

Pin 6

DSR

Data Set Ready

Pin 5

SG

Signal Ground

Pin 1

CD

Carrier Detect

Pin 4

DTR

Data Terminal Ready

Pin 9

RI

Ring Indicator

The various Functions of the above mentioned pins are enlisted in the table-4:

Table4: D-Type-9 Pin Connector Functions

Abbreviation

Full Name

Function

TD

Transmit Data

Serial Data Output (TXD)

RD

Receive Data

Serial Data Input (RXD)

CTS

Clear to Send

This signal is an indication for the link that the Modem is ready to exchange

Data through the serial communication.

DCD

Data Carrier

Detect

On detection of a "Carrier" signal  on the other end of the phone line, this Line becomes active.

DSR

Data Set Ready

ready to establish a link.

DTR

Data Terminal

Ready

UART is ready to link

RTS

Request To Send

UART is ready to exchange data

RI

Ring Indicator

It detects a ringing signal

RS−232 or EIA−232

The serial port is usually a RS−232−C, EIA−232−D, or EIA−232−E.

RS-232 Waveforms

RS-232 communication is asynchronous. It means that the clock is not communicated with the data during the serial communication. Each word is synchronized with it's start bit, the send and receive are responsible to accurately sense the transmission. The serial communication starts with a start bit, normally it is the Logic 0. After that each bit is transmitted through the line, one bit at a time. Usually the LSB (Least Significant Bit) is sent first in serial communication. At the end of transmission, a Stop Bit which is normally Logic 1 is sent.

If the next bit after the Stop Bit be Logic 0. This must mean another word is following, and this is it's Start Bit. Therefore, if the line has not been sent back into an idle state, then the receiving end will interpret this as a break signal. The data sent this way is framed in the form that the data is transmitted in-between a Start and Stop Bit.

Serial Port on 8051

The 8051 family microcontrollers have an on chip hardware based serial port. The built in serial communication hardware of 8051 microcontroller can be used or  operated in many different modes over a wide range of set of frequencies usually called baud rate. The serial port in the full duplex mode of operation means that the simultaneous transmission and reception. It may be noted that the data being received is buffered in special buffer registers in order to avoid any loss of the communication.

Serial Port SFR’s

Before using Serial port, SCON must be initialized for the correct mode and, also specify the baud rate using SMOD which is a bit specified in PCON. The SMOD bit can be used to double the baud rates of the serial port whether generated by the timer 1 overflow rate or the oscillator frequency. The Setting SMOD bit to logic high will double the baud rate of the UART during its operation in mode 1, 2, or 3. When the Timer 2 of microcontroller is used for the generation of baud rates, the value of SMOD don’t effect on the baud rate of UART.

Table5: Serial Control Register (SCON) - Bit Addressable

SM0

SM1

SM2

REN

TB8

RB8

TI

RI

 LCD Interfacing

What is LCD: The LCD stands for “Liquid Crystal Display”.

LCD Initialization

Before we can send commands or data to the LCD module, the module must be initialized. The initialization procedure for both the modes is given below:

For Eight bit mode

For eight-bit mode, LCD is initialized using the following series of steps, The microcontroller will send necessary commands to LCD through the use of data port in a specific order. The commands and LCD response will make it ready to accept the data to be shown on it. The series of command as under:

  1. First of all, the microcontroller should wait about 15 to 20 milli-seconds after the power ON of the system.
  2. Microcontroller will write first command on the data bus which will be consisting of a byte having value 0x030 to LCD and then wait 05 milli-seconds for the instruction to complete.
  3. Microcontroller will write second command on the data bus consisting of an-other byte with value 0x030 to LCD and then again microcontroller shall wait 160 micro-seconds for instruction to complete.
  4. Third time microcontroller will write the same command on the data bus of similar value 0x030 to LCD and then again microcontroller will wait 160 micro-seconds or poll the Busy Flag.
  5. Now at this stage the microcontroller will set the Operating Characteristics of the LCD.
  6. Microcontroller will write the "Set Interface Length".
  7. Microcontroller will send 0x010 to LCD for the execution of turn off the Display.
  8. Microcontroller will send the next command consisting of 0x001 to LCD for the execution of Clear the Display.
  9. Microcontroller will send the command for "Set Cursor Move Direction" Setting Cursor behavior Bits.
  10. At the last microcontroller will send the command to "Enable Display/Cursor" & enable Display and also for Optional Cursor.

 For Four bit mode

In some application we wish to have a smaller number of data bus to be used with the microcontroller to save the microcontroller IO lines. The standard controllers of the alpha-numeric LCD allow the use of half of the data bus for communication purpose. Therefore, there are some difference and similarities the process of initialization of LCD for the four bit mode as compared to the eight bit mode as discussed earlier. Now we will se the sequence of command for the initialization of LCD for the utilization of the LCD in this specific mode of operation in which some of the Microcontroller pins will be saved.

  1. First step is very similar to above that the microcontroller should wait for some delay of about 15 milli-seconds after power is ON.
  2. Microcontroller will send the same command as discus above consisting of one byte and having value 0X30 and after each send Microcontroller shall wait for about a few milli seconds in the sequence as discussed earlier.
  3. Here is the important command which will tell the LCD to select the mode of operation: as the microcontroller will now send 0x02 to the LCD to Enable Four Bit Mode. After this all commands and instruction or data etc will be necessary send in two steps in the form of two nibbles.  All other sequence and rest of commands will be in same sequence and value, except with the care that data shall be transferred in the form of nibbles.

 

IMPLEMENTATION OF THE VELOCITY METER USING MICROCONTROLLER 8051

Component List.

  1. Atmel 89C51 Microcontroller
  2. Max 232 Transceivers
  3. 25 MHz Crystal oscillator 
  4. DB-9 Female connector
  5. Hitachi 44780 character LCD
  6. Two Micro limit switches
  7. One Green LED
  8. Five 10uF – 25V Capacitors
  9. Four Resistors 10K Ohm ¼ watts
Software of the Velocity Meter is written in Kei C51 language. The code listing is presented below:

/* Velocity Measurement  */
 
/* Dated : March  2010 */
 
/* Hardware Description */
/* Microcontroller 89C51 */
/* Operating Frequency : 12 MHz */
/* Inputs : Object sensors displace at 30 cm apart */
/* Outputs: 	a.) 20x2 Character LCD
		b.) Serial Console
		c.) Heartbeat LED */
 
 
#include <AT89X51.H>	
#include <intrins.h>			
#include <stdio.h>		
 
/* function declaration */
void init_serial_port(void);
void init_lcd(void);
void init_timer0(void);
void init_interrupts(void);
void lcd_start_messeges(void);
void serial_start_messeges(void);
void waitms(unsigned int );
void clearlcd(void);
void waitUS (unsigned char );
void putcharlcd(unsigned char);
void putstringlcd(unsigned char *);
void print_vel_lcd(unsigned char *); 
void positioncursor(unsigned char);
void write_lcd(unsigned char ) ;
 
/* bit declaration */
sbit rs_lcd	 =	P3^6;		
// Register Select LCD, H= Data, L = Instruction code
sbit en_lcd	=	P3^7;		// Enable LCD H->L enable
sbit heart_beat = P0^0;
//*************** THE END ****************************************** 
 
/* text messeges */
unsigned char code msg1[]= "   Velocity      \0";
unsigned char code msg2[]= "   Measurement   \0";
unsigned char code msg3[]= "   8051          \0";
unsigned char code msg4[]= " Microcontroller \0";
unsigned char code msg5[]= "     C-language  \0";
unsigned char code msg6[]= "  KEil c51        \0";
unsigned char code msg7[]= "       By        \0";
unsigned char code msg8[]= "  Dr.Rana        \0";
unsigned char code msg9[]= "   as            \0";
unsigned char code msg10[]=" an External     \0";
unsigned char code msg11[]="    Supervisor   \0";
unsigned char code msg12[]="  in this        \0";
unsigned char code msg13[]="     Project     \0";
unsigned char code msg14[]=" Thank You       \0";
 
unsigned char code msg15[]="  v e l o c i t y  \0";
unsigned char code msg16[]=" m / sec\0";
 
/* end of text messeges block */
 
/* global variables declarations */
 
unsigned int x;			//counter for heart beat	
unsigned long count;		//48usec increment counter
bit complete_flag=0 ;		//complete status flag
bit count_flag=0;		
unsigned char buff [10];	//define 10 byte buffer
unsigned char n;		//sprintf return variable
 
 
/*++++++++++++++++++++++++++++*/
/* Main Function */
/* This function initialize the pheripherals, send Initial messeges
to the LCD & Serial Port and wait for the inputs. */
 
void main (void) {			//enable global interrupt
	float velocity;		
	init_interrupts();
	init_serial_port();
	init_lcd();
	serial_start_messeges();
	lcd_start_messeges(); 
	init_timer0();
 
while (1) {
	if(complete_flag) {
		//velocity calculation : s = vt or v = s/t
		// length = 30 cm (0.3/48e-6 = 6250)
		velocity = 6250.0/count ;	
		count = 0;
		printf("Velocity is %f m/sec", velocity);
		puts(msg14);		//THANKYOU MESSEGE
		/*printing of velocity at lcd */		
		n =  sprintf (buff, "%f",velocity);	//convertion to buff
		clearlcd();
		positioncursor(0x00);	//first line (v e l o c i t y)
		putstringlcd(msg15);
 
		print_vel_lcd(buff);	//print velocity at second line
		positioncursor(0x49);	//2nd line	(m / min )
		putstringlcd(msg16);
		complete_flag = 0;
		}
		if(x++==25000) 	heart_beat = ~heart_beat;
	}	
}
 
void timer0(void) interrupt 1 {		//every 48u sec
	if(count_flag) {
		count++;
					}
	}
 
 
void exter_intr_0(void) interrupt 0 {
		count_flag = 1;		// set count flag
}
 
void exter_intr_1(void) interrupt 2 {
		if(count_flag) {
		count_flag = 0;
		complete_flag  = 1; }
}
 
void print_vel_lcd(unsigned char *d) {
		positioncursor(0x42);	//position 42
		while (n>0) {
		write_lcd(*d);
		d++;
		n--; }}
 
void clearlcd(void){
		rs_lcd =0;
		write_lcd(0x01);
		rs_lcd =1;
		}
 
void positioncursor(unsigned char c ){
		rs_lcd = 0;
		write_lcd(0x80 | c);	//  set address of cursor
		rs_lcd =1;	 
	}
 
void putstringlcd(unsigned char *d){
		  while(!(*d == '\0')){		/* detect '\0' */
		  write_lcd(*d);
		  d++; }}
 
void write_lcd(unsigned char a) {
		P1 = a;
		waitUS(250);
		waitUS(250);
		waitUS(250);
		waitUS(250);
		en_lcd = 0;
		en_lcd = 1;
}		
 
void lcd_start_messeges(void) {
 
    positioncursor(0x00);	//first line
	putstringlcd(msg1);
	positioncursor(0x40);	//2nd line
	putstringlcd(msg2);
	waitms(800);
	clearlcd();
 
	positioncursor(0x00);	//first line
	putstringlcd(msg3);
	positioncursor(0x40);	//2nd line
	putstringlcd(msg4);
	waitms(800);
	clearlcd();
 
    positioncursor(0x00);	//first line
	putstringlcd(msg5);
	positioncursor(0x40);	//2nd line
	putstringlcd(msg6);
	waitms(800);
	clearlcd();
 
    positioncursor(0x00);	//first line
	putstringlcd(msg7);
    waitms(800);	
	clearlcd();
 
    positioncursor(0x00);	//first line
	putstringlcd(msg8);
	positioncursor(0x40);	//2nd line
	putstringlcd(msg9);
	waitms(800);
	clearlcd();
 
    positioncursor(0x00);	//first line
	putstringlcd(msg10);
	positioncursor(0x40);	//2nd line
	putstringlcd(msg11);
	waitms(800);
	clearlcd();
 
    positioncursor(0x00);	//first line
	putstringlcd(msg12);
	positioncursor(0x40);	//2nd line
	putstringlcd(msg13);
	waitms(800);
 
}
 
void init_serial_port(void) {
	SCON  = 0x50;	/* SCON: mode 1, 8-bit UART, enable rcvr  */
	PCON |= 0x80;	/* set SMOD = 1 for double buad rate */
	TMOD |= 0x20;	/* timer 1 auto reload mode */
	TH1  = -7;      /* TH1: 19200 Buad @ 12MHz 
	TR1  = 1;       /* TR1:  timer 1 run                          */
   	TI   = 1;       /* TI:   set TI to send first char of UART    */
 
}
void init_timer0(void) {
    	TMOD |= 0x02;       /* TMOD: timer 1,2, mode 2, 8-bit reload   */
    	TH0  = TL0 = -100;  // 48 usec @12M	auto reload value		
	TR0  = 1 ; 		}
 
void init_interrupts(void) {
	IE = 0x87; //enable global , external 0 and external 1 interrupt 
	IP |= 0x05; //high priority for external 0 and external 1 interrupt
	TCON |= 0x05; //low edge triggered for external-0 & external-1 int
}
 
void init_lcd(void){
	//LCD module 
	//generic lcd driver
	//	D0-D7 -> P1
	//  RS	  -> P3.6
	//  RS	  -> P3.7
 
		rs_lcd = 0;	//for cmd
		waitms(500);
        	write_lcd(0x38);//Function Set   0011   1000
		waitms(100);
		write_lcd(0x38);//Function Set   0011   1000
		waitms(100);
		write_lcd(0x38);//Function Set   0011   1000
		waitms(100);
		write_lcd(0x0C);   
   //display off/ON No Cursor No Blinking at cursor        
		waitms(100);
		write_lcd(0x01);	//clear Display
		waitms(100);
		write_lcd(0x06);	//Entry Mode Set 
		rs_lcd = 1;		// for data
	}
 
void serial_start_messeges(void) {
	puts ("WELLCOME");
	puts (msg2);
	puts (msg3);
	puts (msg4);
	puts (msg5);
	puts (msg6);
	puts (msg7);
	puts (msg8);
	puts (msg9);
	puts (msg10);
	puts (msg11);
	puts (msg12);
	puts (msg13);
 
}
 
/* These are uncalibrated approximate delays */
 
void waitUS(unsigned char a){	
	while(--a != 0);	
/* wait = a * 2 + 5 usec @ 12 MHz*/	
	}					
 
void waitms(unsigned int a) { // ~1msec delay @ 25MHz
		while (--a !=0) {
		waitUS(247);	
		waitUS(247);
		waitUS(247);
		waitUS(247);
			}
	  }
 
 
 

No comments:

Post a Comment

Please ask if you have any question regarding the programming of MCU, or have any problem in development of your electronics project. microcontroller51.blogspot.com

Popular Post (All Time)